SUNRPC - queuing improvements.
Subsystems (5)
cover SUNRPC - queuing improvements.
This series is against topic-sunrpc-thread-scheduling (7fddd9be200c). There are two changes since last post (other than dropping patches that have been included in the topic branch.
1/ When I dropped the test on freezing(current), need needed to
add TASK_FREEZABLE to the wait state. I could keep testing freezing(current) before calling schedule, but TASK_FREEZABLE is cleaner and seems to be prefered ... except ...
/proc/PID/stat reports a status for "D" for tasks that are
TASK_IDLE | TASK_FREEZABLE
instead of the correct "I". I've included a fix for that in the relevant patch. I should probably post is separately to the scheduler team.
2/ There was a bug in lwq_dequeue(). It tested lwq_empty() without
taking the spinlock. This could result in call-positive results if it raced with another lwq_dequeue(). So lwq_dequeue() now ensure the queue never looks transiently empty. lwq_empty() is also more careful about memory ordering.
NeilBrown
[PATCH 1/7] SUNRPC: change service idle list to be an llist [PATCH 2/7] SUNRPC: only have one thread waking up at a time [PATCH 3/7] SUNRPC/svc: add light-weight queuing mechanism. [PATCH 4/7] SUNRPC: use lwq for sp_sockets - renamed to sp_xprts [PATCH 5/7] SUNRPC: change sp_nrthreads to atomic_t [PATCH 6/7] SUNRPC: discard sp_lock
[PATCH 7/7] SUNRPC: change the back-channel queue to lwq
PATCH 1/7 SUNRPC: change service idle list to be an llist
With an llist we don't need to take a lock to add a thread to the list, though we still need a lock to remove it. That will go in the next patch.
Unlike double-linked lists, a thread cannot reliably remove itself from the list. Only the first thread can be removed, and that can change asynchronously. So some care is needed.
We already check if there is pending work to do, so we are unlikely to add ourselves to the idle list and then want to remove ourselves again.
If we DO find something needs to be done after adding ourselves to the list, we simply wake up the first thread on the list. If that was us, we successfully removed ourselves and can continue. If it was some other thread, they will do the work that needs to be done. We can safely sleep until woken.
We also remove the test on freezing() from rqst_should_sleep(). Instead we set TASK_FREEZABLE before scheduling. This makes is safe to schedule() when a freeze is pending. As we now loop waiting to be removed from the idle queue, this is a cleaner way to handle freezing.
task_state_index() incorrectly identifies a task with
TASK_IDLE | TASK_FREEZABLE
as 'D'. So fix __task_state_index to ignore extra flags on TASK_IDLE just as it ignores extra flags on other states.
--- include/linux/sched.h | 4 +-- include/linux/sunrpc/svc.h | 14 ++++++----- net/sunrpc/svc.c | 13 +++++----- net/sunrpc/svc_xprt.c | 51 +++++++++++++++++++------------------- 4 files changed, 42 insertions(+), 40 deletions(-)
include/linux/sched.h +2 -2
@@ -1666,7 +1666,7 @@ static inline unsigned int __task_state_index(unsigned int tsk_state,BUILD_BUG_ON_NOT_POWER_OF_2 (TASK_REPORT_MAX ); -if (tsk_state ==TASK_IDLE ) +if ((tsk_state &TASK_IDLE ) ==TASK_IDLE ) state =TASK_REPORT_IDLE ;/*
@@ -1674,7 +1674,7 @@ static inline unsigned int __task_state_index(unsigned int tsk_state,* to userspace, we can make this appear as if the task has gone through * a regular rt_mutex_lock() call. */ -if (tsk_state ==TASK_RTLOCK_WAIT ) +if (tsk_state &TASK_RTLOCK_WAIT ) state =TASK_UNINTERRUPTIBLE ;return fls (state);
include/linux/sunrpc/svc.h +8 -6
@@ -37,7 +37,7 @@ struct svc_pool {struct list_head sp_sockets;/* pending sockets */ unsigned int sp_nrthreads;/* # of threads in pool */ struct list_head sp_all_threads;/* all server threads */ -struct list_head sp_idle_threads;/* idle server threads */ +struct llist_head sp_idle_threads;/* idle server threads */ /* statistics on pool operation */ struct percpu_counter sp_messages_arrived;
@@ -186,7 +186,7 @@ extern u32 svc_max_payload(const struct svc_rqst *rqstp);*/ struct svc_rqst {struct list_head rq_all;/* all threads list */ -struct list_head rq_idle;/* On the idle list */ +struct llist_node rq_idle;/* On the idle list */ struct rcu_head rq_rcu_head;/* for RCU deferred kfree */ struct svc_xprt * rq_xprt;/* transport ptr */
@@ -270,22 +270,24 @@ enum {* svc_thread_set_busy - mark a thread as busy * @rqstp: the thread which is now busy * -* If rq_idle is "empty", the thread must be busy. +* By convention a thread is busy if rq_idle.next points to rq_idle. +* This ensures it is not on the idle list. */ static inline void svc_thread_set_busy (struct svc_rqst *rqstp) { -INIT_LIST_HEAD (&rqstp->rq_idle); + rqstp->rq_idle.next = &rqstp->rq_idle; }/** * svc_thread_busy - check if a thread as busy * @rqstp: the thread which might be busy * -* If rq_idle is "empty", the thread must be busy. +* By convention a thread is busy if rq_idle.next points to rq_idle. +* This ensures it is not on the idle list. */ static inline bool svc_thread_busy (struct svc_rqst *rqstp) { -return list_empty (&rqstp->rq_idle); +return rqstp->rq_idle.next == &rqstp->rq_idle; }#define SVC_NET (rqst) (rqst->rq_xprt ? rqst->rq_xprt->xpt_net : rqst->rq_bc_net)
net/sunrpc/svc.c +7 -6
@@ -510,7 +510,7 @@ __svc_create(struct svc_program *prog, unsigned int bufsize, int npools, pool->sp_id = i;INIT_LIST_HEAD (&pool->sp_sockets);INIT_LIST_HEAD (&pool->sp_all_threads); -INIT_LIST_HEAD (&pool->sp_idle_threads); +init_llist_head (&pool->sp_idle_threads);spin_lock_init (&pool->sp_lock);percpu_counter_init (&pool->sp_messages_arrived,0 ,GFP_KERNEL );
@@ -701,15 +701,16 @@ svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)void svc_pool_wake_idle_thread (struct svc_pool *pool) {struct svc_rqst *rqstp; +struct llist_node *ln;rcu_read_lock ();spin_lock_bh (&pool->sp_lock); - rqstp =list_first_entry_or_null (&pool->sp_idle_threads, - struct svc_rqst, rq_idle); -if (rqstp) -list_del_init (&rqstp->rq_idle); + ln =llist_del_first (&pool->sp_idle_threads);spin_unlock_bh (&pool->sp_lock); -if (rqstp) { +if (ln) { + rqstp = llist_entry(ln, struct svc_rqst, rq_idle); +svc_thread_set_busy (rqstp); +WRITE_ONCE (rqstp->rq_qtime,ktime_get ());wake_up_process (rqstp->rq_task);rcu_read_unlock ();
net/sunrpc/svc_xprt.c +25 -26
74 lines not on this page · raw message
Signature · 1 line
2.40.1
Re: [PATCH 1/7] SUNRPC: change service idle list to be an llist
Quoted 22 lines · NeilBrown
With an llist we don't need to take a lock to add a thread to the list, though we still need a lock to remove it. That will go in the next patch.
Unlike double-linked lists, a thread cannot reliably remove itself from the list. Only the first thread can be removed, and that can change asynchronously. So some care is needed.
We already check if there is pending work to do, so we are unlikely to add ourselves to the idle list and then want to remove ourselves again.
If we DO find something needs to be done after adding ourselves to the list, we simply wake up the first thread on the list. If that was us, we successfully removed ourselves and can continue. If it was some other thread, they will do the work that needs to be done. We can safely sleep until woken.
We also remove the test on
freezing()fromrqst_should_sleep(). Instead we setTASK_FREEZABLEbefore scheduling. This makes is safe toschedule()when a freeze is pending. As we now loop waiting to be removed from the idle queue, this is a cleaner way to handle freezing.
Quoted 4 lines
task_state_index()incorrectly identifies a task with
TASK_IDLE|TASK_FREEZABLE
as 'D'. So fix__task_state_indexto ignore extra flags onTASK_IDLEjust as it ignores extra flags on other states.
Let's split the task_state_index() change into a separate patch that can be sent to the scheduler maintainers for their Review/Ack.
Quoted 55 lines
Signed-off-by NeilBrown <[email protected]>--- include/linux/sched.h | 4 +-- include/linux/sunrpc/svc.h | 14 ++++++----- net/sunrpc/svc.c | 13 +++++----- net/sunrpc/svc_xprt.c | 51 +++++++++++++++++++------------------- 4 files changed, 42 insertions(+), 40 deletions(-)include/linux/sched.h +2
-2@@ -1666,7 +1666,7 @@ static inline unsigned int __task_state_index(unsigned int tsk_state,BUILD_BUG_ON_NOT_POWER_OF_2 (TASK_REPORT_MAX ); -if (tsk_state ==TASK_IDLE ) +if ((tsk_state &TASK_IDLE ) ==TASK_IDLE ) state =TASK_REPORT_IDLE ;/* @@ -1674,7 +1674,7 @@ static inline unsigned int __task_state_index(unsigned int tsk_state,* to userspace, we can make this appear as if the task has gone through * a regular rt_mutex_lock() call. */ -if (tsk_state ==TASK_RTLOCK_WAIT ) +if (tsk_state &TASK_RTLOCK_WAIT ) state =TASK_UNINTERRUPTIBLE ;return fls (state);include/linux/sunrpc/svc.h +2
-2@@ -37,7 +37,7 @@ struct svc_pool {struct list_head sp_sockets;/* pending sockets */ unsigned int sp_nrthreads;/* # of threads in pool */ struct list_head sp_all_threads;/* all server threads */ -struct list_head sp_idle_threads;/* idle server threads */ +struct llist_head sp_idle_threads;/* idle server threads */ /* statistics on pool operation */ struct percpu_counter sp_messages_arrived;@@ -186,7 +186,7 @@ extern u32 svc_max_payload(const struct svc_rqst *rqstp);*/ struct svc_rqst {struct list_head rq_all;/* all threads list */ -struct list_head rq_idle;/* On the idle list */ +struct llist_node rq_idle;/* On the idle list */ struct rcu_head rq_rcu_head;/* for RCU deferred kfree */ struct svc_xprt * rq_xprt;/* transport ptr */ @@ -270,22 +270,24 @@ enum {* svc_thread_set_busy - mark a thread as busy * @rqstp: the thread which is now busy
include/linux/sunrpc/svc.h enum { · line 279
Quoted 10 lines
include/linux/sunrpc/svc.h +3
-2* -* If rq_idle is "empty", the thread must be busy. +* By convention a thread is busy if rq_idle.next points to rq_idle. +* This ensures it is not on the idle list. */ static inline void svc_thread_set_busy (struct svc_rqst *rqstp) { -INIT_LIST_HEAD (&rqstp->rq_idle); + rqstp->rq_idle.next = &rqstp->rq_idle; }
I don't understand the comment "This ensures it is not on the idle list." svc_thread_set_busy() is called in two places: The first when an svc_rqst is created, and once directly after an llist_del_first() has been done. @rqstp is already not on the idle list in either case.
What really needs an explanation here is that there's no existing utility to check whether an llist_node is on a list or not.
include/linux/sunrpc/svc.h enum { · line 286
Quoted 7 lines
include/linux/sunrpc/svc.h +2
-1/** * svc_thread_busy - check if a thread as busy * @rqstp: the thread which might be busy * -* If rq_idle is "empty", the thread must be busy. +* By convention a thread is busy if rq_idle.next points to rq_idle. +* This ensures it is not on the idle list.
This function doesn't modify the thread, so it can't ensure it is not on the idle list.
include/linux/sunrpc/svc.h enum { · line 288
Quoted 2 lines
include/linux/sunrpc/svc.h +0
-0*/ static inline bool svc_thread_busy (struct svc_rqst *rqstp)
const struct svc_rqst *rqstp
Quoted 57 lines
include/linux/sunrpc/svc.h +1
-1{ -return list_empty (&rqstp->rq_idle); +return rqstp->rq_idle.next == &rqstp->rq_idle; }#define SVC_NET (rqst) (rqst->rq_xprt ? rqst->rq_xprt->xpt_net : rqst->rq_bc_net)net/sunrpc/svc.c +7
-6@@ -510,7 +510,7 @@ __svc_create(struct svc_program *prog, unsigned int bufsize, int npools, pool->sp_id = i;INIT_LIST_HEAD (&pool->sp_sockets);INIT_LIST_HEAD (&pool->sp_all_threads); -INIT_LIST_HEAD (&pool->sp_idle_threads); +init_llist_head (&pool->sp_idle_threads);spin_lock_init (&pool->sp_lock);percpu_counter_init (&pool->sp_messages_arrived,0 ,GFP_KERNEL );@@ -701,15 +701,16 @@ svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)void svc_pool_wake_idle_thread (struct svc_pool *pool) {struct svc_rqst *rqstp; +struct llist_node *ln;rcu_read_lock ();spin_lock_bh (&pool->sp_lock); - rqstp =list_first_entry_or_null (&pool->sp_idle_threads, - struct svc_rqst, rq_idle); -if (rqstp) -list_del_init (&rqstp->rq_idle); + ln =llist_del_first (&pool->sp_idle_threads);spin_unlock_bh (&pool->sp_lock); -if (rqstp) { +if (ln) { + rqstp = llist_entry(ln, struct svc_rqst, rq_idle); +svc_thread_set_busy (rqstp); +WRITE_ONCE (rqstp->rq_qtime,ktime_get ());wake_up_process (rqstp->rq_task);rcu_read_unlock ();net/sunrpc/svc_xprt.c +0
-4@@ -715,10 +715,6 @@ rqst_should_sleep(struct svc_rqst *rqstp)if (svc_thread_should_stop (rqstp))return false; -/* are we freezing? */ -if (freezing (current)) -return false; -#if defined(CONFIG_SUNRPC_BACKCHANNEL )if (svc_is_backchannel (rqstp)) {if (!list_empty (&rqstp->rq_server->sv_cb_list))
net/sunrpc/svc_xprt.c static void svc_rqst_wait_for_work(struct svc_rqst *rqstp) · line 733
Quoted 8 lines
net/sunrpc/svc_xprt.c +1
-4@@ -734,30 +730,32 @@ static void svc_rqst_wait_for_work(struct svc_rqst *rqstp)struct svc_pool *pool = rqstp->rq_pool;if (rqst_should_sleep (rqstp)) { -set_current_state (TASK_IDLE ); -spin_lock_bh (&pool->sp_lock); -list_add (&rqstp->rq_idle, &pool->sp_idle_threads); -spin_unlock_bh (&pool->sp_lock); +set_current_state (TASK_IDLE |TASK_FREEZABLE );
I'm trying to learn about the semantics of IDLE|FREEZABLE, but there isn't another instance of this flag combination in the kernel.
net/sunrpc/svc_xprt.c static void svc_rqst_wait_for_work(struct svc_rqst *rqstp) · line 746
Quoted 13 lines
net/sunrpc/svc_xprt.c +13
-0+llist_add (&rqstp->rq_idle, &pool->sp_idle_threads); + +if (unlikely (!rqst_should_sleep (rqstp))) +/* maybe there were no idle threads when some work +* became ready and so nothing was woken. We've just +* become idle so someone can to the work - maybe us. +* But we cannot reliably remove ourselves from the +* idle list - we can only remove the first task which +* might be us, and might not. +* So remove and wake it, then schedule(). If it was +* us, we won't sleep. If it is some other thread, they +* will do the work. +*/
Large block comments suggest that this code doesn't document itself very well.
At least, some of the textual redundancy can be removed, though this comment and the next are removed or replaced in later patches. I'll leave it up to you to find an approach that is a bit cleaner.
net/sunrpc/svc_xprt.c static void svc_rqst_wait_for_work(struct svc_rqst *rqstp) · line 753
Quoted 10 lines
net/sunrpc/svc_xprt.c +5
-3+svc_pool_wake_idle_thread (pool); -/* Need to check should_sleep() again after -* setting task state in case a wakeup happened -* between testing and setting. +/* We mustn't continue while on the idle list, and we +* cannot remove outselves reliably. The only "work" +* we can do while on the idle list is to freeze. +* So loop until someone removes us */
Here and above, the use of "we" obscures the explanation. What is "we" in this context? I think it might be "this thread" or @rqstp, but I can't be certain. For instance:
/* Since a thread cannot remove itself from an llist, * schedule until someone else removes @rqstp from * the idle list. */
net/sunrpc/svc_xprt.c static void svc_rqst_wait_for_work(struct svc_rqst *rqstp) · line 754
Quoted 2 lines
net/sunrpc/svc_xprt.c +1
-1-if (rqst_should_sleep (rqstp)) { +while (!svc_thread_busy (rqstp)) {
I need to convince myself that this while() can't possibly result in an infinite loop.
Quoted 19 lines
net/sunrpc/svc_xprt.c +2
-12schedule (); - }else { -__set_current_state (TASK_RUNNING ); -cond_resched (); - } - -/* We *must* be removed from the list before we can continue. -* If we were woken, this is already done -*/ -if (!svc_thread_busy (rqstp)) { -spin_lock_bh (&pool->sp_lock); -list_del_init (&rqstp->rq_idle); -spin_unlock_bh (&pool->sp_lock); +set_current_state (TASK_IDLE |TASK_FREEZABLE ); } +__set_current_state (TASK_RUNNING ); }else {cond_resched ();
Quoted 1 line
}
There's a try_to_freeze() call just after this hunk. Is there a reason to invoke cond_resched(); before freezing?
Quoted 16 lines
net/sunrpc/svc_xprt.c +3
-2@@ -870,9 +868,10 @@ void svc_recv(struct svc_rqst *rqstp)struct svc_xprt *xprt = rqstp->rq_xprt;/* Normally we will wait up to 5 seconds for any required -* cache information to be provided. +* cache information to be provided. When there are no +* idle threads, we reduce the wait time. */ -if (!list_empty (&pool->sp_idle_threads)) +if (pool->sp_idle_threads.first) rqstp->rq_chandle.thread_wait =5 *HZ ;else rqstp->rq_chandle.thread_wait =1 *HZ ;Signature · 1 line
2.40.1
Signature · 1 line
Chuck Lever
Re: [PATCH 1/7] SUNRPC: change service idle list to be an llist
Quoted 23 lines · Chuck Lever
Quoted 22 lines · NeilBrown
With an llist we don't need to take a lock to add a thread to the list, though we still need a lock to remove it. That will go in the next patch.
Unlike double-linked lists, a thread cannot reliably remove itself from the list. Only the first thread can be removed, and that can change asynchronously. So some care is needed.
We already check if there is pending work to do, so we are unlikely to add ourselves to the idle list and then want to remove ourselves again.
If we DO find something needs to be done after adding ourselves to the list, we simply wake up the first thread on the list. If that was us, we successfully removed ourselves and can continue. If it was some other thread, they will do the work that needs to be done. We can safely sleep until woken.
We also remove the test on
freezing()fromrqst_should_sleep(). Instead we setTASK_FREEZABLEbefore scheduling. This makes is safe toschedule()when a freeze is pending. As we now loop waiting to be removed from the idle queue, this is a cleaner way to handle freezing.
Quoted 8 lines
Quoted 4 lines
task_state_index()incorrectly identifies a task with
TASK_IDLE|TASK_FREEZABLE
as 'D'. So fix__task_state_indexto ignore extra flags onTASK_IDLEjust as it ignores extra flags on other states.Let's split the
task_state_index()change into a separate patch that can be sent to the scheduler maintainers for their Review/Ack.
I've sent that to appropriate people. It doesn't really matter if it lands before or after we change sunrpc to use TASK_FREEZABLE. The only problem is that nfsd threads look like "D" in a ps listing rather than "I". The actually behaviour of the threads isn't changed.
Quoted 64 lines
Quoted 64 lines
Signed-off-by NeilBrown <[email protected]>--- include/linux/sched.h | 4 +-- include/linux/sunrpc/svc.h | 14 ++++++----- net/sunrpc/svc.c | 13 +++++----- net/sunrpc/svc_xprt.c | 51 +++++++++++++++++++------------------- 4 files changed, 42 insertions(+), 40 deletions(-)include/linux/sched.h +2
-2@@ -1666,7 +1666,7 @@ static inline unsigned int __task_state_index(unsigned int tsk_state,BUILD_BUG_ON_NOT_POWER_OF_2 (TASK_REPORT_MAX ); -if (tsk_state ==TASK_IDLE ) +if ((tsk_state &TASK_IDLE ) ==TASK_IDLE ) state =TASK_REPORT_IDLE ;/* @@ -1674,7 +1674,7 @@ static inline unsigned int __task_state_index(unsigned int tsk_state,* to userspace, we can make this appear as if the task has gone through * a regular rt_mutex_lock() call. */ -if (tsk_state ==TASK_RTLOCK_WAIT ) +if (tsk_state &TASK_RTLOCK_WAIT ) state =TASK_UNINTERRUPTIBLE ;return fls (state);include/linux/sunrpc/svc.h +4
-4@@ -37,7 +37,7 @@ struct svc_pool {struct list_head sp_sockets;/* pending sockets */ unsigned int sp_nrthreads;/* # of threads in pool */ struct list_head sp_all_threads;/* all server threads */ -struct list_head sp_idle_threads;/* idle server threads */ +struct llist_head sp_idle_threads;/* idle server threads */ /* statistics on pool operation */ struct percpu_counter sp_messages_arrived;@@ -186,7 +186,7 @@ extern u32 svc_max_payload(const struct svc_rqst *rqstp);*/ struct svc_rqst {struct list_head rq_all;/* all threads list */ -struct list_head rq_idle;/* On the idle list */ +struct llist_node rq_idle;/* On the idle list */ struct rcu_head rq_rcu_head;/* for RCU deferred kfree */ struct svc_xprt * rq_xprt;/* transport ptr */ @@ -270,22 +270,24 @@ enum {* svc_thread_set_busy - mark a thread as busy * @rqstp: the thread which is now busy * -* If rq_idle is "empty", the thread must be busy. +* By convention a thread is busy if rq_idle.next points to rq_idle. +* This ensures it is not on the idle list. */ static inline void svc_thread_set_busy (struct svc_rqst *rqstp) { -INIT_LIST_HEAD (&rqstp->rq_idle);
Quoted 8 lines
include/linux/sunrpc/svc.h enum { · line 279
Quoted 2 lines
include/linux/sunrpc/svc.h +1
-0+ rqstp->rq_idle.next = &rqstp->rq_idle; }I don't understand the comment "This ensures it is not on the idle list."
svc_thread_set_busy()is called in two places: The first when ansvc_rqstis created, and once directly after anllist_del_first()has been done. @rqstp is already not on the idle list in either case.
"ensures" is the wrong word. Maybe I meant "assures" or even "records". I change it to
* This will never be the case for threads on the idle list.
Quoted 4 lines
What really needs an explanation here is that there's no existing utility to check whether an
llist_nodeis on a list or not.
I guess no-one found the need before...
Though I note that md/raid5.c has a bit flag STRIPE_ON_RELEASE_LIST to check if the stripe is on a llist. So it could use that functionality.
I would happily include a patch to add this to llist.h, except that by the end of the series we don't need it any more...
Maybe I should do it anyway?
Quoted 10 lines
include/linux/sunrpc/svc.h enum { · line 286
Quoted 9 lines
include/linux/sunrpc/svc.h +2
-1/** * svc_thread_busy - check if a thread as busy * @rqstp: the thread which might be busy * -* If rq_idle is "empty", the thread must be busy. +* By convention a thread is busy if rq_idle.next points to rq_idle. +* This ensures it is not on the idle list.
Quoted 2 lines
This function doesn't modify the thread, so it can't ensure it is not on the idle list.
I changed that to
* This will never be the case for threads on the idle list.
too.
Quoted 6 lines
include/linux/sunrpc/svc.h enum { · line 288
Quoted 4 lines
include/linux/sunrpc/svc.h +0
-0*/ static inline bool svc_thread_busy (struct svc_rqst *rqstp)const struct
svc_rqst*rqstp
ack.
Quoted 60 lines
Quoted 60 lines
include/linux/sunrpc/svc.h +1
-1{ -return list_empty (&rqstp->rq_idle); +return rqstp->rq_idle.next == &rqstp->rq_idle; }#define SVC_NET (rqst) (rqst->rq_xprt ? rqst->rq_xprt->xpt_net : rqst->rq_bc_net)net/sunrpc/svc.c +7
-6@@ -510,7 +510,7 @@ __svc_create(struct svc_program *prog, unsigned int bufsize, int npools, pool->sp_id = i;INIT_LIST_HEAD (&pool->sp_sockets);INIT_LIST_HEAD (&pool->sp_all_threads); -INIT_LIST_HEAD (&pool->sp_idle_threads); +init_llist_head (&pool->sp_idle_threads);spin_lock_init (&pool->sp_lock);percpu_counter_init (&pool->sp_messages_arrived,0 ,GFP_KERNEL );@@ -701,15 +701,16 @@ svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)void svc_pool_wake_idle_thread (struct svc_pool *pool) {struct svc_rqst *rqstp; +struct llist_node *ln;rcu_read_lock ();spin_lock_bh (&pool->sp_lock); - rqstp =list_first_entry_or_null (&pool->sp_idle_threads, - struct svc_rqst, rq_idle); -if (rqstp) -list_del_init (&rqstp->rq_idle); + ln =llist_del_first (&pool->sp_idle_threads);spin_unlock_bh (&pool->sp_lock); -if (rqstp) { +if (ln) { + rqstp = llist_entry(ln, struct svc_rqst, rq_idle); +svc_thread_set_busy (rqstp); +WRITE_ONCE (rqstp->rq_qtime,ktime_get ());wake_up_process (rqstp->rq_task);rcu_read_unlock ();net/sunrpc/svc_xprt.c +0
-4@@ -715,10 +715,6 @@ rqst_should_sleep(struct svc_rqst *rqstp)if (svc_thread_should_stop (rqstp))return false; -/* are we freezing? */ -if (freezing (current)) -return false; -#if defined(CONFIG_SUNRPC_BACKCHANNEL )if (svc_is_backchannel (rqstp)) {if (!list_empty (&rqstp->rq_server->sv_cb_list))@@ -734,30 +730,32 @@ static void svc_rqst_wait_for_work(struct svc_rqst *rqstp)struct svc_pool *pool = rqstp->rq_pool;
Quoted 9 lines
net/sunrpc/svc_xprt.c static void svc_rqst_wait_for_work(struct svc_rqst *rqstp) · line 733
Quoted 6 lines
net/sunrpc/svc_xprt.c +1
-4if (rqst_should_sleep (rqstp)) { -set_current_state (TASK_IDLE ); -spin_lock_bh (&pool->sp_lock); -list_add (&rqstp->rq_idle, &pool->sp_idle_threads); -spin_unlock_bh (&pool->sp_lock); +set_current_state (TASK_IDLE |TASK_FREEZABLE );I'm trying to learn about the semantics of IDLE|FREEZABLE, but there isn't another instance of this flag combination in the kernel.
No - which I why no one else notice that the state reported by ps was wrong.
Quite a few places use INTERRUPTIBLE|FREEZABLE which is much the same thing except sunrpc threads don't want interrupts.
My interpretation of FREEZABLE is that it avoids the need to check for freezing, or to freeze. A higher-level interface.
Quoted 18 lines
Quoted 5 lines
net/sunrpc/svc_xprt.c +3
-0+llist_add (&rqstp->rq_idle, &pool->sp_idle_threads); + +if (unlikely (!rqst_should_sleep (rqstp)))net/sunrpc/svc_xprt.c static void svc_rqst_wait_for_work(struct svc_rqst *rqstp) · line 746
Quoted 10 lines
net/sunrpc/svc_xprt.c +10
-0+/* maybe there were no idle threads when some work +* became ready and so nothing was woken. We've just +* become idle so someone can to the work - maybe us. +* But we cannot reliably remove ourselves from the +* idle list - we can only remove the first task which +* might be us, and might not. +* So remove and wake it, then schedule(). If it was +* us, we won't sleep. If it is some other thread, they +* will do the work. +*/ Large block comments suggest that this code doesn't document itself very well.
Self documenting code is best, but when the logic is subtle and unfamiliar, a bit of text can help.
Of course the logic is quite familiar to me now and doesn't seem so subtle, so I wonder if any comment it needed.
Quoted 4 lines
At least, some of the textual redundancy can be removed, though this comment and the next are removed or replaced in later patches. I'll leave it up to you to find an approach that is a bit cleaner.
Is now:
/* Work just became available. This thread cannot simply
* choose not to sleep as it *must* wait until removed.
* So wake the first waiter - whether it is this
* thread or some other, it will get the work done. */
Quoted 17 lines
net/sunrpc/svc_xprt.c static void svc_rqst_wait_for_work(struct svc_rqst *rqstp) · line 753
Quoted 12 lines
net/sunrpc/svc_xprt.c +5
-3+svc_pool_wake_idle_thread (pool); -/* Need to check should_sleep() again after -* setting task state in case a wakeup happened -* between testing and setting. +/* We mustn't continue while on the idle list, and we +* cannot remove outselves reliably. The only "work" +* we can do while on the idle list is to freeze. +* So loop until someone removes us */ Here and above, the use of "we" obscures the explanation. What is "we" in this context? I think it might be "this thread" or @rqstp, but I can't be certain. For instance:
Quoted 4 lines
/* Since a thread cannot remove itself from an llist, * schedule until someone else removes @rqstp from * the idle list. */
Looks good - thanks.
Quoted 8 lines
net/sunrpc/svc_xprt.c static void svc_rqst_wait_for_work(struct svc_rqst *rqstp) · line 754
Quoted 4 lines
net/sunrpc/svc_xprt.c +1
-1-if (rqst_should_sleep (rqstp)) { +while (!svc_thread_busy (rqstp)) {I need to convince myself that this
while()can't possibly result in an infinite loop.
It cannot be a busy-loop because we set state and call schedule(). It could loop for a long time if there is no work to do. But at service shutdown, everything will be removed from the busy list and woken, so the loop will exit then.
Quoted 18 lines
Quoted 18 lines
net/sunrpc/svc_xprt.c +2
-12schedule (); - }else { -__set_current_state (TASK_RUNNING ); -cond_resched (); - } - -/* We *must* be removed from the list before we can continue. -* If we were woken, this is already done -*/ -if (!svc_thread_busy (rqstp)) { -spin_lock_bh (&pool->sp_lock); -list_del_init (&rqstp->rq_idle); -spin_unlock_bh (&pool->sp_lock); +set_current_state (TASK_IDLE |TASK_FREEZABLE ); } +__set_current_state (TASK_RUNNING );
Quoted 6 lines
Quoted 3 lines
net/sunrpc/svc_xprt.c +0
-0}else {cond_resched ();}
There's a
try_to_freeze()call just after this hunk. Is there a reason to invokecond_resched(); before freezing?
try_to_freeze() can compile to {return false;}. Even when not, it usually does not more than tests a global variable. So the presence of try_to_freeze() has no bearing on the use of cond_resched().
Quoted 5 lines
Quoted 5 lines
net/sunrpc/svc_xprt.c +0
-0@@ -870,9 +868,10 @@ void svc_recv(struct svc_rqst *rqstp)struct svc_xprt *xprt = rqstp->rq_xprt;
Quoted 13 lines
Quoted 13 lines
net/sunrpc/svc_xprt.c +3
-2/* Normally we will wait up to 5 seconds for any required -* cache information to be provided. +* cache information to be provided. When there are no +* idle threads, we reduce the wait time. */ -if (!list_empty (&pool->sp_idle_threads)) +if (pool->sp_idle_threads.first) rqstp->rq_chandle.thread_wait =5 *HZ ;else rqstp->rq_chandle.thread_wait =1 *HZ ;Signature · 1 line
2.40.1
I'll send a new batch today I hope. I decided to move lwq into lib/ and make all the changes to llist in separate patches.
Thanks,
NeilBrown
PATCH 2/7 SUNRPC: only have one thread waking up at a time
In reply to NeilBrownCurrently if several items of work become available in quick succession, that number of threads (if available) will be woken. By the time some of them wake up another thread that was already cache-warm might have come along and completed the work. Anecdotal evidence suggests as many as 15% of wakes find nothing to do once they get to the point of looking.
This patch changes svc_pool_wake_idle_thread() to wake the first thread on the queue but NOT remove it. Subsequent calls will wake the same thread. Once that thread starts it will dequeue itself and after dequeueing some work to do, it will wake the next thread if there is more work ready. This results in a more orderly increase in the number of busy threads.
As a bonus, this allows us to reduce locking around the idle queue. svc_pool_wake_idle_thread() no longer needs to take a lock (beyond rcu_read_lock()) as it doesn't manipulate the queue, it just looks at the first item.
The thread itself can avoid locking by using the new llist_del_first_this() interface. This will safely remove the thread itself if it is the head. If it isn't the head, it will do nothing. If multiple threads call this concurrently only one will succeed. The others will do nothing, so no corruption can result.
If a thread wakes up and finds that it cannot dequeue itself that means either
- that it wasn't woken because it was the head of the queue. Maybe the freezer woke it. In that case it can go back to sleep (after trying to freeze of course).
- some other thread found there was nothing to do very recently, and placed itself on the head of the queue in front of this thread. It must check again after placing itself there, so it can be deemed to be responsible for any pending work, and this thread can go back to sleep until woken.
No code ever tests for busy threads any more. Only each thread itself cares if it is busy. So functions to set and test the busy status are no longer needed.
--- include/linux/llist.h | 2 ++ include/linux/sunrpc/svc.h | 24 -------------------- lib/llist.c | 27 ++++++++++++++++++++++ net/sunrpc/svc.c | 15 +++++-------- net/sunrpc/svc_xprt.c | 46 ++++++++++++++++++++++---------------- 5 files changed, 62 insertions(+), 52 deletions(-)
include/linux/llist.h +2 -0
@@ -248,6 +248,8 @@ static inline struct llist_node *__llist_del_all(struct llist_head *head) }extern struct llist_node *llist_del_first (struct llist_head *head); +extern struct llist_node *llist_del_first_this (struct llist_head *head, +struct llist_node *this);struct llist_node *llist_reverse_order (struct llist_node *head);
include/linux/sunrpc/svc.h +0 -24
@@ -266,30 +266,6 @@ enum { RQ_DATA, /* request has data */ }; -/** -* svc_thread_set_busy - mark a thread as busy -* @rqstp: the thread which is now busy -* -* By convention a thread is busy if rq_idle.next points to rq_idle. -* This ensures it is not on the idle list. -*/ -static inline void svc_thread_set_busy (struct svc_rqst *rqstp) -{ - rqstp->rq_idle.next = &rqstp->rq_idle; -} - -/** -* svc_thread_busy - check if a thread as busy -* @rqstp: the thread which might be busy -* -* By convention a thread is busy if rq_idle.next points to rq_idle. -* This ensures it is not on the idle list. -*/ -static inline bool svc_thread_busy (struct svc_rqst *rqstp) -{ -return rqstp->rq_idle.next == &rqstp->rq_idle; -} -#define SVC_NET (rqst) (rqst->rq_xprt ? rqst->rq_xprt->xpt_net : rqst->rq_bc_net)/*
lib/llist.c +27 -0
@@ -65,6 +65,33 @@ struct llist_node *llist_del_first(struct llist_head *head) }EXPORT_SYMBOL_GPL (llist_del_first); +/** +* llist_del_first_this - delete given entry of lock-less list if it is first +* @head: the head for your lock-less list +* @this: a list entry. +* +* If head of the list is given entry, delete and return it, else +* return %NULL. +* +* Providing the caller has exclusive access to @this, multiple callers can +* safely call this concurrently with multiple llist_add() callers. +*/ +struct llist_node *llist_del_first_this (struct llist_head *head, +struct llist_node *this) +{ +struct llist_node *entry, *next; + + entry =smp_load_acquire (&head->first); +do { +if (entry != this) +return NULL ; + next =READ_ONCE (entry->next); + }while (!try_cmpxchg (&head->first, &entry, next)); + +return entry; +} +EXPORT_SYMBOL_GPL (llist_del_first_this); +/** * llist_reverse_order - reverse order of a llist chain * @head: first item of the list to be reversed
net/sunrpc/svc.c +6 -9
@@ -642,7 +642,6 @@ svc_rqst_alloc(struct svc_serv *serv, struct svc_pool *pool, int node)folio_batch_init (&rqstp->rq_fbatch); -svc_thread_set_busy (rqstp); rqstp->rq_server = serv; rqstp->rq_pool = pool;
@@ -704,18 +703,16 @@ void svc_pool_wake_idle_thread(struct svc_pool *pool)struct llist_node *ln;rcu_read_lock (); -spin_lock_bh (&pool->sp_lock); - ln =llist_del_first (&pool->sp_idle_threads); -spin_unlock_bh (&pool->sp_lock); + ln =READ_ONCE (pool->sp_idle_threads.first);if (ln) { rqstp = llist_entry(ln, struct svc_rqst, rq_idle); -svc_thread_set_busy (rqstp); -WRITE_ONCE (rqstp->rq_qtime,ktime_get ()); -wake_up_process (rqstp->rq_task); +if (!task_is_running (rqstp->rq_task)) { +wake_up_process (rqstp->rq_task); +trace_svc_wake_up (rqstp->rq_task->pid); +percpu_counter_inc (&pool->sp_threads_woken); + }rcu_read_unlock (); -percpu_counter_inc (&pool->sp_threads_woken); -trace_svc_wake_up (rqstp->rq_task->pid);return ; }rcu_read_unlock ();
net/sunrpc/svc_xprt.c +27 -19
78 lines not on this page · raw message
Signature · 1 line
2.40.1
PATCH 3/7 SUNRPC/svc: add light-weight queuing mechanism.
In reply to NeilBrownlwq is a FIFO single-linked queue that only requires a spinlock for dequeueing, which happens in process context. Enqueueing is atomic with no spinlock and can happen in any context.
Include a unit test for basic functionality - runs a boot/module-load time. Does not use kunit framework.
--- include/linux/sunrpc/svc_lwq.h | 80 +++++++++++++++++++ net/sunrpc/Kconfig | 6 ++ net/sunrpc/Makefile | 2 +- net/sunrpc/svc_lwq.c | 141 +++++++++++++++++++++++++++++++++ 4 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 include/linux/sunrpc/svc_lwq.h create mode 100644 net/sunrpc/svc_lwq.c
include/linux/sunrpc/svc_lwq.h new file +80 -0
@@ -0,0 +1,80 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SUNRPC_SVC_LWQ_H +#define SUNRPC_SVC_LWQ_H + +/* +* light-weight single linked queue +* +* Entries can be enqueued from any context with no locking. +* Entries can be dequeued from process context with integrated locking. +* +*/ +#include <linux/container_of.h> +#include <linux/spinlock.h> +#include <linux/llist.h> + +struct lwq_node { +struct llist_node node; +}; + +struct lwq { +spinlock_t lock; +struct llist_node *ready;/* entries to be dequeued */ +struct llist_head new;/* entries being enqueued */ +}; + +static inline void lwq_init (struct lwq *q) +{ +spin_lock_init (&q->lock); + q->ready =NULL ; +init_llist_head (&q->new); +} + +static inline bool lwq_empty (struct lwq *q) +{ +return smp_load_acquire (&q->ready) ==NULL &&llist_empty (&q->new); +} + +struct llist_node *__lwq_dequeue (struct lwq *q); +#define lwq_dequeue (_q, _type, _member) \ + ({ struct llist_node *_n = __lwq_dequeue(_q); \ + _n ? container_of(_n, _type, _member.node) : NULL; }) + +struct llist_node *lwq_dequeue_all (struct lwq *q); + +/** +* lwq_for_each_safe: iterate over detached queue allowing deletion +* @_n: iterator variable +* @_t1: temporary struct llist_node ** +* @_t2: temporary struct llist_node * +* @_l: address of llist_node pointer from lwq_dequeue_all() +* @_member: member in _n where lwq_node is found. +* +* Iterate over members in a dequeued list. If the iterator variable +* is set to NULL, the iterator removes that entry from the queue. +*/ +#define lwq_for_each_safe (_n, _t1, _t2, _l, _member) \ + for (_t1 = (_l); \ + *(_t1) ? (_n = container_of(*(_t1), typeof(*(_n)), _member.node),\ + _t2 = ((*_t1)->next), \ + true) \ + : false; \ + (_n) ? (_t1 = &(_n)->_member.node.next, 0) \ + : ((*(_t1) = (_t2)), 0)) + +static inline bool lwq_enqueue (struct lwq_node *n,struct lwq *q) +{ +return llist_add (&n->node, &q->new) && +smp_load_acquire (&q->ready) ==NULL ; +} + +static inline bool lwq_enqueue_batch (struct llist_node *n,struct lwq *q) +{ +struct llist_node *e = n; + +return llist_add_batch (llist_reverse_order (n), e, &q->new) && +smp_load_acquire (&q->ready) ==NULL ; +} + +#endif /* SUNRPC_SVC_LWQ_H */
net/sunrpc/Kconfig +6 -0
@@ -115,3 +115,9 @@ config SUNRPC_XPRT_RDMA If unsure, or you know there is no RDMA capability on your hardware platform, say N. + +config SUNRPC_LWQ_TEST + bool "RPC: enable boot-time test for lwq queuing" + depends on SUNRPC + help + Enable boot-time test of lwq functionality.
net/sunrpc/Makefile +1 -1
@@ -10,7 +10,7 @@ obj-$(CONFIG_SUNRPC_XPRT_RDMA) += xprtrdma/ sunrpc-y := clnt.o xprt.o socklib.o xprtsock.o sched.o \ auth.o auth_null.o auth_tls.o auth_unix.o \ - svc.o svcsock.o svcauth.o svcauth_unix.o \ + svc.o svc_lwq.o svcsock.o svcauth.o svcauth_unix.o \ addr.o rpcb_clnt.o timer.o xdr.o \ sunrpc_syms.o cache.o rpc_pipe.o sysfs.o \ svc_xprt.o \
net/sunrpc/svc_lwq.c new file +141 -0
141 lines not on this page · raw message
Signature · 1 line
2.40.1
PATCH 4/7 SUNRPC: use lwq for sp_sockets - renamed to sp_xprts
In reply to NeilBrownlwq avoids using back pointers in lists, and uses less locking. This introduces a new spinlock, but the other one will be removed in a future patch.
For svc_clean_up_xprts(), we now dequeue the entire queue, walk it to remove and process the xprts that need cleaning up, then re-enqueue the remaining queue.
--- include/linux/sunrpc/svc.h | 3 +- include/linux/sunrpc/svc_xprt.h | 2 +- net/sunrpc/svc.c | 2 +- net/sunrpc/svc_xprt.c | 57 ++++++++++----------------------- 4 files changed, 21 insertions(+), 43 deletions(-)
include/linux/sunrpc/svc.h +2 -1
@@ -17,6 +17,7 @@#include <linux/sunrpc/xdr.h> #include <linux/sunrpc/auth.h> #include <linux/sunrpc/svcauth.h> +#include <linux/sunrpc/svc_lwq.h> #include <linux/wait.h> #include <linux/mm.h> #include <linux/pagevec.h>
@@ -34,7 +35,7 @@struct svc_pool {unsigned int sp_id;/* pool id; also node id on NUMA */ spinlock_t sp_lock;/* protects all fields */ -struct list_head sp_sockets;/* pending sockets */ +struct lwq sp_xprts;/* pending transports */ unsigned int sp_nrthreads;/* # of threads in pool */ struct list_head sp_all_threads;/* all server threads */ struct llist_head sp_idle_threads;/* idle server threads */
include/linux/sunrpc/svc_xprt.h +1 -1
@@ -54,7 +54,7 @@ struct svc_xprt {const struct svc_xprt_ops *xpt_ops;struct kref xpt_ref;struct list_head xpt_list; -struct list_head xpt_ready; +struct lwq_node xpt_ready;unsigned long xpt_flags;struct svc_serv *xpt_server;/* service for transport */
net/sunrpc/svc.c +1 -1
@@ -508,7 +508,7 @@ __svc_create(struct svc_program *prog, unsigned int bufsize, int npools, i, serv->sv_name); pool->sp_id = i; -INIT_LIST_HEAD (&pool->sp_sockets); +lwq_init (&pool->sp_xprts);INIT_LIST_HEAD (&pool->sp_all_threads);init_llist_head (&pool->sp_idle_threads);spin_lock_init (&pool->sp_lock);
net/sunrpc/svc_xprt.c +17 -40
101 lines not on this page · raw message
Signature · 1 line
2.40.1
PATCH 5/7 SUNRPC: change sp_nrthreads to atomic_t
In reply to NeilBrownUsing an atomic_t avoids the need to take a spinlock (which can soon be removed).
Choosing a thread to kill needs to be careful as we cannot set the "die now" bit atomically with the test on the count. Instead we temporarily increase the count.
--- fs/nfsd/nfssvc.c | 3 ++- include/linux/sunrpc/svc.h | 2 +- net/sunrpc/svc.c | 37 ++++++++++++++++++++----------------- 3 files changed, 23 insertions(+), 19 deletions(-)
fs/nfsd/nfssvc.c +2 -1
@@ -718,7 +718,8 @@ int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)if (nn->nfsd_serv !=NULL ) {for (i =0 ; i < nn->nfsd_serv->sv_nrpools && i < n; i++) - nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads; + nthreads[i] = +atomic_read (&nn->nfsd_serv->sv_pools[i].sp_nrthreads); }return 0 ;
include/linux/sunrpc/svc.h +1 -1
@@ -36,7 +36,7 @@ struct svc_pool {unsigned int sp_id;/* pool id; also node id on NUMA */ spinlock_t sp_lock;/* protects all fields */ struct lwq sp_xprts;/* pending transports */ -unsigned int sp_nrthreads;/* # of threads in pool */ +atomic_t sp_nrthreads;/* # of threads in pool */ struct list_head sp_all_threads;/* all server threads */ struct llist_head sp_idle_threads;/* idle server threads */
net/sunrpc/svc.c +20 -17
@@ -681,8 +681,8 @@ svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node) serv->sv_nrthreads +=1 ;spin_unlock_bh (&serv->sv_lock); +atomic_inc (&pool->sp_nrthreads);spin_lock_bh (&pool->sp_lock); - pool->sp_nrthreads++;list_add_rcu (&rqstp->rq_all, &pool->sp_all_threads);spin_unlock_bh (&pool->sp_lock);return rqstp;
@@ -727,23 +727,24 @@ svc_pool_next(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state) }static struct svc_pool * -svc_pool_victim (struct svc_serv *serv,struct svc_pool *pool,unsigned int *state) +svc_pool_victim (struct svc_serv *serv,struct svc_pool *target_pool, +unsigned int *state) { +struct svc_pool *pool;unsigned int i; +retry: + pool = target_pool; +if (pool !=NULL ) { -spin_lock_bh (&pool->sp_lock); -if (pool->sp_nrthreads) +if (atomic_inc_not_zero (&pool->sp_nrthreads)) goto found_pool; -spin_unlock_bh (&pool->sp_lock);return NULL ; }else {for (i =0 ; i < serv->sv_nrpools; i++) { pool = &serv->sv_pools[--(*state) % serv->sv_nrpools]; -spin_lock_bh (&pool->sp_lock); -if (pool->sp_nrthreads) +if (atomic_inc_not_zero (&pool->sp_nrthreads)) goto found_pool; -spin_unlock_bh (&pool->sp_lock); }return NULL ; }
@@ -751,8 +752,12 @@ svc_pool_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *stat found_pool:set_bit (SP_VICTIM_REMAINS , &pool->sp_flags);set_bit (SP_NEED_VICTIM , &pool->sp_flags); -spin_unlock_bh (&pool->sp_lock); -return pool; +if (!atomic_dec_and_test (&pool->sp_nrthreads)) +return pool; +/* Nothing left in this pool any more */ +clear_bit (SP_NEED_VICTIM , &pool->sp_flags); +clear_bit (SP_VICTIM_REMAINS , &pool->sp_flags); + goto retry; } static int
@@ -828,13 +833,10 @@ svc_stop_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)int svc_set_num_threads (struct svc_serv *serv,struct svc_pool *pool,int nrservs) { -if (pool ==NULL ) { +if (!pool) nrservs -= serv->sv_nrthreads; - }else { -spin_lock_bh (&pool->sp_lock); - nrservs -= pool->sp_nrthreads; -spin_unlock_bh (&pool->sp_lock); - } +else + nrservs -=atomic_read (&pool->sp_nrthreads);if (nrservs >0 )return svc_start_kthreads (serv, pool, nrservs);
@@ -921,10 +923,11 @@ svc_exit_thread(struct svc_rqst *rqstp)struct svc_pool *pool = rqstp->rq_pool;spin_lock_bh (&pool->sp_lock); - pool->sp_nrthreads--;list_del_rcu (&rqstp->rq_all);spin_unlock_bh (&pool->sp_lock); +atomic_dec (&pool->sp_nrthreads); +spin_lock_bh (&serv->sv_lock); serv->sv_nrthreads -=1 ;spin_unlock_bh (&serv->sv_lock);
Signature · 1 line
2.40.1
PATCH 6/7 SUNRPC: discard sp_lock
In reply to NeilBrownsp_lock is now only used to protect sp_all_threads. This isn't needed as sp_all_threads is only manipulated through svc_set_num_threads(), which must be locked. Read-acccess only requires rcu_read_lock(). So no more locking is needed.
--- include/linux/sunrpc/svc.h | 1 - net/sunrpc/svc.c | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-)
include/linux/sunrpc/svc.h +0 -1
@@ -34,7 +34,6 @@*/ struct svc_pool {unsigned int sp_id;/* pool id; also node id on NUMA */ -spinlock_t sp_lock;/* protects all fields */ struct lwq sp_xprts;/* pending transports */ atomic_t sp_nrthreads;/* # of threads in pool */ struct list_head sp_all_threads;/* all server threads */
net/sunrpc/svc.c +5 -5
@@ -511,7 +511,6 @@ __svc_create(struct svc_program *prog, unsigned int bufsize, int npools,lwq_init (&pool->sp_xprts);INIT_LIST_HEAD (&pool->sp_all_threads);init_llist_head (&pool->sp_idle_threads); -spin_lock_init (&pool->sp_lock);percpu_counter_init (&pool->sp_messages_arrived,0 ,GFP_KERNEL );percpu_counter_init (&pool->sp_sockets_queued,0 ,GFP_KERNEL );
@@ -682,9 +681,12 @@ svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)spin_unlock_bh (&serv->sv_lock);atomic_inc (&pool->sp_nrthreads); -spin_lock_bh (&pool->sp_lock); + +/* Protected by whatever lock the service uses when calling +* svc_set_num_threads() +*/ list_add_rcu (&rqstp->rq_all, &pool->sp_all_threads); -spin_unlock_bh (&pool->sp_lock); +return rqstp; }
@@ -922,9 +924,7 @@ svc_exit_thread(struct svc_rqst *rqstp)struct svc_serv *serv = rqstp->rq_server;struct svc_pool *pool = rqstp->rq_pool; -spin_lock_bh (&pool->sp_lock);list_del_rcu (&rqstp->rq_all); -spin_unlock_bh (&pool->sp_lock);atomic_dec (&pool->sp_nrthreads);
Signature · 1 line
2.40.1
PATCH 7/7 SUNRPC: change the back-channel queue to lwq
In reply to NeilBrownThis removes the need to store and update back-links in the list. It also remove the need for the _bh version of spin_lock().
--- include/linux/sunrpc/svc.h | 3 +-- include/linux/sunrpc/xprt.h | 3 ++- net/sunrpc/backchannel_rqst.c | 5 +---- net/sunrpc/svc.c | 3 +-- net/sunrpc/svc_xprt.c | 12 +++--------- net/sunrpc/xprtrdma/backchannel.c | 4 +--- 6 files changed, 9 insertions(+), 21 deletions(-)
include/linux/sunrpc/svc.h +1 -2
@@ -90,10 +90,9 @@ struct svc_serv {int (*sv_threadfn)(void *data);#if defined(CONFIG_SUNRPC_BACKCHANNEL ) -struct list_head sv_cb_list;/* queue for callback requests +struct lwq sv_cb_list;/* queue for callback requests * that arrive over the same * connection */ -spinlock_t sv_cb_lock;/* protects the svc_cb_list */ bool sv_bc_enabled;/* service uses backchannel */ #endif /* CONFIG_SUNRPC_BACKCHANNEL */ };
include/linux/sunrpc/xprt.h +2 -1
@@ -57,6 +57,7 @@ struct xprt_class;struct seq_file ;struct svc_serv ;struct net ; +#include <linux/sunrpc/svc_lwq.h> /* * This describes a complete RPC request
@@ -121,7 +122,7 @@ struct rpc_rqst {int rq_ntrans;#if defined(CONFIG_SUNRPC_BACKCHANNEL ) -struct list_head rq_bc_list;/* Callback service list */ +struct lwq_node rq_bc_list;/* Callback service list */ unsigned long rq_bc_pa_state;/* Backchannel prealloc state */ struct list_head rq_bc_pa_list;/* Backchannel prealloc list */ #endif /* CONFIG_SUNRPC_BACKCHANEL */
net/sunrpc/backchannel_rqst.c +1 -4
@@ -83,7 +83,6 @@ static struct rpc_rqst *xprt_alloc_bc_req(struct rpc_xprt *xprt)return NULL ; req->rq_xprt = xprt; -INIT_LIST_HEAD (&req->rq_bc_list);/* Preallocate one XDR receive buffer */ if (xprt_alloc_xdr_buf (&req->rq_rcv_buf, gfp_flags) <0 ) {
@@ -367,8 +366,6 @@ void xprt_complete_bc_request(struct rpc_rqst *req, uint32_t copied)dprintk ("RPC: add callback request to list\n" );xprt_get (xprt); -spin_lock (&bc_serv->sv_cb_lock); -list_add (&req->rq_bc_list, &bc_serv->sv_cb_list); -spin_unlock (&bc_serv->sv_cb_lock); +lwq_enqueue (&req->rq_bc_list, &bc_serv->sv_cb_list);svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]); }
net/sunrpc/svc.c +1 -2
@@ -438,8 +438,7 @@ EXPORT_SYMBOL_GPL(svc_bind);static void __svc_init_bc (struct svc_serv *serv) { -INIT_LIST_HEAD (&serv->sv_cb_list); -spin_lock_init (&serv->sv_cb_lock); +lwq_init (&serv->sv_cb_list); }#else static void
net/sunrpc/svc_xprt.c +3 -9
@@ -705,7 +705,7 @@ rqst_should_sleep(struct svc_rqst *rqstp)#if defined(CONFIG_SUNRPC_BACKCHANNEL )if (svc_is_backchannel (rqstp)) { -if (!list_empty (&rqstp->rq_server->sv_cb_list)) +if (!lwq_empty (&rqstp->rq_server->sv_cb_list))return false; }#endif
@@ -880,18 +880,12 @@ void svc_recv(struct svc_rqst *rqstp)struct svc_serv *serv = rqstp->rq_server;struct rpc_rqst *req; -spin_lock_bh (&serv->sv_cb_lock); - req =list_first_entry_or_null (&serv->sv_cb_list, - struct rpc_rqst, rq_bc_list); + req =lwq_dequeue (&serv->sv_cb_list, + struct rpc_rqst, rq_bc_list);if (req) { -list_del (&req->rq_bc_list); -spin_unlock_bh (&serv->sv_cb_lock);wake_next (rqstp); -svc_process_bc (req, rqstp); -return ; } - spin_unlock_bh(&serv->sv_cb_lock); }#endif }
net/sunrpc/xprtrdma/backchannel.c +1 -3
@@ -263,9 +263,7 @@ void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,/* Queue rqst for ULP's callback service */ bc_serv = xprt->bc_serv;xprt_get (xprt); -spin_lock (&bc_serv->sv_cb_lock); -list_add (&rqst->rq_bc_list, &bc_serv->sv_cb_list); -spin_unlock (&bc_serv->sv_cb_lock); +lwq_enqueue (&rqst->rq_bc_list, &bc_serv->sv_cb_list);svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]);
Signature · 1 line
2.40.1