Skip to content

Commit 65f27f3

Browse files
committed
WorkStruct: Pass the work_struct pointer instead of context data
Pass the work_struct pointer to the work function rather than context data. The work function can use container_of() to work out the data. For the cases where the container of the work_struct may go away the moment the pending bit is cleared, it is made possible to defer the release of the structure by deferring the clearing of the pending bit. To make this work, an extra flag is introduced into the management side of the work_struct. This governs auto-release of the structure upon execution. Ordinarily, the work queue executor would release the work_struct for further scheduling or deallocation by clearing the pending bit prior to jumping to the work function. This means that, unless the driver makes some guarantee itself that the work_struct won't go away, the work function may not access anything else in the work_struct or its container lest they be deallocated.. This is a problem if the auxiliary data is taken away (as done by the last patch). However, if the pending bit is *not* cleared before jumping to the work function, then the work function *may* access the work_struct and its container with no problems. But then the work function must itself release the work_struct by calling work_release(). In most cases, automatic release is fine, so this is the default. Special initiators exist for the non-auto-release case (ending in _NAR). Signed-Off-By: David Howells <dhowells@redhat.com>
1 parent 365970a commit 65f27f3

51 files changed

Lines changed: 293 additions & 219 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎arch/x86_64/kernel/mce.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,16 @@ void mce_log_therm_throt_event(unsigned int cpu, __u64 status)
306306
*/
307307

308308
static int check_interval = 5 * 60; /* 5 minutes */
309-
static void mcheck_timer(void *data);
310-
static DECLARE_DELAYED_WORK(mcheck_work, mcheck_timer, NULL);
309+
static void mcheck_timer(struct work_struct *work);
310+
static DECLARE_DELAYED_WORK(mcheck_work, mcheck_timer);
311311

312312
static void mcheck_check_cpu(void *info)
313313
{
314314
if (mce_available(&current_cpu_data))
315315
do_machine_check(NULL, 0);
316316
}
317317

318-
static void mcheck_timer(void *data)
318+
static void mcheck_timer(struct work_struct *work)
319319
{
320320
on_each_cpu(mcheck_check_cpu, NULL, 1, 1);
321321
schedule_delayed_work(&mcheck_work, check_interval * HZ);

‎arch/x86_64/kernel/smpboot.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -753,14 +753,16 @@ static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int sta
753753
}
754754

755755
struct create_idle {
756+
struct work_struct work;
756757
struct task_struct *idle;
757758
struct completion done;
758759
int cpu;
759760
};
760761

