Article View: linux.kernel.netdev
Article #19419Re: [PATCH net v2 1/4] ptp: ocp: prevent timestamp rearming during detach
From: Vadim Fedorenko
Date: Thu, 19 Nov 2026 22:23
Date: Thu, 19 Nov 2026 22:23
151 lines
14301 bytes
14301 bytes
On 19/11/2026 21:19, Ahmad Byagowi wrote:
> Detach disables timestamp interrupts before unregistering the PTP clock.
> Until the POSIX clock is unregistered, an ioctl can rearm a channel
> after that disable. The clock can then be released while the timestamp
> IRQ is still registered. The core cleanup does not necessarily disable
> the channel: EXTTS requests do not require a PTP_PF_EXTTS pin assignment.
I think we have to fix it other way. Basically, there are several ways
to enable EXTTS in ptp_ocp:
1. Via sma* attributes in ptp_ocp sysfs entry
2. Via pin name sysfs entry under ptp device
3. Via PTP_PIN_SETFUNC ioctl
4. Via PTP_EXTTS_REQUEST ioctl
On teardown, sma* attributes (1) are removed before disabling
interrupts. Options 2 and 3 set PTP_PF_EXTTS function and reverted
during ptp_clock_unregister(). Only option 4 does not set PTP_PF_EXTTS
function to pin, because it calls ops->enable() callback directly.
To fix it properly enable() callback has to set PTP_PF_EXTTS function
if it's requested. That will make ptp_disable_all_events() do exactly
what is has to do - clear all events generators of ptp device after
removing ioctl interface.
>
> Use the existing device spinlock to serialize the stopping check, PPS
> subscription updates and timestamp MMIO. Set ts_stopping under that lock
> before detach disables the channels, so an enable already in progress
> finishes first and later enables fail with -ENODEV. Keep disables
> available for PTP core cleanup.
>
> Flush the interrupt mask writes under the lock, then drop the lock and
> restore IRQ flags before pci_irq_vector(), which takes the MSI descriptor
> mutex, and synchronize_irq(), which may sleep. During teardown, bypass
> the PPS no-state-change shortcut: a concurrent disable
> may have cleared pps_req_map and dropped the lock without yet draining
> its IRQ. Detach must still disable and drain that channel before
> ptp_clock_unregister().
>
> Reported by the netdev Sashiko AI review, then checked against the PTP
> and POSIX-clock paths. The source-derived userspace harness reproduces
> rearming with the old source. With this fix it rejects late enables,
> serializes in-flight enables, drains a mocked active IRQ and covers a
> disable paused after unlock but before IRQ synchronization. It checks
> that MMIO is locked and vector lookup and IRQ synchronization run unlocked
> with IRQ flags restored. Shared PPS/EXTTS subscriptions and all six
> channels pass with ASan/UBSan. This is not an in-kernel UAF, lockdep or
> physical-card test.
>
> Codex revised the code, tests and changelog from Vadim's review, at the
> request to check the spinlock approach and rerun the removal-race tests.
>
> Fixes: 627366c51145 ("ptp: ocp: fix resource freeing order")
> Suggested-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
> Link: https://lore.kernel.org/r/179502617840.3136.12704245716223038161@kernel.org
> Link: https://lore.kernel.org/r/ad6883e5-c103-4f82-8040-e5463477ca10@linux.dev
> Signed-off-by: Ahmad Byagowi <ahmadexp@gmail.com>
> ---
> drivers/ptp/ptp_ocp.c | 35 ++++++++++++++++++++++++++++-------
> 1 file changed, 28 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
> index 625a85b2dc..4c98558e96 100644
> --- a/drivers/ptp/ptp_ocp.c
> +++ b/drivers/ptp/ptp_ocp.c
> @@ -367,6 +367,8 @@ struct ptp_ocp {
> struct pci_dev *pdev;
> struct device dev;
> spinlock_t lock;
> + /* Protected by lock; blocks timestamp arming during teardown. */
> + bool ts_stopping;
> struct ocp_reg __iomem *reg;
> struct tod_reg __iomem *tod;
> struct pps_reg __iomem *pps_to_ext;
> @@ -2550,6 +2552,15 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
> struct ptp_ocp_ext_src *ext = priv;
> struct ts_reg __iomem *reg = ext->mem;
> struct ptp_ocp *bp = ext->bp;
> + unsigned long flags;
> + bool need_sync = false;
> + int err = 0;
> +
> + spin_lock_irqsave(&bp->lock, flags);
> + if (enable && bp->ts_stopping) {
> + err = -ENODEV;
> + goto out;
> + }
>
> if (ext == bp->pps) {
> u32 old_map = bp->pps_req_map;
> @@ -2559,9 +2570,9 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
> else
> bp->pps_req_map &= ~req;
>
> - /* if no state change, just return */
> - if ((!!old_map ^ !!bp->pps_req_map) == 0)
> - return 0;
> + /* Teardown must drain IRQs even if another disable cleared the map. */
there was no need of this in the previous patch, why do we need it now?
> + if ((!!old_map ^ !!bp->pps_req_map) == 0 && !bp->ts_stopping)
> + goto out;
> }
>
> if (enable) {
> @@ -2569,16 +2580,21 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
> iowrite32(1, ®->intr_mask);
> iowrite32(1, ®->intr);
> } else {
> - int irq_vec = pci_irq_vector(bp->pdev, ext->irq_vec);
> -
> iowrite32(0, ®->intr_mask);
> iowrite32(0, ®->enable);
> ioread32(®->intr_mask);
> + need_sync = true;
> + }
> +
> +out:
> + spin_unlock_irqrestore(&bp->lock, flags);
> + if (need_sync) {
> + int irq_vec = pci_irq_vector(bp->pdev, ext->irq_vec);
> +
> if (irq_vec > 0)
> synchronize_irq(irq_vec);
> }
> -
> - return 0;
> + return err;
> }
>
> static void
> @@ -5749,12 +5765,17 @@ ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
> static void
> ptp_ocp_detach(struct ptp_ocp *bp)
> {
> + unsigned long flags;
> int i;
>
> ptp_ocp_debugfs_remove_device(bp);
> ptp_ocp_detach_sysfs(bp);
> ptp_ocp_attr_group_del(bp);
> timer_delete_sync(&bp->watchdog);
> + /* Drain in-flight enables and prevent rearming before unregister. */
> + spin_lock_irqsave(&bp->lock, flags);
> + bp->ts_stopping = true;
> + spin_unlock_irqrestore(&bp->lock, flags);
> /* Disable interrupts on all timestampers */
> if (bp->ts0)
> ptp_ocp_ts_enable(bp->ts0, 0, false);
Technical Details
Message-ID:
<4e42134b-b017-4836-9772-9c1915b16e77@linux.dev>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367378-linux-kernel-netdev=dewy.ruReferences:
<20261119-timecard-common-v2-078fe69f33-0-ahmadexp@gmail.com> <20261119-timecard-common-v2-078fe69f33-1-ahmadexp@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367378-linux-kernel-netdev=dewy.ru From: vadim.fedorenko@linux.dev (Vadim Fedorenko) Newsgroups: linux.kernel.netdev Subject: Re: [PATCH net v2 1/4] ptp: ocp: prevent timestamp rearming during detach Date: Thu, 19 Nov 2026 22:23:03 +0000 Organization: Usenet Network Message-ID: <4e42134b-b017-4836-9772-9c1915b16e77@linux.dev> References: <20261119-timecard-common-v2-078fe69f33-0-ahmadexp@gmail.com> <20261119-timecard-common-v2-078fe69f33-1-ahmadexp@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: usenet.network; logging-data="2996494"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:1hAHEIYM7neATWSsoJW9PSC/ek4= sha256:Vb6aBrO/UU1O1zAZvI3z+cK8+RjRPe7iif94Zf5v3aw= sha1:ry35FNrOq6dSCL0Sg/9TCDwVOIA= sha256:UDaPEJfh0kWdygdqCEgtun0bxQFf2B4rStej13V8D9Q= Content-Language: en-US List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id B61CC1157C4; Fri, 20 Nov 2026 01:23:26 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.05 X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key) header.d=linux.dev X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id TDBbrKVJ_Tsy; Fri, 20 Nov 2026 01:23:22 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-367378-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id C78371157BD; Fri, 20 Nov 2026 01:23:12 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 41A2D1C716E; Thu, 19 Nov 2026 22:23:09 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795126988; b=0WMcWDWXOmGd5lLZ4zTeA9bc+XKGK0LRe9j+jGfoQXdpPTgmQKWnubOD23IxsS+GEhxK UFjyru8tEsc2gFV2Sq3rqdvvOzT/cUrS/eM1HgZQLhM4kzrgCDOI55Ih1TuH4J/EMjEW0 kI1nC8Go1h5F9Zi5n2G7wdO/4D1t2EBCZp6ucbAAhBzCVKDcRjU7bcxPlf2CQ8vrsHRLJ cUcaGuRK0bQc3bY1c5WO8jYW5HXfYRaqQsaJJTJ8521rNhUloI5JykwbgUGLjqfXicz2k cC3Z302PRibkGly5e2GL4oPZrweFZa+njciKB0h57B6fwMRt+Dly7LjiZ1xlXt9YtVA== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795126988; h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature: X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=VIR2XXRz/VIYbQ3vo4yDdmGYcHrzMjtf4j4rI56rJeQ=; b=C+wcs5HrOlhz0QEFg7cVTqFm0bZ4S6nG4XYkTl4fYnofvPGb2zuYiKDAJLhwv52RW61r X/gyu9wU7wSlAYp6zzEozzsS+wCkZhSQLrVAVEStyDSXATmFZxIrCNJjXr7JiRw6AxKO6 H9CAS/uej0EOrY61SU3zZYCH9sIBcke3GYbmaqnAWHXhLhGQzU5HBVITyqjI7Zlt3Txk2 8kvZav01lGLDoKDkwty4uVSGhrxX+IaJgaQ8cdFT8iNLLk4ZUnAR5NKG2AN87NYGsI63D Lbr+fns8Z1PoiYtXP9/VIBcWSkA0jgy2NKqjrz44wY9J71e2bjAAseblSK/1ZS5QrCA== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=nzbl9uGy; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6D52B606A335; Thu, 19 Nov 2026 22:23:08 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=nzbl9uGy X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.107 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795126987; b=RKcLesFba/bUZ3jqrW1Nj+AUflUkkp6mw3Z4HGKS36ZwOtrDBqvv4FLVwDJAgZOKJLEq G/CvZ6FXUKeRoFKOrdZkH4fL8/zVAiFWzdxU35fEEk/vN/ss+z9pm1zVQn0C2CUfZcICe yK4sbTxTHvxU+RHaeM9df1GBjXyNLeimtq5kK1zxRVF0aXiZ6+iW/qSU4CZuCuQnq3Jui HawYlbnvVJYK7HQBjiguvXq0v/FM3gsb9L0A5K2GKiZ2HAsH0N9DES07NbLFRpIoZ0Fp/ TWb3F0/L2H/zxKOJYw5eVYwf5tAvklKULfkDSBerrbebdK5/TGdgwl5gAF0HJ9ShhLw== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795126987; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=VIR2XXRz/VIYbQ3vo4yDdmGYcHrzMjtf4j4rI56rJeQ=; b=Ge7lwTgMlGpLwwaMVSWZzzNH5xJHn4lKVio2/6FgPJUMIoO2aIbGBmZ5mLeijaCb4b+C lssfvqrUk5SmKxkONzdBmhfQUkF8SLLS9IKwvt/ENuLNove/hscTZgbWuxEJpwiyi5oQ2 dq7ShXPraQLM8TkfNKAAQjWe2SfaO1jdr7cvVPUWZ6MrrfdCSI+3+Yl8jOoB4oXrQaDxT ZvzNdOYCUBo8dmFdS9JbVqQPXsn98YcifZUPnTcFejdHMJ+PysOnjWDPHoSz+rV73ylAk mxkN8tPBctUE01heK+jqCGIUUU+mKRCvqkTAigueRvShSYO8jazDJVU0kZGCh8ZGixw== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=nzbl9uGy; arc=none smtp.remote-ip=91.218.175.107 X-Received: from mta0.migadu.com (out-107.mta0.migadu.com [91.218.175.107]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13EDD606A320 for <netdev@vger.kernel.org>; Thu, 19 Nov 2026 22:23:05 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 13EDD606A320 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Envelope-To: netdev@vger.kernel.org X-DKIM-Signature: a=rsa-sha256; bh=AtODdgI8E3q9jMSujROV6OOBgGoFpUES4FimZBiQZPI=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795126983; v=1; x=1795731783; b=nzbl9uGy2kx/0r/xMp3VPfNb0piZ1Lw7JaW3LaZGQYvNd96F9+Kz8pLTjhgqXTySgLQajBwC LGenOQVTz4X4p+fjVW6EcCsDuaNjoKPbjP1JTx4GViVENIQ5jQmmPjeOCSSZfZRHqf0dMN8hqCO qFWgnUlDPdUqagpw3OAXAdJk= X-Envelope-To: netdev@vger.kernel.org X-Received: by smtp.migadu.com with ESMTPS id 677f1b06c5739ceb; Thu, 19 Nov 2026 22:23:03 +0000 X-Mizu-Trace-ID: 677f1b06c5739ceb X-Migadu-Flow: FLOW_OUT X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-User-Agent: Mozilla Thunderbird X-Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com X-In-Reply-To: <20261119-timecard-common-v2-078fe69f33-1-ahmadexp@gmail.com>