* [PATCH] workqueue: Document (some) memory-ordering properties of {queue,schedule}_work()
@ 2020-01-18 21:58 Andrea Parri
2020-01-19 3:07 ` Randy Dunlap
2020-01-20 2:02 ` Paul E. McKenney
0 siblings, 2 replies; 4+ messages in thread
From: Andrea Parri @ 2020-01-18 21:58 UTC (permalink / raw)
To: linux-kernel; +Cc: Tejun Heo, Lai Jiangshan, Paul E . McKenney, Andrea Parri
It's desirable to be able to rely on the following property: All stores
preceding (in program order) a call to a successful queue_work() will be
visible from the CPU which will execute the queued work by the time such
work executes, e.g.,
{ x is initially 0 }
CPU0 CPU1
WRITE_ONCE(x, 1); [ "work" is being executed ]
r0 = queue_work(wq, work); r1 = READ_ONCE(x);
Forbids: r0 == true && r1 == 0
The current implementation of queue_work() provides such memory-ordering
property:
- In __queue_work(), the ->lock spinlock is acquired.
- On the other side, in worker_thread(), this same ->lock is held
when dequeueing work.
So the locking ordering makes things work out.
Add this property to the DocBook headers of {queue,schedule}_work().
Suggested-by: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
---
include/linux/workqueue.h | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index 4261d1c6e87b1..4fef6c38b0536 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -487,6 +487,19 @@ extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task);
*
* We queue the work to the CPU on which it was submitted, but if the CPU dies
* it can be processed by another CPU.
+ *
+ * Memory-ordering properties: If it returns %true, guarantees that all stores
+ * preceding the call to queue_work() in the program order will be visible from
+ * the CPU which will execute @work by the time such work executes, e.g.,
+ *
+ * { x is initially 0 }
+ *
+ * CPU0 CPU1
+ *
+ * WRITE_ONCE(x, 1); [ @work is being executed ]
+ * r0 = queue_work(wq, work); r1 = READ_ONCE(x);
+ *
+ * Forbids: r0 == true && r1 == 0
*/
static inline bool queue_work(struct workqueue_struct *wq,
struct work_struct *work)
@@ -546,6 +559,9 @@ static inline bool schedule_work_on(int cpu, struct work_struct *work)
* This puts a job in the kernel-global workqueue if it was not already
* queued and leaves it in the same position on the kernel-global
* workqueue otherwise.
+ *
+ * Shares the same memory-ordering properties of queue_work(), c.f., the
+ * DocBook header of queue_work().
*/
static inline bool schedule_work(struct work_struct *work)
{
--
2.24.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] workqueue: Document (some) memory-ordering properties of {queue,schedule}_work()
2020-01-18 21:58 [PATCH] workqueue: Document (some) memory-ordering properties of {queue,schedule}_work() Andrea Parri
@ 2020-01-19 3:07 ` Randy Dunlap
2020-01-20 2:02 ` Paul E. McKenney
1 sibling, 0 replies; 4+ messages in thread
From: Randy Dunlap @ 2020-01-19 3:07 UTC (permalink / raw)
To: Andrea Parri, linux-kernel; +Cc: Tejun Heo, Lai Jiangshan, Paul E . McKenney
Hi,
On 1/18/20 1:58 PM, Andrea Parri wrote:
> It's desirable to be able to rely on the following property: All stores
> preceding (in program order) a call to a successful queue_work() will be
> visible from the CPU which will execute the queued work by the time such
> work executes, e.g.,
>
> { x is initially 0 }
>
> CPU0 CPU1
>
> WRITE_ONCE(x, 1); [ "work" is being executed ]
> r0 = queue_work(wq, work); r1 = READ_ONCE(x);
>
> Forbids: r0 == true && r1 == 0
>
> The current implementation of queue_work() provides such memory-ordering
> property:
>
> - In __queue_work(), the ->lock spinlock is acquired.
>
> - On the other side, in worker_thread(), this same ->lock is held
> when dequeueing work.
>
> So the locking ordering makes things work out.
>
> Add this property to the DocBook headers of {queue,schedule}_work().
>
> Suggested-by: Paul E. McKenney <paulmck@kernel.org>
> Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
> ---
> include/linux/workqueue.h | 16 ++++++++++++++++
> 1 file changed, 16 insertions(+)
>
> diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
> index 4261d1c6e87b1..4fef6c38b0536 100644
> --- a/include/linux/workqueue.h
> +++ b/include/linux/workqueue.h
> @@ -487,6 +487,19 @@ extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task);
> *
> * We queue the work to the CPU on which it was submitted, but if the CPU dies
> * it can be processed by another CPU.
> + *
> + * Memory-ordering properties: If it returns %true, guarantees that all stores
> + * preceding the call to queue_work() in the program order will be visible from
> + * the CPU which will execute @work by the time such work executes, e.g.,
> + *
> + * { x is initially 0 }
> + *
> + * CPU0 CPU1
> + *
> + * WRITE_ONCE(x, 1); [ @work is being executed ]
> + * r0 = queue_work(wq, work); r1 = READ_ONCE(x);
> + *
> + * Forbids: r0 == true && r1 == 0
> */
> static inline bool queue_work(struct workqueue_struct *wq,
> struct work_struct *work)
> @@ -546,6 +559,9 @@ static inline bool schedule_work_on(int cpu, struct work_struct *work)
> * This puts a job in the kernel-global workqueue if it was not already
> * queued and leaves it in the same position on the kernel-global
> * workqueue otherwise.
> + *
> + * Shares the same memory-ordering properties of queue_work(), c.f., the
nit: cf. the
> + * DocBook header of queue_work().
> */
> static inline bool schedule_work(struct work_struct *work)
> {
>
--
~Randy
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] workqueue: Document (some) memory-ordering properties of {queue,schedule}_work()
2020-01-18 21:58 [PATCH] workqueue: Document (some) memory-ordering properties of {queue,schedule}_work() Andrea Parri
2020-01-19 3:07 ` Randy Dunlap
@ 2020-01-20 2:02 ` Paul E. McKenney
2020-01-20 20:44 ` Andrea Parri
1 sibling, 1 reply; 4+ messages in thread
From: Paul E. McKenney @ 2020-01-20 2:02 UTC (permalink / raw)
To: Andrea Parri; +Cc: linux-kernel, Tejun Heo, Lai Jiangshan
On Sat, Jan 18, 2020 at 10:58:20PM +0100, Andrea Parri wrote:
> It's desirable to be able to rely on the following property: All stores
> preceding (in program order) a call to a successful queue_work() will be
> visible from the CPU which will execute the queued work by the time such
> work executes, e.g.,
>
> { x is initially 0 }
>
> CPU0 CPU1
>
> WRITE_ONCE(x, 1); [ "work" is being executed ]
> r0 = queue_work(wq, work); r1 = READ_ONCE(x);
>
> Forbids: r0 == true && r1 == 0
>
> The current implementation of queue_work() provides such memory-ordering
> property:
>
> - In __queue_work(), the ->lock spinlock is acquired.
>
> - On the other side, in worker_thread(), this same ->lock is held
> when dequeueing work.
>
> So the locking ordering makes things work out.
>
> Add this property to the DocBook headers of {queue,schedule}_work().
>
> Suggested-by: Paul E. McKenney <paulmck@kernel.org>
> Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
Acked-by: Paul E. McKenney <paulmck@kernel.org>
An alternative to Randy's suggestion of dropping the comma following
the "cf." is to just drop that whole phrase. I will let you and Randy
work that one out, though. ;-)
> ---
> include/linux/workqueue.h | 16 ++++++++++++++++
> 1 file changed, 16 insertions(+)
>
> diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
> index 4261d1c6e87b1..4fef6c38b0536 100644
> --- a/include/linux/workqueue.h
> +++ b/include/linux/workqueue.h
> @@ -487,6 +487,19 @@ extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task);
> *
> * We queue the work to the CPU on which it was submitted, but if the CPU dies
> * it can be processed by another CPU.
> + *
> + * Memory-ordering properties: If it returns %true, guarantees that all stores
> + * preceding the call to queue_work() in the program order will be visible from
> + * the CPU which will execute @work by the time such work executes, e.g.,
> + *
> + * { x is initially 0 }
> + *
> + * CPU0 CPU1
> + *
> + * WRITE_ONCE(x, 1); [ @work is being executed ]
> + * r0 = queue_work(wq, work); r1 = READ_ONCE(x);
> + *
> + * Forbids: r0 == true && r1 == 0
> */
> static inline bool queue_work(struct workqueue_struct *wq,
> struct work_struct *work)
> @@ -546,6 +559,9 @@ static inline bool schedule_work_on(int cpu, struct work_struct *work)
> * This puts a job in the kernel-global workqueue if it was not already
> * queued and leaves it in the same position on the kernel-global
> * workqueue otherwise.
> + *
> + * Shares the same memory-ordering properties of queue_work(), c.f., the
> + * DocBook header of queue_work().
> */
> static inline bool schedule_work(struct work_struct *work)
> {
> --
> 2.24.0
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] workqueue: Document (some) memory-ordering properties of {queue,schedule}_work()
2020-01-20 2:02 ` Paul E. McKenney
@ 2020-01-20 20:44 ` Andrea Parri
0 siblings, 0 replies; 4+ messages in thread
From: Andrea Parri @ 2020-01-20 20:44 UTC (permalink / raw)
To: Paul E. McKenney; +Cc: linux-kernel, Tejun Heo, Lai Jiangshan
On Sun, Jan 19, 2020 at 06:02:35PM -0800, Paul E. McKenney wrote:
> On Sat, Jan 18, 2020 at 10:58:20PM +0100, Andrea Parri wrote:
> > It's desirable to be able to rely on the following property: All stores
> > preceding (in program order) a call to a successful queue_work() will be
> > visible from the CPU which will execute the queued work by the time such
> > work executes, e.g.,
> >
> > { x is initially 0 }
> >
> > CPU0 CPU1
> >
> > WRITE_ONCE(x, 1); [ "work" is being executed ]
> > r0 = queue_work(wq, work); r1 = READ_ONCE(x);
> >
> > Forbids: r0 == true && r1 == 0
> >
> > The current implementation of queue_work() provides such memory-ordering
> > property:
> >
> > - In __queue_work(), the ->lock spinlock is acquired.
> >
> > - On the other side, in worker_thread(), this same ->lock is held
> > when dequeueing work.
> >
> > So the locking ordering makes things work out.
> >
> > Add this property to the DocBook headers of {queue,schedule}_work().
> >
> > Suggested-by: Paul E. McKenney <paulmck@kernel.org>
> > Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
>
> Acked-by: Paul E. McKenney <paulmck@kernel.org>
Thanks!
>
> An alternative to Randy's suggestion of dropping the comma following
> the "cf." is to just drop that whole phrase. I will let you and Randy
> work that one out, though. ;-)
Either way works for me.
I'd give Tejun and Lai some more time to review this and send a non-RFC with
your Ack and this nit fixed later this week (unless I hear some objections).
Thanks,
Andrea
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-01-20 20:44 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-18 21:58 [PATCH] workqueue: Document (some) memory-ordering properties of {queue,schedule}_work() Andrea Parri
2020-01-19 3:07 ` Randy Dunlap
2020-01-20 2:02 ` Paul E. McKenney
2020-01-20 20:44 ` Andrea Parri
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.