761-
void do_fork_idle(void *_c_idle)
762+
void do_fork_idle(struct work_struct *work)
762763
{
763-
struct create_idle *c_idle = _c_idle;
764+
struct create_idle *c_idle =
765+
container_of(work, struct create_idle, work);
764766

765767
c_idle->idle = fork_idle(c_idle->cpu);
766768
complete(&c_idle->done);
@@ -775,10 +777,10 @@ static int __cpuinit do_boot_cpu(int cpu, int apicid)
775777
int timeout;
776778
unsigned long start_rip;
777779
struct create_idle c_idle = {
780+
.work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
778781
.cpu = cpu,
779782
.done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
780783
};
781-
DECLARE_WORK(work, do_fork_idle, &c_idle);
782784

783785
/* allocate memory for gdts of secondary cpus. Hotplug is considered */
784786
if (!cpu_gdt_descr[cpu].address &&
@@ -825,9 +827,9 @@ static int __cpuinit do_boot_cpu(int cpu, int apicid)
825827
* thread.
826828
*/
827829
if (!keventd_up() || current_is_keventd())
828-
work.func(work.data);
830+
c_idle.work.func(&c_idle.work);
829831
else {
830-
schedule_work(&work);
832+
schedule_work(&c_idle.work);
831833
wait_for_completion(&c_idle.done);
832834
}
833835

‎arch/x86_64/kernel/time.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ static unsigned int cpufreq_delayed_issched = 0;
563563
static unsigned int cpufreq_init = 0;
564564
static struct work_struct cpufreq_delayed_get_work;
565565

566-
static void handle_cpufreq_delayed_get(void *v)
566+
static void handle_cpufreq_delayed_get(struct work_struct *v)
567567
{
568568
unsigned int cpu;
569569
for_each_online_cpu(cpu) {
@@ -639,7 +639,7 @@ static struct notifier_block time_cpufreq_notifier_block = {
639639

640640
static int __init cpufreq_tsc(void)
641641
{
642-
INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get, NULL);
642+
INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get);
643643
if (!cpufreq_register_notifier(&time_cpufreq_notifier_block,
644644
CPUFREQ_TRANSITION_NOTIFIER))
645645
cpufreq_init = 1;

‎block/as-iosched.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,9 +1274,10 @@ static void as_merged_requests(request_queue_t *q, struct request *req,
12741274
*
12751275
* FIXME! dispatch queue is not a queue at all!
12761276
*/
1277-
static void as_work_handler(void *data)
1277+
static void as_work_handler(struct work_struct *work)
12781278
{
1279-
struct request_queue *q = data;
1279+
struct as_data *ad = container_of(work, struct as_data, antic_work);
1280+
struct request_queue *q = ad->q;
12801281
unsigned long flags;
12811282

12821283
spin_lock_irqsave(q->queue_lock, flags);
@@ -1332,7 +1333,7 @@ static void *as_init_queue(request_queue_t *q, elevator_t *e)
13321333
ad->antic_timer.function = as_antic_timeout;
13331334
ad->antic_timer.data = (unsigned long)q;
13341335
init_timer(&ad->antic_timer);
1335-
INIT_WORK(&ad->antic_work, as_work_handler, q);
1336+
INIT_WORK(&ad->antic_work, as_work_handler);
13361337

13371338
INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
13381339
INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);

‎block/cfq-iosched.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,9 +1841,11 @@ cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
18411841
return 1;
18421842
}
18431843

1844-
static void cfq_kick_queue(void *data)
1844+
static void cfq_kick_queue(struct work_struct *work)
18451845
{
1846-
request_queue_t *q = data;
1846+
struct cfq_data *cfqd =
1847+
container_of(work, struct cfq_data, unplug_work);
1848+
request_queue_t *q = cfqd->queue;
18471849
unsigned long flags;
18481850

18491851
spin_lock_irqsave(q->queue_lock, flags);
@@ -1987,7 +1989,7 @@ static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
19871989
cfqd->idle_class_timer.function = cfq_idle_class_timer;
19881990
cfqd->idle_class_timer.data = (unsigned long) cfqd;
19891991

1990-
INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
1992+
INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
19911993

19921994
cfqd->cfq_quantum = cfq_quantum;
19931995
cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];

‎block/ll_rw_blk.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*/
3535
#include <scsi/scsi_cmnd.h>
3636

37-
static void blk_unplug_work(void *data);
37+
static void blk_unplug_work(struct work_struct *work);
3838
static void blk_unplug_timeout(unsigned long data);
3939
static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io);
4040
static void init_request_from_bio(struct request *req, struct bio *bio);
@@ -227,7 +227,7 @@ void blk_queue_make_request(request_queue_t * q, make_request_fn * mfn)
227227
if (q->unplug_delay == 0)
228228
q->unplug_delay = 1;
229229

230-
INIT_WORK(&q->unplug_work, blk_unplug_work, q);
230+
INIT_WORK(&q->unplug_work, blk_unplug_work);
231231

232232
q->unplug_timer.function = blk_unplug_timeout;
233233
q->unplug_timer.data = (unsigned long)q;
@@ -1631,9 +1631,9 @@ static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
16311631
}
16321632
}
16331633

1634-
static void blk_unplug_work(void *data)
1634+
static void blk_unplug_work(struct work_struct *work)
16351635
{
1636-
request_queue_t *q = data;
1636+
request_queue_t *q = container_of(work, request_queue_t, unplug_work);
16371637

16381638
blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
16391639
q->rq.count[READ] + q->rq.count[WRITE]);

‎crypto/cryptomgr.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ struct cryptomgr_param {
4040
char template[CRYPTO_MAX_ALG_NAME];
4141
};
4242

43-
static void cryptomgr_probe(void *data)
43+
static void cryptomgr_probe(struct work_struct *work)
4444
{
45-
struct cryptomgr_param *param = data;
45+
struct cryptomgr_param *param =
46+
container_of(work, struct cryptomgr_param, work);
4647
struct crypto_template *tmpl;
4748
struct crypto_instance *inst;
4849
int err;
@@ -112,7 +113,7 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval)
112113
param->larval.type = larval->alg.cra_flags;
113114
param->larval.mask = larval->mask;
114115

115-
INIT_WORK(&param->work, cryptomgr_probe, param);
116+
INIT_WORK(&param->work, cryptomgr_probe);
116117
schedule_work(&param->work);
117118

118119
return NOTIFY_STOP;

‎drivers/acpi/osl.c‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ACPI_MODULE_NAME("osl")
5050
struct acpi_os_dpc {
5151
acpi_osd_exec_callback function;
5252
void *context;
53+
struct work_struct work;
5354
};
5455

5556
#ifdef CONFIG_ACPI_CUSTOM_DSDT
@@ -564,12 +565,9 @@ void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
564565
acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
565566
}
566567

