Skip to contentSkip to conversation

epoll: fix sparse error on RCU assignment

PATCH In review Eric Wong 2 participants 5 messages Mar 10, 2013 – Mar 28, 2013 VFS

lore
Share & tools
lore

Eric Wong

author
#0 lore raw

PATCH 1/1 epoll: fix sparse error on RCU assignment

This fixes the following sparse error when using CONFIG_SPARSE_RCU_POINTER=y and "make C=2 fs/eventpoll.o"

fs/eventpoll.c:514:17: error: incompatible types in comparison expression (different address spaces)

Cc Al Viro <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Davide Libenzi <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Oleg Nesterov <[email protected]>

---
Oleg: I found this error since I was working on an unrelated patch to convert wakeup_source users to RCU in epoll. This was introduced in:

commit 971316f0503a5c50633d07b83b6db2f15a3a5b00 (epoll: ep_unregister_pollwait() can use the freed pwq->whead)

 fs/eventpoll.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/eventpoll.c +3 -3
index 9fec183..1326409 100644
@@ -228,7 +228,7 @@ struct eppoll_entry {
 	wait_queue_t wait;
 
 	/* The wait queue head that linked the "wait" wait queue item */
-	wait_queue_head_t *whead;
+	wait_queue_head_t __rcu *whead;
 };
 
 /* Wrapper struct used by poll queueing */
@@ -929,7 +929,7 @@ static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *k
 	struct eventpoll *ep = epi->ep;
 
 	if ((unsigned long)key & POLLFREE) {
-		ep_pwq_from_wait(wait)->whead = NULL;
+		RCU_INIT_POINTER(ep_pwq_from_wait(wait)->whead, NULL);
 		/*
 		 * whead = NULL above can race with ep_remove_wait_queue()
 		 * which can do another remove_wait_queue() after us, so we
@@ -1018,7 +1018,7 @@ static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
 
 	if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
 		init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
-		pwq->whead = whead;
+		RCU_INIT_POINTER(pwq->whead, whead);
 		pwq->base = epi;
 		add_wait_queue(whead, &pwq->wait);
 		list_add_tail(&pwq->llink, &epi->pwqlist);
Signature · 1 line
Eric Wong
#1 lore raw
Quoted 5 lines · Eric Wong

This fixes the following sparse error when using CONFIG_SPARSE_RCU_POINTER=y and "make C=2 fs/eventpoll.o"

fs/eventpoll.c:514:17: error: incompatible types in comparison expression (different address spaces)

ep_remove_wait_queue() does rcu_dereference(pwq->whead) and rcu_dereference_sparse(__rcu) complains, I guess.

fs/eventpoll.c struct eppoll_entry { · line 231

Quoted 8 lines
fs/eventpoll.c +1 -1
@@ -228,7 +228,7 @@ struct eppoll_entry {
 	wait_queue_t wait;
 
 	/* The wait queue head that linked the "wait" wait queue item */
-	wait_queue_head_t *whead;
+	wait_queue_head_t __rcu *whead;

Well, perhaps this change is fine... but otoh this this a bit misleading. It is not actually __rcu. The special case is sighand->signalfd_wqh, and the commemt in ep_remove_wait_queue() means: if ->whead is not stable then we can only race with signalfd_cleanup(), and rcu_read_lock() ensures this memory can't go away.

We do not even need smp_read_barrier_depends() here, ACCESS_ONCE() should be enough.

Perhaps it would be better to simply shut up this warning somehow...

Quoted 23 lines
fs/eventpoll.c +2 -2
 };
 
 /* Wrapper struct used by poll queueing */
