From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.122 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795951302; b=rrfGOoSl3vkg85xcWPT58rC5vFNzAd8dMp1VhtN84+io/zo7j4qf28xSq0aYJGJ94+7+ WkSsPydTy5AK2RyMbQ0FeyaDBDNk1WnwBJ/D8+Xso045Hn4PoFPF43qrCa26hvFmyCarw y6sdFQ9lKPxNsekR3YLcLqvzLeD8fzIYHp8+AhPgldt3PeJTj5+530QOpx+wm9ZHY13cs QIfHovzSCiiilxffFXXWXHMaQ/gcxdaTU028ZImLlIfx3O6LN2UdE0wwG2eFh6505HScX QAieqoC9WioBjsi0iChvHhPJk915y1PpdTmFoesqb0mP+X4d6YUPUcOno5wdtHozD3w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795951302; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Date:From:To:CC:Subject:In-Reply-To: Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=h5K/6csH315LqSUy54liNmn0iqD2so/Hh3xHtKe7Ki0=; b=16bY4fKBwX5Levs6OZy+ASpJPhv8lPgCzY5MfVPLzgGQWh0oJFa5jKql0zcTvMb3SEk0 gn8xis2IfWCwnMn319kqFSNZgCefc3w0HMiZIxDhyhV+jJtxcqOP3CYnlBW/af9QoV+d2 u7D3ujc3j5bNiojYDQC3w6qEcXCEGyyVxIpXwlWuOfC9oonNTDhEl78ANZvWYrk7opqXM vMdFxjUISSoxGd9cDwY1eTmcnTyXvCPWLPUAgtqo/3B3G1SJqRoHB7AvhFwk1uTpo4NYM xr7mYQt4bWytZwTxVnIK6+ZrXwbEBjnAZbOd5jgBFcKxc1B2+XFhBZqZCkdjdBmZlfQ== 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=Bt193bgA; arc=none smtp.remote-ip=95.215.58.122 Received: from mta1.migadu.com (out-122.mta1.migadu.com [95.215.58.122]) (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 D7CC5606A31C for ; Sun, 29 Nov 2026 11:21:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D7CC5606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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=Bt193bgA X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=g/rdbyVSB56YeleAsLhH5mIC+e727HI2LsvpGNa8f/k=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795951298; v=1; x=1796556098; b=Bt193bgANS85DshGnOFXu6oURM8vxAiDT4R2UnY0rDPa6ZXh8DL++ODeYCRokwZf/DOOPNI1 yJImZUPFBlz0Of9KwiXbatNspawoUY2+VNvBUjBm2w42wWC8xspvBGNL9/WmQv7aYSIrPwu+tpq Kwc4wGZ8hGypbCZEjH2MIBpA= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 056b15d9f90b999e; Sun, 29 Nov 2026 11:21:38 +0000 X-Mizu-Trace-ID: 056b15d9f90b999e X-Migadu-Flow: FLOW_OUT Date: Sun, 29 Nov 2026 11:21:39 +0000 From: Bradley Morgan To: zhenwenliang@google.com CC: avagin@google.com, brauner@kernel.org, kees@kernel.org, linux-kernel@vger.kernel.org, luto@amacapital.net, wad@chromium.org Subject: =?US-ASCII?Q?Re=3A_=5BPATCH=5D_seccomp=3A_Use_wq=5Fhas=5Fsleeper=28?= =?US-ASCII?Q?=29_when_an_unused_filter_wakes_its_waiters?= In-Reply-To: <20261129024631.271472-1-zhenwenliang@google.com> Message-ID: <6679D757-5731-43D1-9BC3-B41E396F200D@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit On 29 November 2026 02:46:31 GMT, Zhenwen Liang wrote: >__seccomp_filter_orphan() drops the last user of a filter with >refcount_dec_and_test() and wakes the notifier's waiters only if >waitqueue_active() sees them. refcount_dec_and_test() provides release >ordering, and acquire ordering when it returns true, but it is not a >full barrier: its store to filter->users is not ordered before the >later lockless load in waitqueue_active(). > >The waiters add themselves to filter->wqh before they check users. >recv_wait_event() calls prepare_to_wait_event(), whose >set_current_state() is a full barrier, and then refcount_read(); >seccomp_notify_poll() calls poll_wait(), which ends with smp_mb(), and >then refcount_read(). As the comment above waitqueue_active() explains, >the waker needs a full barrier too. Without it, a waiter can read the >old value of users while the waker sees an empty wait queue, and no >wakeup is sent. SECCOMP_IOCTL_NOTIF_RECV can then sleep until a signal >arrives; this applies since commit 95036a79e7b5 ("seccomp: interrupt >SECCOMP_IOCTL_NOTIF_RECV when all users have exited"), which made it >return once the filter is unused. A poll(), select() or epoll waiter >can likewise miss EPOLLHUP and sleep until a timeout, a signal or >another event. > >Use wq_has_sleeper(), which issues smp_mb() before it checks the wait >queue. This covers both waiters, since they share this waker. >smp_mb__after_atomic() is not used here: it is defined for the atomic_t >operations, not for the refcount_t API. > >For both waiters, the outcome is allowed by the Linux kernel memory >model for a litmus test derived from the code, and by the AArch64 and >PPC architecture models for a litmus test of the same pattern. The fix >was checked with herd7 on litmus tests derived from the patched code >(LKMM, AArch64, PPC models: forbidden). > >Maintainers may want this change for stable. > >The problem was found, and this patch prepared, with the help of a >formal analysis tool we are developing that uses AI assistance; I have >reviewed the analysis and the patch. > >Compile-tested on arm64 (clang) and x86-64 (gcc) with W=1; not >runtime-tested. Small bit too verbose. Sigh.. please shorten down the description a bit, it's a one line fix, nevertheless, Reviewed-by: Bradley Morgan > >Fixes: 99cdb8b9a573 ("seccomp: notify about unused filter") >Signed-off-by: Zhenwen Liang >--- > kernel/seccomp.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > >diff --git a/kernel/seccomp.c b/kernel/seccomp.c >index 3eecba5ccdef..1f48bfdb3076 100644 >--- a/kernel/seccomp.c >+++ b/kernel/seccomp.c >@@ -534,7 +534,7 @@ static inline void seccomp_filter_free(struct seccomp_filter *filter) > static void __seccomp_filter_orphan(struct seccomp_filter *orig) > { > while (orig && refcount_dec_and_test(&orig->users)) { >- if (waitqueue_active(&orig->wqh)) >+ if (wq_has_sleeper(&orig->wqh)) makes sense. > wake_up_poll(&orig->wqh, EPOLLHUP); > orig = orig->prev; > } > --- Thanks!