567-
static void acpi_os_execute_deferred(void *context)
568+
static void acpi_os_execute_deferred(struct work_struct *work)
568569
{
569-
struct acpi_os_dpc *dpc = NULL;
570-
571-
572-
dpc = (struct acpi_os_dpc *)context;
570+
struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
573571
if (!dpc) {
574572
printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
575573
return;
@@ -602,7 +600,6 @@ acpi_status acpi_os_execute(acpi_execute_type type,
602600
{
603601
acpi_status status = AE_OK;
604602
struct acpi_os_dpc *dpc;
605-
struct work_struct *task;
606603

607604
ACPI_FUNCTION_TRACE("os_queue_for_execution");
608605

@@ -615,28 +612,22 @@ acpi_status acpi_os_execute(acpi_execute_type type,
615612

616613
/*
617614
* Allocate/initialize DPC structure. Note that this memory will be
618-
* freed by the callee. The kernel handles the tq_struct list in a
615+
* freed by the callee. The kernel handles the work_struct list in a
619616
* way that allows us to also free its memory inside the callee.
620617
* Because we may want to schedule several tasks with different
621618
* parameters we can't use the approach some kernel code uses of
622-
* having a static tq_struct.
623-
* We can save time and code by allocating the DPC and tq_structs
624-
* from the same memory.
619+
* having a static work_struct.
625620
*/
626621

627-
dpc =
628-
kmalloc(sizeof(struct acpi_os_dpc) + sizeof(struct work_struct),
629-
GFP_ATOMIC);
622+
dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
630623
if (!dpc)
631624
return_ACPI_STATUS(AE_NO_MEMORY);
632625

633626
dpc->function = function;
634627
dpc->context = context;
635628

636-
task = (void *)(dpc + 1);
637-
INIT_WORK(task, acpi_os_execute_deferred, (void *)dpc);
638-
639-
if (!queue_work(kacpid_wq, task)) {
629+
INIT_WORK(&dpc->work, acpi_os_execute_deferred);
630+
if (!queue_work(kacpid_wq, &dpc->work)) {
640631
ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
641632
"Call to queue_work() failed.\n"));
642633
kfree(dpc);

‎drivers/ata/libata-core.c‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ static unsigned int ata_id_xfermask(const u16 *id)
914914
* ata_port_queue_task - Queue port_task
915915
* @ap: The ata_port to queue port_task for
916916
* @fn: workqueue function to be scheduled
917-
* @data: data value to pass to workqueue function
917+
* @data: data for @fn to use
918918
* @delay: delay time for workqueue function
919919
*
920920
* Schedule @fn(@data) for execution after @delay jiffies using
@@ -929,15 +929,16 @@ static unsigned int ata_id_xfermask(const u16 *id)
929929
* LOCKING:
930930
* Inherited from caller.
931931
*/
932-
void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
932+
void ata_port_queue_task(struct ata_port *ap, work_func_t fn, void *data,
933933
unsigned long delay)
934934
{
935935
int rc;
936936

937937
if (ap->pflags & ATA_PFLAG_FLUSH_PORT_TASK)
938938
return;
939939

940-
PREPARE_DELAYED_WORK(&ap->port_task, fn, data);
940+
PREPARE_DELAYED_WORK(&ap->port_task, fn);
941+
ap->port_task_data = data;
941942

942943
rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
943944

@@ -4292,10 +4293,11 @@ int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
42924293
return poll_next;
42934294
}
42944295

4295-
static void ata_pio_task(void *_data)
4296+
static void ata_pio_task(struct work_struct *work)
42964297
{
4297-
struct ata_queued_cmd *qc = _data;
4298-
struct ata_port *ap = qc->ap;
4298+
struct ata_port *ap =
4299+
container_of(work, struct ata_port, port_task.work);
4300+
struct ata_queued_cmd *qc = ap->port_task_data;
42994301
u8 status;
43004302
int poll_next;
43014303

@@ -5317,9 +5319,9 @@ void ata_port_init(struct ata_port *ap, struct ata_host *host,
53175319
ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
53185320
#endif
53195321

5320-
INIT_DELAYED_WORK(&ap->port_task, NULL, NULL);
5321-
INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug, ap);
5322-
INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan, ap);
5322+
INIT_DELAYED_WORK(&ap->port_task, NULL);
5323+
INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5324+
INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
53235325
INIT_LIST_HEAD(&ap->eh_done_q);
53245326
init_waitqueue_head(&ap->eh_wait_q);
53255327

‎drivers/ata/libata-scsi.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,7 +3079,7 @@ static void ata_scsi_remove_dev(struct ata_device *dev)
30793079

30803080
/**
30813081
* ata_scsi_hotplug - SCSI part of hotplug
3082-
* @data: Pointer to ATA port to perform SCSI hotplug on
3082+
* @work: Pointer to ATA port to perform SCSI hotplug on
30833083
*
30843084
* Perform SCSI part of hotplug. It's executed from a separate
30853085
* workqueue after EH completes. This is necessary because SCSI
@@ -3089,9 +3089,10 @@ static void ata_scsi_remove_dev(struct ata_device *dev)
30893089
* LOCKING:
30903090
* Kernel thread context (may sleep).
30913091
*/
3092-
void ata_scsi_hotplug(void *data)
3092+
void ata_scsi_hotplug(struct work_struct *work)
30933093
{
3094-
struct ata_port *ap = data;
3094+
struct ata_port *ap =
3095+
container_of(work, struct ata_port, hotplug_task.work);
30953096
int i;
30963097

30973098
if (ap->pflags & ATA_PFLAG_UNLOADING) {
@@ -3190,7 +3191,7 @@ static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
31903191

31913192
/**
31923193
* ata_scsi_dev_rescan - initiate scsi_rescan_device()
3193-
* @data: Pointer to ATA port to perform scsi_rescan_device()
3194+
* @work: Pointer to ATA port to perform scsi_rescan_device()
31943195
*
31953196
* After ATA pass thru (SAT) commands are executed successfully,
31963197
* libata need to propagate the changes to SCSI layer. This
@@ -3200,9 +3201,10 @@ static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
32003201
* LOCKING:
32013202
* Kernel thread context (may sleep).
32023203
*/
3203-
void ata_scsi_dev_rescan(void *data)
3204+
void ata_scsi_dev_rescan(struct work_struct *work)
32043205
{
3205-
struct ata_port *ap = data;
3206+
struct ata_port *ap =
3207+
container_of(work, struct ata_port, scsi_rescan_task);
32063208
struct ata_device *dev;
32073209
unsigned int i;
32083210

0 commit comments

Comments
 (0)