Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq:
workqueue: remove in_workqueue_context()
workqueue: Clarify that schedule_on_each_cpu is synchronous
memory_hotplug: drop spurious calls to flush_scheduled_work()
shpchp: update workqueue usage
pciehp: update workqueue usage
isdn/eicon: don't call flush_scheduled_work() from diva_os_remove_soft_isr()
workqueue: add and use WQ_MEM_RECLAIM flag
workqueue: fix HIGHPRI handling in keep_working()
workqueue: add queue_work and activate_work trace points
workqueue: prepare for more tracepoints
workqueue: implement flush[_delayed]_work_sync()
workqueue: factor out start_flush_work()
workqueue: cleanup flush/cancel functions
workqueue: implement alloc_ordered_workqueue()

Fix up trivial conflict in fs/gfs2/main.c as per Tejun

+367 -222
+15 -14
Documentation/workqueue.txt
··· 196 196 suspend operations. Work items on the wq are drained and no 197 197 new work item starts execution until thawed. 198 198 199 - WQ_RESCUER 199 + WQ_MEM_RECLAIM 200 200 201 201 All wq which might be used in the memory reclaim paths _MUST_ 202 - have this flag set. This reserves one worker exclusively for 203 - the execution of this wq under memory pressure. 202 + have this flag set. The wq is guaranteed to have at least one 203 + execution context regardless of memory pressure. 204 204 205 205 WQ_HIGHPRI 206 206 ··· 356 356 357 357 6. Guidelines 358 358 359 - * Do not forget to use WQ_RESCUER if a wq may process work items which 360 - are used during memory reclaim. Each wq with WQ_RESCUER set has one 361 - rescuer thread reserved for it. If there is dependency among 362 - multiple work items used during memory reclaim, they should be 363 - queued to separate wq each with WQ_RESCUER. 359 + * Do not forget to use WQ_MEM_RECLAIM if a wq may process work items 360 + which are used during memory reclaim. Each wq with WQ_MEM_RECLAIM 361 + set has an execution context reserved for it. If there is 362 + dependency among multiple work items used during memory reclaim, 363 + they should be queued to separate wq each with WQ_MEM_RECLAIM. 364 364 365 365 * Unless strict ordering is required, there is no need to use ST wq. 366 366 ··· 368 368 recommended. In most use cases, concurrency level usually stays 369 369 well under the default limit. 370 370 371 - * A wq serves as a domain for forward progress guarantee (WQ_RESCUER), 372 - flush and work item attributes. Work items which are not involved 373 - in memory reclaim and don't need to be flushed as a part of a group 374 - of work items, and don't require any special attribute, can use one 375 - of the system wq. There is no difference in execution 376 - characteristics between using a dedicated wq and a system wq. 371 + * A wq serves as a domain for forward progress guarantee 372 + (WQ_MEM_RECLAIM, flush and work item attributes. Work items which 373 + are not involved in memory reclaim and don't need to be flushed as a 374 + part of a group of work items, and don't require any special 375 + attribute, can use one of the system wq. There is no difference in 376 + execution characteristics between using a dedicated wq and a system 377 + wq. 377 378 378 379 * Unless work items are expected to consume a huge amount of CPU 379 380 cycles, using a bound wq is usually beneficial due to the increased
+1 -1
drivers/ata/libata-sff.c
··· 3335 3335 3336 3336 int __init ata_sff_init(void) 3337 3337 { 3338 - ata_sff_wq = alloc_workqueue("ata_sff", WQ_RESCUER, WQ_MAX_ACTIVE); 3338 + ata_sff_wq = alloc_workqueue("ata_sff", WQ_MEM_RECLAIM, WQ_MAX_ACTIVE); 3339 3339 if (!ata_sff_wq) 3340 3340 return -ENOMEM; 3341 3341
-2
drivers/isdn/hardware/eicon/divasmain.c
··· 15 15 #include <asm/uaccess.h> 16 16 #include <asm/io.h> 17 17 #include <linux/ioport.h> 18 - #include <linux/workqueue.h> 19 18 #include <linux/pci.h> 20 19 #include <linux/interrupt.h> 21 20 #include <linux/list.h> ··· 545 546 void *mem; 546 547 547 548 tasklet_kill(&pdpc->divas_task); 548 - flush_scheduled_work(); 549 549 mem = psoft_isr->object; 550 550 psoft_isr->object = NULL; 551 551 diva_os_free(0, mem);
+2
drivers/pci/hotplug/pciehp.h
··· 36 36 #include <linux/sched.h> /* signal_pending() */ 37 37 #include <linux/pcieport_if.h> 38 38 #include <linux/mutex.h> 39 + #include <linux/workqueue.h> 39 40 40 41 #define MY_NAME "pciehp" 41 42 ··· 45 44 extern int pciehp_debug; 46 45 extern int pciehp_force; 47 46 extern struct workqueue_struct *pciehp_wq; 47 + extern struct workqueue_struct *pciehp_ordered_wq; 48 48 49 49 #define dbg(format, arg...) \ 50 50 do { \
+17 -1
drivers/pci/hotplug/pciehp_core.c
··· 43 43 int pciehp_poll_time; 44 44 int pciehp_force; 45 45 struct workqueue_struct *pciehp_wq; 46 + struct workqueue_struct *pciehp_ordered_wq; 46 47 47 48 #define DRIVER_VERSION "0.4" 48 49 #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>" ··· 341 340 { 342 341 int retval = 0; 343 342 343 + pciehp_wq = alloc_workqueue("pciehp", 0, 0); 344 + if (!pciehp_wq) 345 + return -ENOMEM; 346 + 347 + pciehp_ordered_wq = alloc_ordered_workqueue("pciehp_ordered", 0); 348 + if (!pciehp_ordered_wq) { 349 + destroy_workqueue(pciehp_wq); 350 + return -ENOMEM; 351 + } 352 + 344 353 pciehp_firmware_init(); 345 354 retval = pcie_port_service_register(&hpdriver_portdrv); 346 355 dbg("pcie_port_service_register = %d\n", retval); 347 356 info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); 348 - if (retval) 357 + if (retval) { 358 + destroy_workqueue(pciehp_ordered_wq); 359 + destroy_workqueue(pciehp_wq); 349 360 dbg("Failure to register service\n"); 361 + } 350 362 return retval; 351 363 } 352 364 353 365 static void __exit pcied_cleanup(void) 354 366 { 355 367 dbg("unload_pciehpd()\n"); 368 + destroy_workqueue(pciehp_ordered_wq); 369 + destroy_workqueue(pciehp_wq); 356 370 pcie_port_service_unregister(&hpdriver_portdrv); 357 371 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n"); 358 372 }
+4 -5
drivers/pci/hotplug/pciehp_ctrl.c
··· 32 32 #include <linux/types.h> 33 33 #include <linux/slab.h> 34 34 #include <linux/pci.h> 35 - #include <linux/workqueue.h> 36 35 #include "../pci.h" 37 36 #include "pciehp.h" 38 37 ··· 49 50 info->p_slot = p_slot; 50 51 INIT_WORK(&info->work, interrupt_event_handler); 51 52 52 - schedule_work(&info->work); 53 + queue_work(pciehp_wq, &info->work); 53 54 54 55 return 0; 55 56 } ··· 344 345 kfree(info); 345 346 goto out; 346 347 } 347 - queue_work(pciehp_wq, &info->work); 348 + queue_work(pciehp_ordered_wq, &info->work); 348 349 out: 349 350 mutex_unlock(&p_slot->lock); 350 351 } ··· 377 378 if (ATTN_LED(ctrl)) 378 379 pciehp_set_attention_status(p_slot, 0); 379 380 380 - schedule_delayed_work(&p_slot->work, 5*HZ); 381 + queue_delayed_work(pciehp_wq, &p_slot->work, 5*HZ); 381 382 break; 382 383 case BLINKINGOFF_STATE: 383 384 case BLINKINGON_STATE: ··· 439 440 else 440 441 p_slot->state = POWERON_STATE; 441 442 442 - queue_work(pciehp_wq, &info->work); 443 + queue_work(pciehp_ordered_wq, &info->work); 443 444 } 444 445 445 446 static void interrupt_event_handler(struct work_struct *work)
+1 -19
drivers/pci/hotplug/pciehp_hpc.c
··· 41 41 #include "../pci.h" 42 42 #include "pciehp.h" 43 43 44 - static atomic_t pciehp_num_controllers = ATOMIC_INIT(0); 45 - 46 44 static inline int pciehp_readw(struct controller *ctrl, int reg, u16 *value) 47 45 { 48 46 struct pci_dev *dev = ctrl->pcie->port; ··· 803 805 { 804 806 struct slot *slot = ctrl->slot; 805 807 cancel_delayed_work(&slot->work); 806 - flush_scheduled_work(); 807 808 flush_workqueue(pciehp_wq); 809 + flush_workqueue(pciehp_ordered_wq); 808 810 kfree(slot); 809 811 } 810 812 ··· 910 912 /* Disable sotfware notification */ 911 913 pcie_disable_notification(ctrl); 912 914 913 - /* 914 - * If this is the first controller to be initialized, 915 - * initialize the pciehp work queue 916 - */ 917 - if (atomic_add_return(1, &pciehp_num_controllers) == 1) { 918 - pciehp_wq = create_singlethread_workqueue("pciehpd"); 919 - if (!pciehp_wq) 920 - goto abort_ctrl; 921 - } 922 - 923 915 ctrl_info(ctrl, "HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", 924 916 pdev->vendor, pdev->device, pdev->subsystem_vendor, 925 917 pdev->subsystem_device); ··· 929 941 { 930 942 pcie_shutdown_notification(ctrl); 931 943 pcie_cleanup_slot(ctrl); 932 - /* 933 - * If this is the last controller to be released, destroy the 934 - * pciehp work queue 935 - */ 936 - if (atomic_dec_and_test(&pciehp_num_controllers)) 937 - destroy_workqueue(pciehp_wq); 938 944 kfree(ctrl); 939 945 }
+2
drivers/pci/hotplug/shpchp.h
··· 35 35 #include <linux/delay.h> 36 36 #include <linux/sched.h> /* signal_pending(), struct timer_list */ 37 37 #include <linux/mutex.h> 38 + #include <linux/workqueue.h> 38 39 39 40 #if !defined(MODULE) 40 41 #define MY_NAME "shpchp" ··· 47 46 extern int shpchp_poll_time; 48 47 extern int shpchp_debug; 49 48 extern struct workqueue_struct *shpchp_wq; 49 + extern struct workqueue_struct *shpchp_ordered_wq; 50 50 51 51 #define dbg(format, arg...) \ 52 52 do { \
+18 -2
drivers/pci/hotplug/shpchp_core.c
··· 33 33 #include <linux/types.h> 34 34 #include <linux/slab.h> 35 35 #include <linux/pci.h> 36 - #include <linux/workqueue.h> 37 36 #include "shpchp.h" 38 37 39 38 /* Global variables */ ··· 40 41 int shpchp_poll_mode; 41 42 int shpchp_poll_time; 42 43 struct workqueue_struct *shpchp_wq; 44 + struct workqueue_struct *shpchp_ordered_wq; 43 45 44 46 #define DRIVER_VERSION "0.4" 45 47 #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>" ··· 174 174 slot = list_entry(tmp, struct slot, slot_list); 175 175 list_del(&slot->slot_list); 176 176 cancel_delayed_work(&slot->work); 177 - flush_scheduled_work(); 178 177 flush_workqueue(shpchp_wq); 178 + flush_workqueue(shpchp_ordered_wq); 179 179 pci_hp_deregister(slot->hotplug_slot); 180 180 } 181 181 } ··· 360 360 { 361 361 int retval = 0; 362 362 363 + shpchp_wq = alloc_ordered_workqueue("shpchp", 0); 364 + if (!shpchp_wq) 365 + return -ENOMEM; 366 + 367 + shpchp_ordered_wq = alloc_ordered_workqueue("shpchp_ordered", 0); 368 + if (!shpchp_ordered_wq) { 369 + destroy_workqueue(shpchp_wq); 370 + return -ENOMEM; 371 + } 372 + 363 373 retval = pci_register_driver(&shpc_driver); 364 374 dbg("%s: pci_register_driver = %d\n", __func__, retval); 365 375 info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); 376 + if (retval) { 377 + destroy_workqueue(shpchp_ordered_wq); 378 + destroy_workqueue(shpchp_wq); 379 + } 366 380 return retval; 367 381 } 368 382 ··· 384 370 { 385 371 dbg("unload_shpchpd()\n"); 386 372 pci_unregister_driver(&shpc_driver); 373 + destroy_workqueue(shpchp_ordered_wq); 374 + destroy_workqueue(shpchp_wq); 387 375 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n"); 388 376 } 389 377
+3 -4
drivers/pci/hotplug/shpchp_ctrl.c
··· 32 32 #include <linux/types.h> 33 33 #include <linux/slab.h> 34 34 #include <linux/pci.h> 35 - #include <linux/workqueue.h> 36 35 #include "../pci.h" 37 36 #include "shpchp.h" 38 37 ··· 51 52 info->p_slot = p_slot; 52 53 INIT_WORK(&info->work, interrupt_event_handler); 53 54 54 - schedule_work(&info->work); 55 + queue_work(shpchp_wq, &info->work); 55 56 56 57 return 0; 57 58 } ··· 456 457 kfree(info); 457 458 goto out; 458 459 } 459 - queue_work(shpchp_wq, &info->work); 460 + queue_work(shpchp_ordered_wq, &info->work); 460 461 out: 461 462 mutex_unlock(&p_slot->lock); 462 463 } ··· 504 505 p_slot->hpc_ops->green_led_blink(p_slot); 505 506 p_slot->hpc_ops->set_attention_status(p_slot, 0); 506 507 507 - schedule_delayed_work(&p_slot->work, 5*HZ); 508 + queue_delayed_work(shpchp_wq, &p_slot->work, 5*HZ); 508 509 break; 509 510 case BLINKINGOFF_STATE: 510 511 case BLINKINGON_STATE:
+2 -24
drivers/pci/hotplug/shpchp_hpc.c
··· 179 179 #define SLOT_EVENT_LATCH 0x2 180 180 #define SLOT_SERR_INT_MASK 0x3 181 181 182 - static atomic_t shpchp_num_controllers = ATOMIC_INIT(0); 183 - 184 182 static irqreturn_t shpc_isr(int irq, void *dev_id); 185 183 static void start_int_poll_timer(struct controller *ctrl, int sec); 186 184 static int hpc_check_cmd_status(struct controller *ctrl); ··· 612 614 613 615 iounmap(ctrl->creg); 614 616 release_mem_region(ctrl->mmio_base, ctrl->mmio_size); 615 - 616 - /* 617 - * If this is the last controller to be released, destroy the 618 - * shpchpd work queue 619 - */ 620 - if (atomic_dec_and_test(&shpchp_num_controllers)) 621 - destroy_workqueue(shpchp_wq); 622 617 } 623 618 624 619 static int hpc_power_on_slot(struct slot * slot) ··· 1068 1077 1069 1078 rc = request_irq(ctrl->pci_dev->irq, shpc_isr, IRQF_SHARED, 1070 1079 MY_NAME, (void *)ctrl); 1071 - ctrl_dbg(ctrl, "request_irq %d for hpc%d (returns %d)\n", 1072 - ctrl->pci_dev->irq, 1073 - atomic_read(&shpchp_num_controllers), rc); 1080 + ctrl_dbg(ctrl, "request_irq %d (returns %d)\n", 1081 + ctrl->pci_dev->irq, rc); 1074 1082 if (rc) { 1075 1083 ctrl_err(ctrl, "Can't get irq %d for the hotplug " 1076 1084 "controller\n", ctrl->pci_dev->irq); ··· 1080 1090 1081 1091 shpc_get_max_bus_speed(ctrl); 1082 1092 shpc_get_cur_bus_speed(ctrl); 1083 - 1084 - /* 1085 - * If this is the first controller to be initialized, 1086 - * initialize the shpchpd work queue 1087 - */ 1088 - if (atomic_add_return(1, &shpchp_num_controllers) == 1) { 1089 - shpchp_wq = create_singlethread_workqueue("shpchpd"); 1090 - if (!shpchp_wq) { 1091 - rc = -ENOMEM; 1092 - goto abort_iounmap; 1093 - } 1094 - } 1095 1093 1096 1094 /* 1097 1095 * Unmask all event interrupts of all slots
+1 -1
fs/gfs2/main.c
··· 144 144 145 145 error = -ENOMEM; 146 146 gfs_recovery_wq = alloc_workqueue("gfs_recovery", 147 - WQ_RESCUER | WQ_FREEZEABLE, 0); 147 + WQ_MEM_RECLAIM | WQ_FREEZEABLE, 0); 148 148 if (!gfs_recovery_wq) 149 149 goto fail_wq; 150 150
+1 -1
fs/xfs/linux-2.6/xfs_buf.c
··· 1921 1921 goto out; 1922 1922 1923 1923 xfslogd_workqueue = alloc_workqueue("xfslogd", 1924 - WQ_RESCUER | WQ_HIGHPRI, 1); 1924 + WQ_MEM_RECLAIM | WQ_HIGHPRI, 1); 1925 1925 if (!xfslogd_workqueue) 1926 1926 goto out_free_buf_zone; 1927 1927
+34 -17
include/linux/workqueue.h
··· 243 243 WQ_NON_REENTRANT = 1 << 0, /* guarantee non-reentrance */ 244 244 WQ_UNBOUND = 1 << 1, /* not bound to any cpu */ 245 245 WQ_FREEZEABLE = 1 << 2, /* freeze during suspend */ 246 - WQ_RESCUER = 1 << 3, /* has an rescue worker */ 246 + WQ_MEM_RECLAIM = 1 << 3, /* may be used for memory reclaim */ 247 247 WQ_HIGHPRI = 1 << 4, /* high priority */ 248 248 WQ_CPU_INTENSIVE = 1 << 5, /* cpu instensive workqueue */ 249 249 250 250 WQ_DYING = 1 << 6, /* internal: workqueue is dying */ 251 + WQ_RESCUER = 1 << 7, /* internal: workqueue has rescuer */ 251 252 252 253 WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */ 253 254 WQ_MAX_UNBOUND_PER_CPU = 4, /* 4 * #cpus for unbound wq */ ··· 307 306 __alloc_workqueue_key((name), (flags), (max_active), NULL, NULL) 308 307 #endif 309 308 309 + /** 310 + * alloc_ordered_workqueue - allocate an ordered workqueue 311 + * @name: name of the workqueue 312 + * @flags: WQ_* flags (only WQ_FREEZEABLE and WQ_MEM_RECLAIM are meaningful) 313 + * 314 + * Allocate an ordered workqueue. An ordered workqueue executes at 315 + * most one work item at any given time in the queued order. They are 316 + * implemented as unbound workqueues with @max_active of one. 317 + * 318 + * RETURNS: 319 + * Pointer to the allocated workqueue on success, %NULL on failure. 320 + */ 321 + static inline struct workqueue_struct * 322 + alloc_ordered_workqueue(const char *name, unsigned int flags) 323 + { 324 + return alloc_workqueue(name, WQ_UNBOUND | flags, 1); 325 + } 326 + 310 327 #define create_workqueue(name) \ 311 - alloc_workqueue((name), WQ_RESCUER, 1) 328 + alloc_workqueue((name), WQ_MEM_RECLAIM, 1) 312 329 #define create_freezeable_workqueue(name) \ 313 - alloc_workqueue((name), WQ_FREEZEABLE | WQ_UNBOUND | WQ_RESCUER, 1) 330 + alloc_workqueue((name), WQ_FREEZEABLE | WQ_UNBOUND | WQ_MEM_RECLAIM, 1) 314 331 #define create_singlethread_workqueue(name) \ 315 - alloc_workqueue((name), WQ_UNBOUND | WQ_RESCUER, 1) 332 + alloc_workqueue((name), WQ_UNBOUND | WQ_MEM_RECLAIM, 1) 316 333 317 334 extern void destroy_workqueue(struct workqueue_struct *wq); 318 335 ··· 344 325 345 326 extern void flush_workqueue(struct workqueue_struct *wq); 346 327 extern void flush_scheduled_work(void); 347 - extern void flush_delayed_work(struct delayed_work *work); 348 328 349 329 extern int schedule_work(struct work_struct *work); 350 330 extern int schedule_work_on(int cpu, struct work_struct *work); ··· 355 337 356 338 int execute_in_process_context(work_func_t fn, struct execute_work *); 357 339 358 - extern int flush_work(struct work_struct *work); 359 - extern int cancel_work_sync(struct work_struct *work); 340 + extern bool flush_work(struct work_struct *work); 341 + extern bool flush_work_sync(struct work_struct *work); 342 + extern bool cancel_work_sync(struct work_struct *work); 343 + 344 + extern bool flush_delayed_work(struct delayed_work *dwork); 345 + extern bool flush_delayed_work_sync(struct delayed_work *work); 346 + extern bool cancel_delayed_work_sync(struct delayed_work *dwork); 360 347 361 348 extern void workqueue_set_max_active(struct workqueue_struct *wq, 362 349 int max_active); ··· 375 352 * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or 376 353 * cancel_work_sync() to wait on it. 377 354 */ 378 - static inline int cancel_delayed_work(struct delayed_work *work) 355 + static inline bool cancel_delayed_work(struct delayed_work *work) 379 356 { 380 - int ret; 357 + bool ret; 381 358 382 359 ret = del_timer_sync(&work->timer); 383 360 if (ret) ··· 390 367 * if it returns 0 the timer function may be running and the queueing is in 391 368 * progress. 392 369 */ 393 - static inline int __cancel_delayed_work(struct delayed_work *work) 370 + static inline bool __cancel_delayed_work(struct delayed_work *work) 394 371 { 395 - int ret; 372 + bool ret; 396 373 397 374 ret = del_timer(&work->timer); 398 375 if (ret) 399 376 work_clear_pending(&work->work); 400 377 return ret; 401 378 } 402 - 403 - extern int cancel_delayed_work_sync(struct delayed_work *work); 404 379 405 380 /* Obsolete. use cancel_delayed_work_sync() */ 406 381 static inline ··· 429 408 extern bool freeze_workqueues_busy(void); 430 409 extern void thaw_workqueues(void); 431 410 #endif /* CONFIG_FREEZER */ 432 - 433 - #ifdef CONFIG_LOCKDEP 434 - int in_workqueue_context(struct workqueue_struct *wq); 435 - #endif 436 411 437 412 #endif
+72 -13
include/trace/events/workqueue.h
··· 7 7 #include <linux/tracepoint.h> 8 8 #include <linux/workqueue.h> 9 9 10 + DECLARE_EVENT_CLASS(workqueue_work, 11 + 12 + TP_PROTO(struct work_struct *work), 13 + 14 + TP_ARGS(work), 15 + 16 + TP_STRUCT__entry( 17 + __field( void *, work ) 18 + ), 19 + 20 + TP_fast_assign( 21 + __entry->work = work; 22 + ), 23 + 24 + TP_printk("work struct %p", __entry->work) 25 + ); 26 + 27 + /** 28 + * workqueue_queue_work - called when a work gets queued 29 + * @req_cpu: the requested cpu 30 + * @cwq: pointer to struct cpu_workqueue_struct 31 + * @work: pointer to struct work_struct 32 + * 33 + * This event occurs when a work is queued immediately or once a 34 + * delayed work is actually queued on a workqueue (ie: once the delay 35 + * has been reached). 36 + */ 37 + TRACE_EVENT(workqueue_queue_work, 38 + 39 + TP_PROTO(unsigned int req_cpu, struct cpu_workqueue_struct *cwq, 40 + struct work_struct *work), 41 + 42 + TP_ARGS(req_cpu, cwq, work), 43 + 44 + TP_STRUCT__entry( 45 + __field( void *, work ) 46 + __field( void *, function) 47 + __field( void *, workqueue) 48 + __field( unsigned int, req_cpu ) 49 + __field( unsigned int, cpu ) 50 + ), 51 + 52 + TP_fast_assign( 53 + __entry->work = work; 54 + __entry->function = work->func; 55 + __entry->workqueue = cwq->wq; 56 + __entry->req_cpu = req_cpu; 57 + __entry->cpu = cwq->gcwq->cpu; 58 + ), 59 + 60 + TP_printk("work struct=%p function=%pf workqueue=%p req_cpu=%u cpu=%u", 61 + __entry->work, __entry->function, __entry->workqueue, 62 + __entry->req_cpu, __entry->cpu) 63 + ); 64 + 65 + /** 66 + * workqueue_activate_work - called when a work gets activated 67 + * @work: pointer to struct work_struct 68 + * 69 + * This event occurs when a queued work is put on the active queue, 70 + * which happens immediately after queueing unless @max_active limit 71 + * is reached. 72 + */ 73 + DEFINE_EVENT(workqueue_work, workqueue_activate_work, 74 + 75 + TP_PROTO(struct work_struct *work), 76 + 77 + TP_ARGS(work) 78 + ); 79 + 10 80 /** 11 81 * workqueue_execute_start - called immediately before the workqueue callback 12 82 * @work: pointer to struct work_struct ··· 108 38 * 109 39 * Allows to track workqueue execution. 110 40 */ 111 - TRACE_EVENT(workqueue_execute_end, 41 + DEFINE_EVENT(workqueue_work, workqueue_execute_end, 112 42 113 43 TP_PROTO(struct work_struct *work), 114 44 115 - TP_ARGS(work), 116 - 117 - TP_STRUCT__entry( 118 - __field( void *, work ) 119 - ), 120 - 121 - TP_fast_assign( 122 - __entry->work = work; 123 - ), 124 - 125 - TP_printk("work struct %p", __entry->work) 45 + TP_ARGS(work) 126 46 ); 127 - 128 47 129 48 #endif /* _TRACE_WORKQUEUE_H */ 130 49
+194 -116
kernel/workqueue.c
··· 42 42 #include <linux/lockdep.h> 43 43 #include <linux/idr.h> 44 44 45 - #define CREATE_TRACE_POINTS 46 - #include <trace/events/workqueue.h> 47 - 48 45 #include "workqueue_sched.h" 49 46 50 47 enum { ··· 254 257 EXPORT_SYMBOL_GPL(system_nrt_wq); 255 258 EXPORT_SYMBOL_GPL(system_unbound_wq); 256 259 260 + #define CREATE_TRACE_POINTS 261 + #include <trace/events/workqueue.h> 262 + 257 263 #define for_each_busy_worker(worker, i, pos, gcwq) \ 258 264 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \ 259 265 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry) ··· 309 309 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \ 310 310 (cpu) < WORK_CPU_NONE; \ 311 311 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq))) 312 - 313 - #ifdef CONFIG_LOCKDEP 314 - /** 315 - * in_workqueue_context() - in context of specified workqueue? 316 - * @wq: the workqueue of interest 317 - * 318 - * Checks lockdep state to see if the current task is executing from 319 - * within a workqueue item. This function exists only if lockdep is 320 - * enabled. 321 - */ 322 - int in_workqueue_context(struct workqueue_struct *wq) 323 - { 324 - return lock_is_held(&wq->lockdep_map); 325 - } 326 - #endif 327 312 328 313 #ifdef CONFIG_DEBUG_OBJECTS_WORK 329 314 ··· 589 604 { 590 605 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu); 591 606 592 - return !list_empty(&gcwq->worklist) && atomic_read(nr_running) <= 1; 607 + return !list_empty(&gcwq->worklist) && 608 + (atomic_read(nr_running) <= 1 || 609 + gcwq->flags & GCWQ_HIGHPRI_PENDING); 593 610 } 594 611 595 612 /* Do we need a new worker? Called from manager. */ ··· 984 997 985 998 /* gcwq determined, get cwq and queue */ 986 999 cwq = get_cwq(gcwq->cpu, wq); 1000 + trace_workqueue_queue_work(cpu, cwq, work); 987 1001 988 1002 BUG_ON(!list_empty(&work->entry)); 989 1003 ··· 992 1004 work_flags = work_color_to_flags(cwq->work_color); 993 1005 994 1006 if (likely(cwq->nr_active < cwq->max_active)) { 1007 + trace_workqueue_activate_work(work); 995 1008 cwq->nr_active++; 996 1009 worklist = gcwq_determine_ins_pos(gcwq, cwq); 997 1010 } else { ··· 1668 1679 struct work_struct, entry); 1669 1680 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq); 1670 1681 1682 + trace_workqueue_activate_work(work); 1671 1683 move_linked_works(work, pos, NULL); 1672 1684 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 1673 1685 cwq->nr_active++; ··· 2316 2326 } 2317 2327 EXPORT_SYMBOL_GPL(flush_workqueue); 2318 2328 2319 - /** 2320 - * flush_work - block until a work_struct's callback has terminated 2321 - * @work: the work which is to be flushed 2322 - * 2323 - * Returns false if @work has already terminated. 2324 - * 2325 - * It is expected that, prior to calling flush_work(), the caller has 2326 - * arranged for the work to not be requeued, otherwise it doesn't make 2327 - * sense to use this function. 2328 - */ 2329 - int flush_work(struct work_struct *work) 2329 + static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2330 + bool wait_executing) 2330 2331 { 2331 2332 struct worker *worker = NULL; 2332 2333 struct global_cwq *gcwq; 2333 2334 struct cpu_workqueue_struct *cwq; 2334 - struct wq_barrier barr; 2335 2335 2336 2336 might_sleep(); 2337 2337 gcwq = get_work_gcwq(work); 2338 2338 if (!gcwq) 2339 - return 0; 2339 + return false; 2340 2340 2341 2341 spin_lock_irq(&gcwq->lock); 2342 2342 if (!list_empty(&work->entry)) { ··· 2339 2359 cwq = get_work_cwq(work); 2340 2360 if (unlikely(!cwq || gcwq != cwq->gcwq)) 2341 2361 goto already_gone; 2342 - } else { 2362 + } else if (wait_executing) { 2343 2363 worker = find_worker_executing_work(gcwq, work); 2344 2364 if (!worker) 2345 2365 goto already_gone; 2346 2366 cwq = worker->current_cwq; 2347 - } 2367 + } else 2368 + goto already_gone; 2348 2369 2349 - insert_wq_barrier(cwq, &barr, work, worker); 2370 + insert_wq_barrier(cwq, barr, work, worker); 2350 2371 spin_unlock_irq(&gcwq->lock); 2351 2372 2352 2373 lock_map_acquire(&cwq->wq->lockdep_map); 2353 2374 lock_map_release(&cwq->wq->lockdep_map); 2354 - 2355 - wait_for_completion(&barr.done); 2356 - destroy_work_on_stack(&barr.work); 2357 - return 1; 2375 + return true; 2358 2376 already_gone: 2359 2377 spin_unlock_irq(&gcwq->lock); 2360 - return 0; 2378 + return false; 2379 + } 2380 + 2381 + /** 2382 + * flush_work - wait for a work to finish executing the last queueing instance 2383 + * @work: the work to flush 2384 + * 2385 + * Wait until @work has finished execution. This function considers 2386 + * only the last queueing instance of @work. If @work has been 2387 + * enqueued across different CPUs on a non-reentrant workqueue or on 2388 + * multiple workqueues, @work might still be executing on return on 2389 + * some of the CPUs from earlier queueing. 2390 + * 2391 + * If @work was queued only on a non-reentrant, ordered or unbound 2392 + * workqueue, @work is guaranteed to be idle on return if it hasn't 2393 + * been requeued since flush started. 2394 + * 2395 + * RETURNS: 2396 + * %true if flush_work() waited for the work to finish execution, 2397 + * %false if it was already idle. 2398 + */ 2399 + bool flush_work(struct work_struct *work) 2400 + { 2401 + struct wq_barrier barr; 2402 + 2403 + if (start_flush_work(work, &barr, true)) { 2404 + wait_for_completion(&barr.done); 2405 + destroy_work_on_stack(&barr.work); 2406 + return true; 2407 + } else 2408 + return false; 2361 2409 } 2362 2410 EXPORT_SYMBOL_GPL(flush_work); 2411 + 2412 + static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) 2413 + { 2414 + struct wq_barrier barr; 2415 + struct worker *worker; 2416 + 2417 + spin_lock_irq(&gcwq->lock); 2418 + 2419 + worker = find_worker_executing_work(gcwq, work); 2420 + if (unlikely(worker)) 2421 + insert_wq_barrier(worker->current_cwq, &barr, work, worker); 2422 + 2423 + spin_unlock_irq(&gcwq->lock); 2424 + 2425 + if (unlikely(worker)) { 2426 + wait_for_completion(&barr.done); 2427 + destroy_work_on_stack(&barr.work); 2428 + return true; 2429 + } else 2430 + return false; 2431 + } 2432 + 2433 + static bool wait_on_work(struct work_struct *work) 2434 + { 2435 + bool ret = false; 2436 + int cpu; 2437 + 2438 + might_sleep(); 2439 + 2440 + lock_map_acquire(&work->lockdep_map); 2441 + lock_map_release(&work->lockdep_map); 2442 + 2443 + for_each_gcwq_cpu(cpu) 2444 + ret |= wait_on_cpu_work(get_gcwq(cpu), work); 2445 + return ret; 2446 + } 2447 + 2448 + /** 2449 + * flush_work_sync - wait until a work has finished execution 2450 + * @work: the work to flush 2451 + * 2452 + * Wait until @work has finished execution. On return, it's 2453 + * guaranteed that all queueing instances of @work which happened 2454 + * before this function is called are finished. In other words, if 2455 + * @work hasn't been requeued since this function was called, @work is 2456 + * guaranteed to be idle on return. 2457 + * 2458 + * RETURNS: 2459 + * %true if flush_work_sync() waited for the work to finish execution, 2460 + * %false if it was already idle. 2461 + */ 2462 + bool flush_work_sync(struct work_struct *work) 2463 + { 2464 + struct wq_barrier barr; 2465 + bool pending, waited; 2466 + 2467 + /* we'll wait for executions separately, queue barr only if pending */ 2468 + pending = start_flush_work(work, &barr, false); 2469 + 2470 + /* wait for executions to finish */ 2471 + waited = wait_on_work(work); 2472 + 2473 + /* wait for the pending one */ 2474 + if (pending) { 2475 + wait_for_completion(&barr.done); 2476 + destroy_work_on_stack(&barr.work); 2477 + } 2478 + 2479 + return pending || waited; 2480 + } 2481 + EXPORT_SYMBOL_GPL(flush_work_sync); 2363 2482 2364 2483 /* 2365 2484 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, ··· 2502 2423 return ret; 2503 2424 } 2504 2425 2505 - static void wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) 2506 - { 2507 - struct wq_barrier barr; 2508 - struct worker *worker; 2509 - 2510 - spin_lock_irq(&gcwq->lock); 2511 - 2512 - worker = find_worker_executing_work(gcwq, work); 2513 - if (unlikely(worker)) 2514 - insert_wq_barrier(worker->current_cwq, &barr, work, worker); 2515 - 2516 - spin_unlock_irq(&gcwq->lock); 2517 - 2518 - if (unlikely(worker)) { 2519 - wait_for_completion(&barr.done); 2520 - destroy_work_on_stack(&barr.work); 2521 - } 2522 - } 2523 - 2524 - static void wait_on_work(struct work_struct *work) 2525 - { 2526 - int cpu; 2527 - 2528 - might_sleep(); 2529 - 2530 - lock_map_acquire(&work->lockdep_map); 2531 - lock_map_release(&work->lockdep_map); 2532 - 2533 - for_each_gcwq_cpu(cpu) 2534 - wait_on_cpu_work(get_gcwq(cpu), work); 2535 - } 2536 - 2537 - static int __cancel_work_timer(struct work_struct *work, 2426 + static bool __cancel_work_timer(struct work_struct *work, 2538 2427 struct timer_list* timer) 2539 2428 { 2540 2429 int ret; ··· 2519 2472 } 2520 2473 2521 2474 /** 2522 - * cancel_work_sync - block until a work_struct's callback has terminated 2523 - * @work: the work which is to be flushed 2475 + * cancel_work_sync - cancel a work and wait for it to finish 2476 + * @work: the work to cancel 2524 2477 * 2525 - * Returns true if @work was pending. 2478 + * Cancel @work and wait for its execution to finish. This function 2479 + * can be used even if the work re-queues itself or migrates to 2480 + * another workqueue. On return from this function, @work is 2481 + * guaranteed to be not pending or executing on any CPU. 2526 2482 * 2527 - * cancel_work_sync() will cancel the work if it is queued. If the work's 2528 - * callback appears to be running, cancel_work_sync() will block until it 2529 - * has completed. 2483 + * cancel_work_sync(&delayed_work->work) must not be used for 2484 + * delayed_work's. Use cancel_delayed_work_sync() instead. 2530 2485 * 2531 - * It is possible to use this function if the work re-queues itself. It can 2532 - * cancel the work even if it migrates to another workqueue, however in that 2533 - * case it only guarantees that work->func() has completed on the last queued 2534 - * workqueue. 2535 - * 2536 - * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not 2537 - * pending, otherwise it goes into a busy-wait loop until the timer expires. 2538 - * 2539 - * The caller must ensure that workqueue_struct on which this work was last 2486 + * The caller must ensure that the workqueue on which @work was last 2540 2487 * queued can't be destroyed before this function returns. 2488 + * 2489 + * RETURNS: 2490 + * %true if @work was pending, %false otherwise. 2541 2491 */ 2542 - int cancel_work_sync(struct work_struct *work) 2492 + bool cancel_work_sync(struct work_struct *work) 2543 2493 { 2544 2494 return __cancel_work_timer(work, NULL); 2545 2495 } 2546 2496 EXPORT_SYMBOL_GPL(cancel_work_sync); 2547 2497 2548 2498 /** 2549 - * cancel_delayed_work_sync - reliably kill off a delayed work. 2550 - * @dwork: the delayed work struct 2499 + * flush_delayed_work - wait for a dwork to finish executing the last queueing 2500 + * @dwork: the delayed work to flush 2551 2501 * 2552 - * Returns true if @dwork was pending. 2502 + * Delayed timer is cancelled and the pending work is queued for 2503 + * immediate execution. Like flush_work(), this function only 2504 + * considers the last queueing instance of @dwork. 2553 2505 * 2554 - * It is possible to use this function if @dwork rearms itself via queue_work() 2555 - * or queue_delayed_work(). See also the comment for cancel_work_sync(). 2506 + * RETURNS: 2507 + * %true if flush_work() waited for the work to finish execution, 2508 + * %false if it was already idle. 2556 2509 */ 2557 - int cancel_delayed_work_sync(struct delayed_work *dwork) 2510 + bool flush_delayed_work(struct delayed_work *dwork) 2511 + { 2512 + if (del_timer_sync(&dwork->timer)) 2513 + __queue_work(raw_smp_processor_id(), 2514 + get_work_cwq(&dwork->work)->wq, &dwork->work); 2515 + return flush_work(&dwork->work); 2516 + } 2517 + EXPORT_SYMBOL(flush_delayed_work); 2518 + 2519 + /** 2520 + * flush_delayed_work_sync - wait for a dwork to finish 2521 + * @dwork: the delayed work to flush 2522 + * 2523 + * Delayed timer is cancelled and the pending work is queued for 2524 + * execution immediately. Other than timer handling, its behavior 2525 + * is identical to flush_work_sync(). 2526 + * 2527 + * RETURNS: 2528 + * %true if flush_work_sync() waited for the work to finish execution, 2529 + * %false if it was already idle. 2530 + */ 2531 + bool flush_delayed_work_sync(struct delayed_work *dwork) 2532 + { 2533 + if (del_timer_sync(&dwork->timer)) 2534 + __queue_work(raw_smp_processor_id(), 2535 + get_work_cwq(&dwork->work)->wq, &dwork->work); 2536 + return flush_work_sync(&dwork->work); 2537 + } 2538 + EXPORT_SYMBOL(flush_delayed_work_sync); 2539 + 2540 + /** 2541 + * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2542 + * @dwork: the delayed work cancel 2543 + * 2544 + * This is cancel_work_sync() for delayed works. 2545 + * 2546 + * RETURNS: 2547 + * %true if @dwork was pending, %false otherwise. 2548 + */ 2549 + bool cancel_delayed_work_sync(struct delayed_work *dwork) 2558 2550 { 2559 2551 return __cancel_work_timer(&dwork->work, &dwork->timer); 2560 2552 } ··· 2645 2559 EXPORT_SYMBOL(schedule_delayed_work); 2646 2560 2647 2561 /** 2648 - * flush_delayed_work - block until a dwork_struct's callback has terminated 2649 - * @dwork: the delayed work which is to be flushed 2650 - * 2651 - * Any timeout is cancelled, and any pending work is run immediately. 2652 - */ 2653 - void flush_delayed_work(struct delayed_work *dwork) 2654 - { 2655 - if (del_timer_sync(&dwork->timer)) { 2656 - __queue_work(get_cpu(), get_work_cwq(&dwork->work)->wq, 2657 - &dwork->work); 2658 - put_cpu(); 2659 - } 2660 - flush_work(&dwork->work); 2661 - } 2662 - EXPORT_SYMBOL(flush_delayed_work); 2663 - 2664 - /** 2665 2562 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 2666 2563 * @cpu: cpu to use 2667 2564 * @dwork: job to be done ··· 2661 2592 EXPORT_SYMBOL(schedule_delayed_work_on); 2662 2593 2663 2594 /** 2664 - * schedule_on_each_cpu - call a function on each online CPU from keventd 2595 + * schedule_on_each_cpu - execute a function synchronously on each online CPU 2665 2596 * @func: the function to call 2666 2597 * 2667 - * Returns zero on success. 2668 - * Returns -ve errno on failure. 2669 - * 2598 + * schedule_on_each_cpu() executes @func on each online CPU using the 2599 + * system workqueue and blocks until all CPUs have completed. 2670 2600 * schedule_on_each_cpu() is very slow. 2601 + * 2602 + * RETURNS: 2603 + * 0 on success, -errno on failure. 2671 2604 */ 2672 2605 int schedule_on_each_cpu(work_func_t func) 2673 2606 { ··· 2833 2762 { 2834 2763 struct workqueue_struct *wq; 2835 2764 unsigned int cpu; 2765 + 2766 + /* 2767 + * Workqueues which may be used during memory reclaim should 2768 + * have a rescuer to guarantee forward progress. 2769 + */ 2770 + if (flags & WQ_MEM_RECLAIM) 2771 + flags |= WQ_RESCUER; 2836 2772 2837 2773 /* 2838 2774 * Unbound workqueues aren't concurrency managed and should be
-2
mm/memory_hotplug.c
··· 840 840 ret = 0; 841 841 if (drain) { 842 842 lru_add_drain_all(); 843 - flush_scheduled_work(); 844 843 cond_resched(); 845 844 drain_all_pages(); 846 845 } ··· 861 862 } 862 863 /* drain all zone's lru pagevec, this is asyncronous... */ 863 864 lru_add_drain_all(); 864 - flush_scheduled_work(); 865 865 yield(); 866 866 /* drain pcp pages , this is synchrouns. */ 867 867 drain_all_pages();