Two small closures patches
Subsystems (3)
Kent Overstreet
maintainercover Two small closures patches
I'll be sending these to Linus in my next pull request, sending them to linux-bcache so Coly gets a chance to see them.
Kent Overstreet (2):
closures: Better memory barriers
closures: Fix race in closure_sync()
fs/bcachefs/fs-io-direct.c | 1 + include/linux/closure.h | 12 +++++++++--- lib/closure.c | 9 +++++++-- 3 files changed, 17 insertions(+), 5 deletions(-)
Signature · 1 line
2.42.0
Kent Overstreet
maintainerPATCH 1/2 closures: Better memory barriers
atomic_(dec|sub)_return_release() are a thing now - use them.
Also, delete the useless barrier in set_closure_fn(): it's redundant with the memory barrier in closure_put(0.
Since closure_put() would now otherwise just have a release barrier, we also need a new barrier when the ref hits 0 - smp_acquire__after_ctrl_dep().
--- include/linux/closure.h | 2 -- lib/closure.c | 6 ++++-- 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/closure.h +0 -2
@@ -233,8 +233,6 @@ static inline void set_closure_fn(struct closure *cl, closure_fn *fn,closure_set_ip (cl); cl->fn = fn; cl->wq = wq; -/* between atomic_dec() in closure_put() */ -smp_mb__before_atomic (); }static inline void closure_queue (struct closure *cl)
lib/closure.c +4 -2
@@ -21,6 +21,8 @@ static inline void closure_put_after_sub(struct closure *cl, int flags)BUG_ON (!r && (flags & ~CLOSURE_DESTRUCTOR ));if (!r) { +smp_acquire__after_ctrl_dep (); +if (cl->fn && !(flags &CLOSURE_DESTRUCTOR )) {atomic_set (&cl->remaining,CLOSURE_REMAINING_INITIALIZER );
@@ -43,7 +45,7 @@ static inline void closure_put_after_sub(struct closure *cl, int flags)/* For clearing flags with the same atomic op as a put */ void closure_sub (struct closure *cl,int v) { -closure_put_after_sub (cl,atomic_sub_return (v, &cl->remaining)); +closure_put_after_sub (cl,atomic_sub_return_release (v, &cl->remaining)); }EXPORT_SYMBOL (closure_sub);
@@ -52,7 +54,7 @@ EXPORT_SYMBOL(closure_sub);*/ void closure_put (struct closure *cl) { -closure_put_after_sub (cl,atomic_dec_return (&cl->remaining)); +closure_put_after_sub (cl,atomic_dec_return_release (&cl->remaining)); }EXPORT_SYMBOL (closure_put);
Signature · 1 line
2.42.0
Kent Overstreet
maintainerPATCH 2/2 closures: Fix race in closure_sync()
In reply to Kent OverstreetAs pointed out by Linus, closure_sync() was racy; we could skip blocking immediately after a get() and a put(), but then that would skip any barrier corresponding to the other thread's put() barrier.
To fix this, always do the full __closure_sync() sequence whenever any get() has happened and the closure might have been used by other threads.
--- fs/bcachefs/fs-io-direct.c | 1 + include/linux/closure.h | 10 +++++++++- lib/closure.c | 3 +++ 3 files changed, 13 insertions(+), 1 deletion(-)
fs/bcachefs/fs-io-direct.c +1 -0
@@ -113,6 +113,7 @@ static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter) }else {atomic_set (&dio->cl.remaining,CLOSURE_REMAINING_INITIALIZER +1 ); + dio->cl.closure_get_happened = true; } dio->req = req;
include/linux/closure.h +9 -1
@@ -154,6 +154,7 @@ struct closure {struct closure *parent;atomic_t remaining; +bool closure_get_happened;#ifdef CONFIG_DEBUG_CLOSURES #define CLOSURE_MAGIC_DEAD 0xc054dead
@@ -185,7 +186,11 @@ static inline unsigned closure_nr_remaining(struct closure *cl)*/ static inline void closure_sync (struct closure *cl) { -if (closure_nr_remaining (cl) !=1 ) +#ifdef CONFIG_DEBUG_CLOSURES +BUG_ON (closure_nr_remaining (cl) !=1 && !cl->closure_get_happened); +#endif + +if (cl->closure_get_happened)__closure_sync (cl); }
@@ -257,6 +262,8 @@ static inline void closure_queue(struct closure *cl)*/ static inline void closure_get (struct closure *cl) { + cl->closure_get_happened = true; +#ifdef CONFIG_DEBUG_CLOSURES BUG_ON ((atomic_inc_return (&cl->remaining) &CLOSURE_REMAINING_MASK ) <=1 );
@@ -279,6 +286,7 @@ static inline void closure_init(struct closure *cl, struct closure *parent)closure_get (parent);atomic_set (&cl->remaining,CLOSURE_REMAINING_INITIALIZER ); + cl->closure_get_happened = false;closure_debug_create (cl);closure_set_ip (cl);
lib/closure.c +3 -0
@@ -23,6 +23,8 @@ static inline void closure_put_after_sub(struct closure *cl, int flags)if (!r) {smp_acquire__after_ctrl_dep (); + cl->closure_get_happened = false; +if (cl->fn && !(flags &CLOSURE_DESTRUCTOR )) {atomic_set (&cl->remaining,CLOSURE_REMAINING_INITIALIZER );
@@ -92,6 +94,7 @@ bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)if (atomic_read (&cl->remaining) &CLOSURE_WAITING )return false; + cl->closure_get_happened = true;closure_set_waiting (cl, _RET_IP_);atomic_add (CLOSURE_WAITING +1 , &cl->remaining);llist_add (&cl->list, &waitlist->list);
Signature · 1 line
2.42.0
Quoted 7 lines
2023年11月1日 00:24,Kent Overstreet <[email protected]> 写道:
I'll be sending these to Linus in my next pull request, sending them to linux-bcache so Coly gets a chance to see them.
Quoted 8 lines
Kent Overstreet (2):
closures: Better memory barriers
closures: Fix race inclosure_sync()fs/bcachefs/fs-io-direct.c | 1 + include/linux/closure.h | 12 +++++++++--- lib/closure.c | 9 +++++++-- 3 files changed, 17 insertions(+), 5 deletions(-)
The whole series got tested with current bcache code, it works fine.
Thanks.
Coly Li
Coly Li
Acked-byQuoted 15 lines
Quoted 9 lines · 2023年11月19日 00:43,Coly Li <[email protected]> 写道:
2023年11月1日 00:24,Kent Overstreet <[email protected]> 写道:
I'll be sending these to Linus in my next pull request, sending them to linux-bcache so Coly gets a chance to see them.
Kent Overstreet (2):
closures: Better memory barriers
closures: Fix race inclosure_sync()
Quoted 8 lines
Quoted 4 lines
fs/bcachefs/fs-io-direct.c | 1 + include/linux/closure.h | 12 +++++++++--- lib/closure.c | 9 +++++++-- 3 files changed, 17 insertions(+), 5 deletions(-)The whole series got tested with current bcache code, it works fine.
Acked-by Coly Li <[email protected] <mailto:[email protected]>>
The above line was automatically modified by the MacOS Mail client, sigh… Please use the following line,
Thanks.
Coly Li