From: Siddh Raman Pant via Linux-kernel-mentees <linux-kernel-mentees@lists.linuxfoundation.org>
To: Eric Biggers <ebiggers@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
David Howells <dhowells@redhat.com>,
Randy Dunlap <rdunlap@infradead.org>,
Mauro Carvalho Chehab <mchehab@kernel.org>,
Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
Eric Dumazet <edumazet@google.com>
Cc: linux-kernel-mentees
<linux-kernel-mentees@lists.linuxfoundation.org>,
linux-kernel <linux-kernel@vger.kernel.org>
Subject: [PATCH 1/3 v4] kernel/watch_queue: Remove dangling pipe reference while clearing watch_queue
Date: Thu, 4 Aug 2022 19:00:22 +0530 [thread overview]
Message-ID: <cf470fb02971377e32564224f17897c55a374857.1659618705.git.code@siddh.me> (raw)
In-Reply-To: <cover.1659618705.git.code@siddh.me>
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).
We also need to use READ_ONCE() in post_one_notification() to prevent the
compiler from optimising and loading a non-NULL value from wqueue->pipe.
Signed-off-by: Siddh Raman Pant <code@siddh.me>
---
Changes in v4:
- Brought the lines towards the start rather than the end.
- Removed incorrect NULLing of wqueue->pipe->watch_queue.
The latter was pointed out by Eric Biggers <ebiggers@kernel.org>
in reply to v3.
Changes in v3:
- Restored the original unlock order, and clear before unlock.
- Used READ_ONCE() in post path.
This was explained by David Howells <dhowells@redhat.com> in
reply to v1.
Changes in v2:
- Removed the superfluous ifdef guard.
kernel/watch_queue.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/kernel/watch_queue.c b/kernel/watch_queue.c
index a6f9bdd956c3..8999c4e3076d 100644
--- a/kernel/watch_queue.c
+++ b/kernel/watch_queue.c
@@ -99,7 +99,7 @@ static bool post_one_notification(struct watch_queue *wqueue,
struct watch_notification *n)
{
void *p;
- struct pipe_inode_info *pipe = wqueue->pipe;
+ struct pipe_inode_info *pipe = READ_ONCE(wqueue->pipe);
struct pipe_buffer *buf;
struct page *page;
unsigned int head, tail, mask, note, offset, len;
@@ -606,6 +606,9 @@ void watch_queue_clear(struct watch_queue *wqueue)
/* Prevent new notifications from being stored. */
wqueue->defunct = true;
+ /* This pipe will get freed by caller, and we are anyways clearing. */
+ wqueue->pipe = NULL;
+
while (!hlist_empty(&wqueue->watches)) {
watch = hlist_entry(wqueue->watches.first, struct watch, queue_node);
hlist_del_init_rcu(&watch->queue_node);
--
2.35.1
_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
next prev parent reply other threads:[~2022-08-04 13:31 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-04 13:30 [PATCH 0/3] kernel/watch_queue: Clean up some code Siddh Raman Pant via Linux-kernel-mentees
2022-08-04 13:30 ` Siddh Raman Pant via Linux-kernel-mentees [this message]
2022-08-04 13:30 ` [PATCH 2/3] kernel/watch_queue: Improve struct annotation formatting Siddh Raman Pant via Linux-kernel-mentees
2022-08-05 7:22 ` Eric Biggers
2022-08-05 9:35 ` Siddh Raman Pant via Linux-kernel-mentees
2022-08-04 13:30 ` [PATCH 3/3] kernel/watch_queue: Remove wqueue->defunct and use pipe for clear check Siddh Raman Pant via Linux-kernel-mentees
2022-08-04 14:41 ` [PATCH v2 " Siddh Raman Pant via Linux-kernel-mentees
2022-08-05 7:24 ` Eric Biggers
2022-08-05 9:35 ` Siddh Raman Pant via Linux-kernel-mentees
2022-08-05 18:33 ` Eric Biggers
2022-08-06 7:23 ` Siddh Raman Pant via Linux-kernel-mentees
2022-08-05 7:16 ` [PATCH 0/3] kernel/watch_queue: Clean up some code Eric Biggers
2022-08-05 9:35 ` Siddh Raman Pant via Linux-kernel-mentees
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cf470fb02971377e32564224f17897c55a374857.1659618705.git.code@siddh.me \
--to=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=christophe.jaillet@wanadoo.fr \
--cc=code@siddh.me \
--cc=corbet@lwn.net \
--cc=dhowells@redhat.com \
--cc=ebiggers@kernel.org \
--cc=edumazet@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=rdunlap@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).