@@ -929,7 +929,7 @@ static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *k
 	struct eventpoll *ep = epi->ep;
 
 	if ((unsigned long)key & POLLFREE) {
-		ep_pwq_from_wait(wait)->whead = NULL;
+		RCU_INIT_POINTER(ep_pwq_from_wait(wait)->whead, NULL);
 		/*
 		 * whead = NULL above can race with ep_remove_wait_queue()
 		 * which can do another remove_wait_queue() after us, so we
@@ -1018,7 +1018,7 @@ static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
 
 	if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
 		init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
-		pwq->whead = whead;
+		RCU_INIT_POINTER(pwq->whead, whead);
 		pwq->base = epi;
 		add_wait_queue(whead, &pwq->wait);
 		list_add_tail(&pwq->llink, &epi->pwqlist);
Signature · 1 line
Eric Wong
author
#2 lore raw
Quoted 25 lines · Oleg Nesterov
Quoted 5 lines · Eric Wong

This fixes the following sparse error when using CONFIG_SPARSE_RCU_POINTER=y and "make C=2 fs/eventpoll.o"

fs/eventpoll.c:514:17: error: incompatible types in comparison expression (different address spaces)

ep_remove_wait_queue() does rcu_dereference(pwq->whead) and rcu_dereference_sparse(__rcu) complains, I guess.

fs/eventpoll.c struct eppoll_entry { · line 231

Quoted 8 lines
fs/eventpoll.c +1 -1
@@ -228,7 +228,7 @@ struct eppoll_entry {
 	wait_queue_t wait;
 
 	/* The wait queue head that linked the "wait" wait queue item */
-	wait_queue_head_t *whead;
+	wait_queue_head_t __rcu *whead;

Well, perhaps this change is fine... but otoh this this a bit misleading. It is not actually __rcu. The special case is sighand->signalfd_wqh, and the commemt in ep_remove_wait_queue() means: if ->whead is not stable then we can only race with signalfd_cleanup(), and rcu_read_lock() ensures this memory can't go away.

Quoted 4 lines

We do not even need smp_read_barrier_depends() here, ACCESS_ONCE() should be enough.

Perhaps it would be better to simply shut up this warning somehow...

Hi, I've been hoping others would give a reply and offer a better solution than min.

Without my proposed patch, sparse _errors_ out on me, so it prevent sparse from reporting the many other warnings I create in my patches.

#3 lore raw
Quoted 27 lines · Eric Wong
Quoted 25 lines · Oleg Nesterov
Quoted 5 lines · Eric Wong

This fixes the following sparse error when using CONFIG_SPARSE_RCU_POINTER=y and "make C=2 fs/eventpoll.o"

fs/eventpoll.c:514:17: error: incompatible types in comparison expression (different address spaces)

ep_remove_wait_queue() does rcu_dereference(pwq->whead) and rcu_dereference_sparse(__rcu) complains, I guess.

fs/eventpoll.c struct eppoll_entry { · line 231

Quoted 8 lines
fs/eventpoll.c +1 -1
@@ -228,7 +228,7 @@ struct eppoll_entry {
 	wait_queue_t wait;
 
 	/* The wait queue head that linked the "wait" wait queue item */
-	wait_queue_head_t *whead;
+	wait_queue_head_t __rcu *whead;

Well, perhaps this change is fine... but otoh this this a bit misleading. It is not actually __rcu. The special case is sighand->signalfd_wqh, and the commemt in ep_remove_wait_queue() means: if ->whead is not stable then we can only race with signalfd_cleanup(), and rcu_read_lock() ensures this memory can't go away.

Quoted 7 lines
Quoted 4 lines

We do not even need smp_read_barrier_depends() here, ACCESS_ONCE() should be enough.

Perhaps it would be better to simply shut up this warning somehow...

Hi, I've been hoping others would give a reply and offer a better solution than min.

Me too ;)

OK, probably we should use your patch, although personally I'd prefer to simply shut up the warning, say

- whead = rcu_dereference(pwq->whead);
+ whead = rcu_dereference((void __rcu*)pwq->whead);

Quoted 1 line

Without my proposed patch, sparse _errors_ out on me,

it is only sparse...

But OK, I won't argue with you patch.

Oleg.

author
#4 lore raw
Quoted 35 lines · Oleg Nesterov
Quoted 26 lines · Eric Wong
Quoted 25 lines · Oleg Nesterov
Quoted 5 lines · Eric Wong

This fixes the following sparse error when using CONFIG_SPARSE_RCU_POINTER=y and "make C=2 fs/eventpoll.o"

fs/eventpoll.c:514:17: error: incompatible types in comparison expression (different address spaces)

ep_remove_wait_queue() does rcu_dereference(pwq->whead) and rcu_dereference_sparse(__rcu) complains, I guess.

fs/eventpoll.c struct eppoll_entry { · line 231

Quoted 8 lines
fs/eventpoll.c +1 -1
@@ -228,7 +228,7 @@ struct eppoll_entry {
 	wait_queue_t wait;
 
 	/* The wait queue head that linked the "wait" wait queue item */
-	wait_queue_head_t *whead;
+	wait_queue_head_t __rcu *whead;

Well, perhaps this change is fine... but otoh this this a bit misleading. It is not actually __rcu. The special case is sighand->signalfd_wqh, and the commemt in ep_remove_wait_queue() means: if ->whead is not stable then we can only race with signalfd_cleanup(), and rcu_read_lock() ensures this memory can't go away.

Quoted 7 lines
Quoted 4 lines

We do not even need smp_read_barrier_depends() here, ACCESS_ONCE() should be enough.

Perhaps it would be better to simply shut up this warning somehow...

Hi, I've been hoping others would give a reply and offer a better solution than min.

Quoted 7 lines

Me too ;)

OK, probably we should use your patch, although personally I'd prefer to simply shut up the warning, say

- whead = rcu_dereference(pwq->whead);
+ whead = rcu_dereference((void __rcu*)pwq->whead);

I just tried the above and it does not build:

$ make C=2 fs/eventpoll.o
make[1]: Nothing to be done for `all'.
make[1]: Nothing to be done for `relocs'.
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CALL    scripts/checksyscalls.sh
  CHECK   scripts/mod/empty.c
  CHECK   fs/eventpoll.c
fs/eventpoll.c:516:17: error: not addressable
  CC      fs/eventpoll.o
fs/eventpoll.c: In function ‘ep_remove_wait_queue’:
fs/eventpoll.c:516: error: lvalue required as unary ‘&’ operand
make[1]: *** [fs/eventpoll.o] Error 1
make: *** [fs/eventpoll.o] Error 2
Quoted 5 lines
Quoted 1 line

Without my proposed patch, sparse _errors_ out on me,

it is only sparse...

But OK, I won't argue with you patch.

Thanks. I think my patch is the best way to address the issue and help prevent myself from introducing new bugs while working on epoll.

I will continue to wait for comments from others.