From nobody Mon Sep 15 16:13:19 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD7CDC7EE23 for ; Mon, 5 Jun 2023 14:37:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234260AbjFEOhT (ORCPT ); Mon, 5 Jun 2023 10:37:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232301AbjFEOhQ (ORCPT ); Mon, 5 Jun 2023 10:37:16 -0400 Received: from sender-of-o51.zoho.in (sender-of-o51.zoho.in [103.117.158.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C76F9C for ; Mon, 5 Jun 2023 07:37:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685975788; cv=none; d=zohomail.in; s=zohoarc; b=I/MNviWllcoNociBuVMUjvf/4cmNOBv9CinXXyuEx+o6nHyIiQ8Py93j7m8Kxh3cWZjebJEB7xL3kxliRbd9aZzuWq/DKZArO+Az5y4lKHHXQy0ZHS+glVJ0Q2dPlkq+d20FgEvB/tXhWJQKfjxZP/SfdRoB6XHIHgtohJqyq6w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1685975788; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:MIME-Version:Message-ID:Subject:To; bh=G+0R5+PtzmbZTJMOunHX+jthOd0HYHJjkvOrxJuEthU=; b=Kmk8/MmpJEkq7zU+IzPdRlBCfPT++h6bCISBTviNggIch3cqZ6QnD96dR+hcwlQKOydFc7n7hvxsFOPrx7oOHD22RzOMuxmHuruexKMPOuh9/X9smyhciEwC34mUx56PgKYEuf6g0QNHBkcBygLEzT6S75Yz91QUkmEYwKBay3w= ARC-Authentication-Results: i=1; mx.zohomail.in; dkim=pass header.i=siddh.me; spf=pass smtp.mailfrom=code@siddh.me; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1685975788; s=zmail; d=siddh.me; i=code@siddh.me; h=From:From:To:To:Cc:Cc:Message-ID:Subject:Subject:Date:Date:MIME-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Reply-To; bh=G+0R5+PtzmbZTJMOunHX+jthOd0HYHJjkvOrxJuEthU=; b=fxlRRdswQBmxRF8xMd/GGaY3HureaR+7e07JxHb2WC5TrZubv6SIm3gcLB56JTeY PUEWzqkhLbkcGl9Tz/O5XsHMf4vhgwMrGFpx2usInVRDbmol16aCi+qkdUokSWcYDcy vE3Qar9kp9n9+JF05paK312kGguNAXwsd4X66YiI= Received: from kampyooter.. (223.236.126.120 [223.236.126.120]) by mx.zoho.in with SMTPS id 16859757868871021.8385417806124; Mon, 5 Jun 2023 20:06:26 +0530 (IST) From: Siddh Raman Pant To: David Howells , Randy Dunlap , Jonathan Corbet , Mauro Carvalho Chehab , David Disseldorp , Nick Alcock Cc: linux-fsdevel , linux-kernel , stable@vger.kernel.org Message-ID: <20230605143616.640517-1-code@siddh.me> Subject: [PATCH v5] kernel/watch_queue: NULL the dangling *pipe, and use it for clear check Date: Mon, 5 Jun 2023 20:06:16 +0530 X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMailClient: External Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" NULL the dangling pipe reference while clearing watch_queue. If not done, a reference to a freed pipe remains in the watch_queue, as this function is called before freeing a pipe in free_pipe_info() (see line 834 of fs/pipe.c). The sole use of wqueue->defunct is for checking if the watch queue has been cleared, but wqueue->pipe is also NULLed while clearing. Thus, wqueue->defunct is superfluous, as wqueue->pipe can be checked for NULL. Hence, the former can be removed. Tested with keyutils testsuite. Cc: stable@vger.kernel.org # 6.1 Signed-off-by: Siddh Raman Pant --- Changes in v5: - Rebased to latest mainline. - Added Cc to stable. - Specify tests passing. Note that all tests in the keyutils testsuite passed, except tests/features/builtin_trusted, which we should not worry about as it requires some kernel preparation according to David Howells in v4 discussion. Changes in v4 (11 Jan 2023): - Drop preceeding kerneldoc-changes patch and change appropriately. Changes in v3 (8 Jan 2023): - Minor rephrase of comment before NULLing in watch_queue_clear(). Changes in v2 (6 Aug 2022): - Merged the NULLing and removing defunct patches. - Removed READ_ONCE barrier in lock_wqueue(). - Better commit messages. include/linux/watch_queue.h | 3 +-- kernel/watch_queue.c | 12 ++++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/include/linux/watch_queue.h b/include/linux/watch_queue.h index fc6bba20273b..45cd42f55d49 100644 --- a/include/linux/watch_queue.h +++ b/include/linux/watch_queue.h @@ -38,7 +38,7 @@ struct watch_filter { struct watch_queue { struct rcu_head rcu; struct watch_filter __rcu *filter; - struct pipe_inode_info *pipe; /* The pipe we're using as a buffer */ + struct pipe_inode_info *pipe; /* Pipe we use as a buffer, NULL if queue = closed */ struct hlist_head watches; /* Contributory watches */ struct page **notes; /* Preallocated notifications */ unsigned long *notes_bitmap; /* Allocation bitmap for notes */ @@ -46,7 +46,6 @@ struct watch_queue { spinlock_t lock; unsigned int nr_notes; /* Number of notes */ unsigned int nr_pages; /* Number of pages in notes[] */ - bool defunct; /* T when queues closed */ }; =20 /* diff --git a/kernel/watch_queue.c b/kernel/watch_queue.c index e91cb4c2833f..d0b6b390ee42 100644 --- a/kernel/watch_queue.c +++ b/kernel/watch_queue.c @@ -42,7 +42,7 @@ MODULE_AUTHOR("Red Hat, Inc."); static inline bool lock_wqueue(struct watch_queue *wqueue) { spin_lock_bh(&wqueue->lock); - if (unlikely(wqueue->defunct)) { + if (unlikely(!wqueue->pipe)) { spin_unlock_bh(&wqueue->lock); return false; } @@ -104,9 +104,6 @@ static bool post_one_notification(struct watch_queue *w= queue, unsigned int head, tail, mask, note, offset, len; bool done =3D false; =20 - if (!pipe) - return false; - spin_lock_irq(&pipe->rd_wait.lock); =20 mask =3D pipe->ring_size - 1; @@ -603,8 +600,11 @@ void watch_queue_clear(struct watch_queue *wqueue) rcu_read_lock(); spin_lock_bh(&wqueue->lock); =20 - /* Prevent new notifications from being stored. */ - wqueue->defunct =3D true; + /* + * This pipe can be freed by callers like free_pipe_info(). + * Removing this reference also prevents new notifications. + */ + wqueue->pipe =3D NULL; =20 while (!hlist_empty(&wqueue->watches)) { watch =3D hlist_entry(wqueue->watches.first, struct watch, queue_node); --=20 2.39.2