Revisions for topic-sunrpc-thread-scheduling
Subsystems (4)
cover Revisions for topic-sunrpc-thread-scheduling
This a resend of two recently sent patches (1 and 3) with review comments addressed, plus a new patch (2) which renames some function names for improved consistency.
Thanks,
NeilBrown
[PATCH 1/3] lib: add light-weight queuing mechanism. [PATCH 2/3] SUNRPC: rename some functions from rqst_ to svc_thread_ [PATCH 3/3] SUNRPC: only have one thread waking up at a time
PATCH 1/3 lib: add light-weight queuing mechanism.
lwq 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 at boot time. Does not use kunit framework.
--- include/linux/lwq.h | 120 +++++++++++++++++++++++++++++++++++ lib/Kconfig | 5 ++ lib/Makefile | 2 +- lib/lwq.c | 151 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 include/linux/lwq.h create mode 100644 lib/lwq.c
include/linux/lwq.h new file +120 -0
@@ -0,0 +1,120 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef LWQ_H +#define LWQ_H +/* +* light-weight single-linked queue built from llist +* +* 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 */ +}; + +/** +* lwq_init - initialise a lwq +* @q: the lwq object +*/ +static inline void lwq_init (struct lwq *q) +{ +spin_lock_init (&q->lock); + q->ready =NULL ; +init_llist_head (&q->new); +} + +/** +* lwq_empty - test if lwq contains any entry +* @q: the lwq object +* +* This empty test contains an acquire barrier so that if a wakeup +* is sent when lwq_dequeue returns true, it is safe to go to sleep after +* a test on lwq_empty(). +*/ +static inline bool lwq_empty (struct lwq *q) +{ +/* acquire ensures ordering wrt lwq_enqueue() */ +return smp_load_acquire (&q->ready) ==NULL &&llist_empty (&q->new); +} + +struct llist_node *__lwq_dequeue (struct lwq *q); +/** +* lwq_dequeue - dequeue first (oldest) entry from lwq +* @q: the queue to dequeue from +* @type: the type of object to return +* @member: them member in returned object which is an lwq_node. +* +* Remove a single object from the lwq and return it. This will take +* a spinlock and so must always be called in the same context, typcially +* process contet. +*/ +#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)) + +/** +* lwq_enqueue - add a new item to the end of the queue +* @n - the lwq_node embedded in the item to be added +* @q - the lwq to append to. +* +* No locking is needed to append to the queue so this can +* be called from any context. +* Return %true is the list may have previously been empty. +*/ +static inline bool lwq_enqueue (struct lwq_node *n,struct lwq *q) +{ +/* acquire enqures ordering wrt lwq_dequeue */ +return llist_add (&n->node, &q->new) && +smp_load_acquire (&q->ready) ==NULL ; +} + +/** +* lwq_enqueue_batch - add a list of new items to the end of the queue +* @n - the lwq_node embedded in the first item to be added +* @q - the lwq to append to. +* +* No locking is needed to append to the queue so this can +* be called from any context. +* Return %true is the list may have previously been empty. +*/ +static inline bool lwq_enqueue_batch (struct llist_node *n,struct lwq *q) +{ +struct llist_node *e = n; + +/* acquire enqures ordering wrt lwq_dequeue */ +return llist_add_batch (llist_reverse_order (n), e, &q->new) && +smp_load_acquire (&q->ready) ==NULL ; +} +#endif /* LWQ_H */
lib/Kconfig +5 -0
@@ -763,3 +763,8 @@ config ASN1_ENCODER config POLYNOMIAL tristate + +config LWQ_TEST + bool "lib: enable boot-time test for lwq queuing" + help + Enable boot-time test of lwq functionality.
lib/Makefile +1 -1
@@ -45,7 +45,7 @@ obj-y += lockref.o obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \ bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \ list_sort.o uuid.o iov_iter.o clz_ctz.o \ - bsearch.o find_bit.o llist.o memweight.o kfifo.o \ + bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \ percpu-refcount.o rhashtable.o base64.o \ once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \ generic-radix-tree.o
lib/lwq.c new file +151 -0
151 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH 2/3 SUNRPC: rename some functions from rqst_ to svc_thread_
In reply to NeilBrownFunctions which directly manipulate a 'struct rqst', such as svc_rqst_alloc() or svc_rqst_release_pages(), can reasonably have "rqst" in there name.
However functions that act on the running thread, such as XX_should_sleep() or XX_wait_for_work() should seem more natural with a "svc_thread_" prefix.
So make those changes.
--- net/sunrpc/svc_xprt.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
net/sunrpc/svc_xprt.c +5 -5
@@ -699,7 +699,7 @@ static bool svc_alloc_arg(struct svc_rqst *rqstp) }static bool -rqst_should_sleep (struct svc_rqst *rqstp) +svc_thread_should_sleep (struct svc_rqst *rqstp) {struct svc_pool *pool = rqstp->rq_pool;
@@ -725,15 +725,15 @@ rqst_should_sleep(struct svc_rqst *rqstp)return true; } -static void svc_rqst_wait_for_work (struct svc_rqst *rqstp) +static void svc_thread_wait_for_work (struct svc_rqst *rqstp) {struct svc_pool *pool = rqstp->rq_pool; -if (rqst_should_sleep (rqstp)) { +if (svc_thread_should_sleep (rqstp)) {set_current_state (TASK_IDLE |TASK_FREEZABLE );llist_add (&rqstp->rq_idle, &pool->sp_idle_threads); -if (unlikely (!rqst_should_sleep (rqstp))) +if (unlikely (!svc_thread_should_sleep (rqstp)))/* 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
@@ -850,7 +850,7 @@ void svc_recv(struct svc_rqst *rqstp)if (!svc_alloc_arg (rqstp))return ; -svc_rqst_wait_for_work (rqstp); +svc_thread_wait_for_work (rqstp);clear_bit (SP_TASK_PENDING , &pool->sp_flags);
Signature · 1 line
2.41.0
PATCH 3/3 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 svc_thread_busy() is no longer needed.
--- include/linux/sunrpc/svc.h | 11 ----------- net/sunrpc/svc.c | 14 ++++++-------- net/sunrpc/svc_xprt.c | 38 +++++++++++++++++++++++++------------- 3 files changed, 31 insertions(+), 32 deletions(-)
include/linux/sunrpc/svc.h +0 -11
@@ -266,17 +266,6 @@ enum { RQ_DATA, /* request has data */ }; -/** -* svc_thread_busy - check if a thread as busy -* @rqstp: the thread which might be busy -* -* A thread is only busy when it is not an the idle list. -*/ -static inline bool svc_thread_busy (const struct svc_rqst *rqstp) -{ -return !llist_on_list (&rqstp->rq_idle); -} -#define SVC_NET (rqst) (rqst->rq_xprt ? rqst->rq_xprt->xpt_net : rqst->rq_bc_net)/*
net/sunrpc/svc.c +6 -8
@@ -642,7 +642,6 @@ svc_rqst_alloc(struct svc_serv *serv, struct svc_pool *pool, int node)folio_batch_init (&rqstp->rq_fbatch); -init_llist_node (&rqstp->rq_idle); rqstp->rq_server = serv; rqstp->rq_pool = pool;
@@ -704,17 +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_init (&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); -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 +25 -13
@@ -732,20 +732,19 @@ static void svc_thread_wait_for_work(struct svc_rqst *rqstp)if (svc_thread_should_sleep (rqstp)) {set_current_state (TASK_IDLE |TASK_FREEZABLE );llist_add (&rqstp->rq_idle, &pool->sp_idle_threads); +if (likely (svc_thread_should_sleep (rqstp))) +schedule (); -if (unlikely (!svc_thread_should_sleep (rqstp))) -/* 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. +while (!llist_del_first_this (&pool->sp_idle_threads, + &rqstp->rq_idle)) { +/* Work just became available. This thread can only +* handle it after removing rqstp from the idle +* list. If that attempt failed, some other thread +* must have queued itself after finding no +* work to do, so that thread has taken responsibly +* for this new work. This thread can safely sleep +* until woken again. */ -svc_pool_wake_idle_thread (pool); - -/* Since a thread cannot remove itself from an llist, -* schedule until someone else removes @rqstp from -* the idle list. -*/ -while (!svc_thread_busy (rqstp)) {schedule ();set_current_state (TASK_IDLE |TASK_FREEZABLE ); }
@@ -835,6 +834,15 @@ static void svc_handle_xprt(struct svc_rqst *rqstp, struct svc_xprt *xprt)svc_xprt_release (rqstp); } +static void svc_thread_wake_next (struct svc_rqst *rqstp) +{ +if (!svc_thread_should_sleep (rqstp)) +/* More work pending after I dequeued some, +* wake another worker +*/ +svc_pool_wake_idle_thread (rqstp->rq_pool); +} +/** * svc_recv - Receive and process the next request on any transport * @rqstp: an idle RPC service thread
@@ -854,13 +862,16 @@ void svc_recv(struct svc_rqst *rqstp)clear_bit (SP_TASK_PENDING , &pool->sp_flags); -if (svc_thread_should_stop (rqstp)) +if (svc_thread_should_stop (rqstp)) { +svc_thread_wake_next (rqstp);return ; + } rqstp->rq_xprt =svc_xprt_dequeue (pool);if (rqstp->rq_xprt) {struct svc_xprt *xprt = rqstp->rq_xprt; +svc_thread_wake_next (rqstp);/* Normally we will wait up to 5 seconds for any required * cache information to be provided. When there are no * idle threads, we reduce the wait time.
@@ -885,6 +896,7 @@ void svc_recv(struct svc_rqst *rqstp)if (req) {list_del (&req->rq_bc_list);spin_unlock_bh (&serv->sv_cb_lock); +svc_thread_wake_next (rqstp);svc_process_bc (req, rqstp);return ;
Signature · 1 line
2.41.0
Quoted 8 lines · NeilBrown
This a resend of two recently sent patches (1 and 3) with review comments addressed, plus a new patch (2) which renames some function names for improved consistency.
Quoted 6 lines
Thanks,
NeilBrown[PATCH 1/3] lib: add light-weight queuing mechanism. [PATCH 2/3] SUNRPC: rename some functions from
rqst_tosvc_thread_[PATCH 3/3] SUNRPC: only have one thread waking up at a time
Applied these and the remaining patches from the previous series. No build problems with this set. I'll test them for a bit and then post the latest version of the whole series for further review.
--
Chuck Lever
Quoted 10 lines · Chuck Lever III
Quoted 10 lines · NeilBrown
This a resend of two recently sent patches (1 and 3) with review comments addressed, plus a new patch (2) which renames some function names for improved consistency.
Thanks,
NeilBrown
Quoted 8 lines
Quoted 3 lines
[PATCH 1/3] lib: add light-weight queuing mechanism. [PATCH 2/3] SUNRPC: rename some functions from
rqst_tosvc_thread_[PATCH 3/3] SUNRPC: only have one thread waking up at a timeApplied these and the remaining patches from the previous series. No build problems with this set. I'll test them for a bit and then post the latest version of the whole series for further review.
Thanks a lot!
NeilBrown