Skip to content

Commit 05f0fe6

Browse files
committed
RCU, workqueue: Implement rcu_work
There are cases where RCU callback needs to be bounced to a sleepable context. This is currently done by the RCU callback queueing a work item, which can be cumbersome to write and confusing to read. This patch introduces rcu_work, a workqueue work variant which gets executed after a RCU grace period, and converts the open coded bouncing in fs/aio and kernel/cgroup. v3: Dropped queue_rcu_work_on(). Documented rcu grace period behavior after queue_rcu_work(). v2: Use rcu_barrier() instead of synchronize_rcu() to wait for completion of previously queued rcu callback as per Paul. Signed-off-by: Tejun Heo <[email protected]> Acked-by: "Paul E. McKenney" <[email protected]> Cc: Linus Torvalds <[email protected]>
1 parent c698ca5 commit 05f0fe6

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

include/linux/workqueue.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/threads.h>
1414
#include <linux/atomic.h>
1515
#include <linux/cpumask.h>
16+
#include <linux/rcupdate.h>
1617

1718
struct workqueue_struct;
1819

@@ -120,6 +121,14 @@ struct delayed_work {
120121
int cpu;
121122
};
122123

124+
struct rcu_work {
125+
struct work_struct work;
126+
struct rcu_head rcu;
127+
128+
/* target workqueue ->rcu uses to queue ->work */
129+
struct workqueue_struct *wq;
130+
};
131+
123132
/**
124133
* struct workqueue_attrs - A struct for workqueue attributes.
125134
*
@@ -151,6 +160,11 @@ static inline struct delayed_work *to_delayed_work(struct work_struct *work)
151160
return container_of(work, struct delayed_work, work);
152161
}
153162

163+
static inline struct rcu_work *to_rcu_work(struct work_struct *work)
164+
{
165+
return container_of(work, struct rcu_work, work);
166+
}
167+
154168
struct execute_work {
155169
struct work_struct work;
156170
};
@@ -266,6 +280,12 @@ static inline unsigned int work_static(struct work_struct *work) { return 0; }
266280
#define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func) \
267281
__INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE)
268282

283+
#define INIT_RCU_WORK(_work, _func) \
284+
INIT_WORK(&(_work)->work, (_func))
285+
286+
#define INIT_RCU_WORK_ONSTACK(_work, _func) \
287+
INIT_WORK_ONSTACK(&(_work)->work, (_func))
288+
269289
/**
270290
* work_pending - Find out whether a work item is currently pending
271291
* @work: The work item in question
@@ -447,6 +467,7 @@ extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
447467
struct delayed_work *work, unsigned long delay);
448468
extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
449469
struct delayed_work *dwork, unsigned long delay);
470+
extern bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
450471

451472
extern void flush_workqueue(struct workqueue_struct *wq);
452473
extern void drain_workqueue(struct workqueue_struct *wq);
@@ -463,6 +484,8 @@ extern bool flush_delayed_work(struct delayed_work *dwork);
463484
extern bool cancel_delayed_work(struct delayed_work *dwork);
464485
extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
465486

487+
extern bool flush_rcu_work(struct rcu_work *rwork);
488+
466489
extern void workqueue_set_max_active(struct workqueue_struct *wq,
467490
int max_active);
468491
extern struct work_struct *current_work(void);

kernel/workqueue.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,40 @@ bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
16041604
}
16051605
EXPORT_SYMBOL_GPL(mod_delayed_work_on);
16061606

1607+
static void rcu_work_rcufn(struct rcu_head *rcu)
1608+
{
1609+
struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu);
1610+
1611+
/* read the comment in __queue_work() */
1612+
local_irq_disable();
1613+
__queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work);
1614+
local_irq_enable();
1615+
}
1616+
1617+
/**
1618+
* queue_rcu_work - queue work after a RCU grace period
1619+
* @wq: workqueue to use
1620+
* @rwork: work to queue
1621+
*
1622+
* Return: %false if @rwork was already pending, %true otherwise. Note
1623+
* that a full RCU grace period is guaranteed only after a %true return.
1624+
* While @rwork is guarnateed to be executed after a %false return, the
1625+
* execution may happen before a full RCU grace period has passed.
1626+
*/
1627+
bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork)
1628+
{
1629+
struct work_struct *work = &rwork->work;
1630+
1631+
if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
1632+
rwork->wq = wq;
1633+
call_rcu(&rwork->rcu, rcu_work_rcufn);
1634+
return true;
1635+
}
1636+
1637+
return false;
1638+
}
1639+
EXPORT_SYMBOL(queue_rcu_work);
1640+
16071641
/**
16081642
* worker_enter_idle - enter idle state
16091643
* @worker: worker which is entering idle state
@@ -3001,6 +3035,26 @@ bool flush_delayed_work(struct delayed_work *dwork)
30013035
}
30023036
EXPORT_SYMBOL(flush_delayed_work);
30033037

3038+
/**
3039+
* flush_rcu_work - wait for a rwork to finish executing the last queueing
3040+
* @rwork: the rcu work to flush
3041+
*
3042+
* Return:
3043+
* %true if flush_rcu_work() waited for the work to finish execution,
3044+
* %false if it was already idle.
3045+
*/
3046+
bool flush_rcu_work(struct rcu_work *rwork)
3047+
{
3048+
if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) {
3049+
rcu_barrier();
3050+
flush_work(&rwork->work);
3051+
return true;
3052+
} else {
3053+
return flush_work(&rwork->work);
3054+
}
3055+
}
3056+
EXPORT_SYMBOL(flush_rcu_work);
3057+
30043058
static bool __cancel_work(struct work_struct *work, bool is_dwork)
30053059
{
30063060
unsigned long flags;

0 commit comments

Comments
 (0)