2022-08-04 13:36:04

by Siddh Raman Pant

[permalink] [raw]
Subject: [PATCH 3/3] kernel/watch_queue: Remove wqueue->defunct and use pipe for clear check

The sole use of wqueue->defunct is for checking if the watch queue has
been cleared, but wqueue->pipe is also NULL'd while clearing.

Thus, wqueue->defunct is superfluous, as wqueue->pipe can be checked
for NULL.

Signed-off-by: Siddh Raman Pant <[email protected]>
---
include/linux/watch_queue.h | 3 +--
kernel/watch_queue.c | 14 +++++---------
2 files changed, 6 insertions(+), 11 deletions(-)

diff --git a/include/linux/watch_queue.h b/include/linux/watch_queue.h
index c99c39ec6548..2a3b318db49d 100644
--- a/include/linux/watch_queue.h
+++ b/include/linux/watch_queue.h
@@ -55,7 +55,7 @@ struct watch_filter {
*
* @rcu: RCU head
* @filter: Filter on watch_notification::info
- * @pipe: The pipe we're using as a buffer.
+ * @pipe: The pipe we're using as a buffer, NULL when queue is cleared/closed
* @watches: Contributory watches
* @notes: Preallocated notifications
* @notes_bitmap: Allocation bitmap for notes
@@ -63,7 +63,6 @@ struct watch_filter {
* @lock: Spinlock
* @nr_notes: Number of notes
* @nr_pages: Number of pages in notes[]
- * @defunct: True when queues closed
*/
struct watch_queue {
struct rcu_head rcu;
diff --git a/kernel/watch_queue.c b/kernel/watch_queue.c
index 8999c4e3076d..c63b128818f4 100644
--- a/kernel/watch_queue.c
+++ b/kernel/watch_queue.c
@@ -43,7 +43,7 @@ MODULE_LICENSE("GPL");
static inline bool lock_wqueue(struct watch_queue *wqueue)
{
spin_lock_bh(&wqueue->lock);
- if (unlikely(wqueue->defunct)) {
+ if (unlikely(READ_ONCE(wqueue->pipe) == NULL)) {
spin_unlock_bh(&wqueue->lock);
return false;
}
@@ -99,15 +99,12 @@ static bool post_one_notification(struct watch_queue *wqueue,
struct watch_notification *n)
{
void *p;
- struct pipe_inode_info *pipe = READ_ONCE(wqueue->pipe);
+ struct pipe_inode_info *pipe = wqueue->pipe;
struct pipe_buffer *buf;
struct page *page;
unsigned int head, tail, mask, note, offset, len;
bool done = false;

- if (!pipe)
- return false;
-
spin_lock_irq(&pipe->rd_wait.lock);

mask = pipe->ring_size - 1;
@@ -603,10 +600,9 @@ void watch_queue_clear(struct watch_queue *wqueue)
rcu_read_lock();
spin_lock_bh(&wqueue->lock);

- /* Prevent new notifications from being stored. */
- wqueue->defunct = true;
-
- /* This pipe will get freed by caller, and we are anyways clearing. */
+ /* This pipe will get freed by the caller free_pipe_info().
+ * Removing this reference also prevents new notifications.
+ */
wqueue->pipe = NULL;

while (!hlist_empty(&wqueue->watches)) {
--
2.35.1




2022-08-04 14:56:38

by Siddh Raman Pant

[permalink] [raw]
Subject: [PATCH v2 3/3] kernel/watch_queue: Remove wqueue->defunct and use pipe for clear check

The sole use of wqueue->defunct is for checking if the watch queue has
been cleared, but wqueue->pipe is also NULL'd while clearing.

Thus, wqueue->defunct is superfluous, as wqueue->pipe can be checked
for NULL.

Signed-off-by: Siddh Raman Pant <[email protected]>
---
Changes in v2:
- The sent patch had accidentally missed removing the member
from struct, fix that.
- Use !READ_ONCE() instead of == NULL, as said by checkpatch.pl.

include/linux/watch_queue.h | 4 +---
kernel/watch_queue.c | 14 +++++---------
2 files changed, 6 insertions(+), 12 deletions(-)

diff --git a/include/linux/watch_queue.h b/include/linux/watch_queue.h
index c99c39ec6548..88360771c097 100644
--- a/include/linux/watch_queue.h
+++ b/include/linux/watch_queue.h
@@ -55,7 +55,7 @@ struct watch_filter {
*
* @rcu: RCU head
* @filter: Filter on watch_notification::info
- * @pipe: The pipe we're using as a buffer.
+ * @pipe: The pipe we're using as a buffer, NULL when queue is cleared/closed
* @watches: Contributory watches
* @notes: Preallocated notifications
* @notes_bitmap: Allocation bitmap for notes
@@ -63,7 +63,6 @@ struct watch_filter {
* @lock: Spinlock
* @nr_notes: Number of notes
* @nr_pages: Number of pages in notes[]
- * @defunct: True when queues closed
*/
struct watch_queue {
struct rcu_head rcu;
@@ -76,7 +75,6 @@ struct watch_queue {
spinlock_t lock;
unsigned int nr_notes;
unsigned int nr_pages;
- bool defunct;
};

/**
diff --git a/kernel/watch_queue.c b/kernel/watch_queue.c
index 8999c4e3076d..825943cf74b2 100644
--- a/kernel/watch_queue.c
+++ b/kernel/watch_queue.c
@@ -43,7 +43,7 @@ MODULE_LICENSE("GPL");
static inline bool lock_wqueue(struct watch_queue *wqueue)
{
spin_lock_bh(&wqueue->lock);
- if (unlikely(wqueue->defunct)) {
+ if (unlikely(!READ_ONCE(wqueue->pipe))) {
spin_unlock_bh(&wqueue->lock);
return false;
}
@@ -99,15 +99,12 @@ static bool post_one_notification(struct watch_queue *wqueue,
struct watch_notification *n)
{
void *p;
- struct pipe_inode_info *pipe = READ_ONCE(wqueue->pipe);
+ struct pipe_inode_info *pipe = wqueue->pipe;
struct pipe_buffer *buf;
struct page *page;
unsigned int head, tail, mask, note, offset, len;
bool done = false;

- if (!pipe)
- return false;
-
spin_lock_irq(&pipe->rd_wait.lock);

mask = pipe->ring_size - 1;
@@ -603,10 +600,9 @@ void watch_queue_clear(struct watch_queue *wqueue)
rcu_read_lock();
spin_lock_bh(&wqueue->lock);

- /* Prevent new notifications from being stored. */
- wqueue->defunct = true;
-
- /* This pipe will get freed by caller, and we are anyways clearing. */
+ /* This pipe will get freed by the caller free_pipe_info().
+ * Removing this reference also prevents new notifications.
+ */
wqueue->pipe = NULL;

while (!hlist_empty(&wqueue->watches)) {
--
2.35.1



2022-08-05 07:33:04

by Eric Biggers

[permalink] [raw]
Subject: Re: [PATCH v2 3/3] kernel/watch_queue: Remove wqueue->defunct and use pipe for clear check

On Thu, Aug 04, 2022 at 08:11:52PM +0530, Siddh Raman Pant wrote:
> static inline bool lock_wqueue(struct watch_queue *wqueue)
> {
> spin_lock_bh(&wqueue->lock);
> - if (unlikely(wqueue->defunct)) {
> + if (unlikely(!READ_ONCE(wqueue->pipe))) {
> spin_unlock_bh(&wqueue->lock);
> return false;
> }

Why is the READ_ONCE() needed? Doesn't wqueue->lock protect wqueue->pipe?

> + /* This pipe will get freed by the caller free_pipe_info().
> + * Removing this reference also prevents new notifications.
> + */

This isn't the correct block comment format; it should look like:

/*
* This pipe will get freed by the caller free_pipe_info().
* Removing this reference also prevents new notifications.
*/

- Eric

2022-08-05 10:15:50

by Siddh Raman Pant

[permalink] [raw]
Subject: Re: [PATCH v2 3/3] kernel/watch_queue: Remove wqueue->defunct and use pipe for clear check

On Fri, 05 Aug 2022 12:54:31 +0530 Eric Biggers wrote:
> Why is the READ_ONCE() needed? Doesn't wqueue->lock protect wqueue->pipe?

We are changing the pointer while a notification can be potentially waiting to
be posted to the pipe. So a barrier is needed to prevent compiler magic from
reloading the value.

This was remarked by David Howells here:
https://lore.kernel.org/lkml/[email protected]/

> This isn't the correct block comment format; it should look like:
>
> /*
> * This pipe will get freed by the caller free_pipe_info().
> * Removing this reference also prevents new notifications.
> */
>
> - Eric
>

Okay, will make the change.

Thanks,
Siddh

2022-08-05 18:37:04

by Eric Biggers

[permalink] [raw]
Subject: Re: [PATCH v2 3/3] kernel/watch_queue: Remove wqueue->defunct and use pipe for clear check

On Fri, Aug 05, 2022 at 03:05:41PM +0530, Siddh Raman Pant wrote:
> On Fri, 05 Aug 2022 12:54:31 +0530 Eric Biggers wrote:
> > Why is the READ_ONCE() needed? Doesn't wqueue->lock protect wqueue->pipe?
>
> We are changing the pointer while a notification can be potentially waiting to
> be posted to the pipe. So a barrier is needed to prevent compiler magic from
> reloading the value.
>

wqueue->pipe is only read or written while wqueue->lock is held, so that is not
an issue at all.

- Eric

2022-08-06 07:45:44

by Siddh Raman Pant

[permalink] [raw]
Subject: Re: [PATCH v2 3/3] kernel/watch_queue: Remove wqueue->defunct and use pipe for clear check

On Sat, 06 Aug 2022 00:03:20 +0530 Eric Biggers wrote:
> wqueue->pipe is only read or written while wqueue->lock is held, so that is not
> an issue at all.
>
> - Eric

Thanks for explaining. I will send the v2 now.

Thanks,
Siddh