| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * kernel/workqueue.c - generic async execution with shared worker pool |
| 4 | * |
| 5 | * Copyright (C) 2002 Ingo Molnar |
| 6 | * |
| 7 | * Derived from the taskqueue/keventd code by: |
| 8 | * David Woodhouse <dwmw2@infradead.org> |
| 9 | * Andrew Morton |
| 10 | * Kai Petzke <wpp@marie.physik.tu-berlin.de> |
| 11 | * Theodore Ts'o <tytso@mit.edu> |
| 12 | * |
| 13 | * Made to use alloc_percpu by Christoph Lameter. |
| 14 | * |
| 15 | * Copyright (C) 2010 SUSE Linux Products GmbH |
| 16 | * Copyright (C) 2010 Tejun Heo <tj@kernel.org> |
| 17 | * |
| 18 | * This is the generic async execution mechanism. Work items as are |
| 19 | * executed in process context. The worker pool is shared and |
| 20 | * automatically managed. There are two worker pools for each CPU (one for |
| 21 | * normal work items and the other for high priority ones) and some extra |
| 22 | * pools for workqueues which are not bound to any specific CPU - the |
| 23 | * number of these backing pools is dynamic. |
| 24 | * |
| 25 | * Please read Documentation/core-api/workqueue.rst for details. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/export.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/interrupt.h> |
| 33 | #include <linux/signal.h> |
| 34 | #include <linux/completion.h> |
| 35 | #include <linux/workqueue.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/cpu.h> |
| 38 | #include <linux/notifier.h> |
| 39 | #include <linux/kthread.h> |
| 40 | #include <linux/hardirq.h> |
| 41 | #include <linux/mempolicy.h> |
| 42 | #include <linux/freezer.h> |
| 43 | #include <linux/debug_locks.h> |
| 44 | #include <linux/lockdep.h> |
| 45 | #include <linux/idr.h> |
| 46 | #include <linux/jhash.h> |
| 47 | #include <linux/hashtable.h> |
| 48 | #include <linux/rculist.h> |
| 49 | #include <linux/nodemask.h> |
| 50 | #include <linux/moduleparam.h> |
| 51 | #include <linux/uaccess.h> |
| 52 | #include <linux/sched/isolation.h> |
| 53 | #include <linux/sched/debug.h> |
| 54 | #include <linux/nmi.h> |
| 55 | #include <linux/kvm_para.h> |
| 56 | #include <linux/delay.h> |
| 57 | |
| 58 | #include "workqueue_internal.h" |
| 59 | |
| 60 | enum worker_pool_flags { |
| 61 | /* |
| 62 | * worker_pool flags |
| 63 | * |
| 64 | * A bound pool is either associated or disassociated with its CPU. |
| 65 | * While associated (!DISASSOCIATED), all workers are bound to the |
| 66 | * CPU and none has %WORKER_UNBOUND set and concurrency management |
| 67 | * is in effect. |
| 68 | * |
| 69 | * While DISASSOCIATED, the cpu may be offline and all workers have |
| 70 | * %WORKER_UNBOUND set and concurrency management disabled, and may |
| 71 | * be executing on any CPU. The pool behaves as an unbound one. |
| 72 | * |
| 73 | * Note that DISASSOCIATED should be flipped only while holding |
| 74 | * wq_pool_attach_mutex to avoid changing binding state while |
| 75 | * worker_attach_to_pool() is in progress. |
| 76 | * |
| 77 | * As there can only be one concurrent BH execution context per CPU, a |
| 78 | * BH pool is per-CPU and always DISASSOCIATED. |
| 79 | */ |
| 80 | POOL_BH = 1 << 0, /* is a BH pool */ |
| 81 | POOL_MANAGER_ACTIVE = 1 << 1, /* being managed */ |
| 82 | POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ |
| 83 | }; |
| 84 | |
| 85 | enum worker_flags { |
| 86 | /* worker flags */ |
| 87 | WORKER_DIE = 1 << 1, /* die die die */ |
| 88 | WORKER_IDLE = 1 << 2, /* is idle */ |
| 89 | WORKER_PREP = 1 << 3, /* preparing to run works */ |
| 90 | WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ |
| 91 | WORKER_UNBOUND = 1 << 7, /* worker is unbound */ |
| 92 | WORKER_REBOUND = 1 << 8, /* worker was rebound */ |
| 93 | |
| 94 | WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | |
| 95 | WORKER_UNBOUND | WORKER_REBOUND, |
| 96 | }; |
| 97 | |
| 98 | enum wq_internal_consts { |
| 99 | NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ |
| 100 | |
| 101 | UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ |
| 102 | BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ |
| 103 | |
| 104 | MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ |
| 105 | IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ |
| 106 | |
| 107 | MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, |
| 108 | /* call for help after 10ms |
| 109 | (min two ticks) */ |
| 110 | MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ |
| 111 | CREATE_COOLDOWN = HZ, /* time to breath after fail */ |
| 112 | |
| 113 | /* |
| 114 | * Rescue workers are used only on emergencies and shared by |
| 115 | * all cpus. Give MIN_NICE. |
| 116 | */ |
| 117 | RESCUER_NICE_LEVEL = MIN_NICE, |
| 118 | HIGHPRI_NICE_LEVEL = MIN_NICE, |
| 119 | |
| 120 | WQ_NAME_LEN = 32, |
| 121 | }; |
| 122 | |
| 123 | /* |
| 124 | * We don't want to trap softirq for too long. See MAX_SOFTIRQ_TIME and |
| 125 | * MAX_SOFTIRQ_RESTART in kernel/softirq.c. These are macros because |
| 126 | * msecs_to_jiffies() can't be an initializer. |
| 127 | */ |
| 128 | #define BH_WORKER_JIFFIES msecs_to_jiffies(2) |
| 129 | #define BH_WORKER_RESTARTS 10 |
| 130 | |
| 131 | /* |
| 132 | * Structure fields follow one of the following exclusion rules. |
| 133 | * |
| 134 | * I: Modifiable by initialization/destruction paths and read-only for |
| 135 | * everyone else. |
| 136 | * |
| 137 | * P: Preemption protected. Disabling preemption is enough and should |
| 138 | * only be modified and accessed from the local cpu. |
| 139 | * |
| 140 | * L: pool->lock protected. Access with pool->lock held. |
| 141 | * |
| 142 | * LN: pool->lock and wq_node_nr_active->lock protected for writes. Either for |
| 143 | * reads. |
| 144 | * |
| 145 | * K: Only modified by worker while holding pool->lock. Can be safely read by |
| 146 | * self, while holding pool->lock or from IRQ context if %current is the |
| 147 | * kworker. |
| 148 | * |
| 149 | * S: Only modified by worker self. |
| 150 | * |
| 151 | * A: wq_pool_attach_mutex protected. |
| 152 | * |
| 153 | * PL: wq_pool_mutex protected. |
| 154 | * |
| 155 | * PR: wq_pool_mutex protected for writes. RCU protected for reads. |
| 156 | * |
| 157 | * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. |
| 158 | * |
| 159 | * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or |
| 160 | * RCU for reads. |
| 161 | * |
| 162 | * WQ: wq->mutex protected. |
| 163 | * |
| 164 | * WR: wq->mutex protected for writes. RCU protected for reads. |
| 165 | * |
| 166 | * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read |
| 167 | * with READ_ONCE() without locking. |
| 168 | * |
| 169 | * MD: wq_mayday_lock protected. |
| 170 | * |
| 171 | * WD: Used internally by the watchdog. |
| 172 | */ |
| 173 | |
| 174 | /* struct worker is defined in workqueue_internal.h */ |
| 175 | |
| 176 | struct worker_pool { |
| 177 | raw_spinlock_t lock; /* the pool lock */ |
| 178 | int cpu; /* I: the associated cpu */ |
| 179 | int node; /* I: the associated node ID */ |
| 180 | int id; /* I: pool ID */ |
| 181 | unsigned int flags; /* L: flags */ |
| 182 | |
| 183 | unsigned long watchdog_ts; /* L: watchdog timestamp */ |
| 184 | bool cpu_stall; /* WD: stalled cpu bound pool */ |
| 185 | |
| 186 | /* |
| 187 | * The counter is incremented in a process context on the associated CPU |
| 188 | * w/ preemption disabled, and decremented or reset in the same context |
| 189 | * but w/ pool->lock held. The readers grab pool->lock and are |
| 190 | * guaranteed to see if the counter reached zero. |
| 191 | */ |
| 192 | int nr_running; |
| 193 | |
| 194 | struct list_head worklist; /* L: list of pending works */ |
| 195 | |
| 196 | int nr_workers; /* L: total number of workers */ |
| 197 | int nr_idle; /* L: currently idle workers */ |
| 198 | |
| 199 | struct list_head idle_list; /* L: list of idle workers */ |
| 200 | struct timer_list idle_timer; /* L: worker idle timeout */ |
| 201 | struct work_struct idle_cull_work; /* L: worker idle cleanup */ |
| 202 | |
| 203 | struct timer_list mayday_timer; /* L: SOS timer for workers */ |
| 204 | |
| 205 | /* a workers is either on busy_hash or idle_list, or the manager */ |
| 206 | DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); |
| 207 | /* L: hash of busy workers */ |
| 208 | |
| 209 | struct worker *manager; /* L: purely informational */ |
| 210 | struct list_head workers; /* A: attached workers */ |
| 211 | struct list_head dying_workers; /* A: workers about to die */ |
| 212 | struct completion *detach_completion; /* all workers detached */ |
| 213 | |
| 214 | struct ida worker_ida; /* worker IDs for task name */ |
| 215 | |
| 216 | struct workqueue_attrs *attrs; /* I: worker attributes */ |
| 217 | struct hlist_node hash_node; /* PL: unbound_pool_hash node */ |
| 218 | int refcnt; /* PL: refcnt for unbound pools */ |
| 219 | |
| 220 | /* |
| 221 | * Destruction of pool is RCU protected to allow dereferences |
| 222 | * from get_work_pool(). |
| 223 | */ |
| 224 | struct rcu_head rcu; |
| 225 | }; |
| 226 | |
| 227 | /* |
| 228 | * Per-pool_workqueue statistics. These can be monitored using |
| 229 | * tools/workqueue/wq_monitor.py. |
| 230 | */ |
| 231 | enum pool_workqueue_stats { |
| 232 | PWQ_STAT_STARTED, /* work items started execution */ |
| 233 | PWQ_STAT_COMPLETED, /* work items completed execution */ |
| 234 | PWQ_STAT_CPU_TIME, /* total CPU time consumed */ |
| 235 | PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ |
| 236 | PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ |
| 237 | PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */ |
| 238 | PWQ_STAT_MAYDAY, /* maydays to rescuer */ |
| 239 | PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ |
| 240 | |
| 241 | PWQ_NR_STATS, |
| 242 | }; |
| 243 | |
| 244 | /* |
| 245 | * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS |
| 246 | * of work_struct->data are used for flags and the remaining high bits |
| 247 | * point to the pwq; thus, pwqs need to be aligned at two's power of the |
| 248 | * number of flag bits. |
| 249 | */ |
| 250 | struct pool_workqueue { |
| 251 | struct worker_pool *pool; /* I: the associated pool */ |
| 252 | struct workqueue_struct *wq; /* I: the owning workqueue */ |
| 253 | int work_color; /* L: current color */ |
| 254 | int flush_color; /* L: flushing color */ |
| 255 | int refcnt; /* L: reference count */ |
| 256 | int nr_in_flight[WORK_NR_COLORS]; |
| 257 | /* L: nr of in_flight works */ |
| 258 | bool plugged; /* L: execution suspended */ |
| 259 | |
| 260 | /* |
| 261 | * nr_active management and WORK_STRUCT_INACTIVE: |
| 262 | * |
| 263 | * When pwq->nr_active >= max_active, new work item is queued to |
| 264 | * pwq->inactive_works instead of pool->worklist and marked with |
| 265 | * WORK_STRUCT_INACTIVE. |
| 266 | * |
| 267 | * All work items marked with WORK_STRUCT_INACTIVE do not participate in |
| 268 | * nr_active and all work items in pwq->inactive_works are marked with |
| 269 | * WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE work items are |
| 270 | * in pwq->inactive_works. Some of them are ready to run in |
| 271 | * pool->worklist or worker->scheduled. Those work itmes are only struct |
| 272 | * wq_barrier which is used for flush_work() and should not participate |
| 273 | * in nr_active. For non-barrier work item, it is marked with |
| 274 | * WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. |
| 275 | */ |
| 276 | int nr_active; /* L: nr of active works */ |
| 277 | struct list_head inactive_works; /* L: inactive works */ |
| 278 | struct list_head pending_node; /* LN: node on wq_node_nr_active->pending_pwqs */ |
| 279 | struct list_head pwqs_node; /* WR: node on wq->pwqs */ |
| 280 | struct list_head mayday_node; /* MD: node on wq->maydays */ |
| 281 | |
| 282 | u64 stats[PWQ_NR_STATS]; |
| 283 | |
| 284 | /* |
| 285 | * Release of unbound pwq is punted to a kthread_worker. See put_pwq() |
| 286 | * and pwq_release_workfn() for details. pool_workqueue itself is also |
| 287 | * RCU protected so that the first pwq can be determined without |
| 288 | * grabbing wq->mutex. |
| 289 | */ |
| 290 | struct kthread_work release_work; |
| 291 | struct rcu_head rcu; |
| 292 | } __aligned(1 << WORK_STRUCT_FLAG_BITS); |
| 293 | |
| 294 | /* |
| 295 | * Structure used to wait for workqueue flush. |
| 296 | */ |
| 297 | struct wq_flusher { |
| 298 | struct list_head list; /* WQ: list of flushers */ |
| 299 | int flush_color; /* WQ: flush color waiting for */ |
| 300 | struct completion done; /* flush completion */ |
| 301 | }; |
| 302 | |
| 303 | struct wq_device; |
| 304 | |
| 305 | /* |
| 306 | * Unlike in a per-cpu workqueue where max_active limits its concurrency level |
| 307 | * on each CPU, in an unbound workqueue, max_active applies to the whole system. |
| 308 | * As sharing a single nr_active across multiple sockets can be very expensive, |
| 309 | * the counting and enforcement is per NUMA node. |
| 310 | * |
| 311 | * The following struct is used to enforce per-node max_active. When a pwq wants |
| 312 | * to start executing a work item, it should increment ->nr using |
| 313 | * tryinc_node_nr_active(). If acquisition fails due to ->nr already being over |
| 314 | * ->max, the pwq is queued on ->pending_pwqs. As in-flight work items finish |
| 315 | * and decrement ->nr, node_activate_pending_pwq() activates the pending pwqs in |
| 316 | * round-robin order. |
| 317 | */ |
| 318 | struct wq_node_nr_active { |
| 319 | int max; /* per-node max_active */ |
| 320 | atomic_t nr; /* per-node nr_active */ |
| 321 | raw_spinlock_t lock; /* nests inside pool locks */ |
| 322 | struct list_head pending_pwqs; /* LN: pwqs with inactive works */ |
| 323 | }; |
| 324 | |
| 325 | /* |
| 326 | * The externally visible workqueue. It relays the issued work items to |
| 327 | * the appropriate worker_pool through its pool_workqueues. |
| 328 | */ |
| 329 | struct workqueue_struct { |
| 330 | struct list_head pwqs; /* WR: all pwqs of this wq */ |
| 331 | struct list_head list; /* PR: list of all workqueues */ |
| 332 | |
| 333 | struct mutex mutex; /* protects this wq */ |
| 334 | int work_color; /* WQ: current work color */ |
| 335 | int flush_color; /* WQ: current flush color */ |
| 336 | atomic_t nr_pwqs_to_flush; /* flush in progress */ |
| 337 | struct wq_flusher *first_flusher; /* WQ: first flusher */ |
| 338 | struct list_head flusher_queue; /* WQ: flush waiters */ |
| 339 | struct list_head flusher_overflow; /* WQ: flush overflow list */ |
| 340 | |
| 341 | struct list_head maydays; /* MD: pwqs requesting rescue */ |
| 342 | struct worker *rescuer; /* MD: rescue worker */ |
| 343 | |
| 344 | int nr_drainers; /* WQ: drain in progress */ |
| 345 | |
| 346 | /* See alloc_workqueue() function comment for info on min/max_active */ |
| 347 | int max_active; /* WO: max active works */ |
| 348 | int min_active; /* WO: min active works */ |
| 349 | int saved_max_active; /* WQ: saved max_active */ |
| 350 | int saved_min_active; /* WQ: saved min_active */ |
| 351 | |
| 352 | struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ |
| 353 | struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */ |
| 354 | |
| 355 | #ifdef CONFIG_SYSFS |
| 356 | struct wq_device *wq_dev; /* I: for sysfs interface */ |
| 357 | #endif |
| 358 | #ifdef CONFIG_LOCKDEP |
| 359 | char *lock_name; |
| 360 | struct lock_class_key key; |
| 361 | struct lockdep_map lockdep_map; |
| 362 | #endif |
| 363 | char name[WQ_NAME_LEN]; /* I: workqueue name */ |
| 364 | |
| 365 | /* |
| 366 | * Destruction of workqueue_struct is RCU protected to allow walking |
| 367 | * the workqueues list without grabbing wq_pool_mutex. |
| 368 | * This is used to dump all workqueues from sysrq. |
| 369 | */ |
| 370 | struct rcu_head rcu; |
| 371 | |
| 372 | /* hot fields used during command issue, aligned to cacheline */ |
| 373 | unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ |
| 374 | struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ |
| 375 | struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */ |
| 376 | }; |
| 377 | |
| 378 | static struct kmem_cache *pwq_cache; |
| 379 | |
| 380 | /* |
| 381 | * Each pod type describes how CPUs should be grouped for unbound workqueues. |
| 382 | * See the comment above workqueue_attrs->affn_scope. |
| 383 | */ |
| 384 | struct wq_pod_type { |
| 385 | int nr_pods; /* number of pods */ |
| 386 | cpumask_var_t *pod_cpus; /* pod -> cpus */ |
| 387 | int *pod_node; /* pod -> node */ |
| 388 | int *cpu_pod; /* cpu -> pod */ |
| 389 | }; |
| 390 | |
| 391 | static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; |
| 392 | static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; |
| 393 | |
| 394 | static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { |
| 395 | [WQ_AFFN_DFL] = "default", |
| 396 | [WQ_AFFN_CPU] = "cpu", |
| 397 | [WQ_AFFN_SMT] = "smt", |
| 398 | [WQ_AFFN_CACHE] = "cache", |
| 399 | [WQ_AFFN_NUMA] = "numa", |
| 400 | [WQ_AFFN_SYSTEM] = "system", |
| 401 | }; |
| 402 | |
| 403 | static bool wq_topo_initialized __read_mostly = false; |
| 404 | |
| 405 | /* |
| 406 | * Per-cpu work items which run for longer than the following threshold are |
| 407 | * automatically considered CPU intensive and excluded from concurrency |
| 408 | * management to prevent them from noticeably delaying other per-cpu work items. |
| 409 | * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. |
| 410 | * The actual value is initialized in wq_cpu_intensive_thresh_init(). |
| 411 | */ |
| 412 | static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; |
| 413 | module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); |
| 414 | |
| 415 | /* see the comment above the definition of WQ_POWER_EFFICIENT */ |
| 416 | static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); |
| 417 | module_param_named(power_efficient, wq_power_efficient, bool, 0444); |
| 418 | |
| 419 | static bool wq_online; /* can kworkers be created yet? */ |
| 420 | |
| 421 | /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ |
| 422 | static struct workqueue_attrs *wq_update_pod_attrs_buf; |
| 423 | |
| 424 | static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ |
| 425 | static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ |
| 426 | static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ |
| 427 | /* wait for manager to go away */ |
| 428 | static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); |
| 429 | |
| 430 | static LIST_HEAD(workqueues); /* PR: list of all workqueues */ |
| 431 | static bool workqueue_freezing; /* PL: have wqs started freezing? */ |
| 432 | |
| 433 | /* PL&A: allowable cpus for unbound wqs and work items */ |
| 434 | static cpumask_var_t wq_unbound_cpumask; |
| 435 | |
| 436 | /* PL: user requested unbound cpumask via sysfs */ |
| 437 | static cpumask_var_t wq_requested_unbound_cpumask; |
| 438 | |
| 439 | /* PL: isolated cpumask to be excluded from unbound cpumask */ |
| 440 | static cpumask_var_t wq_isolated_cpumask; |
| 441 | |
| 442 | /* for further constrain wq_unbound_cpumask by cmdline parameter*/ |
| 443 | static struct cpumask wq_cmdline_cpumask __initdata; |
| 444 | |
| 445 | /* CPU where unbound work was last round robin scheduled from this CPU */ |
| 446 | static DEFINE_PER_CPU(int, wq_rr_cpu_last); |
| 447 | |
| 448 | /* |
| 449 | * Local execution of unbound work items is no longer guaranteed. The |
| 450 | * following always forces round-robin CPU selection on unbound work items |
| 451 | * to uncover usages which depend on it. |
| 452 | */ |
| 453 | #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU |
| 454 | static bool wq_debug_force_rr_cpu = true; |
| 455 | #else |
| 456 | static bool wq_debug_force_rr_cpu = false; |
| 457 | #endif |
| 458 | module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); |
| 459 | |
| 460 | /* the BH worker pools */ |
| 461 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], |
| 462 | bh_worker_pools); |
| 463 | |
| 464 | /* the per-cpu worker pools */ |
| 465 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], |
| 466 | cpu_worker_pools); |
| 467 | |
| 468 | static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ |
| 469 | |
| 470 | /* PL: hash of all unbound pools keyed by pool->attrs */ |
| 471 | static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); |
| 472 | |
| 473 | /* I: attributes used when instantiating standard unbound pools on demand */ |
| 474 | static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; |
| 475 | |
| 476 | /* I: attributes used when instantiating ordered pools on demand */ |
| 477 | static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; |
| 478 | |
| 479 | /* |
| 480 | * I: kthread_worker to release pwq's. pwq release needs to be bounced to a |
| 481 | * process context while holding a pool lock. Bounce to a dedicated kthread |
| 482 | * worker to avoid A-A deadlocks. |
| 483 | */ |
| 484 | static struct kthread_worker *pwq_release_worker __ro_after_init; |
| 485 | |
| 486 | struct workqueue_struct *system_wq __ro_after_init; |
| 487 | EXPORT_SYMBOL(system_wq); |
| 488 | struct workqueue_struct *system_highpri_wq __ro_after_init; |
| 489 | EXPORT_SYMBOL_GPL(system_highpri_wq); |
| 490 | struct workqueue_struct *system_long_wq __ro_after_init; |
| 491 | EXPORT_SYMBOL_GPL(system_long_wq); |
| 492 | struct workqueue_struct *system_unbound_wq __ro_after_init; |
| 493 | EXPORT_SYMBOL_GPL(system_unbound_wq); |
| 494 | struct workqueue_struct *system_freezable_wq __ro_after_init; |
| 495 | EXPORT_SYMBOL_GPL(system_freezable_wq); |
| 496 | struct workqueue_struct *system_power_efficient_wq __ro_after_init; |
| 497 | EXPORT_SYMBOL_GPL(system_power_efficient_wq); |
| 498 | struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; |
| 499 | EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); |
| 500 | struct workqueue_struct *system_bh_wq; |
| 501 | EXPORT_SYMBOL_GPL(system_bh_wq); |
| 502 | struct workqueue_struct *system_bh_highpri_wq; |
| 503 | EXPORT_SYMBOL_GPL(system_bh_highpri_wq); |
| 504 | |
| 505 | static int worker_thread(void *__worker); |
| 506 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq); |
| 507 | static void show_pwq(struct pool_workqueue *pwq); |
| 508 | static void show_one_worker_pool(struct worker_pool *pool); |
| 509 | |
| 510 | #define CREATE_TRACE_POINTS |
| 511 | #include <trace/events/workqueue.h> |
| 512 | |
| 513 | #define assert_rcu_or_pool_mutex() \ |
| 514 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
| 515 | !lockdep_is_held(&wq_pool_mutex), \ |
| 516 | "RCU or wq_pool_mutex should be held") |
| 517 | |
| 518 | #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ |
| 519 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
| 520 | !lockdep_is_held(&wq->mutex) && \ |
| 521 | !lockdep_is_held(&wq_pool_mutex), \ |
| 522 | "RCU, wq->mutex or wq_pool_mutex should be held") |
| 523 | |
| 524 | #define for_each_bh_worker_pool(pool, cpu) \ |
| 525 | for ((pool) = &per_cpu(bh_worker_pools, cpu)[0]; \ |
| 526 | (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ |
| 527 | (pool)++) |
| 528 | |
| 529 | #define for_each_cpu_worker_pool(pool, cpu) \ |
| 530 | for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ |
| 531 | (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ |
| 532 | (pool)++) |
| 533 | |
| 534 | /** |
| 535 | * for_each_pool - iterate through all worker_pools in the system |
| 536 | * @pool: iteration cursor |
| 537 | * @pi: integer used for iteration |
| 538 | * |
| 539 | * This must be called either with wq_pool_mutex held or RCU read |
| 540 | * locked. If the pool needs to be used beyond the locking in effect, the |
| 541 | * caller is responsible for guaranteeing that the pool stays online. |
| 542 | * |
| 543 | * The if/else clause exists only for the lockdep assertion and can be |
| 544 | * ignored. |
| 545 | */ |
| 546 | #define for_each_pool(pool, pi) \ |
| 547 | idr_for_each_entry(&worker_pool_idr, pool, pi) \ |
| 548 | if (({ assert_rcu_or_pool_mutex(); false; })) { } \ |
| 549 | else |
| 550 | |
| 551 | /** |
| 552 | * for_each_pool_worker - iterate through all workers of a worker_pool |
| 553 | * @worker: iteration cursor |
| 554 | * @pool: worker_pool to iterate workers of |
| 555 | * |
| 556 | * This must be called with wq_pool_attach_mutex. |
| 557 | * |
| 558 | * The if/else clause exists only for the lockdep assertion and can be |
| 559 | * ignored. |
| 560 | */ |
| 561 | #define for_each_pool_worker(worker, pool) \ |
| 562 | list_for_each_entry((worker), &(pool)->workers, node) \ |
| 563 | if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ |
| 564 | else |
| 565 | |
| 566 | /** |
| 567 | * for_each_pwq - iterate through all pool_workqueues of the specified workqueue |
| 568 | * @pwq: iteration cursor |
| 569 | * @wq: the target workqueue |
| 570 | * |
| 571 | * This must be called either with wq->mutex held or RCU read locked. |
| 572 | * If the pwq needs to be used beyond the locking in effect, the caller is |
| 573 | * responsible for guaranteeing that the pwq stays online. |
| 574 | * |
| 575 | * The if/else clause exists only for the lockdep assertion and can be |
| 576 | * ignored. |
| 577 | */ |
| 578 | #define for_each_pwq(pwq, wq) \ |
| 579 | list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ |
| 580 | lockdep_is_held(&(wq->mutex))) |
| 581 | |
| 582 | #ifdef CONFIG_DEBUG_OBJECTS_WORK |
| 583 | |
| 584 | static const struct debug_obj_descr work_debug_descr; |
| 585 | |
| 586 | static void *work_debug_hint(void *addr) |
| 587 | { |
| 588 | return ((struct work_struct *) addr)->func; |
| 589 | } |
| 590 | |
| 591 | static bool work_is_static_object(void *addr) |
| 592 | { |
| 593 | struct work_struct *work = addr; |
| 594 | |
| 595 | return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | * fixup_init is called when: |
| 600 | * - an active object is initialized |
| 601 | */ |
| 602 | static bool work_fixup_init(void *addr, enum debug_obj_state state) |
| 603 | { |
| 604 | struct work_struct *work = addr; |
| 605 | |
| 606 | switch (state) { |
| 607 | case ODEBUG_STATE_ACTIVE: |
| 608 | cancel_work_sync(work); |
| 609 | debug_object_init(work, &work_debug_descr); |
| 610 | return true; |
| 611 | default: |
| 612 | return false; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * fixup_free is called when: |
| 618 | * - an active object is freed |
| 619 | */ |
| 620 | static bool work_fixup_free(void *addr, enum debug_obj_state state) |
| 621 | { |
| 622 | struct work_struct *work = addr; |
| 623 | |
| 624 | switch (state) { |
| 625 | case ODEBUG_STATE_ACTIVE: |
| 626 | cancel_work_sync(work); |
| 627 | debug_object_free(work, &work_debug_descr); |
| 628 | return true; |
| 629 | default: |
| 630 | return false; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | static const struct debug_obj_descr work_debug_descr = { |
| 635 | .name = "work_struct", |
| 636 | .debug_hint = work_debug_hint, |
| 637 | .is_static_object = work_is_static_object, |
| 638 | .fixup_init = work_fixup_init, |
| 639 | .fixup_free = work_fixup_free, |
| 640 | }; |
| 641 | |
| 642 | static inline void debug_work_activate(struct work_struct *work) |
| 643 | { |
| 644 | debug_object_activate(work, &work_debug_descr); |
| 645 | } |
| 646 | |
| 647 | static inline void debug_work_deactivate(struct work_struct *work) |
| 648 | { |
| 649 | debug_object_deactivate(work, &work_debug_descr); |
| 650 | } |
| 651 | |
| 652 | void __init_work(struct work_struct *work, int onstack) |
| 653 | { |
| 654 | if (onstack) |
| 655 | debug_object_init_on_stack(work, &work_debug_descr); |
| 656 | else |
| 657 | debug_object_init(work, &work_debug_descr); |
| 658 | } |
| 659 | EXPORT_SYMBOL_GPL(__init_work); |
| 660 | |
| 661 | void destroy_work_on_stack(struct work_struct *work) |
| 662 | { |
| 663 | debug_object_free(work, &work_debug_descr); |
| 664 | } |
| 665 | EXPORT_SYMBOL_GPL(destroy_work_on_stack); |
| 666 | |
| 667 | void destroy_delayed_work_on_stack(struct delayed_work *work) |
| 668 | { |
| 669 | destroy_timer_on_stack(&work->timer); |
| 670 | debug_object_free(&work->work, &work_debug_descr); |
| 671 | } |
| 672 | EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); |
| 673 | |
| 674 | #else |
| 675 | static inline void debug_work_activate(struct work_struct *work) { } |
| 676 | static inline void debug_work_deactivate(struct work_struct *work) { } |
| 677 | #endif |
| 678 | |
| 679 | /** |
| 680 | * worker_pool_assign_id - allocate ID and assign it to @pool |
| 681 | * @pool: the pool pointer of interest |
| 682 | * |
| 683 | * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned |
| 684 | * successfully, -errno on failure. |
| 685 | */ |
| 686 | static int worker_pool_assign_id(struct worker_pool *pool) |
| 687 | { |
| 688 | int ret; |
| 689 | |
| 690 | lockdep_assert_held(&wq_pool_mutex); |
| 691 | |
| 692 | ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, |
| 693 | GFP_KERNEL); |
| 694 | if (ret >= 0) { |
| 695 | pool->id = ret; |
| 696 | return 0; |
| 697 | } |
| 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | static struct pool_workqueue __rcu ** |
| 702 | unbound_pwq_slot(struct workqueue_struct *wq, int cpu) |
| 703 | { |
| 704 | if (cpu >= 0) |
| 705 | return per_cpu_ptr(wq->cpu_pwq, cpu); |
| 706 | else |
| 707 | return &wq->dfl_pwq; |
| 708 | } |
| 709 | |
| 710 | /* @cpu < 0 for dfl_pwq */ |
| 711 | static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) |
| 712 | { |
| 713 | return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), |
| 714 | lockdep_is_held(&wq_pool_mutex) || |
| 715 | lockdep_is_held(&wq->mutex)); |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * unbound_effective_cpumask - effective cpumask of an unbound workqueue |
| 720 | * @wq: workqueue of interest |
| 721 | * |
| 722 | * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which |
| 723 | * is masked with wq_unbound_cpumask to determine the effective cpumask. The |
| 724 | * default pwq is always mapped to the pool with the current effective cpumask. |
| 725 | */ |
| 726 | static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq) |
| 727 | { |
| 728 | return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask; |
| 729 | } |
| 730 | |
| 731 | static unsigned int work_color_to_flags(int color) |
| 732 | { |
| 733 | return color << WORK_STRUCT_COLOR_SHIFT; |
| 734 | } |
| 735 | |
| 736 | static int get_work_color(unsigned long work_data) |
| 737 | { |
| 738 | return (work_data >> WORK_STRUCT_COLOR_SHIFT) & |
| 739 | ((1 << WORK_STRUCT_COLOR_BITS) - 1); |
| 740 | } |
| 741 | |
| 742 | static int work_next_color(int color) |
| 743 | { |
| 744 | return (color + 1) % WORK_NR_COLORS; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data |
| 749 | * contain the pointer to the queued pwq. Once execution starts, the flag |
| 750 | * is cleared and the high bits contain OFFQ flags and pool ID. |
| 751 | * |
| 752 | * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() |
| 753 | * and clear_work_data() can be used to set the pwq, pool or clear |
| 754 | * work->data. These functions should only be called while the work is |
| 755 | * owned - ie. while the PENDING bit is set. |
| 756 | * |
| 757 | * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq |
| 758 | * corresponding to a work. Pool is available once the work has been |
| 759 | * queued anywhere after initialization until it is sync canceled. pwq is |
| 760 | * available only while the work item is queued. |
| 761 | * |
| 762 | * %WORK_OFFQ_CANCELING is used to mark a work item which is being |
| 763 | * canceled. While being canceled, a work item may have its PENDING set |
| 764 | * but stay off timer and worklist for arbitrarily long and nobody should |
| 765 | * try to steal the PENDING bit. |
| 766 | */ |
| 767 | static inline void set_work_data(struct work_struct *work, unsigned long data, |
| 768 | unsigned long flags) |
| 769 | { |
| 770 | WARN_ON_ONCE(!work_pending(work)); |
| 771 | atomic_long_set(&work->data, data | flags | work_static(work)); |
| 772 | } |
| 773 | |
| 774 | static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, |
| 775 | unsigned long extra_flags) |
| 776 | { |
| 777 | set_work_data(work, (unsigned long)pwq, |
| 778 | WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); |
| 779 | } |
| 780 | |
| 781 | static void set_work_pool_and_keep_pending(struct work_struct *work, |
| 782 | int pool_id) |
| 783 | { |
| 784 | set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, |
| 785 | WORK_STRUCT_PENDING); |
| 786 | } |
| 787 | |
| 788 | static void set_work_pool_and_clear_pending(struct work_struct *work, |
| 789 | int pool_id) |
| 790 | { |
| 791 | /* |
| 792 | * The following wmb is paired with the implied mb in |
| 793 | * test_and_set_bit(PENDING) and ensures all updates to @work made |
| 794 | * here are visible to and precede any updates by the next PENDING |
| 795 | * owner. |
| 796 | */ |
| 797 | smp_wmb(); |
| 798 | set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); |
| 799 | /* |
| 800 | * The following mb guarantees that previous clear of a PENDING bit |
| 801 | * will not be reordered with any speculative LOADS or STORES from |
| 802 | * work->current_func, which is executed afterwards. This possible |
| 803 | * reordering can lead to a missed execution on attempt to queue |
| 804 | * the same @work. E.g. consider this case: |
| 805 | * |
| 806 | * CPU#0 CPU#1 |
| 807 | * ---------------------------- -------------------------------- |
| 808 | * |
| 809 | * 1 STORE event_indicated |
| 810 | * 2 queue_work_on() { |
| 811 | * 3 test_and_set_bit(PENDING) |
| 812 | * 4 } set_..._and_clear_pending() { |
| 813 | * 5 set_work_data() # clear bit |
| 814 | * 6 smp_mb() |
| 815 | * 7 work->current_func() { |
| 816 | * 8 LOAD event_indicated |
| 817 | * } |
| 818 | * |
| 819 | * Without an explicit full barrier speculative LOAD on line 8 can |
| 820 | * be executed before CPU#0 does STORE on line 1. If that happens, |
| 821 | * CPU#0 observes the PENDING bit is still set and new execution of |
| 822 | * a @work is not queued in a hope, that CPU#1 will eventually |
| 823 | * finish the queued @work. Meanwhile CPU#1 does not see |
| 824 | * event_indicated is set, because speculative LOAD was executed |
| 825 | * before actual STORE. |
| 826 | */ |
| 827 | smp_mb(); |
| 828 | } |
| 829 | |
| 830 | static void clear_work_data(struct work_struct *work) |
| 831 | { |
| 832 | smp_wmb(); /* see set_work_pool_and_clear_pending() */ |
| 833 | set_work_data(work, WORK_STRUCT_NO_POOL, 0); |
| 834 | } |
| 835 | |
| 836 | static inline struct pool_workqueue *work_struct_pwq(unsigned long data) |
| 837 | { |
| 838 | return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK); |
| 839 | } |
| 840 | |
| 841 | static struct pool_workqueue *get_work_pwq(struct work_struct *work) |
| 842 | { |
| 843 | unsigned long data = atomic_long_read(&work->data); |
| 844 | |
| 845 | if (data & WORK_STRUCT_PWQ) |
| 846 | return work_struct_pwq(data); |
| 847 | else |
| 848 | return NULL; |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * get_work_pool - return the worker_pool a given work was associated with |
| 853 | * @work: the work item of interest |
| 854 | * |
| 855 | * Pools are created and destroyed under wq_pool_mutex, and allows read |
| 856 | * access under RCU read lock. As such, this function should be |
| 857 | * called under wq_pool_mutex or inside of a rcu_read_lock() region. |
| 858 | * |
| 859 | * All fields of the returned pool are accessible as long as the above |
| 860 | * mentioned locking is in effect. If the returned pool needs to be used |
| 861 | * beyond the critical section, the caller is responsible for ensuring the |
| 862 | * returned pool is and stays online. |
| 863 | * |
| 864 | * Return: The worker_pool @work was last associated with. %NULL if none. |
| 865 | */ |
| 866 | static struct worker_pool *get_work_pool(struct work_struct *work) |
| 867 | { |
| 868 | unsigned long data = atomic_long_read(&work->data); |
| 869 | int pool_id; |
| 870 | |
| 871 | assert_rcu_or_pool_mutex(); |
| 872 | |
| 873 | if (data & WORK_STRUCT_PWQ) |
| 874 | return work_struct_pwq(data)->pool; |
| 875 | |
| 876 | pool_id = data >> WORK_OFFQ_POOL_SHIFT; |
| 877 | if (pool_id == WORK_OFFQ_POOL_NONE) |
| 878 | return NULL; |
| 879 | |
| 880 | return idr_find(&worker_pool_idr, pool_id); |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * get_work_pool_id - return the worker pool ID a given work is associated with |
| 885 | * @work: the work item of interest |
| 886 | * |
| 887 | * Return: The worker_pool ID @work was last associated with. |
| 888 | * %WORK_OFFQ_POOL_NONE if none. |
| 889 | */ |
| 890 | static int get_work_pool_id(struct work_struct *work) |
| 891 | { |
| 892 | unsigned long data = atomic_long_read(&work->data); |
| 893 | |
| 894 | if (data & WORK_STRUCT_PWQ) |
| 895 | return work_struct_pwq(data)->pool->id; |
| 896 | |
| 897 | return data >> WORK_OFFQ_POOL_SHIFT; |
| 898 | } |
| 899 | |
| 900 | static void mark_work_canceling(struct work_struct *work) |
| 901 | { |
| 902 | unsigned long pool_id = get_work_pool_id(work); |
| 903 | |
| 904 | pool_id <<= WORK_OFFQ_POOL_SHIFT; |
| 905 | set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); |
| 906 | } |
| 907 | |
| 908 | static bool work_is_canceling(struct work_struct *work) |
| 909 | { |
| 910 | unsigned long data = atomic_long_read(&work->data); |
| 911 | |
| 912 | return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | * Policy functions. These define the policies on how the global worker |
| 917 | * pools are managed. Unless noted otherwise, these functions assume that |
| 918 | * they're being called with pool->lock held. |
| 919 | */ |
| 920 | |
| 921 | /* |
| 922 | * Need to wake up a worker? Called from anything but currently |
| 923 | * running workers. |
| 924 | * |
| 925 | * Note that, because unbound workers never contribute to nr_running, this |
| 926 | * function will always return %true for unbound pools as long as the |
| 927 | * worklist isn't empty. |
| 928 | */ |
| 929 | static bool need_more_worker(struct worker_pool *pool) |
| 930 | { |
| 931 | return !list_empty(&pool->worklist) && !pool->nr_running; |
| 932 | } |
| 933 | |
| 934 | /* Can I start working? Called from busy but !running workers. */ |
| 935 | static bool may_start_working(struct worker_pool *pool) |
| 936 | { |
| 937 | return pool->nr_idle; |
| 938 | } |
| 939 | |
| 940 | /* Do I need to keep working? Called from currently running workers. */ |
| 941 | static bool keep_working(struct worker_pool *pool) |
| 942 | { |
| 943 | return !list_empty(&pool->worklist) && (pool->nr_running <= 1); |
| 944 | } |
| 945 | |
| 946 | /* Do we need a new worker? Called from manager. */ |
| 947 | static bool need_to_create_worker(struct worker_pool *pool) |
| 948 | { |
| 949 | return need_more_worker(pool) && !may_start_working(pool); |
| 950 | } |
| 951 | |
| 952 | /* Do we have too many workers and should some go away? */ |
| 953 | static bool too_many_workers(struct worker_pool *pool) |
| 954 | { |
| 955 | bool managing = pool->flags & POOL_MANAGER_ACTIVE; |
| 956 | int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ |
| 957 | int nr_busy = pool->nr_workers - nr_idle; |
| 958 | |
| 959 | return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; |
| 960 | } |
| 961 | |
| 962 | /** |
| 963 | * worker_set_flags - set worker flags and adjust nr_running accordingly |
| 964 | * @worker: self |
| 965 | * @flags: flags to set |
| 966 | * |
| 967 | * Set @flags in @worker->flags and adjust nr_running accordingly. |
| 968 | */ |
| 969 | static inline void worker_set_flags(struct worker *worker, unsigned int flags) |
| 970 | { |
| 971 | struct worker_pool *pool = worker->pool; |
| 972 | |
| 973 | lockdep_assert_held(&pool->lock); |
| 974 | |
| 975 | /* If transitioning into NOT_RUNNING, adjust nr_running. */ |
| 976 | if ((flags & WORKER_NOT_RUNNING) && |
| 977 | !(worker->flags & WORKER_NOT_RUNNING)) { |
| 978 | pool->nr_running--; |
| 979 | } |
| 980 | |
| 981 | worker->flags |= flags; |
| 982 | } |
| 983 | |
| 984 | /** |
| 985 | * worker_clr_flags - clear worker flags and adjust nr_running accordingly |
| 986 | * @worker: self |
| 987 | * @flags: flags to clear |
| 988 | * |
| 989 | * Clear @flags in @worker->flags and adjust nr_running accordingly. |
| 990 | */ |
| 991 | static inline void worker_clr_flags(struct worker *worker, unsigned int flags) |
| 992 | { |
| 993 | struct worker_pool *pool = worker->pool; |
| 994 | unsigned int oflags = worker->flags; |
| 995 | |
| 996 | lockdep_assert_held(&pool->lock); |
| 997 | |
| 998 | worker->flags &= ~flags; |
| 999 | |
| 1000 | /* |
| 1001 | * If transitioning out of NOT_RUNNING, increment nr_running. Note |
| 1002 | * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask |
| 1003 | * of multiple flags, not a single flag. |
| 1004 | */ |
| 1005 | if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) |
| 1006 | if (!(worker->flags & WORKER_NOT_RUNNING)) |
| 1007 | pool->nr_running++; |
| 1008 | } |
| 1009 | |
| 1010 | /* Return the first idle worker. Called with pool->lock held. */ |
| 1011 | static struct worker *first_idle_worker(struct worker_pool *pool) |
| 1012 | { |
| 1013 | if (unlikely(list_empty(&pool->idle_list))) |
| 1014 | return NULL; |
| 1015 | |
| 1016 | return list_first_entry(&pool->idle_list, struct worker, entry); |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * worker_enter_idle - enter idle state |
| 1021 | * @worker: worker which is entering idle state |
| 1022 | * |
| 1023 | * @worker is entering idle state. Update stats and idle timer if |
| 1024 | * necessary. |
| 1025 | * |
| 1026 | * LOCKING: |
| 1027 | * raw_spin_lock_irq(pool->lock). |
| 1028 | */ |
| 1029 | static void worker_enter_idle(struct worker *worker) |
| 1030 | { |
| 1031 | struct worker_pool *pool = worker->pool; |
| 1032 | |
| 1033 | if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || |
| 1034 | WARN_ON_ONCE(!list_empty(&worker->entry) && |
| 1035 | (worker->hentry.next || worker->hentry.pprev))) |
| 1036 | return; |
| 1037 | |
| 1038 | /* can't use worker_set_flags(), also called from create_worker() */ |
| 1039 | worker->flags |= WORKER_IDLE; |
| 1040 | pool->nr_idle++; |
| 1041 | worker->last_active = jiffies; |
| 1042 | |
| 1043 | /* idle_list is LIFO */ |
| 1044 | list_add(&worker->entry, &pool->idle_list); |
| 1045 | |
| 1046 | if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) |
| 1047 | mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); |
| 1048 | |
| 1049 | /* Sanity check nr_running. */ |
| 1050 | WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * worker_leave_idle - leave idle state |
| 1055 | * @worker: worker which is leaving idle state |
| 1056 | * |
| 1057 | * @worker is leaving idle state. Update stats. |
| 1058 | * |
| 1059 | * LOCKING: |
| 1060 | * raw_spin_lock_irq(pool->lock). |
| 1061 | */ |
| 1062 | static void worker_leave_idle(struct worker *worker) |
| 1063 | { |
| 1064 | struct worker_pool *pool = worker->pool; |
| 1065 | |
| 1066 | if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) |
| 1067 | return; |
| 1068 | worker_clr_flags(worker, WORKER_IDLE); |
| 1069 | pool->nr_idle--; |
| 1070 | list_del_init(&worker->entry); |
| 1071 | } |
| 1072 | |
| 1073 | /** |
| 1074 | * find_worker_executing_work - find worker which is executing a work |
| 1075 | * @pool: pool of interest |
| 1076 | * @work: work to find worker for |
| 1077 | * |
| 1078 | * Find a worker which is executing @work on @pool by searching |
| 1079 | * @pool->busy_hash which is keyed by the address of @work. For a worker |
| 1080 | * to match, its current execution should match the address of @work and |
| 1081 | * its work function. This is to avoid unwanted dependency between |
| 1082 | * unrelated work executions through a work item being recycled while still |
| 1083 | * being executed. |
| 1084 | * |
| 1085 | * This is a bit tricky. A work item may be freed once its execution |
| 1086 | * starts and nothing prevents the freed area from being recycled for |
| 1087 | * another work item. If the same work item address ends up being reused |
| 1088 | * before the original execution finishes, workqueue will identify the |
| 1089 | * recycled work item as currently executing and make it wait until the |
| 1090 | * current execution finishes, introducing an unwanted dependency. |
| 1091 | * |
| 1092 | * This function checks the work item address and work function to avoid |
| 1093 | * false positives. Note that this isn't complete as one may construct a |
| 1094 | * work function which can introduce dependency onto itself through a |
| 1095 | * recycled work item. Well, if somebody wants to shoot oneself in the |
| 1096 | * foot that badly, there's only so much we can do, and if such deadlock |
| 1097 | * actually occurs, it should be easy to locate the culprit work function. |
| 1098 | * |
| 1099 | * CONTEXT: |
| 1100 | * raw_spin_lock_irq(pool->lock). |
| 1101 | * |
| 1102 | * Return: |
| 1103 | * Pointer to worker which is executing @work if found, %NULL |
| 1104 | * otherwise. |
| 1105 | */ |
| 1106 | static struct worker *find_worker_executing_work(struct worker_pool *pool, |
| 1107 | struct work_struct *work) |
| 1108 | { |
| 1109 | struct worker *worker; |
| 1110 | |
| 1111 | hash_for_each_possible(pool->busy_hash, worker, hentry, |
| 1112 | (unsigned long)work) |
| 1113 | if (worker->current_work == work && |
| 1114 | worker->current_func == work->func) |
| 1115 | return worker; |
| 1116 | |
| 1117 | return NULL; |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * move_linked_works - move linked works to a list |
| 1122 | * @work: start of series of works to be scheduled |
| 1123 | * @head: target list to append @work to |
| 1124 | * @nextp: out parameter for nested worklist walking |
| 1125 | * |
| 1126 | * Schedule linked works starting from @work to @head. Work series to be |
| 1127 | * scheduled starts at @work and includes any consecutive work with |
| 1128 | * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on |
| 1129 | * @nextp. |
| 1130 | * |
| 1131 | * CONTEXT: |
| 1132 | * raw_spin_lock_irq(pool->lock). |
| 1133 | */ |
| 1134 | static void move_linked_works(struct work_struct *work, struct list_head *head, |
| 1135 | struct work_struct **nextp) |
| 1136 | { |
| 1137 | struct work_struct *n; |
| 1138 | |
| 1139 | /* |
| 1140 | * Linked worklist will always end before the end of the list, |
| 1141 | * use NULL for list head. |
| 1142 | */ |
| 1143 | list_for_each_entry_safe_from(work, n, NULL, entry) { |
| 1144 | list_move_tail(&work->entry, head); |
| 1145 | if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) |
| 1146 | break; |
| 1147 | } |
| 1148 | |
| 1149 | /* |
| 1150 | * If we're already inside safe list traversal and have moved |
| 1151 | * multiple works to the scheduled queue, the next position |
| 1152 | * needs to be updated. |
| 1153 | */ |
| 1154 | if (nextp) |
| 1155 | *nextp = n; |
| 1156 | } |
| 1157 | |
| 1158 | /** |
| 1159 | * assign_work - assign a work item and its linked work items to a worker |
| 1160 | * @work: work to assign |
| 1161 | * @worker: worker to assign to |
| 1162 | * @nextp: out parameter for nested worklist walking |
| 1163 | * |
| 1164 | * Assign @work and its linked work items to @worker. If @work is already being |
| 1165 | * executed by another worker in the same pool, it'll be punted there. |
| 1166 | * |
| 1167 | * If @nextp is not NULL, it's updated to point to the next work of the last |
| 1168 | * scheduled work. This allows assign_work() to be nested inside |
| 1169 | * list_for_each_entry_safe(). |
| 1170 | * |
| 1171 | * Returns %true if @work was successfully assigned to @worker. %false if @work |
| 1172 | * was punted to another worker already executing it. |
| 1173 | */ |
| 1174 | static bool assign_work(struct work_struct *work, struct worker *worker, |
| 1175 | struct work_struct **nextp) |
| 1176 | { |
| 1177 | struct worker_pool *pool = worker->pool; |
| 1178 | struct worker *collision; |
| 1179 | |
| 1180 | lockdep_assert_held(&pool->lock); |
| 1181 | |
| 1182 | /* |
| 1183 | * A single work shouldn't be executed concurrently by multiple workers. |
| 1184 | * __queue_work() ensures that @work doesn't jump to a different pool |
| 1185 | * while still running in the previous pool. Here, we should ensure that |
| 1186 | * @work is not executed concurrently by multiple workers from the same |
| 1187 | * pool. Check whether anyone is already processing the work. If so, |
| 1188 | * defer the work to the currently executing one. |
| 1189 | */ |
| 1190 | collision = find_worker_executing_work(pool, work); |
| 1191 | if (unlikely(collision)) { |
| 1192 | move_linked_works(work, &collision->scheduled, nextp); |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
| 1196 | move_linked_works(work, &worker->scheduled, nextp); |
| 1197 | return true; |
| 1198 | } |
| 1199 | |
| 1200 | /** |
| 1201 | * kick_pool - wake up an idle worker if necessary |
| 1202 | * @pool: pool to kick |
| 1203 | * |
| 1204 | * @pool may have pending work items. Wake up worker if necessary. Returns |
| 1205 | * whether a worker was woken up. |
| 1206 | */ |
| 1207 | static bool kick_pool(struct worker_pool *pool) |
| 1208 | { |
| 1209 | struct worker *worker = first_idle_worker(pool); |
| 1210 | struct task_struct *p; |
| 1211 | |
| 1212 | lockdep_assert_held(&pool->lock); |
| 1213 | |
| 1214 | if (!need_more_worker(pool) || !worker) |
| 1215 | return false; |
| 1216 | |
| 1217 | if (pool->flags & POOL_BH) { |
| 1218 | if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) |
| 1219 | raise_softirq_irqoff(HI_SOFTIRQ); |
| 1220 | else |
| 1221 | raise_softirq_irqoff(TASKLET_SOFTIRQ); |
| 1222 | return true; |
| 1223 | } |
| 1224 | |
| 1225 | p = worker->task; |
| 1226 | |
| 1227 | #ifdef CONFIG_SMP |
| 1228 | /* |
| 1229 | * Idle @worker is about to execute @work and waking up provides an |
| 1230 | * opportunity to migrate @worker at a lower cost by setting the task's |
| 1231 | * wake_cpu field. Let's see if we want to move @worker to improve |
| 1232 | * execution locality. |
| 1233 | * |
| 1234 | * We're waking the worker that went idle the latest and there's some |
| 1235 | * chance that @worker is marked idle but hasn't gone off CPU yet. If |
| 1236 | * so, setting the wake_cpu won't do anything. As this is a best-effort |
| 1237 | * optimization and the race window is narrow, let's leave as-is for |
| 1238 | * now. If this becomes pronounced, we can skip over workers which are |
| 1239 | * still on cpu when picking an idle worker. |
| 1240 | * |
| 1241 | * If @pool has non-strict affinity, @worker might have ended up outside |
| 1242 | * its affinity scope. Repatriate. |
| 1243 | */ |
| 1244 | if (!pool->attrs->affn_strict && |
| 1245 | !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { |
| 1246 | struct work_struct *work = list_first_entry(&pool->worklist, |
| 1247 | struct work_struct, entry); |
| 1248 | p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); |
| 1249 | get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; |
| 1250 | } |
| 1251 | #endif |
| 1252 | wake_up_process(p); |
| 1253 | return true; |
| 1254 | } |
| 1255 | |
| 1256 | #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT |
| 1257 | |
| 1258 | /* |
| 1259 | * Concurrency-managed per-cpu work items that hog CPU for longer than |
| 1260 | * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, |
| 1261 | * which prevents them from stalling other concurrency-managed work items. If a |
| 1262 | * work function keeps triggering this mechanism, it's likely that the work item |
| 1263 | * should be using an unbound workqueue instead. |
| 1264 | * |
| 1265 | * wq_cpu_intensive_report() tracks work functions which trigger such conditions |
| 1266 | * and report them so that they can be examined and converted to use unbound |
| 1267 | * workqueues as appropriate. To avoid flooding the console, each violating work |
| 1268 | * function is tracked and reported with exponential backoff. |
| 1269 | */ |
| 1270 | #define WCI_MAX_ENTS 128 |
| 1271 | |
| 1272 | struct wci_ent { |
| 1273 | work_func_t func; |
| 1274 | atomic64_t cnt; |
| 1275 | struct hlist_node hash_node; |
| 1276 | }; |
| 1277 | |
| 1278 | static struct wci_ent wci_ents[WCI_MAX_ENTS]; |
| 1279 | static int wci_nr_ents; |
| 1280 | static DEFINE_RAW_SPINLOCK(wci_lock); |
| 1281 | static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); |
| 1282 | |
| 1283 | static struct wci_ent *wci_find_ent(work_func_t func) |
| 1284 | { |
| 1285 | struct wci_ent *ent; |
| 1286 | |
| 1287 | hash_for_each_possible_rcu(wci_hash, ent, hash_node, |
| 1288 | (unsigned long)func) { |
| 1289 | if (ent->func == func) |
| 1290 | return ent; |
| 1291 | } |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | |
| 1295 | static void wq_cpu_intensive_report(work_func_t func) |
| 1296 | { |
| 1297 | struct wci_ent *ent; |
| 1298 | |
| 1299 | restart: |
| 1300 | ent = wci_find_ent(func); |
| 1301 | if (ent) { |
| 1302 | u64 cnt; |
| 1303 | |
| 1304 | /* |
| 1305 | * Start reporting from the fourth time and back off |
| 1306 | * exponentially. |
| 1307 | */ |
| 1308 | cnt = atomic64_inc_return_relaxed(&ent->cnt); |
| 1309 | if (cnt >= 4 && is_power_of_2(cnt)) |
| 1310 | printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", |
| 1311 | ent->func, wq_cpu_intensive_thresh_us, |
| 1312 | atomic64_read(&ent->cnt)); |
| 1313 | return; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * @func is a new violation. Allocate a new entry for it. If wcn_ents[] |
| 1318 | * is exhausted, something went really wrong and we probably made enough |
| 1319 | * noise already. |
| 1320 | */ |
| 1321 | if (wci_nr_ents >= WCI_MAX_ENTS) |
| 1322 | return; |
| 1323 | |
| 1324 | raw_spin_lock(&wci_lock); |
| 1325 | |
| 1326 | if (wci_nr_ents >= WCI_MAX_ENTS) { |
| 1327 | raw_spin_unlock(&wci_lock); |
| 1328 | return; |
| 1329 | } |
| 1330 | |
| 1331 | if (wci_find_ent(func)) { |
| 1332 | raw_spin_unlock(&wci_lock); |
| 1333 | goto restart; |
| 1334 | } |
| 1335 | |
| 1336 | ent = &wci_ents[wci_nr_ents++]; |
| 1337 | ent->func = func; |
| 1338 | atomic64_set(&ent->cnt, 1); |
| 1339 | hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); |
| 1340 | |
| 1341 | raw_spin_unlock(&wci_lock); |
| 1342 | } |
| 1343 | |
| 1344 | #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ |
| 1345 | static void wq_cpu_intensive_report(work_func_t func) {} |
| 1346 | #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ |
| 1347 | |
| 1348 | /** |
| 1349 | * wq_worker_running - a worker is running again |
| 1350 | * @task: task waking up |
| 1351 | * |
| 1352 | * This function is called when a worker returns from schedule() |
| 1353 | */ |
| 1354 | void wq_worker_running(struct task_struct *task) |
| 1355 | { |
| 1356 | struct worker *worker = kthread_data(task); |
| 1357 | |
| 1358 | if (!READ_ONCE(worker->sleeping)) |
| 1359 | return; |
| 1360 | |
| 1361 | /* |
| 1362 | * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check |
| 1363 | * and the nr_running increment below, we may ruin the nr_running reset |
| 1364 | * and leave with an unexpected pool->nr_running == 1 on the newly unbound |
| 1365 | * pool. Protect against such race. |
| 1366 | */ |
| 1367 | preempt_disable(); |
| 1368 | if (!(worker->flags & WORKER_NOT_RUNNING)) |
| 1369 | worker->pool->nr_running++; |
| 1370 | preempt_enable(); |
| 1371 | |
| 1372 | /* |
| 1373 | * CPU intensive auto-detection cares about how long a work item hogged |
| 1374 | * CPU without sleeping. Reset the starting timestamp on wakeup. |
| 1375 | */ |
| 1376 | worker->current_at = worker->task->se.sum_exec_runtime; |
| 1377 | |
| 1378 | WRITE_ONCE(worker->sleeping, 0); |
| 1379 | } |
| 1380 | |
| 1381 | /** |
| 1382 | * wq_worker_sleeping - a worker is going to sleep |
| 1383 | * @task: task going to sleep |
| 1384 | * |
| 1385 | * This function is called from schedule() when a busy worker is |
| 1386 | * going to sleep. |
| 1387 | */ |
| 1388 | void wq_worker_sleeping(struct task_struct *task) |
| 1389 | { |
| 1390 | struct worker *worker = kthread_data(task); |
| 1391 | struct worker_pool *pool; |
| 1392 | |
| 1393 | /* |
| 1394 | * Rescuers, which may not have all the fields set up like normal |
| 1395 | * workers, also reach here, let's not access anything before |
| 1396 | * checking NOT_RUNNING. |
| 1397 | */ |
| 1398 | if (worker->flags & WORKER_NOT_RUNNING) |
| 1399 | return; |
| 1400 | |
| 1401 | pool = worker->pool; |
| 1402 | |
| 1403 | /* Return if preempted before wq_worker_running() was reached */ |
| 1404 | if (READ_ONCE(worker->sleeping)) |
| 1405 | return; |
| 1406 | |
| 1407 | WRITE_ONCE(worker->sleeping, 1); |
| 1408 | raw_spin_lock_irq(&pool->lock); |
| 1409 | |
| 1410 | /* |
| 1411 | * Recheck in case unbind_workers() preempted us. We don't |
| 1412 | * want to decrement nr_running after the worker is unbound |
| 1413 | * and nr_running has been reset. |
| 1414 | */ |
| 1415 | if (worker->flags & WORKER_NOT_RUNNING) { |
| 1416 | raw_spin_unlock_irq(&pool->lock); |
| 1417 | return; |
| 1418 | } |
| 1419 | |
| 1420 | pool->nr_running--; |
| 1421 | if (kick_pool(pool)) |
| 1422 | worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; |
| 1423 | |
| 1424 | raw_spin_unlock_irq(&pool->lock); |
| 1425 | } |
| 1426 | |
| 1427 | /** |
| 1428 | * wq_worker_tick - a scheduler tick occurred while a kworker is running |
| 1429 | * @task: task currently running |
| 1430 | * |
| 1431 | * Called from scheduler_tick(). We're in the IRQ context and the current |
| 1432 | * worker's fields which follow the 'K' locking rule can be accessed safely. |
| 1433 | */ |
| 1434 | void wq_worker_tick(struct task_struct *task) |
| 1435 | { |
| 1436 | struct worker *worker = kthread_data(task); |
| 1437 | struct pool_workqueue *pwq = worker->current_pwq; |
| 1438 | struct worker_pool *pool = worker->pool; |
| 1439 | |
| 1440 | if (!pwq) |
| 1441 | return; |
| 1442 | |
| 1443 | pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; |
| 1444 | |
| 1445 | if (!wq_cpu_intensive_thresh_us) |
| 1446 | return; |
| 1447 | |
| 1448 | /* |
| 1449 | * If the current worker is concurrency managed and hogged the CPU for |
| 1450 | * longer than wq_cpu_intensive_thresh_us, it's automatically marked |
| 1451 | * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. |
| 1452 | * |
| 1453 | * Set @worker->sleeping means that @worker is in the process of |
| 1454 | * switching out voluntarily and won't be contributing to |
| 1455 | * @pool->nr_running until it wakes up. As wq_worker_sleeping() also |
| 1456 | * decrements ->nr_running, setting CPU_INTENSIVE here can lead to |
| 1457 | * double decrements. The task is releasing the CPU anyway. Let's skip. |
| 1458 | * We probably want to make this prettier in the future. |
| 1459 | */ |
| 1460 | if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || |
| 1461 | worker->task->se.sum_exec_runtime - worker->current_at < |
| 1462 | wq_cpu_intensive_thresh_us * NSEC_PER_USEC) |
| 1463 | return; |
| 1464 | |
| 1465 | raw_spin_lock(&pool->lock); |
| 1466 | |
| 1467 | worker_set_flags(worker, WORKER_CPU_INTENSIVE); |
| 1468 | wq_cpu_intensive_report(worker->current_func); |
| 1469 | pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; |
| 1470 | |
| 1471 | if (kick_pool(pool)) |
| 1472 | pwq->stats[PWQ_STAT_CM_WAKEUP]++; |
| 1473 | |
| 1474 | raw_spin_unlock(&pool->lock); |
| 1475 | } |
| 1476 | |
| 1477 | /** |
| 1478 | * wq_worker_last_func - retrieve worker's last work function |
| 1479 | * @task: Task to retrieve last work function of. |
| 1480 | * |
| 1481 | * Determine the last function a worker executed. This is called from |
| 1482 | * the scheduler to get a worker's last known identity. |
| 1483 | * |
| 1484 | * CONTEXT: |
| 1485 | * raw_spin_lock_irq(rq->lock) |
| 1486 | * |
| 1487 | * This function is called during schedule() when a kworker is going |
| 1488 | * to sleep. It's used by psi to identify aggregation workers during |
| 1489 | * dequeuing, to allow periodic aggregation to shut-off when that |
| 1490 | * worker is the last task in the system or cgroup to go to sleep. |
| 1491 | * |
| 1492 | * As this function doesn't involve any workqueue-related locking, it |
| 1493 | * only returns stable values when called from inside the scheduler's |
| 1494 | * queuing and dequeuing paths, when @task, which must be a kworker, |
| 1495 | * is guaranteed to not be processing any works. |
| 1496 | * |
| 1497 | * Return: |
| 1498 | * The last work function %current executed as a worker, NULL if it |
| 1499 | * hasn't executed any work yet. |
| 1500 | */ |
| 1501 | work_func_t wq_worker_last_func(struct task_struct *task) |
| 1502 | { |
| 1503 | struct worker *worker = kthread_data(task); |
| 1504 | |
| 1505 | return worker->last_func; |
| 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * wq_node_nr_active - Determine wq_node_nr_active to use |
| 1510 | * @wq: workqueue of interest |
| 1511 | * @node: NUMA node, can be %NUMA_NO_NODE |
| 1512 | * |
| 1513 | * Determine wq_node_nr_active to use for @wq on @node. Returns: |
| 1514 | * |
| 1515 | * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. |
| 1516 | * |
| 1517 | * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. |
| 1518 | * |
| 1519 | * - Otherwise, node_nr_active[@node]. |
| 1520 | */ |
| 1521 | static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, |
| 1522 | int node) |
| 1523 | { |
| 1524 | if (!(wq->flags & WQ_UNBOUND)) |
| 1525 | return NULL; |
| 1526 | |
| 1527 | if (node == NUMA_NO_NODE) |
| 1528 | node = nr_node_ids; |
| 1529 | |
| 1530 | return wq->node_nr_active[node]; |
| 1531 | } |
| 1532 | |
| 1533 | /** |
| 1534 | * wq_update_node_max_active - Update per-node max_actives to use |
| 1535 | * @wq: workqueue to update |
| 1536 | * @off_cpu: CPU that's going down, -1 if a CPU is not going down |
| 1537 | * |
| 1538 | * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is |
| 1539 | * distributed among nodes according to the proportions of numbers of online |
| 1540 | * cpus. The result is always between @wq->min_active and max_active. |
| 1541 | */ |
| 1542 | static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu) |
| 1543 | { |
| 1544 | struct cpumask *effective = unbound_effective_cpumask(wq); |
| 1545 | int min_active = READ_ONCE(wq->min_active); |
| 1546 | int max_active = READ_ONCE(wq->max_active); |
| 1547 | int total_cpus, node; |
| 1548 | |
| 1549 | lockdep_assert_held(&wq->mutex); |
| 1550 | |
| 1551 | if (!wq_topo_initialized) |
| 1552 | return; |
| 1553 | |
| 1554 | if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective)) |
| 1555 | off_cpu = -1; |
| 1556 | |
| 1557 | total_cpus = cpumask_weight_and(effective, cpu_online_mask); |
| 1558 | if (off_cpu >= 0) |
| 1559 | total_cpus--; |
| 1560 | |
| 1561 | for_each_node(node) { |
| 1562 | int node_cpus; |
| 1563 | |
| 1564 | node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); |
| 1565 | if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) |
| 1566 | node_cpus--; |
| 1567 | |
| 1568 | wq_node_nr_active(wq, node)->max = |
| 1569 | clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), |
| 1570 | min_active, max_active); |
| 1571 | } |
| 1572 | |
| 1573 | wq_node_nr_active(wq, NUMA_NO_NODE)->max = min_active; |
| 1574 | } |
| 1575 | |
| 1576 | /** |
| 1577 | * get_pwq - get an extra reference on the specified pool_workqueue |
| 1578 | * @pwq: pool_workqueue to get |
| 1579 | * |
| 1580 | * Obtain an extra reference on @pwq. The caller should guarantee that |
| 1581 | * @pwq has positive refcnt and be holding the matching pool->lock. |
| 1582 | */ |
| 1583 | static void get_pwq(struct pool_workqueue *pwq) |
| 1584 | { |
| 1585 | lockdep_assert_held(&pwq->pool->lock); |
| 1586 | WARN_ON_ONCE(pwq->refcnt <= 0); |
| 1587 | pwq->refcnt++; |
| 1588 | } |
| 1589 | |
| 1590 | /** |
| 1591 | * put_pwq - put a pool_workqueue reference |
| 1592 | * @pwq: pool_workqueue to put |
| 1593 | * |
| 1594 | * Drop a reference of @pwq. If its refcnt reaches zero, schedule its |
| 1595 | * destruction. The caller should be holding the matching pool->lock. |
| 1596 | */ |
| 1597 | static void put_pwq(struct pool_workqueue *pwq) |
| 1598 | { |
| 1599 | lockdep_assert_held(&pwq->pool->lock); |
| 1600 | if (likely(--pwq->refcnt)) |
| 1601 | return; |
| 1602 | /* |
| 1603 | * @pwq can't be released under pool->lock, bounce to a dedicated |
| 1604 | * kthread_worker to avoid A-A deadlocks. |
| 1605 | */ |
| 1606 | kthread_queue_work(pwq_release_worker, &pwq->release_work); |
| 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock |
| 1611 | * @pwq: pool_workqueue to put (can be %NULL) |
| 1612 | * |
| 1613 | * put_pwq() with locking. This function also allows %NULL @pwq. |
| 1614 | */ |
| 1615 | static void put_pwq_unlocked(struct pool_workqueue *pwq) |
| 1616 | { |
| 1617 | if (pwq) { |
| 1618 | /* |
| 1619 | * As both pwqs and pools are RCU protected, the |
| 1620 | * following lock operations are safe. |
| 1621 | */ |
| 1622 | raw_spin_lock_irq(&pwq->pool->lock); |
| 1623 | put_pwq(pwq); |
| 1624 | raw_spin_unlock_irq(&pwq->pool->lock); |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | static bool pwq_is_empty(struct pool_workqueue *pwq) |
| 1629 | { |
| 1630 | return !pwq->nr_active && list_empty(&pwq->inactive_works); |
| 1631 | } |
| 1632 | |
| 1633 | static void __pwq_activate_work(struct pool_workqueue *pwq, |
| 1634 | struct work_struct *work) |
| 1635 | { |
| 1636 | unsigned long *wdb = work_data_bits(work); |
| 1637 | |
| 1638 | WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); |
| 1639 | trace_workqueue_activate_work(work); |
| 1640 | if (list_empty(&pwq->pool->worklist)) |
| 1641 | pwq->pool->watchdog_ts = jiffies; |
| 1642 | move_linked_works(work, &pwq->pool->worklist, NULL); |
| 1643 | __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); |
| 1644 | } |
| 1645 | |
| 1646 | /** |
| 1647 | * pwq_activate_work - Activate a work item if inactive |
| 1648 | * @pwq: pool_workqueue @work belongs to |
| 1649 | * @work: work item to activate |
| 1650 | * |
| 1651 | * Returns %true if activated. %false if already active. |
| 1652 | */ |
| 1653 | static bool pwq_activate_work(struct pool_workqueue *pwq, |
| 1654 | struct work_struct *work) |
| 1655 | { |
| 1656 | struct worker_pool *pool = pwq->pool; |
| 1657 | struct wq_node_nr_active *nna; |
| 1658 | |
| 1659 | lockdep_assert_held(&pool->lock); |
| 1660 | |
| 1661 | if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) |
| 1662 | return false; |
| 1663 | |
| 1664 | nna = wq_node_nr_active(pwq->wq, pool->node); |
| 1665 | if (nna) |
| 1666 | atomic_inc(&nna->nr); |
| 1667 | |
| 1668 | pwq->nr_active++; |
| 1669 | __pwq_activate_work(pwq, work); |
| 1670 | return true; |
| 1671 | } |
| 1672 | |
| 1673 | static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) |
| 1674 | { |
| 1675 | int max = READ_ONCE(nna->max); |
| 1676 | |
| 1677 | while (true) { |
| 1678 | int old, tmp; |
| 1679 | |
| 1680 | old = atomic_read(&nna->nr); |
| 1681 | if (old >= max) |
| 1682 | return false; |
| 1683 | tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); |
| 1684 | if (tmp == old) |
| 1685 | return true; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | /** |
| 1690 | * pwq_tryinc_nr_active - Try to increment nr_active for a pwq |
| 1691 | * @pwq: pool_workqueue of interest |
| 1692 | * @fill: max_active may have increased, try to increase concurrency level |
| 1693 | * |
| 1694 | * Try to increment nr_active for @pwq. Returns %true if an nr_active count is |
| 1695 | * successfully obtained. %false otherwise. |
| 1696 | */ |
| 1697 | static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) |
| 1698 | { |
| 1699 | struct workqueue_struct *wq = pwq->wq; |
| 1700 | struct worker_pool *pool = pwq->pool; |
| 1701 | struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); |
| 1702 | bool obtained = false; |
| 1703 | |
| 1704 | lockdep_assert_held(&pool->lock); |
| 1705 | |
| 1706 | if (!nna) { |
| 1707 | /* BH or per-cpu workqueue, pwq->nr_active is sufficient */ |
| 1708 | obtained = pwq->nr_active < READ_ONCE(wq->max_active); |
| 1709 | goto out; |
| 1710 | } |
| 1711 | |
| 1712 | if (unlikely(pwq->plugged)) |
| 1713 | return false; |
| 1714 | |
| 1715 | /* |
| 1716 | * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is |
| 1717 | * already waiting on $nna, pwq_dec_nr_active() will maintain the |
| 1718 | * concurrency level. Don't jump the line. |
| 1719 | * |
| 1720 | * We need to ignore the pending test after max_active has increased as |
| 1721 | * pwq_dec_nr_active() can only maintain the concurrency level but not |
| 1722 | * increase it. This is indicated by @fill. |
| 1723 | */ |
| 1724 | if (!list_empty(&pwq->pending_node) && likely(!fill)) |
| 1725 | goto out; |
| 1726 | |
| 1727 | obtained = tryinc_node_nr_active(nna); |
| 1728 | if (obtained) |
| 1729 | goto out; |
| 1730 | |
| 1731 | /* |
| 1732 | * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs |
| 1733 | * and try again. The smp_mb() is paired with the implied memory barrier |
| 1734 | * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either |
| 1735 | * we see the decremented $nna->nr or they see non-empty |
| 1736 | * $nna->pending_pwqs. |
| 1737 | */ |
| 1738 | raw_spin_lock(&nna->lock); |
| 1739 | |
| 1740 | if (list_empty(&pwq->pending_node)) |
| 1741 | list_add_tail(&pwq->pending_node, &nna->pending_pwqs); |
| 1742 | else if (likely(!fill)) |
| 1743 | goto out_unlock; |
| 1744 | |
| 1745 | smp_mb(); |
| 1746 | |
| 1747 | obtained = tryinc_node_nr_active(nna); |
| 1748 | |
| 1749 | /* |
| 1750 | * If @fill, @pwq might have already been pending. Being spuriously |
| 1751 | * pending in cold paths doesn't affect anything. Let's leave it be. |
| 1752 | */ |
| 1753 | if (obtained && likely(!fill)) |
| 1754 | list_del_init(&pwq->pending_node); |
| 1755 | |
| 1756 | out_unlock: |
| 1757 | raw_spin_unlock(&nna->lock); |
| 1758 | out: |
| 1759 | if (obtained) |
| 1760 | pwq->nr_active++; |
| 1761 | return obtained; |
| 1762 | } |
| 1763 | |
| 1764 | /** |
| 1765 | * pwq_activate_first_inactive - Activate the first inactive work item on a pwq |
| 1766 | * @pwq: pool_workqueue of interest |
| 1767 | * @fill: max_active may have increased, try to increase concurrency level |
| 1768 | * |
| 1769 | * Activate the first inactive work item of @pwq if available and allowed by |
| 1770 | * max_active limit. |
| 1771 | * |
| 1772 | * Returns %true if an inactive work item has been activated. %false if no |
| 1773 | * inactive work item is found or max_active limit is reached. |
| 1774 | */ |
| 1775 | static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) |
| 1776 | { |
| 1777 | struct work_struct *work = |
| 1778 | list_first_entry_or_null(&pwq->inactive_works, |
| 1779 | struct work_struct, entry); |
| 1780 | |
| 1781 | if (work && pwq_tryinc_nr_active(pwq, fill)) { |
| 1782 | __pwq_activate_work(pwq, work); |
| 1783 | return true; |
| 1784 | } else { |
| 1785 | return false; |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | /** |
| 1790 | * unplug_oldest_pwq - restart an oldest plugged pool_workqueue |
| 1791 | * @wq: workqueue_struct to be restarted |
| 1792 | * |
| 1793 | * pwq's are linked into wq->pwqs with the oldest first. For ordered |
| 1794 | * workqueues, only the oldest pwq is unplugged, the others are plugged to |
| 1795 | * suspend execution until the oldest one is drained. When this happens, the |
| 1796 | * next oldest one (first plugged pwq in iteration) will be unplugged to |
| 1797 | * restart work item execution to ensure proper work item ordering. |
| 1798 | * |
| 1799 | * dfl_pwq --------------+ [P] - plugged |
| 1800 | * | |
| 1801 | * v |
| 1802 | * pwqs -> A -> B [P] -> C [P] (newest) |
| 1803 | * | | | |
| 1804 | * 1 3 5 |
| 1805 | * | | | |
| 1806 | * 2 4 6 |
| 1807 | */ |
| 1808 | static void unplug_oldest_pwq(struct workqueue_struct *wq) |
| 1809 | { |
| 1810 | struct pool_workqueue *pwq; |
| 1811 | |
| 1812 | lockdep_assert_held(&wq->mutex); |
| 1813 | |
| 1814 | /* Caller should make sure that pwqs isn't empty before calling */ |
| 1815 | pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue, |
| 1816 | pwqs_node); |
| 1817 | raw_spin_lock_irq(&pwq->pool->lock); |
| 1818 | if (pwq->plugged) { |
| 1819 | pwq->plugged = false; |
| 1820 | if (pwq_activate_first_inactive(pwq, true)) |
| 1821 | kick_pool(pwq->pool); |
| 1822 | } |
| 1823 | raw_spin_unlock_irq(&pwq->pool->lock); |
| 1824 | } |
| 1825 | |
| 1826 | /** |
| 1827 | * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active |
| 1828 | * @nna: wq_node_nr_active to activate a pending pwq for |
| 1829 | * @caller_pool: worker_pool the caller is locking |
| 1830 | * |
| 1831 | * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. |
| 1832 | * @caller_pool may be unlocked and relocked to lock other worker_pools. |
| 1833 | */ |
| 1834 | static void node_activate_pending_pwq(struct wq_node_nr_active *nna, |
| 1835 | struct worker_pool *caller_pool) |
| 1836 | { |
| 1837 | struct worker_pool *locked_pool = caller_pool; |
| 1838 | struct pool_workqueue *pwq; |
| 1839 | struct work_struct *work; |
| 1840 | |
| 1841 | lockdep_assert_held(&caller_pool->lock); |
| 1842 | |
| 1843 | raw_spin_lock(&nna->lock); |
| 1844 | retry: |
| 1845 | pwq = list_first_entry_or_null(&nna->pending_pwqs, |
| 1846 | struct pool_workqueue, pending_node); |
| 1847 | if (!pwq) |
| 1848 | goto out_unlock; |
| 1849 | |
| 1850 | /* |
| 1851 | * If @pwq is for a different pool than @locked_pool, we need to lock |
| 1852 | * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock |
| 1853 | * / lock dance. For that, we also need to release @nna->lock as it's |
| 1854 | * nested inside pool locks. |
| 1855 | */ |
| 1856 | if (pwq->pool != locked_pool) { |
| 1857 | raw_spin_unlock(&locked_pool->lock); |
| 1858 | locked_pool = pwq->pool; |
| 1859 | if (!raw_spin_trylock(&locked_pool->lock)) { |
| 1860 | raw_spin_unlock(&nna->lock); |
| 1861 | raw_spin_lock(&locked_pool->lock); |
| 1862 | raw_spin_lock(&nna->lock); |
| 1863 | goto retry; |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | * $pwq may not have any inactive work items due to e.g. cancellations. |
| 1869 | * Drop it from pending_pwqs and see if there's another one. |
| 1870 | */ |
| 1871 | work = list_first_entry_or_null(&pwq->inactive_works, |
| 1872 | struct work_struct, entry); |
| 1873 | if (!work) { |
| 1874 | list_del_init(&pwq->pending_node); |
| 1875 | goto retry; |
| 1876 | } |
| 1877 | |
| 1878 | /* |
| 1879 | * Acquire an nr_active count and activate the inactive work item. If |
| 1880 | * $pwq still has inactive work items, rotate it to the end of the |
| 1881 | * pending_pwqs so that we round-robin through them. This means that |
| 1882 | * inactive work items are not activated in queueing order which is fine |
| 1883 | * given that there has never been any ordering across different pwqs. |
| 1884 | */ |
| 1885 | if (likely(tryinc_node_nr_active(nna))) { |
| 1886 | pwq->nr_active++; |
| 1887 | __pwq_activate_work(pwq, work); |
| 1888 | |
| 1889 | if (list_empty(&pwq->inactive_works)) |
| 1890 | list_del_init(&pwq->pending_node); |
| 1891 | else |
| 1892 | list_move_tail(&pwq->pending_node, &nna->pending_pwqs); |
| 1893 | |
| 1894 | /* if activating a foreign pool, make sure it's running */ |
| 1895 | if (pwq->pool != caller_pool) |
| 1896 | kick_pool(pwq->pool); |
| 1897 | } |
| 1898 | |
| 1899 | out_unlock: |
| 1900 | raw_spin_unlock(&nna->lock); |
| 1901 | if (locked_pool != caller_pool) { |
| 1902 | raw_spin_unlock(&locked_pool->lock); |
| 1903 | raw_spin_lock(&caller_pool->lock); |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | /** |
| 1908 | * pwq_dec_nr_active - Retire an active count |
| 1909 | * @pwq: pool_workqueue of interest |
| 1910 | * |
| 1911 | * Decrement @pwq's nr_active and try to activate the first inactive work item. |
| 1912 | * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. |
| 1913 | */ |
| 1914 | static void pwq_dec_nr_active(struct pool_workqueue *pwq) |
| 1915 | { |
| 1916 | struct worker_pool *pool = pwq->pool; |
| 1917 | struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); |
| 1918 | |
| 1919 | lockdep_assert_held(&pool->lock); |
| 1920 | |
| 1921 | /* |
| 1922 | * @pwq->nr_active should be decremented for both percpu and unbound |
| 1923 | * workqueues. |
| 1924 | */ |
| 1925 | pwq->nr_active--; |
| 1926 | |
| 1927 | /* |
| 1928 | * For a percpu workqueue, it's simple. Just need to kick the first |
| 1929 | * inactive work item on @pwq itself. |
| 1930 | */ |
| 1931 | if (!nna) { |
| 1932 | pwq_activate_first_inactive(pwq, false); |
| 1933 | return; |
| 1934 | } |
| 1935 | |
| 1936 | /* |
| 1937 | * If @pwq is for an unbound workqueue, it's more complicated because |
| 1938 | * multiple pwqs and pools may be sharing the nr_active count. When a |
| 1939 | * pwq needs to wait for an nr_active count, it puts itself on |
| 1940 | * $nna->pending_pwqs. The following atomic_dec_return()'s implied |
| 1941 | * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to |
| 1942 | * guarantee that either we see non-empty pending_pwqs or they see |
| 1943 | * decremented $nna->nr. |
| 1944 | * |
| 1945 | * $nna->max may change as CPUs come online/offline and @pwq->wq's |
| 1946 | * max_active gets updated. However, it is guaranteed to be equal to or |
| 1947 | * larger than @pwq->wq->min_active which is above zero unless freezing. |
| 1948 | * This maintains the forward progress guarantee. |
| 1949 | */ |
| 1950 | if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) |
| 1951 | return; |
| 1952 | |
| 1953 | if (!list_empty(&nna->pending_pwqs)) |
| 1954 | node_activate_pending_pwq(nna, pool); |
| 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight |
| 1959 | * @pwq: pwq of interest |
| 1960 | * @work_data: work_data of work which left the queue |
| 1961 | * |
| 1962 | * A work either has completed or is removed from pending queue, |
| 1963 | * decrement nr_in_flight of its pwq and handle workqueue flushing. |
| 1964 | * |
| 1965 | * NOTE: |
| 1966 | * For unbound workqueues, this function may temporarily drop @pwq->pool->lock |
| 1967 | * and thus should be called after all other state updates for the in-flight |
| 1968 | * work item is complete. |
| 1969 | * |
| 1970 | * CONTEXT: |
| 1971 | * raw_spin_lock_irq(pool->lock). |
| 1972 | */ |
| 1973 | static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) |
| 1974 | { |
| 1975 | int color = get_work_color(work_data); |
| 1976 | |
| 1977 | if (!(work_data & WORK_STRUCT_INACTIVE)) |
| 1978 | pwq_dec_nr_active(pwq); |
| 1979 | |
| 1980 | pwq->nr_in_flight[color]--; |
| 1981 | |
| 1982 | /* is flush in progress and are we at the flushing tip? */ |
| 1983 | if (likely(pwq->flush_color != color)) |
| 1984 | goto out_put; |
| 1985 | |
| 1986 | /* are there still in-flight works? */ |
| 1987 | if (pwq->nr_in_flight[color]) |
| 1988 | goto out_put; |
| 1989 | |
| 1990 | /* this pwq is done, clear flush_color */ |
| 1991 | pwq->flush_color = -1; |
| 1992 | |
| 1993 | /* |
| 1994 | * If this was the last pwq, wake up the first flusher. It |
| 1995 | * will handle the rest. |
| 1996 | */ |
| 1997 | if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) |
| 1998 | complete(&pwq->wq->first_flusher->done); |
| 1999 | out_put: |
| 2000 | put_pwq(pwq); |
| 2001 | } |
| 2002 | |
| 2003 | /** |
| 2004 | * try_to_grab_pending - steal work item from worklist and disable irq |
| 2005 | * @work: work item to steal |
| 2006 | * @is_dwork: @work is a delayed_work |
| 2007 | * @flags: place to store irq state |
| 2008 | * |
| 2009 | * Try to grab PENDING bit of @work. This function can handle @work in any |
| 2010 | * stable state - idle, on timer or on worklist. |
| 2011 | * |
| 2012 | * Return: |
| 2013 | * |
| 2014 | * ======== ================================================================ |
| 2015 | * 1 if @work was pending and we successfully stole PENDING |
| 2016 | * 0 if @work was idle and we claimed PENDING |
| 2017 | * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry |
| 2018 | * -ENOENT if someone else is canceling @work, this state may persist |
| 2019 | * for arbitrarily long |
| 2020 | * ======== ================================================================ |
| 2021 | * |
| 2022 | * Note: |
| 2023 | * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting |
| 2024 | * interrupted while holding PENDING and @work off queue, irq must be |
| 2025 | * disabled on entry. This, combined with delayed_work->timer being |
| 2026 | * irqsafe, ensures that we return -EAGAIN for finite short period of time. |
| 2027 | * |
| 2028 | * On successful return, >= 0, irq is disabled and the caller is |
| 2029 | * responsible for releasing it using local_irq_restore(*@flags). |
| 2030 | * |
| 2031 | * This function is safe to call from any context including IRQ handler. |
| 2032 | */ |
| 2033 | static int try_to_grab_pending(struct work_struct *work, bool is_dwork, |
| 2034 | unsigned long *flags) |
| 2035 | { |
| 2036 | struct worker_pool *pool; |
| 2037 | struct pool_workqueue *pwq; |
| 2038 | |
| 2039 | local_irq_save(*flags); |
| 2040 | |
| 2041 | /* try to steal the timer if it exists */ |
| 2042 | if (is_dwork) { |
| 2043 | struct delayed_work *dwork = to_delayed_work(work); |
| 2044 | |
| 2045 | /* |
| 2046 | * dwork->timer is irqsafe. If del_timer() fails, it's |
| 2047 | * guaranteed that the timer is not queued anywhere and not |
| 2048 | * running on the local CPU. |
| 2049 | */ |
| 2050 | if (likely(del_timer(&dwork->timer))) |
| 2051 | return 1; |
| 2052 | } |
| 2053 | |
| 2054 | /* try to claim PENDING the normal way */ |
| 2055 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) |
| 2056 | return 0; |
| 2057 | |
| 2058 | rcu_read_lock(); |
| 2059 | /* |
| 2060 | * The queueing is in progress, or it is already queued. Try to |
| 2061 | * steal it from ->worklist without clearing WORK_STRUCT_PENDING. |
| 2062 | */ |
| 2063 | pool = get_work_pool(work); |
| 2064 | if (!pool) |
| 2065 | goto fail; |
| 2066 | |
| 2067 | raw_spin_lock(&pool->lock); |
| 2068 | /* |
| 2069 | * work->data is guaranteed to point to pwq only while the work |
| 2070 | * item is queued on pwq->wq, and both updating work->data to point |
| 2071 | * to pwq on queueing and to pool on dequeueing are done under |
| 2072 | * pwq->pool->lock. This in turn guarantees that, if work->data |
| 2073 | * points to pwq which is associated with a locked pool, the work |
| 2074 | * item is currently queued on that pool. |
| 2075 | */ |
| 2076 | pwq = get_work_pwq(work); |
| 2077 | if (pwq && pwq->pool == pool) { |
| 2078 | unsigned long work_data; |
| 2079 | |
| 2080 | debug_work_deactivate(work); |
| 2081 | |
| 2082 | /* |
| 2083 | * A cancelable inactive work item must be in the |
| 2084 | * pwq->inactive_works since a queued barrier can't be |
| 2085 | * canceled (see the comments in insert_wq_barrier()). |
| 2086 | * |
| 2087 | * An inactive work item cannot be grabbed directly because |
| 2088 | * it might have linked barrier work items which, if left |
| 2089 | * on the inactive_works list, will confuse pwq->nr_active |
| 2090 | * management later on and cause stall. Make sure the work |
| 2091 | * item is activated before grabbing. |
| 2092 | */ |
| 2093 | pwq_activate_work(pwq, work); |
| 2094 | |
| 2095 | list_del_init(&work->entry); |
| 2096 | |
| 2097 | /* |
| 2098 | * work->data points to pwq iff queued. Let's point to pool. As |
| 2099 | * this destroys work->data needed by the next step, stash it. |
| 2100 | */ |
| 2101 | work_data = *work_data_bits(work); |
| 2102 | set_work_pool_and_keep_pending(work, pool->id); |
| 2103 | |
| 2104 | /* must be the last step, see the function comment */ |
| 2105 | pwq_dec_nr_in_flight(pwq, work_data); |
| 2106 | |
| 2107 | raw_spin_unlock(&pool->lock); |
| 2108 | rcu_read_unlock(); |
| 2109 | return 1; |
| 2110 | } |
| 2111 | raw_spin_unlock(&pool->lock); |
| 2112 | fail: |
| 2113 | rcu_read_unlock(); |
| 2114 | local_irq_restore(*flags); |
| 2115 | if (work_is_canceling(work)) |
| 2116 | return -ENOENT; |
| 2117 | cpu_relax(); |
| 2118 | return -EAGAIN; |
| 2119 | } |
| 2120 | |
| 2121 | /** |
| 2122 | * insert_work - insert a work into a pool |
| 2123 | * @pwq: pwq @work belongs to |
| 2124 | * @work: work to insert |
| 2125 | * @head: insertion point |
| 2126 | * @extra_flags: extra WORK_STRUCT_* flags to set |
| 2127 | * |
| 2128 | * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to |
| 2129 | * work_struct flags. |
| 2130 | * |
| 2131 | * CONTEXT: |
| 2132 | * raw_spin_lock_irq(pool->lock). |
| 2133 | */ |
| 2134 | static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, |
| 2135 | struct list_head *head, unsigned int extra_flags) |
| 2136 | { |
| 2137 | debug_work_activate(work); |
| 2138 | |
| 2139 | /* record the work call stack in order to print it in KASAN reports */ |
| 2140 | kasan_record_aux_stack_noalloc(work); |
| 2141 | |
| 2142 | /* we own @work, set data and link */ |
| 2143 | set_work_pwq(work, pwq, extra_flags); |
| 2144 | list_add_tail(&work->entry, head); |
| 2145 | get_pwq(pwq); |
| 2146 | } |
| 2147 | |
| 2148 | /* |
| 2149 | * Test whether @work is being queued from another work executing on the |
| 2150 | * same workqueue. |
| 2151 | */ |
| 2152 | static bool is_chained_work(struct workqueue_struct *wq) |
| 2153 | { |
| 2154 | struct worker *worker; |
| 2155 | |
| 2156 | worker = current_wq_worker(); |
| 2157 | /* |
| 2158 | * Return %true iff I'm a worker executing a work item on @wq. If |
| 2159 | * I'm @worker, it's safe to dereference it without locking. |
| 2160 | */ |
| 2161 | return worker && worker->current_pwq->wq == wq; |
| 2162 | } |
| 2163 | |
| 2164 | /* |
| 2165 | * When queueing an unbound work item to a wq, prefer local CPU if allowed |
| 2166 | * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to |
| 2167 | * avoid perturbing sensitive tasks. |
| 2168 | */ |
| 2169 | static int wq_select_unbound_cpu(int cpu) |
| 2170 | { |
| 2171 | int new_cpu; |
| 2172 | |
| 2173 | if (likely(!wq_debug_force_rr_cpu)) { |
| 2174 | if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) |
| 2175 | return cpu; |
| 2176 | } else { |
| 2177 | pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); |
| 2178 | } |
| 2179 | |
| 2180 | new_cpu = __this_cpu_read(wq_rr_cpu_last); |
| 2181 | new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); |
| 2182 | if (unlikely(new_cpu >= nr_cpu_ids)) { |
| 2183 | new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); |
| 2184 | if (unlikely(new_cpu >= nr_cpu_ids)) |
| 2185 | return cpu; |
| 2186 | } |
| 2187 | __this_cpu_write(wq_rr_cpu_last, new_cpu); |
| 2188 | |
| 2189 | return new_cpu; |
| 2190 | } |
| 2191 | |
| 2192 | static void __queue_work(int cpu, struct workqueue_struct *wq, |
| 2193 | struct work_struct *work) |
| 2194 | { |
| 2195 | struct pool_workqueue *pwq; |
| 2196 | struct worker_pool *last_pool, *pool; |
| 2197 | unsigned int work_flags; |
| 2198 | unsigned int req_cpu = cpu; |
| 2199 | |
| 2200 | /* |
| 2201 | * While a work item is PENDING && off queue, a task trying to |
| 2202 | * steal the PENDING will busy-loop waiting for it to either get |
| 2203 | * queued or lose PENDING. Grabbing PENDING and queueing should |
| 2204 | * happen with IRQ disabled. |
| 2205 | */ |
| 2206 | lockdep_assert_irqs_disabled(); |
| 2207 | |
| 2208 | |
| 2209 | /* |
| 2210 | * For a draining wq, only works from the same workqueue are |
| 2211 | * allowed. The __WQ_DESTROYING helps to spot the issue that |
| 2212 | * queues a new work item to a wq after destroy_workqueue(wq). |
| 2213 | */ |
| 2214 | if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && |
| 2215 | WARN_ON_ONCE(!is_chained_work(wq)))) |
| 2216 | return; |
| 2217 | rcu_read_lock(); |
| 2218 | retry: |
| 2219 | /* pwq which will be used unless @work is executing elsewhere */ |
| 2220 | if (req_cpu == WORK_CPU_UNBOUND) { |
| 2221 | if (wq->flags & WQ_UNBOUND) |
| 2222 | cpu = wq_select_unbound_cpu(raw_smp_processor_id()); |
| 2223 | else |
| 2224 | cpu = raw_smp_processor_id(); |
| 2225 | } |
| 2226 | |
| 2227 | pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); |
| 2228 | pool = pwq->pool; |
| 2229 | |
| 2230 | /* |
| 2231 | * If @work was previously on a different pool, it might still be |
| 2232 | * running there, in which case the work needs to be queued on that |
| 2233 | * pool to guarantee non-reentrancy. |
| 2234 | */ |
| 2235 | last_pool = get_work_pool(work); |
| 2236 | if (last_pool && last_pool != pool) { |
| 2237 | struct worker *worker; |
| 2238 | |
| 2239 | raw_spin_lock(&last_pool->lock); |
| 2240 | |
| 2241 | worker = find_worker_executing_work(last_pool, work); |
| 2242 | |
| 2243 | if (worker && worker->current_pwq->wq == wq) { |
| 2244 | pwq = worker->current_pwq; |
| 2245 | pool = pwq->pool; |
| 2246 | WARN_ON_ONCE(pool != last_pool); |
| 2247 | } else { |
| 2248 | /* meh... not running there, queue here */ |
| 2249 | raw_spin_unlock(&last_pool->lock); |
| 2250 | raw_spin_lock(&pool->lock); |
| 2251 | } |
| 2252 | } else { |
| 2253 | raw_spin_lock(&pool->lock); |
| 2254 | } |
| 2255 | |
| 2256 | /* |
| 2257 | * pwq is determined and locked. For unbound pools, we could have raced |
| 2258 | * with pwq release and it could already be dead. If its refcnt is zero, |
| 2259 | * repeat pwq selection. Note that unbound pwqs never die without |
| 2260 | * another pwq replacing it in cpu_pwq or while work items are executing |
| 2261 | * on it, so the retrying is guaranteed to make forward-progress. |
| 2262 | */ |
| 2263 | if (unlikely(!pwq->refcnt)) { |
| 2264 | if (wq->flags & WQ_UNBOUND) { |
| 2265 | raw_spin_unlock(&pool->lock); |
| 2266 | cpu_relax(); |
| 2267 | goto retry; |
| 2268 | } |
| 2269 | /* oops */ |
| 2270 | WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", |
| 2271 | wq->name, cpu); |
| 2272 | } |
| 2273 | |
| 2274 | /* pwq determined, queue */ |
| 2275 | trace_workqueue_queue_work(req_cpu, pwq, work); |
| 2276 | |
| 2277 | if (WARN_ON(!list_empty(&work->entry))) |
| 2278 | goto out; |
| 2279 | |
| 2280 | pwq->nr_in_flight[pwq->work_color]++; |
| 2281 | work_flags = work_color_to_flags(pwq->work_color); |
| 2282 | |
| 2283 | /* |
| 2284 | * Limit the number of concurrently active work items to max_active. |
| 2285 | * @work must also queue behind existing inactive work items to maintain |
| 2286 | * ordering when max_active changes. See wq_adjust_max_active(). |
| 2287 | */ |
| 2288 | if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { |
| 2289 | if (list_empty(&pool->worklist)) |
| 2290 | pool->watchdog_ts = jiffies; |
| 2291 | |
| 2292 | trace_workqueue_activate_work(work); |
| 2293 | insert_work(pwq, work, &pool->worklist, work_flags); |
| 2294 | kick_pool(pool); |
| 2295 | } else { |
| 2296 | work_flags |= WORK_STRUCT_INACTIVE; |
| 2297 | insert_work(pwq, work, &pwq->inactive_works, work_flags); |
| 2298 | } |
| 2299 | |
| 2300 | out: |
| 2301 | raw_spin_unlock(&pool->lock); |
| 2302 | rcu_read_unlock(); |
| 2303 | } |
| 2304 | |
| 2305 | /** |
| 2306 | * queue_work_on - queue work on specific cpu |
| 2307 | * @cpu: CPU number to execute work on |
| 2308 | * @wq: workqueue to use |
| 2309 | * @work: work to queue |
| 2310 | * |
| 2311 | * We queue the work to a specific CPU, the caller must ensure it |
| 2312 | * can't go away. Callers that fail to ensure that the specified |
| 2313 | * CPU cannot go away will execute on a randomly chosen CPU. |
| 2314 | * But note well that callers specifying a CPU that never has been |
| 2315 | * online will get a splat. |
| 2316 | * |
| 2317 | * Return: %false if @work was already on a queue, %true otherwise. |
| 2318 | */ |
| 2319 | bool queue_work_on(int cpu, struct workqueue_struct *wq, |
| 2320 | struct work_struct *work) |
| 2321 | { |
| 2322 | bool ret = false; |
| 2323 | unsigned long flags; |
| 2324 | |
| 2325 | local_irq_save(flags); |
| 2326 | |
| 2327 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
| 2328 | __queue_work(cpu, wq, work); |
| 2329 | ret = true; |
| 2330 | } |
| 2331 | |
| 2332 | local_irq_restore(flags); |
| 2333 | return ret; |
| 2334 | } |
| 2335 | EXPORT_SYMBOL(queue_work_on); |
| 2336 | |
| 2337 | /** |
| 2338 | * select_numa_node_cpu - Select a CPU based on NUMA node |
| 2339 | * @node: NUMA node ID that we want to select a CPU from |
| 2340 | * |
| 2341 | * This function will attempt to find a "random" cpu available on a given |
| 2342 | * node. If there are no CPUs available on the given node it will return |
| 2343 | * WORK_CPU_UNBOUND indicating that we should just schedule to any |
| 2344 | * available CPU if we need to schedule this work. |
| 2345 | */ |
| 2346 | static int select_numa_node_cpu(int node) |
| 2347 | { |
| 2348 | int cpu; |
| 2349 | |
| 2350 | /* Delay binding to CPU if node is not valid or online */ |
| 2351 | if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) |
| 2352 | return WORK_CPU_UNBOUND; |
| 2353 | |
| 2354 | /* Use local node/cpu if we are already there */ |
| 2355 | cpu = raw_smp_processor_id(); |
| 2356 | if (node == cpu_to_node(cpu)) |
| 2357 | return cpu; |
| 2358 | |
| 2359 | /* Use "random" otherwise know as "first" online CPU of node */ |
| 2360 | cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); |
| 2361 | |
| 2362 | /* If CPU is valid return that, otherwise just defer */ |
| 2363 | return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; |
| 2364 | } |
| 2365 | |
| 2366 | /** |
| 2367 | * queue_work_node - queue work on a "random" cpu for a given NUMA node |
| 2368 | * @node: NUMA node that we are targeting the work for |
| 2369 | * @wq: workqueue to use |
| 2370 | * @work: work to queue |
| 2371 | * |
| 2372 | * We queue the work to a "random" CPU within a given NUMA node. The basic |
| 2373 | * idea here is to provide a way to somehow associate work with a given |
| 2374 | * NUMA node. |
| 2375 | * |
| 2376 | * This function will only make a best effort attempt at getting this onto |
| 2377 | * the right NUMA node. If no node is requested or the requested node is |
| 2378 | * offline then we just fall back to standard queue_work behavior. |
| 2379 | * |
| 2380 | * Currently the "random" CPU ends up being the first available CPU in the |
| 2381 | * intersection of cpu_online_mask and the cpumask of the node, unless we |
| 2382 | * are running on the node. In that case we just use the current CPU. |
| 2383 | * |
| 2384 | * Return: %false if @work was already on a queue, %true otherwise. |
| 2385 | */ |
| 2386 | bool queue_work_node(int node, struct workqueue_struct *wq, |
| 2387 | struct work_struct *work) |
| 2388 | { |
| 2389 | unsigned long flags; |
| 2390 | bool ret = false; |
| 2391 | |
| 2392 | /* |
| 2393 | * This current implementation is specific to unbound workqueues. |
| 2394 | * Specifically we only return the first available CPU for a given |
| 2395 | * node instead of cycling through individual CPUs within the node. |
| 2396 | * |
| 2397 | * If this is used with a per-cpu workqueue then the logic in |
| 2398 | * workqueue_select_cpu_near would need to be updated to allow for |
| 2399 | * some round robin type logic. |
| 2400 | */ |
| 2401 | WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); |
| 2402 | |
| 2403 | local_irq_save(flags); |
| 2404 | |
| 2405 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
| 2406 | int cpu = select_numa_node_cpu(node); |
| 2407 | |
| 2408 | __queue_work(cpu, wq, work); |
| 2409 | ret = true; |
| 2410 | } |
| 2411 | |
| 2412 | local_irq_restore(flags); |
| 2413 | return ret; |
| 2414 | } |
| 2415 | EXPORT_SYMBOL_GPL(queue_work_node); |
| 2416 | |
| 2417 | void delayed_work_timer_fn(struct timer_list *t) |
| 2418 | { |
| 2419 | struct delayed_work *dwork = from_timer(dwork, t, timer); |
| 2420 | |
| 2421 | /* should have been called from irqsafe timer with irq already off */ |
| 2422 | __queue_work(dwork->cpu, dwork->wq, &dwork->work); |
| 2423 | } |
| 2424 | EXPORT_SYMBOL(delayed_work_timer_fn); |
| 2425 | |
| 2426 | static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, |
| 2427 | struct delayed_work *dwork, unsigned long delay) |
| 2428 | { |
| 2429 | struct timer_list *timer = &dwork->timer; |
| 2430 | struct work_struct *work = &dwork->work; |
| 2431 | |
| 2432 | WARN_ON_ONCE(!wq); |
| 2433 | WARN_ON_ONCE(timer->function != delayed_work_timer_fn); |
| 2434 | WARN_ON_ONCE(timer_pending(timer)); |
| 2435 | WARN_ON_ONCE(!list_empty(&work->entry)); |
| 2436 | |
| 2437 | /* |
| 2438 | * If @delay is 0, queue @dwork->work immediately. This is for |
| 2439 | * both optimization and correctness. The earliest @timer can |
| 2440 | * expire is on the closest next tick and delayed_work users depend |
| 2441 | * on that there's no such delay when @delay is 0. |
| 2442 | */ |
| 2443 | if (!delay) { |
| 2444 | __queue_work(cpu, wq, &dwork->work); |
| 2445 | return; |
| 2446 | } |
| 2447 | |
| 2448 | dwork->wq = wq; |
| 2449 | dwork->cpu = cpu; |
| 2450 | timer->expires = jiffies + delay; |
| 2451 | |
| 2452 | if (housekeeping_enabled(HK_TYPE_TIMER)) { |
| 2453 | /* If the current cpu is a housekeeping cpu, use it. */ |
| 2454 | cpu = smp_processor_id(); |
| 2455 | if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) |
| 2456 | cpu = housekeeping_any_cpu(HK_TYPE_TIMER); |
| 2457 | add_timer_on(timer, cpu); |
| 2458 | } else { |
| 2459 | if (likely(cpu == WORK_CPU_UNBOUND)) |
| 2460 | add_timer(timer); |
| 2461 | else |
| 2462 | add_timer_on(timer, cpu); |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | /** |
| 2467 | * queue_delayed_work_on - queue work on specific CPU after delay |
| 2468 | * @cpu: CPU number to execute work on |
| 2469 | * @wq: workqueue to use |
| 2470 | * @dwork: work to queue |
| 2471 | * @delay: number of jiffies to wait before queueing |
| 2472 | * |
| 2473 | * Return: %false if @work was already on a queue, %true otherwise. If |
| 2474 | * @delay is zero and @dwork is idle, it will be scheduled for immediate |
| 2475 | * execution. |
| 2476 | */ |
| 2477 | bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
| 2478 | struct delayed_work *dwork, unsigned long delay) |
| 2479 | { |
| 2480 | struct work_struct *work = &dwork->work; |
| 2481 | bool ret = false; |
| 2482 | unsigned long flags; |
| 2483 | |
| 2484 | /* read the comment in __queue_work() */ |
| 2485 | local_irq_save(flags); |
| 2486 | |
| 2487 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
| 2488 | __queue_delayed_work(cpu, wq, dwork, delay); |
| 2489 | ret = true; |
| 2490 | } |
| 2491 | |
| 2492 | local_irq_restore(flags); |
| 2493 | return ret; |
| 2494 | } |
| 2495 | EXPORT_SYMBOL(queue_delayed_work_on); |
| 2496 | |
| 2497 | /** |
| 2498 | * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU |
| 2499 | * @cpu: CPU number to execute work on |
| 2500 | * @wq: workqueue to use |
| 2501 | * @dwork: work to queue |
| 2502 | * @delay: number of jiffies to wait before queueing |
| 2503 | * |
| 2504 | * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, |
| 2505 | * modify @dwork's timer so that it expires after @delay. If @delay is |
| 2506 | * zero, @work is guaranteed to be scheduled immediately regardless of its |
| 2507 | * current state. |
| 2508 | * |
| 2509 | * Return: %false if @dwork was idle and queued, %true if @dwork was |
| 2510 | * pending and its timer was modified. |
| 2511 | * |
| 2512 | * This function is safe to call from any context including IRQ handler. |
| 2513 | * See try_to_grab_pending() for details. |
| 2514 | */ |
| 2515 | bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, |
| 2516 | struct delayed_work *dwork, unsigned long delay) |
| 2517 | { |
| 2518 | unsigned long flags; |
| 2519 | int ret; |
| 2520 | |
| 2521 | do { |
| 2522 | ret = try_to_grab_pending(&dwork->work, true, &flags); |
| 2523 | } while (unlikely(ret == -EAGAIN)); |
| 2524 | |
| 2525 | if (likely(ret >= 0)) { |
| 2526 | __queue_delayed_work(cpu, wq, dwork, delay); |
| 2527 | local_irq_restore(flags); |
| 2528 | } |
| 2529 | |
| 2530 | /* -ENOENT from try_to_grab_pending() becomes %true */ |
| 2531 | return ret; |
| 2532 | } |
| 2533 | EXPORT_SYMBOL_GPL(mod_delayed_work_on); |
| 2534 | |
| 2535 | static void rcu_work_rcufn(struct rcu_head *rcu) |
| 2536 | { |
| 2537 | struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); |
| 2538 | |
| 2539 | /* read the comment in __queue_work() */ |
| 2540 | local_irq_disable(); |
| 2541 | __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); |
| 2542 | local_irq_enable(); |
| 2543 | } |
| 2544 | |
| 2545 | /** |
| 2546 | * queue_rcu_work - queue work after a RCU grace period |
| 2547 | * @wq: workqueue to use |
| 2548 | * @rwork: work to queue |
| 2549 | * |
| 2550 | * Return: %false if @rwork was already pending, %true otherwise. Note |
| 2551 | * that a full RCU grace period is guaranteed only after a %true return. |
| 2552 | * While @rwork is guaranteed to be executed after a %false return, the |
| 2553 | * execution may happen before a full RCU grace period has passed. |
| 2554 | */ |
| 2555 | bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) |
| 2556 | { |
| 2557 | struct work_struct *work = &rwork->work; |
| 2558 | |
| 2559 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
| 2560 | rwork->wq = wq; |
| 2561 | call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); |
| 2562 | return true; |
| 2563 | } |
| 2564 | |
| 2565 | return false; |
| 2566 | } |
| 2567 | EXPORT_SYMBOL(queue_rcu_work); |
| 2568 | |
| 2569 | static struct worker *alloc_worker(int node) |
| 2570 | { |
| 2571 | struct worker *worker; |
| 2572 | |
| 2573 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); |
| 2574 | if (worker) { |
| 2575 | INIT_LIST_HEAD(&worker->entry); |
| 2576 | INIT_LIST_HEAD(&worker->scheduled); |
| 2577 | INIT_LIST_HEAD(&worker->node); |
| 2578 | /* on creation a worker is in !idle && prep state */ |
| 2579 | worker->flags = WORKER_PREP; |
| 2580 | } |
| 2581 | return worker; |
| 2582 | } |
| 2583 | |
| 2584 | static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) |
| 2585 | { |
| 2586 | if (pool->cpu < 0 && pool->attrs->affn_strict) |
| 2587 | return pool->attrs->__pod_cpumask; |
| 2588 | else |
| 2589 | return pool->attrs->cpumask; |
| 2590 | } |
| 2591 | |
| 2592 | /** |
| 2593 | * worker_attach_to_pool() - attach a worker to a pool |
| 2594 | * @worker: worker to be attached |
| 2595 | * @pool: the target pool |
| 2596 | * |
| 2597 | * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and |
| 2598 | * cpu-binding of @worker are kept coordinated with the pool across |
| 2599 | * cpu-[un]hotplugs. |
| 2600 | */ |
| 2601 | static void worker_attach_to_pool(struct worker *worker, |
| 2602 | struct worker_pool *pool) |
| 2603 | { |
| 2604 | mutex_lock(&wq_pool_attach_mutex); |
| 2605 | |
| 2606 | /* |
| 2607 | * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable |
| 2608 | * across this function. See the comments above the flag definition for |
| 2609 | * details. BH workers are, while per-CPU, always DISASSOCIATED. |
| 2610 | */ |
| 2611 | if (pool->flags & POOL_DISASSOCIATED) { |
| 2612 | worker->flags |= WORKER_UNBOUND; |
| 2613 | } else { |
| 2614 | WARN_ON_ONCE(pool->flags & POOL_BH); |
| 2615 | kthread_set_per_cpu(worker->task, pool->cpu); |
| 2616 | } |
| 2617 | |
| 2618 | if (worker->rescue_wq) |
| 2619 | set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); |
| 2620 | |
| 2621 | list_add_tail(&worker->node, &pool->workers); |
| 2622 | worker->pool = pool; |
| 2623 | |
| 2624 | mutex_unlock(&wq_pool_attach_mutex); |
| 2625 | } |
| 2626 | |
| 2627 | /** |
| 2628 | * worker_detach_from_pool() - detach a worker from its pool |
| 2629 | * @worker: worker which is attached to its pool |
| 2630 | * |
| 2631 | * Undo the attaching which had been done in worker_attach_to_pool(). The |
| 2632 | * caller worker shouldn't access to the pool after detached except it has |
| 2633 | * other reference to the pool. |
| 2634 | */ |
| 2635 | static void worker_detach_from_pool(struct worker *worker) |
| 2636 | { |
| 2637 | struct worker_pool *pool = worker->pool; |
| 2638 | struct completion *detach_completion = NULL; |
| 2639 | |
| 2640 | /* there is one permanent BH worker per CPU which should never detach */ |
| 2641 | WARN_ON_ONCE(pool->flags & POOL_BH); |
| 2642 | |
| 2643 | mutex_lock(&wq_pool_attach_mutex); |
| 2644 | |
| 2645 | kthread_set_per_cpu(worker->task, -1); |
| 2646 | list_del(&worker->node); |
| 2647 | worker->pool = NULL; |
| 2648 | |
| 2649 | if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) |
| 2650 | detach_completion = pool->detach_completion; |
| 2651 | mutex_unlock(&wq_pool_attach_mutex); |
| 2652 | |
| 2653 | /* clear leftover flags without pool->lock after it is detached */ |
| 2654 | worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); |
| 2655 | |
| 2656 | if (detach_completion) |
| 2657 | complete(detach_completion); |
| 2658 | } |
| 2659 | |
| 2660 | /** |
| 2661 | * create_worker - create a new workqueue worker |
| 2662 | * @pool: pool the new worker will belong to |
| 2663 | * |
| 2664 | * Create and start a new worker which is attached to @pool. |
| 2665 | * |
| 2666 | * CONTEXT: |
| 2667 | * Might sleep. Does GFP_KERNEL allocations. |
| 2668 | * |
| 2669 | * Return: |
| 2670 | * Pointer to the newly created worker. |
| 2671 | */ |
| 2672 | static struct worker *create_worker(struct worker_pool *pool) |
| 2673 | { |
| 2674 | struct worker *worker; |
| 2675 | int id; |
| 2676 | char id_buf[23]; |
| 2677 | |
| 2678 | /* ID is needed to determine kthread name */ |
| 2679 | id = ida_alloc(&pool->worker_ida, GFP_KERNEL); |
| 2680 | if (id < 0) { |
| 2681 | pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", |
| 2682 | ERR_PTR(id)); |
| 2683 | return NULL; |
| 2684 | } |
| 2685 | |
| 2686 | worker = alloc_worker(pool->node); |
| 2687 | if (!worker) { |
| 2688 | pr_err_once("workqueue: Failed to allocate a worker\n"); |
| 2689 | goto fail; |
| 2690 | } |
| 2691 | |
| 2692 | worker->id = id; |
| 2693 | |
| 2694 | if (!(pool->flags & POOL_BH)) { |
| 2695 | if (pool->cpu >= 0) |
| 2696 | snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, |
| 2697 | pool->attrs->nice < 0 ? "H" : ""); |
| 2698 | else |
| 2699 | snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); |
| 2700 | |
| 2701 | worker->task = kthread_create_on_node(worker_thread, worker, |
| 2702 | pool->node, "kworker/%s", id_buf); |
| 2703 | if (IS_ERR(worker->task)) { |
| 2704 | if (PTR_ERR(worker->task) == -EINTR) { |
| 2705 | pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", |
| 2706 | id_buf); |
| 2707 | } else { |
| 2708 | pr_err_once("workqueue: Failed to create a worker thread: %pe", |
| 2709 | worker->task); |
| 2710 | } |
| 2711 | goto fail; |
| 2712 | } |
| 2713 | |
| 2714 | set_user_nice(worker->task, pool->attrs->nice); |
| 2715 | kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); |
| 2716 | } |
| 2717 | |
| 2718 | /* successful, attach the worker to the pool */ |
| 2719 | worker_attach_to_pool(worker, pool); |
| 2720 | |
| 2721 | /* start the newly created worker */ |
| 2722 | raw_spin_lock_irq(&pool->lock); |
| 2723 | |
| 2724 | worker->pool->nr_workers++; |
| 2725 | worker_enter_idle(worker); |
| 2726 | |
| 2727 | /* |
| 2728 | * @worker is waiting on a completion in kthread() and will trigger hung |
| 2729 | * check if not woken up soon. As kick_pool() is noop if @pool is empty, |
| 2730 | * wake it up explicitly. |
| 2731 | */ |
| 2732 | if (worker->task) |
| 2733 | wake_up_process(worker->task); |
| 2734 | |
| 2735 | raw_spin_unlock_irq(&pool->lock); |
| 2736 | |
| 2737 | return worker; |
| 2738 | |
| 2739 | fail: |
| 2740 | ida_free(&pool->worker_ida, id); |
| 2741 | kfree(worker); |
| 2742 | return NULL; |
| 2743 | } |
| 2744 | |
| 2745 | static void unbind_worker(struct worker *worker) |
| 2746 | { |
| 2747 | lockdep_assert_held(&wq_pool_attach_mutex); |
| 2748 | |
| 2749 | kthread_set_per_cpu(worker->task, -1); |
| 2750 | if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) |
| 2751 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); |
| 2752 | else |
| 2753 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); |
| 2754 | } |
| 2755 | |
| 2756 | static void wake_dying_workers(struct list_head *cull_list) |
| 2757 | { |
| 2758 | struct worker *worker, *tmp; |
| 2759 | |
| 2760 | list_for_each_entry_safe(worker, tmp, cull_list, entry) { |
| 2761 | list_del_init(&worker->entry); |
| 2762 | unbind_worker(worker); |
| 2763 | /* |
| 2764 | * If the worker was somehow already running, then it had to be |
| 2765 | * in pool->idle_list when set_worker_dying() happened or we |
| 2766 | * wouldn't have gotten here. |
| 2767 | * |
| 2768 | * Thus, the worker must either have observed the WORKER_DIE |
| 2769 | * flag, or have set its state to TASK_IDLE. Either way, the |
| 2770 | * below will be observed by the worker and is safe to do |
| 2771 | * outside of pool->lock. |
| 2772 | */ |
| 2773 | wake_up_process(worker->task); |
| 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | /** |
| 2778 | * set_worker_dying - Tag a worker for destruction |
| 2779 | * @worker: worker to be destroyed |
| 2780 | * @list: transfer worker away from its pool->idle_list and into list |
| 2781 | * |
| 2782 | * Tag @worker for destruction and adjust @pool stats accordingly. The worker |
| 2783 | * should be idle. |
| 2784 | * |
| 2785 | * CONTEXT: |
| 2786 | * raw_spin_lock_irq(pool->lock). |
| 2787 | */ |
| 2788 | static void set_worker_dying(struct worker *worker, struct list_head *list) |
| 2789 | { |
| 2790 | struct worker_pool *pool = worker->pool; |
| 2791 | |
| 2792 | lockdep_assert_held(&pool->lock); |
| 2793 | lockdep_assert_held(&wq_pool_attach_mutex); |
| 2794 | |
| 2795 | /* sanity check frenzy */ |
| 2796 | if (WARN_ON(worker->current_work) || |
| 2797 | WARN_ON(!list_empty(&worker->scheduled)) || |
| 2798 | WARN_ON(!(worker->flags & WORKER_IDLE))) |
| 2799 | return; |
| 2800 | |
| 2801 | pool->nr_workers--; |
| 2802 | pool->nr_idle--; |
| 2803 | |
| 2804 | worker->flags |= WORKER_DIE; |
| 2805 | |
| 2806 | list_move(&worker->entry, list); |
| 2807 | list_move(&worker->node, &pool->dying_workers); |
| 2808 | } |
| 2809 | |
| 2810 | /** |
| 2811 | * idle_worker_timeout - check if some idle workers can now be deleted. |
| 2812 | * @t: The pool's idle_timer that just expired |
| 2813 | * |
| 2814 | * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in |
| 2815 | * worker_leave_idle(), as a worker flicking between idle and active while its |
| 2816 | * pool is at the too_many_workers() tipping point would cause too much timer |
| 2817 | * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let |
| 2818 | * it expire and re-evaluate things from there. |
| 2819 | */ |
| 2820 | static void idle_worker_timeout(struct timer_list *t) |
| 2821 | { |
| 2822 | struct worker_pool *pool = from_timer(pool, t, idle_timer); |
| 2823 | bool do_cull = false; |
| 2824 | |
| 2825 | if (work_pending(&pool->idle_cull_work)) |
| 2826 | return; |
| 2827 | |
| 2828 | raw_spin_lock_irq(&pool->lock); |
| 2829 | |
| 2830 | if (too_many_workers(pool)) { |
| 2831 | struct worker *worker; |
| 2832 | unsigned long expires; |
| 2833 | |
| 2834 | /* idle_list is kept in LIFO order, check the last one */ |
| 2835 | worker = list_entry(pool->idle_list.prev, struct worker, entry); |
| 2836 | expires = worker->last_active + IDLE_WORKER_TIMEOUT; |
| 2837 | do_cull = !time_before(jiffies, expires); |
| 2838 | |
| 2839 | if (!do_cull) |
| 2840 | mod_timer(&pool->idle_timer, expires); |
| 2841 | } |
| 2842 | raw_spin_unlock_irq(&pool->lock); |
| 2843 | |
| 2844 | if (do_cull) |
| 2845 | queue_work(system_unbound_wq, &pool->idle_cull_work); |
| 2846 | } |
| 2847 | |
| 2848 | /** |
| 2849 | * idle_cull_fn - cull workers that have been idle for too long. |
| 2850 | * @work: the pool's work for handling these idle workers |
| 2851 | * |
| 2852 | * This goes through a pool's idle workers and gets rid of those that have been |
| 2853 | * idle for at least IDLE_WORKER_TIMEOUT seconds. |
| 2854 | * |
| 2855 | * We don't want to disturb isolated CPUs because of a pcpu kworker being |
| 2856 | * culled, so this also resets worker affinity. This requires a sleepable |
| 2857 | * context, hence the split between timer callback and work item. |
| 2858 | */ |
| 2859 | static void idle_cull_fn(struct work_struct *work) |
| 2860 | { |
| 2861 | struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); |
| 2862 | LIST_HEAD(cull_list); |
| 2863 | |
| 2864 | /* |
| 2865 | * Grabbing wq_pool_attach_mutex here ensures an already-running worker |
| 2866 | * cannot proceed beyong worker_detach_from_pool() in its self-destruct |
| 2867 | * path. This is required as a previously-preempted worker could run after |
| 2868 | * set_worker_dying() has happened but before wake_dying_workers() did. |
| 2869 | */ |
| 2870 | mutex_lock(&wq_pool_attach_mutex); |
| 2871 | raw_spin_lock_irq(&pool->lock); |
| 2872 | |
| 2873 | while (too_many_workers(pool)) { |
| 2874 | struct worker *worker; |
| 2875 | unsigned long expires; |
| 2876 | |
| 2877 | worker = list_entry(pool->idle_list.prev, struct worker, entry); |
| 2878 | expires = worker->last_active + IDLE_WORKER_TIMEOUT; |
| 2879 | |
| 2880 | if (time_before(jiffies, expires)) { |
| 2881 | mod_timer(&pool->idle_timer, expires); |
| 2882 | break; |
| 2883 | } |
| 2884 | |
| 2885 | set_worker_dying(worker, &cull_list); |
| 2886 | } |
| 2887 | |
| 2888 | raw_spin_unlock_irq(&pool->lock); |
| 2889 | wake_dying_workers(&cull_list); |
| 2890 | mutex_unlock(&wq_pool_attach_mutex); |
| 2891 | } |
| 2892 | |
| 2893 | static void send_mayday(struct work_struct *work) |
| 2894 | { |
| 2895 | struct pool_workqueue *pwq = get_work_pwq(work); |
| 2896 | struct workqueue_struct *wq = pwq->wq; |
| 2897 | |
| 2898 | lockdep_assert_held(&wq_mayday_lock); |
| 2899 | |
| 2900 | if (!wq->rescuer) |
| 2901 | return; |
| 2902 | |
| 2903 | /* mayday mayday mayday */ |
| 2904 | if (list_empty(&pwq->mayday_node)) { |
| 2905 | /* |
| 2906 | * If @pwq is for an unbound wq, its base ref may be put at |
| 2907 | * any time due to an attribute change. Pin @pwq until the |
| 2908 | * rescuer is done with it. |
| 2909 | */ |
| 2910 | get_pwq(pwq); |
| 2911 | list_add_tail(&pwq->mayday_node, &wq->maydays); |
| 2912 | wake_up_process(wq->rescuer->task); |
| 2913 | pwq->stats[PWQ_STAT_MAYDAY]++; |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | static void pool_mayday_timeout(struct timer_list *t) |
| 2918 | { |
| 2919 | struct worker_pool *pool = from_timer(pool, t, mayday_timer); |
| 2920 | struct work_struct *work; |
| 2921 | |
| 2922 | raw_spin_lock_irq(&pool->lock); |
| 2923 | raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ |
| 2924 | |
| 2925 | if (need_to_create_worker(pool)) { |
| 2926 | /* |
| 2927 | * We've been trying to create a new worker but |
| 2928 | * haven't been successful. We might be hitting an |
| 2929 | * allocation deadlock. Send distress signals to |
| 2930 | * rescuers. |
| 2931 | */ |
| 2932 | list_for_each_entry(work, &pool->worklist, entry) |
| 2933 | send_mayday(work); |
| 2934 | } |
| 2935 | |
| 2936 | raw_spin_unlock(&wq_mayday_lock); |
| 2937 | raw_spin_unlock_irq(&pool->lock); |
| 2938 | |
| 2939 | mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); |
| 2940 | } |
| 2941 | |
| 2942 | /** |
| 2943 | * maybe_create_worker - create a new worker if necessary |
| 2944 | * @pool: pool to create a new worker for |
| 2945 | * |
| 2946 | * Create a new worker for @pool if necessary. @pool is guaranteed to |
| 2947 | * have at least one idle worker on return from this function. If |
| 2948 | * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is |
| 2949 | * sent to all rescuers with works scheduled on @pool to resolve |
| 2950 | * possible allocation deadlock. |
| 2951 | * |
| 2952 | * On return, need_to_create_worker() is guaranteed to be %false and |
| 2953 | * may_start_working() %true. |
| 2954 | * |
| 2955 | * LOCKING: |
| 2956 | * raw_spin_lock_irq(pool->lock) which may be released and regrabbed |
| 2957 | * multiple times. Does GFP_KERNEL allocations. Called only from |
| 2958 | * manager. |
| 2959 | */ |
| 2960 | static void maybe_create_worker(struct worker_pool *pool) |
| 2961 | __releases(&pool->lock) |
| 2962 | __acquires(&pool->lock) |
| 2963 | { |
| 2964 | restart: |
| 2965 | raw_spin_unlock_irq(&pool->lock); |
| 2966 | |
| 2967 | /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ |
| 2968 | mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); |
| 2969 | |
| 2970 | while (true) { |
| 2971 | if (create_worker(pool) || !need_to_create_worker(pool)) |
| 2972 | break; |
| 2973 | |
| 2974 | schedule_timeout_interruptible(CREATE_COOLDOWN); |
| 2975 | |
| 2976 | if (!need_to_create_worker(pool)) |
| 2977 | break; |
| 2978 | } |
| 2979 | |
| 2980 | del_timer_sync(&pool->mayday_timer); |
| 2981 | raw_spin_lock_irq(&pool->lock); |
| 2982 | /* |
| 2983 | * This is necessary even after a new worker was just successfully |
| 2984 | * created as @pool->lock was dropped and the new worker might have |
| 2985 | * already become busy. |
| 2986 | */ |
| 2987 | if (need_to_create_worker(pool)) |
| 2988 | goto restart; |
| 2989 | } |
| 2990 | |
| 2991 | /** |
| 2992 | * manage_workers - manage worker pool |
| 2993 | * @worker: self |
| 2994 | * |
| 2995 | * Assume the manager role and manage the worker pool @worker belongs |
| 2996 | * to. At any given time, there can be only zero or one manager per |
| 2997 | * pool. The exclusion is handled automatically by this function. |
| 2998 | * |
| 2999 | * The caller can safely start processing works on false return. On |
| 3000 | * true return, it's guaranteed that need_to_create_worker() is false |
| 3001 | * and may_start_working() is true. |
| 3002 | * |
| 3003 | * CONTEXT: |
| 3004 | * raw_spin_lock_irq(pool->lock) which may be released and regrabbed |
| 3005 | * multiple times. Does GFP_KERNEL allocations. |
| 3006 | * |
| 3007 | * Return: |
| 3008 | * %false if the pool doesn't need management and the caller can safely |
| 3009 | * start processing works, %true if management function was performed and |
| 3010 | * the conditions that the caller verified before calling the function may |
| 3011 | * no longer be true. |
| 3012 | */ |
| 3013 | static bool manage_workers(struct worker *worker) |
| 3014 | { |
| 3015 | struct worker_pool *pool = worker->pool; |
| 3016 | |
| 3017 | if (pool->flags & POOL_MANAGER_ACTIVE) |
| 3018 | return false; |
| 3019 | |
| 3020 | pool->flags |= POOL_MANAGER_ACTIVE; |
| 3021 | pool->manager = worker; |
| 3022 | |
| 3023 | maybe_create_worker(pool); |
| 3024 | |
| 3025 | pool->manager = NULL; |
| 3026 | pool->flags &= ~POOL_MANAGER_ACTIVE; |
| 3027 | rcuwait_wake_up(&manager_wait); |
| 3028 | return true; |
| 3029 | } |
| 3030 | |
| 3031 | /** |
| 3032 | * process_one_work - process single work |
| 3033 | * @worker: self |
| 3034 | * @work: work to process |
| 3035 | * |
| 3036 | * Process @work. This function contains all the logics necessary to |
| 3037 | * process a single work including synchronization against and |
| 3038 | * interaction with other workers on the same cpu, queueing and |
| 3039 | * flushing. As long as context requirement is met, any worker can |
| 3040 | * call this function to process a work. |
| 3041 | * |
| 3042 | * CONTEXT: |
| 3043 | * raw_spin_lock_irq(pool->lock) which is released and regrabbed. |
| 3044 | */ |
| 3045 | static void process_one_work(struct worker *worker, struct work_struct *work) |
| 3046 | __releases(&pool->lock) |
| 3047 | __acquires(&pool->lock) |
| 3048 | { |
| 3049 | struct pool_workqueue *pwq = get_work_pwq(work); |
| 3050 | struct worker_pool *pool = worker->pool; |
| 3051 | unsigned long work_data; |
| 3052 | int lockdep_start_depth, rcu_start_depth; |
| 3053 | #ifdef CONFIG_LOCKDEP |
| 3054 | /* |
| 3055 | * It is permissible to free the struct work_struct from |
| 3056 | * inside the function that is called from it, this we need to |
| 3057 | * take into account for lockdep too. To avoid bogus "held |
| 3058 | * lock freed" warnings as well as problems when looking into |
| 3059 | * work->lockdep_map, make a copy and use that here. |
| 3060 | */ |
| 3061 | struct lockdep_map lockdep_map; |
| 3062 | |
| 3063 | lockdep_copy_map(&lockdep_map, &work->lockdep_map); |
| 3064 | #endif |
| 3065 | /* ensure we're on the correct CPU */ |
| 3066 | WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && |
| 3067 | raw_smp_processor_id() != pool->cpu); |
| 3068 | |
| 3069 | /* claim and dequeue */ |
| 3070 | debug_work_deactivate(work); |
| 3071 | hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); |
| 3072 | worker->current_work = work; |
| 3073 | worker->current_func = work->func; |
| 3074 | worker->current_pwq = pwq; |
| 3075 | if (worker->task) |
| 3076 | worker->current_at = worker->task->se.sum_exec_runtime; |
| 3077 | work_data = *work_data_bits(work); |
| 3078 | worker->current_color = get_work_color(work_data); |
| 3079 | |
| 3080 | /* |
| 3081 | * Record wq name for cmdline and debug reporting, may get |
| 3082 | * overridden through set_worker_desc(). |
| 3083 | */ |
| 3084 | strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); |
| 3085 | |
| 3086 | list_del_init(&work->entry); |
| 3087 | |
| 3088 | /* |
| 3089 | * CPU intensive works don't participate in concurrency management. |
| 3090 | * They're the scheduler's responsibility. This takes @worker out |
| 3091 | * of concurrency management and the next code block will chain |
| 3092 | * execution of the pending work items. |
| 3093 | */ |
| 3094 | if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) |
| 3095 | worker_set_flags(worker, WORKER_CPU_INTENSIVE); |
| 3096 | |
| 3097 | /* |
| 3098 | * Kick @pool if necessary. It's always noop for per-cpu worker pools |
| 3099 | * since nr_running would always be >= 1 at this point. This is used to |
| 3100 | * chain execution of the pending work items for WORKER_NOT_RUNNING |
| 3101 | * workers such as the UNBOUND and CPU_INTENSIVE ones. |
| 3102 | */ |
| 3103 | kick_pool(pool); |
| 3104 | |
| 3105 | /* |
| 3106 | * Record the last pool and clear PENDING which should be the last |
| 3107 | * update to @work. Also, do this inside @pool->lock so that |
| 3108 | * PENDING and queued state changes happen together while IRQ is |
| 3109 | * disabled. |
| 3110 | */ |
| 3111 | set_work_pool_and_clear_pending(work, pool->id); |
| 3112 | |
| 3113 | pwq->stats[PWQ_STAT_STARTED]++; |
| 3114 | raw_spin_unlock_irq(&pool->lock); |
| 3115 | |
| 3116 | rcu_start_depth = rcu_preempt_depth(); |
| 3117 | lockdep_start_depth = lockdep_depth(current); |
| 3118 | lock_map_acquire(&pwq->wq->lockdep_map); |
| 3119 | lock_map_acquire(&lockdep_map); |
| 3120 | /* |
| 3121 | * Strictly speaking we should mark the invariant state without holding |
| 3122 | * any locks, that is, before these two lock_map_acquire()'s. |
| 3123 | * |
| 3124 | * However, that would result in: |
| 3125 | * |
| 3126 | * A(W1) |
| 3127 | * WFC(C) |
| 3128 | * A(W1) |
| 3129 | * C(C) |
| 3130 | * |
| 3131 | * Which would create W1->C->W1 dependencies, even though there is no |
| 3132 | * actual deadlock possible. There are two solutions, using a |
| 3133 | * read-recursive acquire on the work(queue) 'locks', but this will then |
| 3134 | * hit the lockdep limitation on recursive locks, or simply discard |
| 3135 | * these locks. |
| 3136 | * |
| 3137 | * AFAICT there is no possible deadlock scenario between the |
| 3138 | * flush_work() and complete() primitives (except for single-threaded |
| 3139 | * workqueues), so hiding them isn't a problem. |
| 3140 | */ |
| 3141 | lockdep_invariant_state(true); |
| 3142 | trace_workqueue_execute_start(work); |
| 3143 | worker->current_func(work); |
| 3144 | /* |
| 3145 | * While we must be careful to not use "work" after this, the trace |
| 3146 | * point will only record its address. |
| 3147 | */ |
| 3148 | trace_workqueue_execute_end(work, worker->current_func); |
| 3149 | pwq->stats[PWQ_STAT_COMPLETED]++; |
| 3150 | lock_map_release(&lockdep_map); |
| 3151 | lock_map_release(&pwq->wq->lockdep_map); |
| 3152 | |
| 3153 | if (unlikely((worker->task && in_atomic()) || |
| 3154 | lockdep_depth(current) != lockdep_start_depth || |
| 3155 | rcu_preempt_depth() != rcu_start_depth)) { |
| 3156 | pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" |
| 3157 | " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", |
| 3158 | current->comm, task_pid_nr(current), preempt_count(), |
| 3159 | lockdep_start_depth, lockdep_depth(current), |
| 3160 | rcu_start_depth, rcu_preempt_depth(), |
| 3161 | worker->current_func); |
| 3162 | debug_show_held_locks(current); |
| 3163 | dump_stack(); |
| 3164 | } |
| 3165 | |
| 3166 | /* |
| 3167 | * The following prevents a kworker from hogging CPU on !PREEMPTION |
| 3168 | * kernels, where a requeueing work item waiting for something to |
| 3169 | * happen could deadlock with stop_machine as such work item could |
| 3170 | * indefinitely requeue itself while all other CPUs are trapped in |
| 3171 | * stop_machine. At the same time, report a quiescent RCU state so |
| 3172 | * the same condition doesn't freeze RCU. |
| 3173 | */ |
| 3174 | if (worker->task) |
| 3175 | cond_resched(); |
| 3176 | |
| 3177 | raw_spin_lock_irq(&pool->lock); |
| 3178 | |
| 3179 | /* |
| 3180 | * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked |
| 3181 | * CPU intensive by wq_worker_tick() if @work hogged CPU longer than |
| 3182 | * wq_cpu_intensive_thresh_us. Clear it. |
| 3183 | */ |
| 3184 | worker_clr_flags(worker, WORKER_CPU_INTENSIVE); |
| 3185 | |
| 3186 | /* tag the worker for identification in schedule() */ |
| 3187 | worker->last_func = worker->current_func; |
| 3188 | |
| 3189 | /* we're done with it, release */ |
| 3190 | hash_del(&worker->hentry); |
| 3191 | worker->current_work = NULL; |
| 3192 | worker->current_func = NULL; |
| 3193 | worker->current_pwq = NULL; |
| 3194 | worker->current_color = INT_MAX; |
| 3195 | |
| 3196 | /* must be the last step, see the function comment */ |
| 3197 | pwq_dec_nr_in_flight(pwq, work_data); |
| 3198 | } |
| 3199 | |
| 3200 | /** |
| 3201 | * process_scheduled_works - process scheduled works |
| 3202 | * @worker: self |
| 3203 | * |
| 3204 | * Process all scheduled works. Please note that the scheduled list |
| 3205 | * may change while processing a work, so this function repeatedly |
| 3206 | * fetches a work from the top and executes it. |
| 3207 | * |
| 3208 | * CONTEXT: |
| 3209 | * raw_spin_lock_irq(pool->lock) which may be released and regrabbed |
| 3210 | * multiple times. |
| 3211 | */ |
| 3212 | static void process_scheduled_works(struct worker *worker) |
| 3213 | { |
| 3214 | struct work_struct *work; |
| 3215 | bool first = true; |
| 3216 | |
| 3217 | while ((work = list_first_entry_or_null(&worker->scheduled, |
| 3218 | struct work_struct, entry))) { |
| 3219 | if (first) { |
| 3220 | worker->pool->watchdog_ts = jiffies; |
| 3221 | first = false; |
| 3222 | } |
| 3223 | process_one_work(worker, work); |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | static void set_pf_worker(bool val) |
| 3228 | { |
| 3229 | mutex_lock(&wq_pool_attach_mutex); |
| 3230 | if (val) |
| 3231 | current->flags |= PF_WQ_WORKER; |
| 3232 | else |
| 3233 | current->flags &= ~PF_WQ_WORKER; |
| 3234 | mutex_unlock(&wq_pool_attach_mutex); |
| 3235 | } |
| 3236 | |
| 3237 | /** |
| 3238 | * worker_thread - the worker thread function |
| 3239 | * @__worker: self |
| 3240 | * |
| 3241 | * The worker thread function. All workers belong to a worker_pool - |
| 3242 | * either a per-cpu one or dynamic unbound one. These workers process all |
| 3243 | * work items regardless of their specific target workqueue. The only |
| 3244 | * exception is work items which belong to workqueues with a rescuer which |
| 3245 | * will be explained in rescuer_thread(). |
| 3246 | * |
| 3247 | * Return: 0 |
| 3248 | */ |
| 3249 | static int worker_thread(void *__worker) |
| 3250 | { |
| 3251 | struct worker *worker = __worker; |
| 3252 | struct worker_pool *pool = worker->pool; |
| 3253 | |
| 3254 | /* tell the scheduler that this is a workqueue worker */ |
| 3255 | set_pf_worker(true); |
| 3256 | woke_up: |
| 3257 | raw_spin_lock_irq(&pool->lock); |
| 3258 | |
| 3259 | /* am I supposed to die? */ |
| 3260 | if (unlikely(worker->flags & WORKER_DIE)) { |
| 3261 | raw_spin_unlock_irq(&pool->lock); |
| 3262 | set_pf_worker(false); |
| 3263 | |
| 3264 | set_task_comm(worker->task, "kworker/dying"); |
| 3265 | ida_free(&pool->worker_ida, worker->id); |
| 3266 | worker_detach_from_pool(worker); |
| 3267 | WARN_ON_ONCE(!list_empty(&worker->entry)); |
| 3268 | kfree(worker); |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | worker_leave_idle(worker); |
| 3273 | recheck: |
| 3274 | /* no more worker necessary? */ |
| 3275 | if (!need_more_worker(pool)) |
| 3276 | goto sleep; |
| 3277 | |
| 3278 | /* do we need to manage? */ |
| 3279 | if (unlikely(!may_start_working(pool)) && manage_workers(worker)) |
| 3280 | goto recheck; |
| 3281 | |
| 3282 | /* |
| 3283 | * ->scheduled list can only be filled while a worker is |
| 3284 | * preparing to process a work or actually processing it. |
| 3285 | * Make sure nobody diddled with it while I was sleeping. |
| 3286 | */ |
| 3287 | WARN_ON_ONCE(!list_empty(&worker->scheduled)); |
| 3288 | |
| 3289 | /* |
| 3290 | * Finish PREP stage. We're guaranteed to have at least one idle |
| 3291 | * worker or that someone else has already assumed the manager |
| 3292 | * role. This is where @worker starts participating in concurrency |
| 3293 | * management if applicable and concurrency management is restored |
| 3294 | * after being rebound. See rebind_workers() for details. |
| 3295 | */ |
| 3296 | worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); |
| 3297 | |
| 3298 | do { |
| 3299 | struct work_struct *work = |
| 3300 | list_first_entry(&pool->worklist, |
| 3301 | struct work_struct, entry); |
| 3302 | |
| 3303 | if (assign_work(work, worker, NULL)) |
| 3304 | process_scheduled_works(worker); |
| 3305 | } while (keep_working(pool)); |
| 3306 | |
| 3307 | worker_set_flags(worker, WORKER_PREP); |
| 3308 | sleep: |
| 3309 | /* |
| 3310 | * pool->lock is held and there's no work to process and no need to |
| 3311 | * manage, sleep. Workers are woken up only while holding |
| 3312 | * pool->lock or from local cpu, so setting the current state |
| 3313 | * before releasing pool->lock is enough to prevent losing any |
| 3314 | * event. |
| 3315 | */ |
| 3316 | worker_enter_idle(worker); |
| 3317 | __set_current_state(TASK_IDLE); |
| 3318 | raw_spin_unlock_irq(&pool->lock); |
| 3319 | schedule(); |
| 3320 | goto woke_up; |
| 3321 | } |
| 3322 | |
| 3323 | /** |
| 3324 | * rescuer_thread - the rescuer thread function |
| 3325 | * @__rescuer: self |
| 3326 | * |
| 3327 | * Workqueue rescuer thread function. There's one rescuer for each |
| 3328 | * workqueue which has WQ_MEM_RECLAIM set. |
| 3329 | * |
| 3330 | * Regular work processing on a pool may block trying to create a new |
| 3331 | * worker which uses GFP_KERNEL allocation which has slight chance of |
| 3332 | * developing into deadlock if some works currently on the same queue |
| 3333 | * need to be processed to satisfy the GFP_KERNEL allocation. This is |
| 3334 | * the problem rescuer solves. |
| 3335 | * |
| 3336 | * When such condition is possible, the pool summons rescuers of all |
| 3337 | * workqueues which have works queued on the pool and let them process |
| 3338 | * those works so that forward progress can be guaranteed. |
| 3339 | * |
| 3340 | * This should happen rarely. |
| 3341 | * |
| 3342 | * Return: 0 |
| 3343 | */ |
| 3344 | static int rescuer_thread(void *__rescuer) |
| 3345 | { |
| 3346 | struct worker *rescuer = __rescuer; |
| 3347 | struct workqueue_struct *wq = rescuer->rescue_wq; |
| 3348 | bool should_stop; |
| 3349 | |
| 3350 | set_user_nice(current, RESCUER_NICE_LEVEL); |
| 3351 | |
| 3352 | /* |
| 3353 | * Mark rescuer as worker too. As WORKER_PREP is never cleared, it |
| 3354 | * doesn't participate in concurrency management. |
| 3355 | */ |
| 3356 | set_pf_worker(true); |
| 3357 | repeat: |
| 3358 | set_current_state(TASK_IDLE); |
| 3359 | |
| 3360 | /* |
| 3361 | * By the time the rescuer is requested to stop, the workqueue |
| 3362 | * shouldn't have any work pending, but @wq->maydays may still have |
| 3363 | * pwq(s) queued. This can happen by non-rescuer workers consuming |
| 3364 | * all the work items before the rescuer got to them. Go through |
| 3365 | * @wq->maydays processing before acting on should_stop so that the |
| 3366 | * list is always empty on exit. |
| 3367 | */ |
| 3368 | should_stop = kthread_should_stop(); |
| 3369 | |
| 3370 | /* see whether any pwq is asking for help */ |
| 3371 | raw_spin_lock_irq(&wq_mayday_lock); |
| 3372 | |
| 3373 | while (!list_empty(&wq->maydays)) { |
| 3374 | struct pool_workqueue *pwq = list_first_entry(&wq->maydays, |
| 3375 | struct pool_workqueue, mayday_node); |
| 3376 | struct worker_pool *pool = pwq->pool; |
| 3377 | struct work_struct *work, *n; |
| 3378 | |
| 3379 | __set_current_state(TASK_RUNNING); |
| 3380 | list_del_init(&pwq->mayday_node); |
| 3381 | |
| 3382 | raw_spin_unlock_irq(&wq_mayday_lock); |
| 3383 | |
| 3384 | worker_attach_to_pool(rescuer, pool); |
| 3385 | |
| 3386 | raw_spin_lock_irq(&pool->lock); |
| 3387 | |
| 3388 | /* |
| 3389 | * Slurp in all works issued via this workqueue and |
| 3390 | * process'em. |
| 3391 | */ |
| 3392 | WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); |
| 3393 | list_for_each_entry_safe(work, n, &pool->worklist, entry) { |
| 3394 | if (get_work_pwq(work) == pwq && |
| 3395 | assign_work(work, rescuer, &n)) |
| 3396 | pwq->stats[PWQ_STAT_RESCUED]++; |
| 3397 | } |
| 3398 | |
| 3399 | if (!list_empty(&rescuer->scheduled)) { |
| 3400 | process_scheduled_works(rescuer); |
| 3401 | |
| 3402 | /* |
| 3403 | * The above execution of rescued work items could |
| 3404 | * have created more to rescue through |
| 3405 | * pwq_activate_first_inactive() or chained |
| 3406 | * queueing. Let's put @pwq back on mayday list so |
| 3407 | * that such back-to-back work items, which may be |
| 3408 | * being used to relieve memory pressure, don't |
| 3409 | * incur MAYDAY_INTERVAL delay inbetween. |
| 3410 | */ |
| 3411 | if (pwq->nr_active && need_to_create_worker(pool)) { |
| 3412 | raw_spin_lock(&wq_mayday_lock); |
| 3413 | /* |
| 3414 | * Queue iff we aren't racing destruction |
| 3415 | * and somebody else hasn't queued it already. |
| 3416 | */ |
| 3417 | if (wq->rescuer && list_empty(&pwq->mayday_node)) { |
| 3418 | get_pwq(pwq); |
| 3419 | list_add_tail(&pwq->mayday_node, &wq->maydays); |
| 3420 | } |
| 3421 | raw_spin_unlock(&wq_mayday_lock); |
| 3422 | } |
| 3423 | } |
| 3424 | |
| 3425 | /* |
| 3426 | * Put the reference grabbed by send_mayday(). @pool won't |
| 3427 | * go away while we're still attached to it. |
| 3428 | */ |
| 3429 | put_pwq(pwq); |
| 3430 | |
| 3431 | /* |
| 3432 | * Leave this pool. Notify regular workers; otherwise, we end up |
| 3433 | * with 0 concurrency and stalling the execution. |
| 3434 | */ |
| 3435 | kick_pool(pool); |
| 3436 | |
| 3437 | raw_spin_unlock_irq(&pool->lock); |
| 3438 | |
| 3439 | worker_detach_from_pool(rescuer); |
| 3440 | |
| 3441 | raw_spin_lock_irq(&wq_mayday_lock); |
| 3442 | } |
| 3443 | |
| 3444 | raw_spin_unlock_irq(&wq_mayday_lock); |
| 3445 | |
| 3446 | if (should_stop) { |
| 3447 | __set_current_state(TASK_RUNNING); |
| 3448 | set_pf_worker(false); |
| 3449 | return 0; |
| 3450 | } |
| 3451 | |
| 3452 | /* rescuers should never participate in concurrency management */ |
| 3453 | WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); |
| 3454 | schedule(); |
| 3455 | goto repeat; |
| 3456 | } |
| 3457 | |
| 3458 | static void bh_worker(struct worker *worker) |
| 3459 | { |
| 3460 | struct worker_pool *pool = worker->pool; |
| 3461 | int nr_restarts = BH_WORKER_RESTARTS; |
| 3462 | unsigned long end = jiffies + BH_WORKER_JIFFIES; |
| 3463 | |
| 3464 | raw_spin_lock_irq(&pool->lock); |
| 3465 | worker_leave_idle(worker); |
| 3466 | |
| 3467 | /* |
| 3468 | * This function follows the structure of worker_thread(). See there for |
| 3469 | * explanations on each step. |
| 3470 | */ |
| 3471 | if (!need_more_worker(pool)) |
| 3472 | goto done; |
| 3473 | |
| 3474 | WARN_ON_ONCE(!list_empty(&worker->scheduled)); |
| 3475 | worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); |
| 3476 | |
| 3477 | do { |
| 3478 | struct work_struct *work = |
| 3479 | list_first_entry(&pool->worklist, |
| 3480 | struct work_struct, entry); |
| 3481 | |
| 3482 | if (assign_work(work, worker, NULL)) |
| 3483 | process_scheduled_works(worker); |
| 3484 | } while (keep_working(pool) && |
| 3485 | --nr_restarts && time_before(jiffies, end)); |
| 3486 | |
| 3487 | worker_set_flags(worker, WORKER_PREP); |
| 3488 | done: |
| 3489 | worker_enter_idle(worker); |
| 3490 | kick_pool(pool); |
| 3491 | raw_spin_unlock_irq(&pool->lock); |
| 3492 | } |
| 3493 | |
| 3494 | /* |
| 3495 | * TODO: Convert all tasklet users to workqueue and use softirq directly. |
| 3496 | * |
| 3497 | * This is currently called from tasklet[_hi]action() and thus is also called |
| 3498 | * whenever there are tasklets to run. Let's do an early exit if there's nothing |
| 3499 | * queued. Once conversion from tasklet is complete, the need_more_worker() test |
| 3500 | * can be dropped. |
| 3501 | * |
| 3502 | * After full conversion, we'll add worker->softirq_action, directly use the |
| 3503 | * softirq action and obtain the worker pointer from the softirq_action pointer. |
| 3504 | */ |
| 3505 | void workqueue_softirq_action(bool highpri) |
| 3506 | { |
| 3507 | struct worker_pool *pool = |
| 3508 | &per_cpu(bh_worker_pools, smp_processor_id())[highpri]; |
| 3509 | if (need_more_worker(pool)) |
| 3510 | bh_worker(list_first_entry(&pool->workers, struct worker, node)); |
| 3511 | } |
| 3512 | |
| 3513 | /** |
| 3514 | * check_flush_dependency - check for flush dependency sanity |
| 3515 | * @target_wq: workqueue being flushed |
| 3516 | * @target_work: work item being flushed (NULL for workqueue flushes) |
| 3517 | * |
| 3518 | * %current is trying to flush the whole @target_wq or @target_work on it. |
| 3519 | * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not |
| 3520 | * reclaiming memory or running on a workqueue which doesn't have |
| 3521 | * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to |
| 3522 | * a deadlock. |
| 3523 | */ |
| 3524 | static void check_flush_dependency(struct workqueue_struct *target_wq, |
| 3525 | struct work_struct *target_work) |
| 3526 | { |
| 3527 | work_func_t target_func = target_work ? target_work->func : NULL; |
| 3528 | struct worker *worker; |
| 3529 | |
| 3530 | if (target_wq->flags & WQ_MEM_RECLAIM) |
| 3531 | return; |
| 3532 | |
| 3533 | worker = current_wq_worker(); |
| 3534 | |
| 3535 | WARN_ONCE(current->flags & PF_MEMALLOC, |
| 3536 | "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", |
| 3537 | current->pid, current->comm, target_wq->name, target_func); |
| 3538 | WARN_ONCE(worker && ((worker->current_pwq->wq->flags & |
| 3539 | (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), |
| 3540 | "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", |
| 3541 | worker->current_pwq->wq->name, worker->current_func, |
| 3542 | target_wq->name, target_func); |
| 3543 | } |
| 3544 | |
| 3545 | struct wq_barrier { |
| 3546 | struct work_struct work; |
| 3547 | struct completion done; |
| 3548 | struct task_struct *task; /* purely informational */ |
| 3549 | }; |
| 3550 | |
| 3551 | static void wq_barrier_func(struct work_struct *work) |
| 3552 | { |
| 3553 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); |
| 3554 | complete(&barr->done); |
| 3555 | } |
| 3556 | |
| 3557 | /** |
| 3558 | * insert_wq_barrier - insert a barrier work |
| 3559 | * @pwq: pwq to insert barrier into |
| 3560 | * @barr: wq_barrier to insert |
| 3561 | * @target: target work to attach @barr to |
| 3562 | * @worker: worker currently executing @target, NULL if @target is not executing |
| 3563 | * |
| 3564 | * @barr is linked to @target such that @barr is completed only after |
| 3565 | * @target finishes execution. Please note that the ordering |
| 3566 | * guarantee is observed only with respect to @target and on the local |
| 3567 | * cpu. |
| 3568 | * |
| 3569 | * Currently, a queued barrier can't be canceled. This is because |
| 3570 | * try_to_grab_pending() can't determine whether the work to be |
| 3571 | * grabbed is at the head of the queue and thus can't clear LINKED |
| 3572 | * flag of the previous work while there must be a valid next work |
| 3573 | * after a work with LINKED flag set. |
| 3574 | * |
| 3575 | * Note that when @worker is non-NULL, @target may be modified |
| 3576 | * underneath us, so we can't reliably determine pwq from @target. |
| 3577 | * |
| 3578 | * CONTEXT: |
| 3579 | * raw_spin_lock_irq(pool->lock). |
| 3580 | */ |
| 3581 | static void insert_wq_barrier(struct pool_workqueue *pwq, |
| 3582 | struct wq_barrier *barr, |
| 3583 | struct work_struct *target, struct worker *worker) |
| 3584 | { |
| 3585 | static __maybe_unused struct lock_class_key bh_key, thr_key; |
| 3586 | unsigned int work_flags = 0; |
| 3587 | unsigned int work_color; |
| 3588 | struct list_head *head; |
| 3589 | |
| 3590 | /* |
| 3591 | * debugobject calls are safe here even with pool->lock locked |
| 3592 | * as we know for sure that this will not trigger any of the |
| 3593 | * checks and call back into the fixup functions where we |
| 3594 | * might deadlock. |
| 3595 | * |
| 3596 | * BH and threaded workqueues need separate lockdep keys to avoid |
| 3597 | * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} |
| 3598 | * usage". |
| 3599 | */ |
| 3600 | INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func, |
| 3601 | (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key); |
| 3602 | __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); |
| 3603 | |
| 3604 | init_completion_map(&barr->done, &target->lockdep_map); |
| 3605 | |
| 3606 | barr->task = current; |
| 3607 | |
| 3608 | /* The barrier work item does not participate in nr_active. */ |
| 3609 | work_flags |= WORK_STRUCT_INACTIVE; |
| 3610 | |
| 3611 | /* |
| 3612 | * If @target is currently being executed, schedule the |
| 3613 | * barrier to the worker; otherwise, put it after @target. |
| 3614 | */ |
| 3615 | if (worker) { |
| 3616 | head = worker->scheduled.next; |
| 3617 | work_color = worker->current_color; |
| 3618 | } else { |
| 3619 | unsigned long *bits = work_data_bits(target); |
| 3620 | |
| 3621 | head = target->entry.next; |
| 3622 | /* there can already be other linked works, inherit and set */ |
| 3623 | work_flags |= *bits & WORK_STRUCT_LINKED; |
| 3624 | work_color = get_work_color(*bits); |
| 3625 | __set_bit(WORK_STRUCT_LINKED_BIT, bits); |
| 3626 | } |
| 3627 | |
| 3628 | pwq->nr_in_flight[work_color]++; |
| 3629 | work_flags |= work_color_to_flags(work_color); |
| 3630 | |
| 3631 | insert_work(pwq, &barr->work, head, work_flags); |
| 3632 | } |
| 3633 | |
| 3634 | /** |
| 3635 | * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing |
| 3636 | * @wq: workqueue being flushed |
| 3637 | * @flush_color: new flush color, < 0 for no-op |
| 3638 | * @work_color: new work color, < 0 for no-op |
| 3639 | * |
| 3640 | * Prepare pwqs for workqueue flushing. |
| 3641 | * |
| 3642 | * If @flush_color is non-negative, flush_color on all pwqs should be |
| 3643 | * -1. If no pwq has in-flight commands at the specified color, all |
| 3644 | * pwq->flush_color's stay at -1 and %false is returned. If any pwq |
| 3645 | * has in flight commands, its pwq->flush_color is set to |
| 3646 | * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq |
| 3647 | * wakeup logic is armed and %true is returned. |
| 3648 | * |
| 3649 | * The caller should have initialized @wq->first_flusher prior to |
| 3650 | * calling this function with non-negative @flush_color. If |
| 3651 | * @flush_color is negative, no flush color update is done and %false |
| 3652 | * is returned. |
| 3653 | * |
| 3654 | * If @work_color is non-negative, all pwqs should have the same |
| 3655 | * work_color which is previous to @work_color and all will be |
| 3656 | * advanced to @work_color. |
| 3657 | * |
| 3658 | * CONTEXT: |
| 3659 | * mutex_lock(wq->mutex). |
| 3660 | * |
| 3661 | * Return: |
| 3662 | * %true if @flush_color >= 0 and there's something to flush. %false |
| 3663 | * otherwise. |
| 3664 | */ |
| 3665 | static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, |
| 3666 | int flush_color, int work_color) |
| 3667 | { |
| 3668 | bool wait = false; |
| 3669 | struct pool_workqueue *pwq; |
| 3670 | |
| 3671 | if (flush_color >= 0) { |
| 3672 | WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); |
| 3673 | atomic_set(&wq->nr_pwqs_to_flush, 1); |
| 3674 | } |
| 3675 | |
| 3676 | for_each_pwq(pwq, wq) { |
| 3677 | struct worker_pool *pool = pwq->pool; |
| 3678 | |
| 3679 | raw_spin_lock_irq(&pool->lock); |
| 3680 | |
| 3681 | if (flush_color >= 0) { |
| 3682 | WARN_ON_ONCE(pwq->flush_color != -1); |
| 3683 | |
| 3684 | if (pwq->nr_in_flight[flush_color]) { |
| 3685 | pwq->flush_color = flush_color; |
| 3686 | atomic_inc(&wq->nr_pwqs_to_flush); |
| 3687 | wait = true; |
| 3688 | } |
| 3689 | } |
| 3690 | |
| 3691 | if (work_color >= 0) { |
| 3692 | WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); |
| 3693 | pwq->work_color = work_color; |
| 3694 | } |
| 3695 | |
| 3696 | raw_spin_unlock_irq(&pool->lock); |
| 3697 | } |
| 3698 | |
| 3699 | if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) |
| 3700 | complete(&wq->first_flusher->done); |
| 3701 | |
| 3702 | return wait; |
| 3703 | } |
| 3704 | |
| 3705 | static void touch_wq_lockdep_map(struct workqueue_struct *wq) |
| 3706 | { |
| 3707 | #ifdef CONFIG_LOCKDEP |
| 3708 | if (wq->flags & WQ_BH) |
| 3709 | local_bh_disable(); |
| 3710 | |
| 3711 | lock_map_acquire(&wq->lockdep_map); |
| 3712 | lock_map_release(&wq->lockdep_map); |
| 3713 | |
| 3714 | if (wq->flags & WQ_BH) |
| 3715 | local_bh_enable(); |
| 3716 | #endif |
| 3717 | } |
| 3718 | |
| 3719 | static void touch_work_lockdep_map(struct work_struct *work, |
| 3720 | struct workqueue_struct *wq) |
| 3721 | { |
| 3722 | #ifdef CONFIG_LOCKDEP |
| 3723 | if (wq->flags & WQ_BH) |
| 3724 | local_bh_disable(); |
| 3725 | |
| 3726 | lock_map_acquire(&work->lockdep_map); |
| 3727 | lock_map_release(&work->lockdep_map); |
| 3728 | |
| 3729 | if (wq->flags & WQ_BH) |
| 3730 | local_bh_enable(); |
| 3731 | #endif |
| 3732 | } |
| 3733 | |
| 3734 | /** |
| 3735 | * __flush_workqueue - ensure that any scheduled work has run to completion. |
| 3736 | * @wq: workqueue to flush |
| 3737 | * |
| 3738 | * This function sleeps until all work items which were queued on entry |
| 3739 | * have finished execution, but it is not livelocked by new incoming ones. |
| 3740 | */ |
| 3741 | void __flush_workqueue(struct workqueue_struct *wq) |
| 3742 | { |
| 3743 | struct wq_flusher this_flusher = { |
| 3744 | .list = LIST_HEAD_INIT(this_flusher.list), |
| 3745 | .flush_color = -1, |
| 3746 | .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), |
| 3747 | }; |
| 3748 | int next_color; |
| 3749 | |
| 3750 | if (WARN_ON(!wq_online)) |
| 3751 | return; |
| 3752 | |
| 3753 | touch_wq_lockdep_map(wq); |
| 3754 | |
| 3755 | mutex_lock(&wq->mutex); |
| 3756 | |
| 3757 | /* |
| 3758 | * Start-to-wait phase |
| 3759 | */ |
| 3760 | next_color = work_next_color(wq->work_color); |
| 3761 | |
| 3762 | if (next_color != wq->flush_color) { |
| 3763 | /* |
| 3764 | * Color space is not full. The current work_color |
| 3765 | * becomes our flush_color and work_color is advanced |
| 3766 | * by one. |
| 3767 | */ |
| 3768 | WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); |
| 3769 | this_flusher.flush_color = wq->work_color; |
| 3770 | wq->work_color = next_color; |
| 3771 | |
| 3772 | if (!wq->first_flusher) { |
| 3773 | /* no flush in progress, become the first flusher */ |
| 3774 | WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); |
| 3775 | |
| 3776 | wq->first_flusher = &this_flusher; |
| 3777 | |
| 3778 | if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, |
| 3779 | wq->work_color)) { |
| 3780 | /* nothing to flush, done */ |
| 3781 | wq->flush_color = next_color; |
| 3782 | wq->first_flusher = NULL; |
| 3783 | goto out_unlock; |
| 3784 | } |
| 3785 | } else { |
| 3786 | /* wait in queue */ |
| 3787 | WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); |
| 3788 | list_add_tail(&this_flusher.list, &wq->flusher_queue); |
| 3789 | flush_workqueue_prep_pwqs(wq, -1, wq->work_color); |
| 3790 | } |
| 3791 | } else { |
| 3792 | /* |
| 3793 | * Oops, color space is full, wait on overflow queue. |
| 3794 | * The next flush completion will assign us |
| 3795 | * flush_color and transfer to flusher_queue. |
| 3796 | */ |
| 3797 | list_add_tail(&this_flusher.list, &wq->flusher_overflow); |
| 3798 | } |
| 3799 | |
| 3800 | check_flush_dependency(wq, NULL); |
| 3801 | |
| 3802 | mutex_unlock(&wq->mutex); |
| 3803 | |
| 3804 | wait_for_completion(&this_flusher.done); |
| 3805 | |
| 3806 | /* |
| 3807 | * Wake-up-and-cascade phase |
| 3808 | * |
| 3809 | * First flushers are responsible for cascading flushes and |
| 3810 | * handling overflow. Non-first flushers can simply return. |
| 3811 | */ |
| 3812 | if (READ_ONCE(wq->first_flusher) != &this_flusher) |
| 3813 | return; |
| 3814 | |
| 3815 | mutex_lock(&wq->mutex); |
| 3816 | |
| 3817 | /* we might have raced, check again with mutex held */ |
| 3818 | if (wq->first_flusher != &this_flusher) |
| 3819 | goto out_unlock; |
| 3820 | |
| 3821 | WRITE_ONCE(wq->first_flusher, NULL); |
| 3822 | |
| 3823 | WARN_ON_ONCE(!list_empty(&this_flusher.list)); |
| 3824 | WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); |
| 3825 | |
| 3826 | while (true) { |
| 3827 | struct wq_flusher *next, *tmp; |
| 3828 | |
| 3829 | /* complete all the flushers sharing the current flush color */ |
| 3830 | list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { |
| 3831 | if (next->flush_color != wq->flush_color) |
| 3832 | break; |
| 3833 | list_del_init(&next->list); |
| 3834 | complete(&next->done); |
| 3835 | } |
| 3836 | |
| 3837 | WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && |
| 3838 | wq->flush_color != work_next_color(wq->work_color)); |
| 3839 | |
| 3840 | /* this flush_color is finished, advance by one */ |
| 3841 | wq->flush_color = work_next_color(wq->flush_color); |
| 3842 | |
| 3843 | /* one color has been freed, handle overflow queue */ |
| 3844 | if (!list_empty(&wq->flusher_overflow)) { |
| 3845 | /* |
| 3846 | * Assign the same color to all overflowed |
| 3847 | * flushers, advance work_color and append to |
| 3848 | * flusher_queue. This is the start-to-wait |
| 3849 | * phase for these overflowed flushers. |
| 3850 | */ |
| 3851 | list_for_each_entry(tmp, &wq->flusher_overflow, list) |
| 3852 | tmp->flush_color = wq->work_color; |
| 3853 | |
| 3854 | wq->work_color = work_next_color(wq->work_color); |
| 3855 | |
| 3856 | list_splice_tail_init(&wq->flusher_overflow, |
| 3857 | &wq->flusher_queue); |
| 3858 | flush_workqueue_prep_pwqs(wq, -1, wq->work_color); |
| 3859 | } |
| 3860 | |
| 3861 | if (list_empty(&wq->flusher_queue)) { |
| 3862 | WARN_ON_ONCE(wq->flush_color != wq->work_color); |
| 3863 | break; |
| 3864 | } |
| 3865 | |
| 3866 | /* |
| 3867 | * Need to flush more colors. Make the next flusher |
| 3868 | * the new first flusher and arm pwqs. |
| 3869 | */ |
| 3870 | WARN_ON_ONCE(wq->flush_color == wq->work_color); |
| 3871 | WARN_ON_ONCE(wq->flush_color != next->flush_color); |
| 3872 | |
| 3873 | list_del_init(&next->list); |
| 3874 | wq->first_flusher = next; |
| 3875 | |
| 3876 | if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) |
| 3877 | break; |
| 3878 | |
| 3879 | /* |
| 3880 | * Meh... this color is already done, clear first |
| 3881 | * flusher and repeat cascading. |
| 3882 | */ |
| 3883 | wq->first_flusher = NULL; |
| 3884 | } |
| 3885 | |
| 3886 | out_unlock: |
| 3887 | mutex_unlock(&wq->mutex); |
| 3888 | } |
| 3889 | EXPORT_SYMBOL(__flush_workqueue); |
| 3890 | |
| 3891 | /** |
| 3892 | * drain_workqueue - drain a workqueue |
| 3893 | * @wq: workqueue to drain |
| 3894 | * |
| 3895 | * Wait until the workqueue becomes empty. While draining is in progress, |
| 3896 | * only chain queueing is allowed. IOW, only currently pending or running |
| 3897 | * work items on @wq can queue further work items on it. @wq is flushed |
| 3898 | * repeatedly until it becomes empty. The number of flushing is determined |
| 3899 | * by the depth of chaining and should be relatively short. Whine if it |
| 3900 | * takes too long. |
| 3901 | */ |
| 3902 | void drain_workqueue(struct workqueue_struct *wq) |
| 3903 | { |
| 3904 | unsigned int flush_cnt = 0; |
| 3905 | struct pool_workqueue *pwq; |
| 3906 | |
| 3907 | /* |
| 3908 | * __queue_work() needs to test whether there are drainers, is much |
| 3909 | * hotter than drain_workqueue() and already looks at @wq->flags. |
| 3910 | * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. |
| 3911 | */ |
| 3912 | mutex_lock(&wq->mutex); |
| 3913 | if (!wq->nr_drainers++) |
| 3914 | wq->flags |= __WQ_DRAINING; |
| 3915 | mutex_unlock(&wq->mutex); |
| 3916 | reflush: |
| 3917 | __flush_workqueue(wq); |
| 3918 | |
| 3919 | mutex_lock(&wq->mutex); |
| 3920 | |
| 3921 | for_each_pwq(pwq, wq) { |
| 3922 | bool drained; |
| 3923 | |
| 3924 | raw_spin_lock_irq(&pwq->pool->lock); |
| 3925 | drained = pwq_is_empty(pwq); |
| 3926 | raw_spin_unlock_irq(&pwq->pool->lock); |
| 3927 | |
| 3928 | if (drained) |
| 3929 | continue; |
| 3930 | |
| 3931 | if (++flush_cnt == 10 || |
| 3932 | (flush_cnt % 100 == 0 && flush_cnt <= 1000)) |
| 3933 | pr_warn("workqueue %s: %s() isn't complete after %u tries\n", |
| 3934 | wq->name, __func__, flush_cnt); |
| 3935 | |
| 3936 | mutex_unlock(&wq->mutex); |
| 3937 | goto reflush; |
| 3938 | } |
| 3939 | |
| 3940 | if (!--wq->nr_drainers) |
| 3941 | wq->flags &= ~__WQ_DRAINING; |
| 3942 | mutex_unlock(&wq->mutex); |
| 3943 | } |
| 3944 | EXPORT_SYMBOL_GPL(drain_workqueue); |
| 3945 | |
| 3946 | static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, |
| 3947 | bool from_cancel) |
| 3948 | { |
| 3949 | struct worker *worker = NULL; |
| 3950 | struct worker_pool *pool; |
| 3951 | struct pool_workqueue *pwq; |
| 3952 | struct workqueue_struct *wq; |
| 3953 | |
| 3954 | might_sleep(); |
| 3955 | |
| 3956 | rcu_read_lock(); |
| 3957 | pool = get_work_pool(work); |
| 3958 | if (!pool) { |
| 3959 | rcu_read_unlock(); |
| 3960 | return false; |
| 3961 | } |
| 3962 | |
| 3963 | raw_spin_lock_irq(&pool->lock); |
| 3964 | /* see the comment in try_to_grab_pending() with the same code */ |
| 3965 | pwq = get_work_pwq(work); |
| 3966 | if (pwq) { |
| 3967 | if (unlikely(pwq->pool != pool)) |
| 3968 | goto already_gone; |
| 3969 | } else { |
| 3970 | worker = find_worker_executing_work(pool, work); |
| 3971 | if (!worker) |
| 3972 | goto already_gone; |
| 3973 | pwq = worker->current_pwq; |
| 3974 | } |
| 3975 | |
| 3976 | wq = pwq->wq; |
| 3977 | check_flush_dependency(wq, work); |
| 3978 | |
| 3979 | insert_wq_barrier(pwq, barr, work, worker); |
| 3980 | raw_spin_unlock_irq(&pool->lock); |
| 3981 | |
| 3982 | touch_work_lockdep_map(work, wq); |
| 3983 | |
| 3984 | /* |
| 3985 | * Force a lock recursion deadlock when using flush_work() inside a |
| 3986 | * single-threaded or rescuer equipped workqueue. |
| 3987 | * |
| 3988 | * For single threaded workqueues the deadlock happens when the work |
| 3989 | * is after the work issuing the flush_work(). For rescuer equipped |
| 3990 | * workqueues the deadlock happens when the rescuer stalls, blocking |
| 3991 | * forward progress. |
| 3992 | */ |
| 3993 | if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) |
| 3994 | touch_wq_lockdep_map(wq); |
| 3995 | |
| 3996 | rcu_read_unlock(); |
| 3997 | return true; |
| 3998 | already_gone: |
| 3999 | raw_spin_unlock_irq(&pool->lock); |
| 4000 | rcu_read_unlock(); |
| 4001 | return false; |
| 4002 | } |
| 4003 | |
| 4004 | static bool __flush_work(struct work_struct *work, bool from_cancel) |
| 4005 | { |
| 4006 | struct wq_barrier barr; |
| 4007 | |
| 4008 | if (WARN_ON(!wq_online)) |
| 4009 | return false; |
| 4010 | |
| 4011 | if (WARN_ON(!work->func)) |
| 4012 | return false; |
| 4013 | |
| 4014 | if (start_flush_work(work, &barr, from_cancel)) { |
| 4015 | wait_for_completion(&barr.done); |
| 4016 | destroy_work_on_stack(&barr.work); |
| 4017 | return true; |
| 4018 | } else { |
| 4019 | return false; |
| 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | /** |
| 4024 | * flush_work - wait for a work to finish executing the last queueing instance |
| 4025 | * @work: the work to flush |
| 4026 | * |
| 4027 | * Wait until @work has finished execution. @work is guaranteed to be idle |
| 4028 | * on return if it hasn't been requeued since flush started. |
| 4029 | * |
| 4030 | * Return: |
| 4031 | * %true if flush_work() waited for the work to finish execution, |
| 4032 | * %false if it was already idle. |
| 4033 | */ |
| 4034 | bool flush_work(struct work_struct *work) |
| 4035 | { |
| 4036 | return __flush_work(work, false); |
| 4037 | } |
| 4038 | EXPORT_SYMBOL_GPL(flush_work); |
| 4039 | |
| 4040 | struct cwt_wait { |
| 4041 | wait_queue_entry_t wait; |
| 4042 | struct work_struct *work; |
| 4043 | }; |
| 4044 | |
| 4045 | static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) |
| 4046 | { |
| 4047 | struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); |
| 4048 | |
| 4049 | if (cwait->work != key) |
| 4050 | return 0; |
| 4051 | return autoremove_wake_function(wait, mode, sync, key); |
| 4052 | } |
| 4053 | |
| 4054 | static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) |
| 4055 | { |
| 4056 | static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); |
| 4057 | unsigned long flags; |
| 4058 | int ret; |
| 4059 | |
| 4060 | do { |
| 4061 | ret = try_to_grab_pending(work, is_dwork, &flags); |
| 4062 | /* |
| 4063 | * If someone else is already canceling, wait for it to |
| 4064 | * finish. flush_work() doesn't work for PREEMPT_NONE |
| 4065 | * because we may get scheduled between @work's completion |
| 4066 | * and the other canceling task resuming and clearing |
| 4067 | * CANCELING - flush_work() will return false immediately |
| 4068 | * as @work is no longer busy, try_to_grab_pending() will |
| 4069 | * return -ENOENT as @work is still being canceled and the |
| 4070 | * other canceling task won't be able to clear CANCELING as |
| 4071 | * we're hogging the CPU. |
| 4072 | * |
| 4073 | * Let's wait for completion using a waitqueue. As this |
| 4074 | * may lead to the thundering herd problem, use a custom |
| 4075 | * wake function which matches @work along with exclusive |
| 4076 | * wait and wakeup. |
| 4077 | */ |
| 4078 | if (unlikely(ret == -ENOENT)) { |
| 4079 | struct cwt_wait cwait; |
| 4080 | |
| 4081 | init_wait(&cwait.wait); |
| 4082 | cwait.wait.func = cwt_wakefn; |
| 4083 | cwait.work = work; |
| 4084 | |
| 4085 | prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, |
| 4086 | TASK_UNINTERRUPTIBLE); |
| 4087 | if (work_is_canceling(work)) |
| 4088 | schedule(); |
| 4089 | finish_wait(&cancel_waitq, &cwait.wait); |
| 4090 | } |
| 4091 | } while (unlikely(ret < 0)); |
| 4092 | |
| 4093 | /* tell other tasks trying to grab @work to back off */ |
| 4094 | mark_work_canceling(work); |
| 4095 | local_irq_restore(flags); |
| 4096 | |
| 4097 | /* |
| 4098 | * This allows canceling during early boot. We know that @work |
| 4099 | * isn't executing. |
| 4100 | */ |
| 4101 | if (wq_online) |
| 4102 | __flush_work(work, true); |
| 4103 | |
| 4104 | clear_work_data(work); |
| 4105 | |
| 4106 | /* |
| 4107 | * Paired with prepare_to_wait() above so that either |
| 4108 | * waitqueue_active() is visible here or !work_is_canceling() is |
| 4109 | * visible there. |
| 4110 | */ |
| 4111 | smp_mb(); |
| 4112 | if (waitqueue_active(&cancel_waitq)) |
| 4113 | __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); |
| 4114 | |
| 4115 | return ret; |
| 4116 | } |
| 4117 | |
| 4118 | /** |
| 4119 | * cancel_work_sync - cancel a work and wait for it to finish |
| 4120 | * @work: the work to cancel |
| 4121 | * |
| 4122 | * Cancel @work and wait for its execution to finish. This function |
| 4123 | * can be used even if the work re-queues itself or migrates to |
| 4124 | * another workqueue. On return from this function, @work is |
| 4125 | * guaranteed to be not pending or executing on any CPU. |
| 4126 | * |
| 4127 | * cancel_work_sync(&delayed_work->work) must not be used for |
| 4128 | * delayed_work's. Use cancel_delayed_work_sync() instead. |
| 4129 | * |
| 4130 | * The caller must ensure that the workqueue on which @work was last |
| 4131 | * queued can't be destroyed before this function returns. |
| 4132 | * |
| 4133 | * Return: |
| 4134 | * %true if @work was pending, %false otherwise. |
| 4135 | */ |
| 4136 | bool cancel_work_sync(struct work_struct *work) |
| 4137 | { |
| 4138 | return __cancel_work_timer(work, false); |
| 4139 | } |
| 4140 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
| 4141 | |
| 4142 | /** |
| 4143 | * flush_delayed_work - wait for a dwork to finish executing the last queueing |
| 4144 | * @dwork: the delayed work to flush |
| 4145 | * |
| 4146 | * Delayed timer is cancelled and the pending work is queued for |
| 4147 | * immediate execution. Like flush_work(), this function only |
| 4148 | * considers the last queueing instance of @dwork. |
| 4149 | * |
| 4150 | * Return: |
| 4151 | * %true if flush_work() waited for the work to finish execution, |
| 4152 | * %false if it was already idle. |
| 4153 | */ |
| 4154 | bool flush_delayed_work(struct delayed_work *dwork) |
| 4155 | { |
| 4156 | local_irq_disable(); |
| 4157 | if (del_timer_sync(&dwork->timer)) |
| 4158 | __queue_work(dwork->cpu, dwork->wq, &dwork->work); |
| 4159 | local_irq_enable(); |
| 4160 | return flush_work(&dwork->work); |
| 4161 | } |
| 4162 | EXPORT_SYMBOL(flush_delayed_work); |
| 4163 | |
| 4164 | /** |
| 4165 | * flush_rcu_work - wait for a rwork to finish executing the last queueing |
| 4166 | * @rwork: the rcu work to flush |
| 4167 | * |
| 4168 | * Return: |
| 4169 | * %true if flush_rcu_work() waited for the work to finish execution, |
| 4170 | * %false if it was already idle. |
| 4171 | */ |
| 4172 | bool flush_rcu_work(struct rcu_work *rwork) |
| 4173 | { |
| 4174 | if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { |
| 4175 | rcu_barrier(); |
| 4176 | flush_work(&rwork->work); |
| 4177 | return true; |
| 4178 | } else { |
| 4179 | return flush_work(&rwork->work); |
| 4180 | } |
| 4181 | } |
| 4182 | EXPORT_SYMBOL(flush_rcu_work); |
| 4183 | |
| 4184 | static bool __cancel_work(struct work_struct *work, bool is_dwork) |
| 4185 | { |
| 4186 | unsigned long flags; |
| 4187 | int ret; |
| 4188 | |
| 4189 | do { |
| 4190 | ret = try_to_grab_pending(work, is_dwork, &flags); |
| 4191 | } while (unlikely(ret == -EAGAIN)); |
| 4192 | |
| 4193 | if (unlikely(ret < 0)) |
| 4194 | return false; |
| 4195 | |
| 4196 | set_work_pool_and_clear_pending(work, get_work_pool_id(work)); |
| 4197 | local_irq_restore(flags); |
| 4198 | return ret; |
| 4199 | } |
| 4200 | |
| 4201 | /* |
| 4202 | * See cancel_delayed_work() |
| 4203 | */ |
| 4204 | bool cancel_work(struct work_struct *work) |
| 4205 | { |
| 4206 | return __cancel_work(work, false); |
| 4207 | } |
| 4208 | EXPORT_SYMBOL(cancel_work); |
| 4209 | |
| 4210 | /** |
| 4211 | * cancel_delayed_work - cancel a delayed work |
| 4212 | * @dwork: delayed_work to cancel |
| 4213 | * |
| 4214 | * Kill off a pending delayed_work. |
| 4215 | * |
| 4216 | * Return: %true if @dwork was pending and canceled; %false if it wasn't |
| 4217 | * pending. |
| 4218 | * |
| 4219 | * Note: |
| 4220 | * The work callback function may still be running on return, unless |
| 4221 | * it returns %true and the work doesn't re-arm itself. Explicitly flush or |
| 4222 | * use cancel_delayed_work_sync() to wait on it. |
| 4223 | * |
| 4224 | * This function is safe to call from any context including IRQ handler. |
| 4225 | */ |
| 4226 | bool cancel_delayed_work(struct delayed_work *dwork) |
| 4227 | { |
| 4228 | return __cancel_work(&dwork->work, true); |
| 4229 | } |
| 4230 | EXPORT_SYMBOL(cancel_delayed_work); |
| 4231 | |
| 4232 | /** |
| 4233 | * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish |
| 4234 | * @dwork: the delayed work cancel |
| 4235 | * |
| 4236 | * This is cancel_work_sync() for delayed works. |
| 4237 | * |
| 4238 | * Return: |
| 4239 | * %true if @dwork was pending, %false otherwise. |
| 4240 | */ |
| 4241 | bool cancel_delayed_work_sync(struct delayed_work *dwork) |
| 4242 | { |
| 4243 | return __cancel_work_timer(&dwork->work, true); |
| 4244 | } |
| 4245 | EXPORT_SYMBOL(cancel_delayed_work_sync); |
| 4246 | |
| 4247 | /** |
| 4248 | * schedule_on_each_cpu - execute a function synchronously on each online CPU |
| 4249 | * @func: the function to call |
| 4250 | * |
| 4251 | * schedule_on_each_cpu() executes @func on each online CPU using the |
| 4252 | * system workqueue and blocks until all CPUs have completed. |
| 4253 | * schedule_on_each_cpu() is very slow. |
| 4254 | * |
| 4255 | * Return: |
| 4256 | * 0 on success, -errno on failure. |
| 4257 | */ |
| 4258 | int schedule_on_each_cpu(work_func_t func) |
| 4259 | { |
| 4260 | int cpu; |
| 4261 | struct work_struct __percpu *works; |
| 4262 | |
| 4263 | works = alloc_percpu(struct work_struct); |
| 4264 | if (!works) |
| 4265 | return -ENOMEM; |
| 4266 | |
| 4267 | cpus_read_lock(); |
| 4268 | |
| 4269 | for_each_online_cpu(cpu) { |
| 4270 | struct work_struct *work = per_cpu_ptr(works, cpu); |
| 4271 | |
| 4272 | INIT_WORK(work, func); |
| 4273 | schedule_work_on(cpu, work); |
| 4274 | } |
| 4275 | |
| 4276 | for_each_online_cpu(cpu) |
| 4277 | flush_work(per_cpu_ptr(works, cpu)); |
| 4278 | |
| 4279 | cpus_read_unlock(); |
| 4280 | free_percpu(works); |
| 4281 | return 0; |
| 4282 | } |
| 4283 | |
| 4284 | /** |
| 4285 | * execute_in_process_context - reliably execute the routine with user context |
| 4286 | * @fn: the function to execute |
| 4287 | * @ew: guaranteed storage for the execute work structure (must |
| 4288 | * be available when the work executes) |
| 4289 | * |
| 4290 | * Executes the function immediately if process context is available, |
| 4291 | * otherwise schedules the function for delayed execution. |
| 4292 | * |
| 4293 | * Return: 0 - function was executed |
| 4294 | * 1 - function was scheduled for execution |
| 4295 | */ |
| 4296 | int execute_in_process_context(work_func_t fn, struct execute_work *ew) |
| 4297 | { |
| 4298 | if (!in_interrupt()) { |
| 4299 | fn(&ew->work); |
| 4300 | return 0; |
| 4301 | } |
| 4302 | |
| 4303 | INIT_WORK(&ew->work, fn); |
| 4304 | schedule_work(&ew->work); |
| 4305 | |
| 4306 | return 1; |
| 4307 | } |
| 4308 | EXPORT_SYMBOL_GPL(execute_in_process_context); |
| 4309 | |
| 4310 | /** |
| 4311 | * free_workqueue_attrs - free a workqueue_attrs |
| 4312 | * @attrs: workqueue_attrs to free |
| 4313 | * |
| 4314 | * Undo alloc_workqueue_attrs(). |
| 4315 | */ |
| 4316 | void free_workqueue_attrs(struct workqueue_attrs *attrs) |
| 4317 | { |
| 4318 | if (attrs) { |
| 4319 | free_cpumask_var(attrs->cpumask); |
| 4320 | free_cpumask_var(attrs->__pod_cpumask); |
| 4321 | kfree(attrs); |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | /** |
| 4326 | * alloc_workqueue_attrs - allocate a workqueue_attrs |
| 4327 | * |
| 4328 | * Allocate a new workqueue_attrs, initialize with default settings and |
| 4329 | * return it. |
| 4330 | * |
| 4331 | * Return: The allocated new workqueue_attr on success. %NULL on failure. |
| 4332 | */ |
| 4333 | struct workqueue_attrs *alloc_workqueue_attrs(void) |
| 4334 | { |
| 4335 | struct workqueue_attrs *attrs; |
| 4336 | |
| 4337 | attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); |
| 4338 | if (!attrs) |
| 4339 | goto fail; |
| 4340 | if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) |
| 4341 | goto fail; |
| 4342 | if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) |
| 4343 | goto fail; |
| 4344 | |
| 4345 | cpumask_copy(attrs->cpumask, cpu_possible_mask); |
| 4346 | attrs->affn_scope = WQ_AFFN_DFL; |
| 4347 | return attrs; |
| 4348 | fail: |
| 4349 | free_workqueue_attrs(attrs); |
| 4350 | return NULL; |
| 4351 | } |
| 4352 | |
| 4353 | static void copy_workqueue_attrs(struct workqueue_attrs *to, |
| 4354 | const struct workqueue_attrs *from) |
| 4355 | { |
| 4356 | to->nice = from->nice; |
| 4357 | cpumask_copy(to->cpumask, from->cpumask); |
| 4358 | cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); |
| 4359 | to->affn_strict = from->affn_strict; |
| 4360 | |
| 4361 | /* |
| 4362 | * Unlike hash and equality test, copying shouldn't ignore wq-only |
| 4363 | * fields as copying is used for both pool and wq attrs. Instead, |
| 4364 | * get_unbound_pool() explicitly clears the fields. |
| 4365 | */ |
| 4366 | to->affn_scope = from->affn_scope; |
| 4367 | to->ordered = from->ordered; |
| 4368 | } |
| 4369 | |
| 4370 | /* |
| 4371 | * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the |
| 4372 | * comments in 'struct workqueue_attrs' definition. |
| 4373 | */ |
| 4374 | static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) |
| 4375 | { |
| 4376 | attrs->affn_scope = WQ_AFFN_NR_TYPES; |
| 4377 | attrs->ordered = false; |
| 4378 | } |
| 4379 | |
| 4380 | /* hash value of the content of @attr */ |
| 4381 | static u32 wqattrs_hash(const struct workqueue_attrs *attrs) |
| 4382 | { |
| 4383 | u32 hash = 0; |
| 4384 | |
| 4385 | hash = jhash_1word(attrs->nice, hash); |
| 4386 | hash = jhash(cpumask_bits(attrs->cpumask), |
| 4387 | BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); |
| 4388 | hash = jhash(cpumask_bits(attrs->__pod_cpumask), |
| 4389 | BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); |
| 4390 | hash = jhash_1word(attrs->affn_strict, hash); |
| 4391 | return hash; |
| 4392 | } |
| 4393 | |
| 4394 | /* content equality test */ |
| 4395 | static bool wqattrs_equal(const struct workqueue_attrs *a, |
| 4396 | const struct workqueue_attrs *b) |
| 4397 | { |
| 4398 | if (a->nice != b->nice) |
| 4399 | return false; |
| 4400 | if (!cpumask_equal(a->cpumask, b->cpumask)) |
| 4401 | return false; |
| 4402 | if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) |
| 4403 | return false; |
| 4404 | if (a->affn_strict != b->affn_strict) |
| 4405 | return false; |
| 4406 | return true; |
| 4407 | } |
| 4408 | |
| 4409 | /* Update @attrs with actually available CPUs */ |
| 4410 | static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, |
| 4411 | const cpumask_t *unbound_cpumask) |
| 4412 | { |
| 4413 | /* |
| 4414 | * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If |
| 4415 | * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to |
| 4416 | * @unbound_cpumask. |
| 4417 | */ |
| 4418 | cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); |
| 4419 | if (unlikely(cpumask_empty(attrs->cpumask))) |
| 4420 | cpumask_copy(attrs->cpumask, unbound_cpumask); |
| 4421 | } |
| 4422 | |
| 4423 | /* find wq_pod_type to use for @attrs */ |
| 4424 | static const struct wq_pod_type * |
| 4425 | wqattrs_pod_type(const struct workqueue_attrs *attrs) |
| 4426 | { |
| 4427 | enum wq_affn_scope scope; |
| 4428 | struct wq_pod_type *pt; |
| 4429 | |
| 4430 | /* to synchronize access to wq_affn_dfl */ |
| 4431 | lockdep_assert_held(&wq_pool_mutex); |
| 4432 | |
| 4433 | if (attrs->affn_scope == WQ_AFFN_DFL) |
| 4434 | scope = wq_affn_dfl; |
| 4435 | else |
| 4436 | scope = attrs->affn_scope; |
| 4437 | |
| 4438 | pt = &wq_pod_types[scope]; |
| 4439 | |
| 4440 | if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && |
| 4441 | likely(pt->nr_pods)) |
| 4442 | return pt; |
| 4443 | |
| 4444 | /* |
| 4445 | * Before workqueue_init_topology(), only SYSTEM is available which is |
| 4446 | * initialized in workqueue_init_early(). |
| 4447 | */ |
| 4448 | pt = &wq_pod_types[WQ_AFFN_SYSTEM]; |
| 4449 | BUG_ON(!pt->nr_pods); |
| 4450 | return pt; |
| 4451 | } |
| 4452 | |
| 4453 | /** |
| 4454 | * init_worker_pool - initialize a newly zalloc'd worker_pool |
| 4455 | * @pool: worker_pool to initialize |
| 4456 | * |
| 4457 | * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. |
| 4458 | * |
| 4459 | * Return: 0 on success, -errno on failure. Even on failure, all fields |
| 4460 | * inside @pool proper are initialized and put_unbound_pool() can be called |
| 4461 | * on @pool safely to release it. |
| 4462 | */ |
| 4463 | static int init_worker_pool(struct worker_pool *pool) |
| 4464 | { |
| 4465 | raw_spin_lock_init(&pool->lock); |
| 4466 | pool->id = -1; |
| 4467 | pool->cpu = -1; |
| 4468 | pool->node = NUMA_NO_NODE; |
| 4469 | pool->flags |= POOL_DISASSOCIATED; |
| 4470 | pool->watchdog_ts = jiffies; |
| 4471 | INIT_LIST_HEAD(&pool->worklist); |
| 4472 | INIT_LIST_HEAD(&pool->idle_list); |
| 4473 | hash_init(pool->busy_hash); |
| 4474 | |
| 4475 | timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); |
| 4476 | INIT_WORK(&pool->idle_cull_work, idle_cull_fn); |
| 4477 | |
| 4478 | timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); |
| 4479 | |
| 4480 | INIT_LIST_HEAD(&pool->workers); |
| 4481 | INIT_LIST_HEAD(&pool->dying_workers); |
| 4482 | |
| 4483 | ida_init(&pool->worker_ida); |
| 4484 | INIT_HLIST_NODE(&pool->hash_node); |
| 4485 | pool->refcnt = 1; |
| 4486 | |
| 4487 | /* shouldn't fail above this point */ |
| 4488 | pool->attrs = alloc_workqueue_attrs(); |
| 4489 | if (!pool->attrs) |
| 4490 | return -ENOMEM; |
| 4491 | |
| 4492 | wqattrs_clear_for_pool(pool->attrs); |
| 4493 | |
| 4494 | return 0; |
| 4495 | } |
| 4496 | |
| 4497 | #ifdef CONFIG_LOCKDEP |
| 4498 | static void wq_init_lockdep(struct workqueue_struct *wq) |
| 4499 | { |
| 4500 | char *lock_name; |
| 4501 | |
| 4502 | lockdep_register_key(&wq->key); |
| 4503 | lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); |
| 4504 | if (!lock_name) |
| 4505 | lock_name = wq->name; |
| 4506 | |
| 4507 | wq->lock_name = lock_name; |
| 4508 | lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); |
| 4509 | } |
| 4510 | |
| 4511 | static void wq_unregister_lockdep(struct workqueue_struct *wq) |
| 4512 | { |
| 4513 | lockdep_unregister_key(&wq->key); |
| 4514 | } |
| 4515 | |
| 4516 | static void wq_free_lockdep(struct workqueue_struct *wq) |
| 4517 | { |
| 4518 | if (wq->lock_name != wq->name) |
| 4519 | kfree(wq->lock_name); |
| 4520 | } |
| 4521 | #else |
| 4522 | static void wq_init_lockdep(struct workqueue_struct *wq) |
| 4523 | { |
| 4524 | } |
| 4525 | |
| 4526 | static void wq_unregister_lockdep(struct workqueue_struct *wq) |
| 4527 | { |
| 4528 | } |
| 4529 | |
| 4530 | static void wq_free_lockdep(struct workqueue_struct *wq) |
| 4531 | { |
| 4532 | } |
| 4533 | #endif |
| 4534 | |
| 4535 | static void free_node_nr_active(struct wq_node_nr_active **nna_ar) |
| 4536 | { |
| 4537 | int node; |
| 4538 | |
| 4539 | for_each_node(node) { |
| 4540 | kfree(nna_ar[node]); |
| 4541 | nna_ar[node] = NULL; |
| 4542 | } |
| 4543 | |
| 4544 | kfree(nna_ar[nr_node_ids]); |
| 4545 | nna_ar[nr_node_ids] = NULL; |
| 4546 | } |
| 4547 | |
| 4548 | static void init_node_nr_active(struct wq_node_nr_active *nna) |
| 4549 | { |
| 4550 | nna->max = WQ_DFL_MIN_ACTIVE; |
| 4551 | atomic_set(&nna->nr, 0); |
| 4552 | raw_spin_lock_init(&nna->lock); |
| 4553 | INIT_LIST_HEAD(&nna->pending_pwqs); |
| 4554 | } |
| 4555 | |
| 4556 | /* |
| 4557 | * Each node's nr_active counter will be accessed mostly from its own node and |
| 4558 | * should be allocated in the node. |
| 4559 | */ |
| 4560 | static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) |
| 4561 | { |
| 4562 | struct wq_node_nr_active *nna; |
| 4563 | int node; |
| 4564 | |
| 4565 | for_each_node(node) { |
| 4566 | nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); |
| 4567 | if (!nna) |
| 4568 | goto err_free; |
| 4569 | init_node_nr_active(nna); |
| 4570 | nna_ar[node] = nna; |
| 4571 | } |
| 4572 | |
| 4573 | /* [nr_node_ids] is used as the fallback */ |
| 4574 | nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); |
| 4575 | if (!nna) |
| 4576 | goto err_free; |
| 4577 | init_node_nr_active(nna); |
| 4578 | nna_ar[nr_node_ids] = nna; |
| 4579 | |
| 4580 | return 0; |
| 4581 | |
| 4582 | err_free: |
| 4583 | free_node_nr_active(nna_ar); |
| 4584 | return -ENOMEM; |
| 4585 | } |
| 4586 | |
| 4587 | static void rcu_free_wq(struct rcu_head *rcu) |
| 4588 | { |
| 4589 | struct workqueue_struct *wq = |
| 4590 | container_of(rcu, struct workqueue_struct, rcu); |
| 4591 | |
| 4592 | if (wq->flags & WQ_UNBOUND) |
| 4593 | free_node_nr_active(wq->node_nr_active); |
| 4594 | |
| 4595 | wq_free_lockdep(wq); |
| 4596 | free_percpu(wq->cpu_pwq); |
| 4597 | free_workqueue_attrs(wq->unbound_attrs); |
| 4598 | kfree(wq); |
| 4599 | } |
| 4600 | |
| 4601 | static void rcu_free_pool(struct rcu_head *rcu) |
| 4602 | { |
| 4603 | struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); |
| 4604 | |
| 4605 | ida_destroy(&pool->worker_ida); |
| 4606 | free_workqueue_attrs(pool->attrs); |
| 4607 | kfree(pool); |
| 4608 | } |
| 4609 | |
| 4610 | /** |
| 4611 | * put_unbound_pool - put a worker_pool |
| 4612 | * @pool: worker_pool to put |
| 4613 | * |
| 4614 | * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU |
| 4615 | * safe manner. get_unbound_pool() calls this function on its failure path |
| 4616 | * and this function should be able to release pools which went through, |
| 4617 | * successfully or not, init_worker_pool(). |
| 4618 | * |
| 4619 | * Should be called with wq_pool_mutex held. |
| 4620 | */ |
| 4621 | static void put_unbound_pool(struct worker_pool *pool) |
| 4622 | { |
| 4623 | DECLARE_COMPLETION_ONSTACK(detach_completion); |
| 4624 | struct worker *worker; |
| 4625 | LIST_HEAD(cull_list); |
| 4626 | |
| 4627 | lockdep_assert_held(&wq_pool_mutex); |
| 4628 | |
| 4629 | if (--pool->refcnt) |
| 4630 | return; |
| 4631 | |
| 4632 | /* sanity checks */ |
| 4633 | if (WARN_ON(!(pool->cpu < 0)) || |
| 4634 | WARN_ON(!list_empty(&pool->worklist))) |
| 4635 | return; |
| 4636 | |
| 4637 | /* release id and unhash */ |
| 4638 | if (pool->id >= 0) |
| 4639 | idr_remove(&worker_pool_idr, pool->id); |
| 4640 | hash_del(&pool->hash_node); |
| 4641 | |
| 4642 | /* |
| 4643 | * Become the manager and destroy all workers. This prevents |
| 4644 | * @pool's workers from blocking on attach_mutex. We're the last |
| 4645 | * manager and @pool gets freed with the flag set. |
| 4646 | * |
| 4647 | * Having a concurrent manager is quite unlikely to happen as we can |
| 4648 | * only get here with |
| 4649 | * pwq->refcnt == pool->refcnt == 0 |
| 4650 | * which implies no work queued to the pool, which implies no worker can |
| 4651 | * become the manager. However a worker could have taken the role of |
| 4652 | * manager before the refcnts dropped to 0, since maybe_create_worker() |
| 4653 | * drops pool->lock |
| 4654 | */ |
| 4655 | while (true) { |
| 4656 | rcuwait_wait_event(&manager_wait, |
| 4657 | !(pool->flags & POOL_MANAGER_ACTIVE), |
| 4658 | TASK_UNINTERRUPTIBLE); |
| 4659 | |
| 4660 | mutex_lock(&wq_pool_attach_mutex); |
| 4661 | raw_spin_lock_irq(&pool->lock); |
| 4662 | if (!(pool->flags & POOL_MANAGER_ACTIVE)) { |
| 4663 | pool->flags |= POOL_MANAGER_ACTIVE; |
| 4664 | break; |
| 4665 | } |
| 4666 | raw_spin_unlock_irq(&pool->lock); |
| 4667 | mutex_unlock(&wq_pool_attach_mutex); |
| 4668 | } |
| 4669 | |
| 4670 | while ((worker = first_idle_worker(pool))) |
| 4671 | set_worker_dying(worker, &cull_list); |
| 4672 | WARN_ON(pool->nr_workers || pool->nr_idle); |
| 4673 | raw_spin_unlock_irq(&pool->lock); |
| 4674 | |
| 4675 | wake_dying_workers(&cull_list); |
| 4676 | |
| 4677 | if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) |
| 4678 | pool->detach_completion = &detach_completion; |
| 4679 | mutex_unlock(&wq_pool_attach_mutex); |
| 4680 | |
| 4681 | if (pool->detach_completion) |
| 4682 | wait_for_completion(pool->detach_completion); |
| 4683 | |
| 4684 | /* shut down the timers */ |
| 4685 | del_timer_sync(&pool->idle_timer); |
| 4686 | cancel_work_sync(&pool->idle_cull_work); |
| 4687 | del_timer_sync(&pool->mayday_timer); |
| 4688 | |
| 4689 | /* RCU protected to allow dereferences from get_work_pool() */ |
| 4690 | call_rcu(&pool->rcu, rcu_free_pool); |
| 4691 | } |
| 4692 | |
| 4693 | /** |
| 4694 | * get_unbound_pool - get a worker_pool with the specified attributes |
| 4695 | * @attrs: the attributes of the worker_pool to get |
| 4696 | * |
| 4697 | * Obtain a worker_pool which has the same attributes as @attrs, bump the |
| 4698 | * reference count and return it. If there already is a matching |
| 4699 | * worker_pool, it will be used; otherwise, this function attempts to |
| 4700 | * create a new one. |
| 4701 | * |
| 4702 | * Should be called with wq_pool_mutex held. |
| 4703 | * |
| 4704 | * Return: On success, a worker_pool with the same attributes as @attrs. |
| 4705 | * On failure, %NULL. |
| 4706 | */ |
| 4707 | static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) |
| 4708 | { |
| 4709 | struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; |
| 4710 | u32 hash = wqattrs_hash(attrs); |
| 4711 | struct worker_pool *pool; |
| 4712 | int pod, node = NUMA_NO_NODE; |
| 4713 | |
| 4714 | lockdep_assert_held(&wq_pool_mutex); |
| 4715 | |
| 4716 | /* do we already have a matching pool? */ |
| 4717 | hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { |
| 4718 | if (wqattrs_equal(pool->attrs, attrs)) { |
| 4719 | pool->refcnt++; |
| 4720 | return pool; |
| 4721 | } |
| 4722 | } |
| 4723 | |
| 4724 | /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ |
| 4725 | for (pod = 0; pod < pt->nr_pods; pod++) { |
| 4726 | if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { |
| 4727 | node = pt->pod_node[pod]; |
| 4728 | break; |
| 4729 | } |
| 4730 | } |
| 4731 | |
| 4732 | /* nope, create a new one */ |
| 4733 | pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); |
| 4734 | if (!pool || init_worker_pool(pool) < 0) |
| 4735 | goto fail; |
| 4736 | |
| 4737 | pool->node = node; |
| 4738 | copy_workqueue_attrs(pool->attrs, attrs); |
| 4739 | wqattrs_clear_for_pool(pool->attrs); |
| 4740 | |
| 4741 | if (worker_pool_assign_id(pool) < 0) |
| 4742 | goto fail; |
| 4743 | |
| 4744 | /* create and start the initial worker */ |
| 4745 | if (wq_online && !create_worker(pool)) |
| 4746 | goto fail; |
| 4747 | |
| 4748 | /* install */ |
| 4749 | hash_add(unbound_pool_hash, &pool->hash_node, hash); |
| 4750 | |
| 4751 | return pool; |
| 4752 | fail: |
| 4753 | if (pool) |
| 4754 | put_unbound_pool(pool); |
| 4755 | return NULL; |
| 4756 | } |
| 4757 | |
| 4758 | static void rcu_free_pwq(struct rcu_head *rcu) |
| 4759 | { |
| 4760 | kmem_cache_free(pwq_cache, |
| 4761 | container_of(rcu, struct pool_workqueue, rcu)); |
| 4762 | } |
| 4763 | |
| 4764 | /* |
| 4765 | * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero |
| 4766 | * refcnt and needs to be destroyed. |
| 4767 | */ |
| 4768 | static void pwq_release_workfn(struct kthread_work *work) |
| 4769 | { |
| 4770 | struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, |
| 4771 | release_work); |
| 4772 | struct workqueue_struct *wq = pwq->wq; |
| 4773 | struct worker_pool *pool = pwq->pool; |
| 4774 | bool is_last = false; |
| 4775 | |
| 4776 | /* |
| 4777 | * When @pwq is not linked, it doesn't hold any reference to the |
| 4778 | * @wq, and @wq is invalid to access. |
| 4779 | */ |
| 4780 | if (!list_empty(&pwq->pwqs_node)) { |
| 4781 | mutex_lock(&wq->mutex); |
| 4782 | list_del_rcu(&pwq->pwqs_node); |
| 4783 | is_last = list_empty(&wq->pwqs); |
| 4784 | |
| 4785 | /* |
| 4786 | * For ordered workqueue with a plugged dfl_pwq, restart it now. |
| 4787 | */ |
| 4788 | if (!is_last && (wq->flags & __WQ_ORDERED)) |
| 4789 | unplug_oldest_pwq(wq); |
| 4790 | |
| 4791 | mutex_unlock(&wq->mutex); |
| 4792 | } |
| 4793 | |
| 4794 | if (wq->flags & WQ_UNBOUND) { |
| 4795 | mutex_lock(&wq_pool_mutex); |
| 4796 | put_unbound_pool(pool); |
| 4797 | mutex_unlock(&wq_pool_mutex); |
| 4798 | } |
| 4799 | |
| 4800 | if (!list_empty(&pwq->pending_node)) { |
| 4801 | struct wq_node_nr_active *nna = |
| 4802 | wq_node_nr_active(pwq->wq, pwq->pool->node); |
| 4803 | |
| 4804 | raw_spin_lock_irq(&nna->lock); |
| 4805 | list_del_init(&pwq->pending_node); |
| 4806 | raw_spin_unlock_irq(&nna->lock); |
| 4807 | } |
| 4808 | |
| 4809 | call_rcu(&pwq->rcu, rcu_free_pwq); |
| 4810 | |
| 4811 | /* |
| 4812 | * If we're the last pwq going away, @wq is already dead and no one |
| 4813 | * is gonna access it anymore. Schedule RCU free. |
| 4814 | */ |
| 4815 | if (is_last) { |
| 4816 | wq_unregister_lockdep(wq); |
| 4817 | call_rcu(&wq->rcu, rcu_free_wq); |
| 4818 | } |
| 4819 | } |
| 4820 | |
| 4821 | /* initialize newly allocated @pwq which is associated with @wq and @pool */ |
| 4822 | static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, |
| 4823 | struct worker_pool *pool) |
| 4824 | { |
| 4825 | BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); |
| 4826 | |
| 4827 | memset(pwq, 0, sizeof(*pwq)); |
| 4828 | |
| 4829 | pwq->pool = pool; |
| 4830 | pwq->wq = wq; |
| 4831 | pwq->flush_color = -1; |
| 4832 | pwq->refcnt = 1; |
| 4833 | INIT_LIST_HEAD(&pwq->inactive_works); |
| 4834 | INIT_LIST_HEAD(&pwq->pending_node); |
| 4835 | INIT_LIST_HEAD(&pwq->pwqs_node); |
| 4836 | INIT_LIST_HEAD(&pwq->mayday_node); |
| 4837 | kthread_init_work(&pwq->release_work, pwq_release_workfn); |
| 4838 | } |
| 4839 | |
| 4840 | /* sync @pwq with the current state of its associated wq and link it */ |
| 4841 | static void link_pwq(struct pool_workqueue *pwq) |
| 4842 | { |
| 4843 | struct workqueue_struct *wq = pwq->wq; |
| 4844 | |
| 4845 | lockdep_assert_held(&wq->mutex); |
| 4846 | |
| 4847 | /* may be called multiple times, ignore if already linked */ |
| 4848 | if (!list_empty(&pwq->pwqs_node)) |
| 4849 | return; |
| 4850 | |
| 4851 | /* set the matching work_color */ |
| 4852 | pwq->work_color = wq->work_color; |
| 4853 | |
| 4854 | /* link in @pwq */ |
| 4855 | list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); |
| 4856 | } |
| 4857 | |
| 4858 | /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ |
| 4859 | static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, |
| 4860 | const struct workqueue_attrs *attrs) |
| 4861 | { |
| 4862 | struct worker_pool *pool; |
| 4863 | struct pool_workqueue *pwq; |
| 4864 | |
| 4865 | lockdep_assert_held(&wq_pool_mutex); |
| 4866 | |
| 4867 | pool = get_unbound_pool(attrs); |
| 4868 | if (!pool) |
| 4869 | return NULL; |
| 4870 | |
| 4871 | pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); |
| 4872 | if (!pwq) { |
| 4873 | put_unbound_pool(pool); |
| 4874 | return NULL; |
| 4875 | } |
| 4876 | |
| 4877 | init_pwq(pwq, wq, pool); |
| 4878 | return pwq; |
| 4879 | } |
| 4880 | |
| 4881 | /** |
| 4882 | * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod |
| 4883 | * @attrs: the wq_attrs of the default pwq of the target workqueue |
| 4884 | * @cpu: the target CPU |
| 4885 | * @cpu_going_down: if >= 0, the CPU to consider as offline |
| 4886 | * |
| 4887 | * Calculate the cpumask a workqueue with @attrs should use on @pod. If |
| 4888 | * @cpu_going_down is >= 0, that cpu is considered offline during calculation. |
| 4889 | * The result is stored in @attrs->__pod_cpumask. |
| 4890 | * |
| 4891 | * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled |
| 4892 | * and @pod has online CPUs requested by @attrs, the returned cpumask is the |
| 4893 | * intersection of the possible CPUs of @pod and @attrs->cpumask. |
| 4894 | * |
| 4895 | * The caller is responsible for ensuring that the cpumask of @pod stays stable. |
| 4896 | */ |
| 4897 | static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, |
| 4898 | int cpu_going_down) |
| 4899 | { |
| 4900 | const struct wq_pod_type *pt = wqattrs_pod_type(attrs); |
| 4901 | int pod = pt->cpu_pod[cpu]; |
| 4902 | |
| 4903 | /* does @pod have any online CPUs @attrs wants? */ |
| 4904 | cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); |
| 4905 | cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); |
| 4906 | if (cpu_going_down >= 0) |
| 4907 | cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); |
| 4908 | |
| 4909 | if (cpumask_empty(attrs->__pod_cpumask)) { |
| 4910 | cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); |
| 4911 | return; |
| 4912 | } |
| 4913 | |
| 4914 | /* yeap, return possible CPUs in @pod that @attrs wants */ |
| 4915 | cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); |
| 4916 | |
| 4917 | if (cpumask_empty(attrs->__pod_cpumask)) |
| 4918 | pr_warn_once("WARNING: workqueue cpumask: online intersect > " |
| 4919 | "possible intersect\n"); |
| 4920 | } |
| 4921 | |
| 4922 | /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ |
| 4923 | static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, |
| 4924 | int cpu, struct pool_workqueue *pwq) |
| 4925 | { |
| 4926 | struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); |
| 4927 | struct pool_workqueue *old_pwq; |
| 4928 | |
| 4929 | lockdep_assert_held(&wq_pool_mutex); |
| 4930 | lockdep_assert_held(&wq->mutex); |
| 4931 | |
| 4932 | /* link_pwq() can handle duplicate calls */ |
| 4933 | link_pwq(pwq); |
| 4934 | |
| 4935 | old_pwq = rcu_access_pointer(*slot); |
| 4936 | rcu_assign_pointer(*slot, pwq); |
| 4937 | return old_pwq; |
| 4938 | } |
| 4939 | |
| 4940 | /* context to store the prepared attrs & pwqs before applying */ |
| 4941 | struct apply_wqattrs_ctx { |
| 4942 | struct workqueue_struct *wq; /* target workqueue */ |
| 4943 | struct workqueue_attrs *attrs; /* attrs to apply */ |
| 4944 | struct list_head list; /* queued for batching commit */ |
| 4945 | struct pool_workqueue *dfl_pwq; |
| 4946 | struct pool_workqueue *pwq_tbl[]; |
| 4947 | }; |
| 4948 | |
| 4949 | /* free the resources after success or abort */ |
| 4950 | static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) |
| 4951 | { |
| 4952 | if (ctx) { |
| 4953 | int cpu; |
| 4954 | |
| 4955 | for_each_possible_cpu(cpu) |
| 4956 | put_pwq_unlocked(ctx->pwq_tbl[cpu]); |
| 4957 | put_pwq_unlocked(ctx->dfl_pwq); |
| 4958 | |
| 4959 | free_workqueue_attrs(ctx->attrs); |
| 4960 | |
| 4961 | kfree(ctx); |
| 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | /* allocate the attrs and pwqs for later installation */ |
| 4966 | static struct apply_wqattrs_ctx * |
| 4967 | apply_wqattrs_prepare(struct workqueue_struct *wq, |
| 4968 | const struct workqueue_attrs *attrs, |
| 4969 | const cpumask_var_t unbound_cpumask) |
| 4970 | { |
| 4971 | struct apply_wqattrs_ctx *ctx; |
| 4972 | struct workqueue_attrs *new_attrs; |
| 4973 | int cpu; |
| 4974 | |
| 4975 | lockdep_assert_held(&wq_pool_mutex); |
| 4976 | |
| 4977 | if (WARN_ON(attrs->affn_scope < 0 || |
| 4978 | attrs->affn_scope >= WQ_AFFN_NR_TYPES)) |
| 4979 | return ERR_PTR(-EINVAL); |
| 4980 | |
| 4981 | ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); |
| 4982 | |
| 4983 | new_attrs = alloc_workqueue_attrs(); |
| 4984 | if (!ctx || !new_attrs) |
| 4985 | goto out_free; |
| 4986 | |
| 4987 | /* |
| 4988 | * If something goes wrong during CPU up/down, we'll fall back to |
| 4989 | * the default pwq covering whole @attrs->cpumask. Always create |
| 4990 | * it even if we don't use it immediately. |
| 4991 | */ |
| 4992 | copy_workqueue_attrs(new_attrs, attrs); |
| 4993 | wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); |
| 4994 | cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); |
| 4995 | ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); |
| 4996 | if (!ctx->dfl_pwq) |
| 4997 | goto out_free; |
| 4998 | |
| 4999 | for_each_possible_cpu(cpu) { |
| 5000 | if (new_attrs->ordered) { |
| 5001 | ctx->dfl_pwq->refcnt++; |
| 5002 | ctx->pwq_tbl[cpu] = ctx->dfl_pwq; |
| 5003 | } else { |
| 5004 | wq_calc_pod_cpumask(new_attrs, cpu, -1); |
| 5005 | ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); |
| 5006 | if (!ctx->pwq_tbl[cpu]) |
| 5007 | goto out_free; |
| 5008 | } |
| 5009 | } |
| 5010 | |
| 5011 | /* save the user configured attrs and sanitize it. */ |
| 5012 | copy_workqueue_attrs(new_attrs, attrs); |
| 5013 | cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); |
| 5014 | cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); |
| 5015 | ctx->attrs = new_attrs; |
| 5016 | |
| 5017 | /* |
| 5018 | * For initialized ordered workqueues, there should only be one pwq |
| 5019 | * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution |
| 5020 | * of newly queued work items until execution of older work items in |
| 5021 | * the old pwq's have completed. |
| 5022 | */ |
| 5023 | if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)) |
| 5024 | ctx->dfl_pwq->plugged = true; |
| 5025 | |
| 5026 | ctx->wq = wq; |
| 5027 | return ctx; |
| 5028 | |
| 5029 | out_free: |
| 5030 | free_workqueue_attrs(new_attrs); |
| 5031 | apply_wqattrs_cleanup(ctx); |
| 5032 | return ERR_PTR(-ENOMEM); |
| 5033 | } |
| 5034 | |
| 5035 | /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ |
| 5036 | static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) |
| 5037 | { |
| 5038 | int cpu; |
| 5039 | |
| 5040 | /* all pwqs have been created successfully, let's install'em */ |
| 5041 | mutex_lock(&ctx->wq->mutex); |
| 5042 | |
| 5043 | copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); |
| 5044 | |
| 5045 | /* save the previous pwqs and install the new ones */ |
| 5046 | for_each_possible_cpu(cpu) |
| 5047 | ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, |
| 5048 | ctx->pwq_tbl[cpu]); |
| 5049 | ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); |
| 5050 | |
| 5051 | /* update node_nr_active->max */ |
| 5052 | wq_update_node_max_active(ctx->wq, -1); |
| 5053 | |
| 5054 | /* rescuer needs to respect wq cpumask changes */ |
| 5055 | if (ctx->wq->rescuer) |
| 5056 | set_cpus_allowed_ptr(ctx->wq->rescuer->task, |
| 5057 | unbound_effective_cpumask(ctx->wq)); |
| 5058 | |
| 5059 | mutex_unlock(&ctx->wq->mutex); |
| 5060 | } |
| 5061 | |
| 5062 | static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, |
| 5063 | const struct workqueue_attrs *attrs) |
| 5064 | { |
| 5065 | struct apply_wqattrs_ctx *ctx; |
| 5066 | |
| 5067 | /* only unbound workqueues can change attributes */ |
| 5068 | if (WARN_ON(!(wq->flags & WQ_UNBOUND))) |
| 5069 | return -EINVAL; |
| 5070 | |
| 5071 | ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); |
| 5072 | if (IS_ERR(ctx)) |
| 5073 | return PTR_ERR(ctx); |
| 5074 | |
| 5075 | /* the ctx has been prepared successfully, let's commit it */ |
| 5076 | apply_wqattrs_commit(ctx); |
| 5077 | apply_wqattrs_cleanup(ctx); |
| 5078 | |
| 5079 | return 0; |
| 5080 | } |
| 5081 | |
| 5082 | /** |
| 5083 | * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue |
| 5084 | * @wq: the target workqueue |
| 5085 | * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() |
| 5086 | * |
| 5087 | * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps |
| 5088 | * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that |
| 5089 | * work items are affine to the pod it was issued on. Older pwqs are released as |
| 5090 | * in-flight work items finish. Note that a work item which repeatedly requeues |
| 5091 | * itself back-to-back will stay on its current pwq. |
| 5092 | * |
| 5093 | * Performs GFP_KERNEL allocations. |
| 5094 | * |
| 5095 | * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). |
| 5096 | * |
| 5097 | * Return: 0 on success and -errno on failure. |
| 5098 | */ |
| 5099 | int apply_workqueue_attrs(struct workqueue_struct *wq, |
| 5100 | const struct workqueue_attrs *attrs) |
| 5101 | { |
| 5102 | int ret; |
| 5103 | |
| 5104 | lockdep_assert_cpus_held(); |
| 5105 | |
| 5106 | mutex_lock(&wq_pool_mutex); |
| 5107 | ret = apply_workqueue_attrs_locked(wq, attrs); |
| 5108 | mutex_unlock(&wq_pool_mutex); |
| 5109 | |
| 5110 | return ret; |
| 5111 | } |
| 5112 | |
| 5113 | /** |
| 5114 | * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug |
| 5115 | * @wq: the target workqueue |
| 5116 | * @cpu: the CPU to update pool association for |
| 5117 | * @hotplug_cpu: the CPU coming up or going down |
| 5118 | * @online: whether @cpu is coming up or going down |
| 5119 | * |
| 5120 | * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and |
| 5121 | * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of |
| 5122 | * @wq accordingly. |
| 5123 | * |
| 5124 | * |
| 5125 | * If pod affinity can't be adjusted due to memory allocation failure, it falls |
| 5126 | * back to @wq->dfl_pwq which may not be optimal but is always correct. |
| 5127 | * |
| 5128 | * Note that when the last allowed CPU of a pod goes offline for a workqueue |
| 5129 | * with a cpumask spanning multiple pods, the workers which were already |
| 5130 | * executing the work items for the workqueue will lose their CPU affinity and |
| 5131 | * may execute on any CPU. This is similar to how per-cpu workqueues behave on |
| 5132 | * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's |
| 5133 | * responsibility to flush the work item from CPU_DOWN_PREPARE. |
| 5134 | */ |
| 5135 | static void wq_update_pod(struct workqueue_struct *wq, int cpu, |
| 5136 | int hotplug_cpu, bool online) |
| 5137 | { |
| 5138 | int off_cpu = online ? -1 : hotplug_cpu; |
| 5139 | struct pool_workqueue *old_pwq = NULL, *pwq; |
| 5140 | struct workqueue_attrs *target_attrs; |
| 5141 | |
| 5142 | lockdep_assert_held(&wq_pool_mutex); |
| 5143 | |
| 5144 | if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) |
| 5145 | return; |
| 5146 | |
| 5147 | /* |
| 5148 | * We don't wanna alloc/free wq_attrs for each wq for each CPU. |
| 5149 | * Let's use a preallocated one. The following buf is protected by |
| 5150 | * CPU hotplug exclusion. |
| 5151 | */ |
| 5152 | target_attrs = wq_update_pod_attrs_buf; |
| 5153 | |
| 5154 | copy_workqueue_attrs(target_attrs, wq->unbound_attrs); |
| 5155 | wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); |
| 5156 | |
| 5157 | /* nothing to do if the target cpumask matches the current pwq */ |
| 5158 | wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); |
| 5159 | if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) |
| 5160 | return; |
| 5161 | |
| 5162 | /* create a new pwq */ |
| 5163 | pwq = alloc_unbound_pwq(wq, target_attrs); |
| 5164 | if (!pwq) { |
| 5165 | pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", |
| 5166 | wq->name); |
| 5167 | goto use_dfl_pwq; |
| 5168 | } |
| 5169 | |
| 5170 | /* Install the new pwq. */ |
| 5171 | mutex_lock(&wq->mutex); |
| 5172 | old_pwq = install_unbound_pwq(wq, cpu, pwq); |
| 5173 | goto out_unlock; |
| 5174 | |
| 5175 | use_dfl_pwq: |
| 5176 | mutex_lock(&wq->mutex); |
| 5177 | pwq = unbound_pwq(wq, -1); |
| 5178 | raw_spin_lock_irq(&pwq->pool->lock); |
| 5179 | get_pwq(pwq); |
| 5180 | raw_spin_unlock_irq(&pwq->pool->lock); |
| 5181 | old_pwq = install_unbound_pwq(wq, cpu, pwq); |
| 5182 | out_unlock: |
| 5183 | mutex_unlock(&wq->mutex); |
| 5184 | put_pwq_unlocked(old_pwq); |
| 5185 | } |
| 5186 | |
| 5187 | static int alloc_and_link_pwqs(struct workqueue_struct *wq) |
| 5188 | { |
| 5189 | bool highpri = wq->flags & WQ_HIGHPRI; |
| 5190 | int cpu, ret; |
| 5191 | |
| 5192 | wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); |
| 5193 | if (!wq->cpu_pwq) |
| 5194 | goto enomem; |
| 5195 | |
| 5196 | if (!(wq->flags & WQ_UNBOUND)) { |
| 5197 | for_each_possible_cpu(cpu) { |
| 5198 | struct pool_workqueue **pwq_p; |
| 5199 | struct worker_pool __percpu *pools; |
| 5200 | struct worker_pool *pool; |
| 5201 | |
| 5202 | if (wq->flags & WQ_BH) |
| 5203 | pools = bh_worker_pools; |
| 5204 | else |
| 5205 | pools = cpu_worker_pools; |
| 5206 | |
| 5207 | pool = &(per_cpu_ptr(pools, cpu)[highpri]); |
| 5208 | pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); |
| 5209 | |
| 5210 | *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, |
| 5211 | pool->node); |
| 5212 | if (!*pwq_p) |
| 5213 | goto enomem; |
| 5214 | |
| 5215 | init_pwq(*pwq_p, wq, pool); |
| 5216 | |
| 5217 | mutex_lock(&wq->mutex); |
| 5218 | link_pwq(*pwq_p); |
| 5219 | mutex_unlock(&wq->mutex); |
| 5220 | } |
| 5221 | return 0; |
| 5222 | } |
| 5223 | |
| 5224 | cpus_read_lock(); |
| 5225 | if (wq->flags & __WQ_ORDERED) { |
| 5226 | struct pool_workqueue *dfl_pwq; |
| 5227 | |
| 5228 | ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); |
| 5229 | /* there should only be single pwq for ordering guarantee */ |
| 5230 | dfl_pwq = rcu_access_pointer(wq->dfl_pwq); |
| 5231 | WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || |
| 5232 | wq->pwqs.prev != &dfl_pwq->pwqs_node), |
| 5233 | "ordering guarantee broken for workqueue %s\n", wq->name); |
| 5234 | } else { |
| 5235 | ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); |
| 5236 | } |
| 5237 | cpus_read_unlock(); |
| 5238 | |
| 5239 | /* for unbound pwq, flush the pwq_release_worker ensures that the |
| 5240 | * pwq_release_workfn() completes before calling kfree(wq). |
| 5241 | */ |
| 5242 | if (ret) |
| 5243 | kthread_flush_worker(pwq_release_worker); |
| 5244 | |
| 5245 | return ret; |
| 5246 | |
| 5247 | enomem: |
| 5248 | if (wq->cpu_pwq) { |
| 5249 | for_each_possible_cpu(cpu) { |
| 5250 | struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); |
| 5251 | |
| 5252 | if (pwq) |
| 5253 | kmem_cache_free(pwq_cache, pwq); |
| 5254 | } |
| 5255 | free_percpu(wq->cpu_pwq); |
| 5256 | wq->cpu_pwq = NULL; |
| 5257 | } |
| 5258 | return -ENOMEM; |
| 5259 | } |
| 5260 | |
| 5261 | static int wq_clamp_max_active(int max_active, unsigned int flags, |
| 5262 | const char *name) |
| 5263 | { |
| 5264 | if (max_active < 1 || max_active > WQ_MAX_ACTIVE) |
| 5265 | pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", |
| 5266 | max_active, name, 1, WQ_MAX_ACTIVE); |
| 5267 | |
| 5268 | return clamp_val(max_active, 1, WQ_MAX_ACTIVE); |
| 5269 | } |
| 5270 | |
| 5271 | /* |
| 5272 | * Workqueues which may be used during memory reclaim should have a rescuer |
| 5273 | * to guarantee forward progress. |
| 5274 | */ |
| 5275 | static int init_rescuer(struct workqueue_struct *wq) |
| 5276 | { |
| 5277 | struct worker *rescuer; |
| 5278 | int ret; |
| 5279 | |
| 5280 | if (!(wq->flags & WQ_MEM_RECLAIM)) |
| 5281 | return 0; |
| 5282 | |
| 5283 | rescuer = alloc_worker(NUMA_NO_NODE); |
| 5284 | if (!rescuer) { |
| 5285 | pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", |
| 5286 | wq->name); |
| 5287 | return -ENOMEM; |
| 5288 | } |
| 5289 | |
| 5290 | rescuer->rescue_wq = wq; |
| 5291 | rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); |
| 5292 | if (IS_ERR(rescuer->task)) { |
| 5293 | ret = PTR_ERR(rescuer->task); |
| 5294 | pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", |
| 5295 | wq->name, ERR_PTR(ret)); |
| 5296 | kfree(rescuer); |
| 5297 | return ret; |
| 5298 | } |
| 5299 | |
| 5300 | wq->rescuer = rescuer; |
| 5301 | if (wq->flags & WQ_UNBOUND) |
| 5302 | kthread_bind_mask(rescuer->task, wq->unbound_attrs->cpumask); |
| 5303 | else |
| 5304 | kthread_bind_mask(rescuer->task, cpu_possible_mask); |
| 5305 | wake_up_process(rescuer->task); |
| 5306 | |
| 5307 | return 0; |
| 5308 | } |
| 5309 | |
| 5310 | /** |
| 5311 | * wq_adjust_max_active - update a wq's max_active to the current setting |
| 5312 | * @wq: target workqueue |
| 5313 | * |
| 5314 | * If @wq isn't freezing, set @wq->max_active to the saved_max_active and |
| 5315 | * activate inactive work items accordingly. If @wq is freezing, clear |
| 5316 | * @wq->max_active to zero. |
| 5317 | */ |
| 5318 | static void wq_adjust_max_active(struct workqueue_struct *wq) |
| 5319 | { |
| 5320 | bool activated; |
| 5321 | int new_max, new_min; |
| 5322 | |
| 5323 | lockdep_assert_held(&wq->mutex); |
| 5324 | |
| 5325 | if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { |
| 5326 | new_max = 0; |
| 5327 | new_min = 0; |
| 5328 | } else { |
| 5329 | new_max = wq->saved_max_active; |
| 5330 | new_min = wq->saved_min_active; |
| 5331 | } |
| 5332 | |
| 5333 | if (wq->max_active == new_max && wq->min_active == new_min) |
| 5334 | return; |
| 5335 | |
| 5336 | /* |
| 5337 | * Update @wq->max/min_active and then kick inactive work items if more |
| 5338 | * active work items are allowed. This doesn't break work item ordering |
| 5339 | * because new work items are always queued behind existing inactive |
| 5340 | * work items if there are any. |
| 5341 | */ |
| 5342 | WRITE_ONCE(wq->max_active, new_max); |
| 5343 | WRITE_ONCE(wq->min_active, new_min); |
| 5344 | |
| 5345 | if (wq->flags & WQ_UNBOUND) |
| 5346 | wq_update_node_max_active(wq, -1); |
| 5347 | |
| 5348 | if (new_max == 0) |
| 5349 | return; |
| 5350 | |
| 5351 | /* |
| 5352 | * Round-robin through pwq's activating the first inactive work item |
| 5353 | * until max_active is filled. |
| 5354 | */ |
| 5355 | do { |
| 5356 | struct pool_workqueue *pwq; |
| 5357 | |
| 5358 | activated = false; |
| 5359 | for_each_pwq(pwq, wq) { |
| 5360 | unsigned long flags; |
| 5361 | |
| 5362 | /* can be called during early boot w/ irq disabled */ |
| 5363 | raw_spin_lock_irqsave(&pwq->pool->lock, flags); |
| 5364 | if (pwq_activate_first_inactive(pwq, true)) { |
| 5365 | activated = true; |
| 5366 | kick_pool(pwq->pool); |
| 5367 | } |
| 5368 | raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); |
| 5369 | } |
| 5370 | } while (activated); |
| 5371 | } |
| 5372 | |
| 5373 | __printf(1, 4) |
| 5374 | struct workqueue_struct *alloc_workqueue(const char *fmt, |
| 5375 | unsigned int flags, |
| 5376 | int max_active, ...) |
| 5377 | { |
| 5378 | va_list args; |
| 5379 | struct workqueue_struct *wq; |
| 5380 | size_t wq_size; |
| 5381 | int name_len; |
| 5382 | |
| 5383 | if (flags & WQ_BH) { |
| 5384 | if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS)) |
| 5385 | return NULL; |
| 5386 | if (WARN_ON_ONCE(max_active)) |
| 5387 | return NULL; |
| 5388 | } |
| 5389 | |
| 5390 | /* see the comment above the definition of WQ_POWER_EFFICIENT */ |
| 5391 | if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) |
| 5392 | flags |= WQ_UNBOUND; |
| 5393 | |
| 5394 | /* allocate wq and format name */ |
| 5395 | if (flags & WQ_UNBOUND) |
| 5396 | wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); |
| 5397 | else |
| 5398 | wq_size = sizeof(*wq); |
| 5399 | |
| 5400 | wq = kzalloc(wq_size, GFP_KERNEL); |
| 5401 | if (!wq) |
| 5402 | return NULL; |
| 5403 | |
| 5404 | if (flags & WQ_UNBOUND) { |
| 5405 | wq->unbound_attrs = alloc_workqueue_attrs(); |
| 5406 | if (!wq->unbound_attrs) |
| 5407 | goto err_free_wq; |
| 5408 | } |
| 5409 | |
| 5410 | va_start(args, max_active); |
| 5411 | name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); |
| 5412 | va_end(args); |
| 5413 | |
| 5414 | if (name_len >= WQ_NAME_LEN) |
| 5415 | pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", |
| 5416 | wq->name); |
| 5417 | |
| 5418 | if (flags & WQ_BH) { |
| 5419 | /* |
| 5420 | * BH workqueues always share a single execution context per CPU |
| 5421 | * and don't impose any max_active limit. |
| 5422 | */ |
| 5423 | max_active = INT_MAX; |
| 5424 | } else { |
| 5425 | max_active = max_active ?: WQ_DFL_ACTIVE; |
| 5426 | max_active = wq_clamp_max_active(max_active, flags, wq->name); |
| 5427 | } |
| 5428 | |
| 5429 | /* init wq */ |
| 5430 | wq->flags = flags; |
| 5431 | wq->max_active = max_active; |
| 5432 | wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); |
| 5433 | wq->saved_max_active = wq->max_active; |
| 5434 | wq->saved_min_active = wq->min_active; |
| 5435 | mutex_init(&wq->mutex); |
| 5436 | atomic_set(&wq->nr_pwqs_to_flush, 0); |
| 5437 | INIT_LIST_HEAD(&wq->pwqs); |
| 5438 | INIT_LIST_HEAD(&wq->flusher_queue); |
| 5439 | INIT_LIST_HEAD(&wq->flusher_overflow); |
| 5440 | INIT_LIST_HEAD(&wq->maydays); |
| 5441 | |
| 5442 | wq_init_lockdep(wq); |
| 5443 | INIT_LIST_HEAD(&wq->list); |
| 5444 | |
| 5445 | if (flags & WQ_UNBOUND) { |
| 5446 | if (alloc_node_nr_active(wq->node_nr_active) < 0) |
| 5447 | goto err_unreg_lockdep; |
| 5448 | } |
| 5449 | |
| 5450 | if (alloc_and_link_pwqs(wq) < 0) |
| 5451 | goto err_free_node_nr_active; |
| 5452 | |
| 5453 | if (wq_online && init_rescuer(wq) < 0) |
| 5454 | goto err_destroy; |
| 5455 | |
| 5456 | if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) |
| 5457 | goto err_destroy; |
| 5458 | |
| 5459 | /* |
| 5460 | * wq_pool_mutex protects global freeze state and workqueues list. |
| 5461 | * Grab it, adjust max_active and add the new @wq to workqueues |
| 5462 | * list. |
| 5463 | */ |
| 5464 | mutex_lock(&wq_pool_mutex); |
| 5465 | |
| 5466 | mutex_lock(&wq->mutex); |
| 5467 | wq_adjust_max_active(wq); |
| 5468 | mutex_unlock(&wq->mutex); |
| 5469 | |
| 5470 | list_add_tail_rcu(&wq->list, &workqueues); |
| 5471 | |
| 5472 | mutex_unlock(&wq_pool_mutex); |
| 5473 | |
| 5474 | return wq; |
| 5475 | |
| 5476 | err_free_node_nr_active: |
| 5477 | if (wq->flags & WQ_UNBOUND) |
| 5478 | free_node_nr_active(wq->node_nr_active); |
| 5479 | err_unreg_lockdep: |
| 5480 | wq_unregister_lockdep(wq); |
| 5481 | wq_free_lockdep(wq); |
| 5482 | err_free_wq: |
| 5483 | free_workqueue_attrs(wq->unbound_attrs); |
| 5484 | kfree(wq); |
| 5485 | return NULL; |
| 5486 | err_destroy: |
| 5487 | destroy_workqueue(wq); |
| 5488 | return NULL; |
| 5489 | } |
| 5490 | EXPORT_SYMBOL_GPL(alloc_workqueue); |
| 5491 | |
| 5492 | static bool pwq_busy(struct pool_workqueue *pwq) |
| 5493 | { |
| 5494 | int i; |
| 5495 | |
| 5496 | for (i = 0; i < WORK_NR_COLORS; i++) |
| 5497 | if (pwq->nr_in_flight[i]) |
| 5498 | return true; |
| 5499 | |
| 5500 | if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) |
| 5501 | return true; |
| 5502 | if (!pwq_is_empty(pwq)) |
| 5503 | return true; |
| 5504 | |
| 5505 | return false; |
| 5506 | } |
| 5507 | |
| 5508 | /** |
| 5509 | * destroy_workqueue - safely terminate a workqueue |
| 5510 | * @wq: target workqueue |
| 5511 | * |
| 5512 | * Safely destroy a workqueue. All work currently pending will be done first. |
| 5513 | */ |
| 5514 | void destroy_workqueue(struct workqueue_struct *wq) |
| 5515 | { |
| 5516 | struct pool_workqueue *pwq; |
| 5517 | int cpu; |
| 5518 | |
| 5519 | /* |
| 5520 | * Remove it from sysfs first so that sanity check failure doesn't |
| 5521 | * lead to sysfs name conflicts. |
| 5522 | */ |
| 5523 | workqueue_sysfs_unregister(wq); |
| 5524 | |
| 5525 | /* mark the workqueue destruction is in progress */ |
| 5526 | mutex_lock(&wq->mutex); |
| 5527 | wq->flags |= __WQ_DESTROYING; |
| 5528 | mutex_unlock(&wq->mutex); |
| 5529 | |
| 5530 | /* drain it before proceeding with destruction */ |
| 5531 | drain_workqueue(wq); |
| 5532 | |
| 5533 | /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ |
| 5534 | if (wq->rescuer) { |
| 5535 | struct worker *rescuer = wq->rescuer; |
| 5536 | |
| 5537 | /* this prevents new queueing */ |
| 5538 | raw_spin_lock_irq(&wq_mayday_lock); |
| 5539 | wq->rescuer = NULL; |
| 5540 | raw_spin_unlock_irq(&wq_mayday_lock); |
| 5541 | |
| 5542 | /* rescuer will empty maydays list before exiting */ |
| 5543 | kthread_stop(rescuer->task); |
| 5544 | kfree(rescuer); |
| 5545 | } |
| 5546 | |
| 5547 | /* |
| 5548 | * Sanity checks - grab all the locks so that we wait for all |
| 5549 | * in-flight operations which may do put_pwq(). |
| 5550 | */ |
| 5551 | mutex_lock(&wq_pool_mutex); |
| 5552 | mutex_lock(&wq->mutex); |
| 5553 | for_each_pwq(pwq, wq) { |
| 5554 | raw_spin_lock_irq(&pwq->pool->lock); |
| 5555 | if (WARN_ON(pwq_busy(pwq))) { |
| 5556 | pr_warn("%s: %s has the following busy pwq\n", |
| 5557 | __func__, wq->name); |
| 5558 | show_pwq(pwq); |
| 5559 | raw_spin_unlock_irq(&pwq->pool->lock); |
| 5560 | mutex_unlock(&wq->mutex); |
| 5561 | mutex_unlock(&wq_pool_mutex); |
| 5562 | show_one_workqueue(wq); |
| 5563 | return; |
| 5564 | } |
| 5565 | raw_spin_unlock_irq(&pwq->pool->lock); |
| 5566 | } |
| 5567 | mutex_unlock(&wq->mutex); |
| 5568 | |
| 5569 | /* |
| 5570 | * wq list is used to freeze wq, remove from list after |
| 5571 | * flushing is complete in case freeze races us. |
| 5572 | */ |
| 5573 | list_del_rcu(&wq->list); |
| 5574 | mutex_unlock(&wq_pool_mutex); |
| 5575 | |
| 5576 | /* |
| 5577 | * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq |
| 5578 | * to put the base refs. @wq will be auto-destroyed from the last |
| 5579 | * pwq_put. RCU read lock prevents @wq from going away from under us. |
| 5580 | */ |
| 5581 | rcu_read_lock(); |
| 5582 | |
| 5583 | for_each_possible_cpu(cpu) { |
| 5584 | put_pwq_unlocked(unbound_pwq(wq, cpu)); |
| 5585 | RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); |
| 5586 | } |
| 5587 | |
| 5588 | put_pwq_unlocked(unbound_pwq(wq, -1)); |
| 5589 | RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); |
| 5590 | |
| 5591 | rcu_read_unlock(); |
| 5592 | } |
| 5593 | EXPORT_SYMBOL_GPL(destroy_workqueue); |
| 5594 | |
| 5595 | /** |
| 5596 | * workqueue_set_max_active - adjust max_active of a workqueue |
| 5597 | * @wq: target workqueue |
| 5598 | * @max_active: new max_active value. |
| 5599 | * |
| 5600 | * Set max_active of @wq to @max_active. See the alloc_workqueue() function |
| 5601 | * comment. |
| 5602 | * |
| 5603 | * CONTEXT: |
| 5604 | * Don't call from IRQ context. |
| 5605 | */ |
| 5606 | void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) |
| 5607 | { |
| 5608 | /* max_active doesn't mean anything for BH workqueues */ |
| 5609 | if (WARN_ON(wq->flags & WQ_BH)) |
| 5610 | return; |
| 5611 | /* disallow meddling with max_active for ordered workqueues */ |
| 5612 | if (WARN_ON(wq->flags & __WQ_ORDERED)) |
| 5613 | return; |
| 5614 | |
| 5615 | max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); |
| 5616 | |
| 5617 | mutex_lock(&wq->mutex); |
| 5618 | |
| 5619 | wq->saved_max_active = max_active; |
| 5620 | if (wq->flags & WQ_UNBOUND) |
| 5621 | wq->saved_min_active = min(wq->saved_min_active, max_active); |
| 5622 | |
| 5623 | wq_adjust_max_active(wq); |
| 5624 | |
| 5625 | mutex_unlock(&wq->mutex); |
| 5626 | } |
| 5627 | EXPORT_SYMBOL_GPL(workqueue_set_max_active); |
| 5628 | |
| 5629 | /** |
| 5630 | * current_work - retrieve %current task's work struct |
| 5631 | * |
| 5632 | * Determine if %current task is a workqueue worker and what it's working on. |
| 5633 | * Useful to find out the context that the %current task is running in. |
| 5634 | * |
| 5635 | * Return: work struct if %current task is a workqueue worker, %NULL otherwise. |
| 5636 | */ |
| 5637 | struct work_struct *current_work(void) |
| 5638 | { |
| 5639 | struct worker *worker = current_wq_worker(); |
| 5640 | |
| 5641 | return worker ? worker->current_work : NULL; |
| 5642 | } |
| 5643 | EXPORT_SYMBOL(current_work); |
| 5644 | |
| 5645 | /** |
| 5646 | * current_is_workqueue_rescuer - is %current workqueue rescuer? |
| 5647 | * |
| 5648 | * Determine whether %current is a workqueue rescuer. Can be used from |
| 5649 | * work functions to determine whether it's being run off the rescuer task. |
| 5650 | * |
| 5651 | * Return: %true if %current is a workqueue rescuer. %false otherwise. |
| 5652 | */ |
| 5653 | bool current_is_workqueue_rescuer(void) |
| 5654 | { |
| 5655 | struct worker *worker = current_wq_worker(); |
| 5656 | |
| 5657 | return worker && worker->rescue_wq; |
| 5658 | } |
| 5659 | |
| 5660 | /** |
| 5661 | * workqueue_congested - test whether a workqueue is congested |
| 5662 | * @cpu: CPU in question |
| 5663 | * @wq: target workqueue |
| 5664 | * |
| 5665 | * Test whether @wq's cpu workqueue for @cpu is congested. There is |
| 5666 | * no synchronization around this function and the test result is |
| 5667 | * unreliable and only useful as advisory hints or for debugging. |
| 5668 | * |
| 5669 | * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. |
| 5670 | * |
| 5671 | * With the exception of ordered workqueues, all workqueues have per-cpu |
| 5672 | * pool_workqueues, each with its own congested state. A workqueue being |
| 5673 | * congested on one CPU doesn't mean that the workqueue is contested on any |
| 5674 | * other CPUs. |
| 5675 | * |
| 5676 | * Return: |
| 5677 | * %true if congested, %false otherwise. |
| 5678 | */ |
| 5679 | bool workqueue_congested(int cpu, struct workqueue_struct *wq) |
| 5680 | { |
| 5681 | struct pool_workqueue *pwq; |
| 5682 | bool ret; |
| 5683 | |
| 5684 | rcu_read_lock(); |
| 5685 | preempt_disable(); |
| 5686 | |
| 5687 | if (cpu == WORK_CPU_UNBOUND) |
| 5688 | cpu = smp_processor_id(); |
| 5689 | |
| 5690 | pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); |
| 5691 | ret = !list_empty(&pwq->inactive_works); |
| 5692 | |
| 5693 | preempt_enable(); |
| 5694 | rcu_read_unlock(); |
| 5695 | |
| 5696 | return ret; |
| 5697 | } |
| 5698 | EXPORT_SYMBOL_GPL(workqueue_congested); |
| 5699 | |
| 5700 | /** |
| 5701 | * work_busy - test whether a work is currently pending or running |
| 5702 | * @work: the work to be tested |
| 5703 | * |
| 5704 | * Test whether @work is currently pending or running. There is no |
| 5705 | * synchronization around this function and the test result is |
| 5706 | * unreliable and only useful as advisory hints or for debugging. |
| 5707 | * |
| 5708 | * Return: |
| 5709 | * OR'd bitmask of WORK_BUSY_* bits. |
| 5710 | */ |
| 5711 | unsigned int work_busy(struct work_struct *work) |
| 5712 | { |
| 5713 | struct worker_pool *pool; |
| 5714 | unsigned long flags; |
| 5715 | unsigned int ret = 0; |
| 5716 | |
| 5717 | if (work_pending(work)) |
| 5718 | ret |= WORK_BUSY_PENDING; |
| 5719 | |
| 5720 | rcu_read_lock(); |
| 5721 | pool = get_work_pool(work); |
| 5722 | if (pool) { |
| 5723 | raw_spin_lock_irqsave(&pool->lock, flags); |
| 5724 | if (find_worker_executing_work(pool, work)) |
| 5725 | ret |= WORK_BUSY_RUNNING; |
| 5726 | raw_spin_unlock_irqrestore(&pool->lock, flags); |
| 5727 | } |
| 5728 | rcu_read_unlock(); |
| 5729 | |
| 5730 | return ret; |
| 5731 | } |
| 5732 | EXPORT_SYMBOL_GPL(work_busy); |
| 5733 | |
| 5734 | /** |
| 5735 | * set_worker_desc - set description for the current work item |
| 5736 | * @fmt: printf-style format string |
| 5737 | * @...: arguments for the format string |
| 5738 | * |
| 5739 | * This function can be called by a running work function to describe what |
| 5740 | * the work item is about. If the worker task gets dumped, this |
| 5741 | * information will be printed out together to help debugging. The |
| 5742 | * description can be at most WORKER_DESC_LEN including the trailing '\0'. |
| 5743 | */ |
| 5744 | void set_worker_desc(const char *fmt, ...) |
| 5745 | { |
| 5746 | struct worker *worker = current_wq_worker(); |
| 5747 | va_list args; |
| 5748 | |
| 5749 | if (worker) { |
| 5750 | va_start(args, fmt); |
| 5751 | vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); |
| 5752 | va_end(args); |
| 5753 | } |
| 5754 | } |
| 5755 | EXPORT_SYMBOL_GPL(set_worker_desc); |
| 5756 | |
| 5757 | /** |
| 5758 | * print_worker_info - print out worker information and description |
| 5759 | * @log_lvl: the log level to use when printing |
| 5760 | * @task: target task |
| 5761 | * |
| 5762 | * If @task is a worker and currently executing a work item, print out the |
| 5763 | * name of the workqueue being serviced and worker description set with |
| 5764 | * set_worker_desc() by the currently executing work item. |
| 5765 | * |
| 5766 | * This function can be safely called on any task as long as the |
| 5767 | * task_struct itself is accessible. While safe, this function isn't |
| 5768 | * synchronized and may print out mixups or garbages of limited length. |
| 5769 | */ |
| 5770 | void print_worker_info(const char *log_lvl, struct task_struct *task) |
| 5771 | { |
| 5772 | work_func_t *fn = NULL; |
| 5773 | char name[WQ_NAME_LEN] = { }; |
| 5774 | char desc[WORKER_DESC_LEN] = { }; |
| 5775 | struct pool_workqueue *pwq = NULL; |
| 5776 | struct workqueue_struct *wq = NULL; |
| 5777 | struct worker *worker; |
| 5778 | |
| 5779 | if (!(task->flags & PF_WQ_WORKER)) |
| 5780 | return; |
| 5781 | |
| 5782 | /* |
| 5783 | * This function is called without any synchronization and @task |
| 5784 | * could be in any state. Be careful with dereferences. |
| 5785 | */ |
| 5786 | worker = kthread_probe_data(task); |
| 5787 | |
| 5788 | /* |
| 5789 | * Carefully copy the associated workqueue's workfn, name and desc. |
| 5790 | * Keep the original last '\0' in case the original is garbage. |
| 5791 | */ |
| 5792 | copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); |
| 5793 | copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); |
| 5794 | copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); |
| 5795 | copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); |
| 5796 | copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); |
| 5797 | |
| 5798 | if (fn || name[0] || desc[0]) { |
| 5799 | printk("%sWorkqueue: %s %ps", log_lvl, name, fn); |
| 5800 | if (strcmp(name, desc)) |
| 5801 | pr_cont(" (%s)", desc); |
| 5802 | pr_cont("\n"); |
| 5803 | } |
| 5804 | } |
| 5805 | |
| 5806 | static void pr_cont_pool_info(struct worker_pool *pool) |
| 5807 | { |
| 5808 | pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); |
| 5809 | if (pool->node != NUMA_NO_NODE) |
| 5810 | pr_cont(" node=%d", pool->node); |
| 5811 | pr_cont(" flags=0x%x", pool->flags); |
| 5812 | if (pool->flags & POOL_BH) |
| 5813 | pr_cont(" bh%s", |
| 5814 | pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); |
| 5815 | else |
| 5816 | pr_cont(" nice=%d", pool->attrs->nice); |
| 5817 | } |
| 5818 | |
| 5819 | static void pr_cont_worker_id(struct worker *worker) |
| 5820 | { |
| 5821 | struct worker_pool *pool = worker->pool; |
| 5822 | |
| 5823 | if (pool->flags & WQ_BH) |
| 5824 | pr_cont("bh%s", |
| 5825 | pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); |
| 5826 | else |
| 5827 | pr_cont("%d%s", task_pid_nr(worker->task), |
| 5828 | worker->rescue_wq ? "(RESCUER)" : ""); |
| 5829 | } |
| 5830 | |
| 5831 | struct pr_cont_work_struct { |
| 5832 | bool comma; |
| 5833 | work_func_t func; |
| 5834 | long ctr; |
| 5835 | }; |
| 5836 | |
| 5837 | static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) |
| 5838 | { |
| 5839 | if (!pcwsp->ctr) |
| 5840 | goto out_record; |
| 5841 | if (func == pcwsp->func) { |
| 5842 | pcwsp->ctr++; |
| 5843 | return; |
| 5844 | } |
| 5845 | if (pcwsp->ctr == 1) |
| 5846 | pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); |
| 5847 | else |
| 5848 | pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); |
| 5849 | pcwsp->ctr = 0; |
| 5850 | out_record: |
| 5851 | if ((long)func == -1L) |
| 5852 | return; |
| 5853 | pcwsp->comma = comma; |
| 5854 | pcwsp->func = func; |
| 5855 | pcwsp->ctr = 1; |
| 5856 | } |
| 5857 | |
| 5858 | static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) |
| 5859 | { |
| 5860 | if (work->func == wq_barrier_func) { |
| 5861 | struct wq_barrier *barr; |
| 5862 | |
| 5863 | barr = container_of(work, struct wq_barrier, work); |
| 5864 | |
| 5865 | pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); |
| 5866 | pr_cont("%s BAR(%d)", comma ? "," : "", |
| 5867 | task_pid_nr(barr->task)); |
| 5868 | } else { |
| 5869 | if (!comma) |
| 5870 | pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); |
| 5871 | pr_cont_work_flush(comma, work->func, pcwsp); |
| 5872 | } |
| 5873 | } |
| 5874 | |
| 5875 | static void show_pwq(struct pool_workqueue *pwq) |
| 5876 | { |
| 5877 | struct pr_cont_work_struct pcws = { .ctr = 0, }; |
| 5878 | struct worker_pool *pool = pwq->pool; |
| 5879 | struct work_struct *work; |
| 5880 | struct worker *worker; |
| 5881 | bool has_in_flight = false, has_pending = false; |
| 5882 | int bkt; |
| 5883 | |
| 5884 | pr_info(" pwq %d:", pool->id); |
| 5885 | pr_cont_pool_info(pool); |
| 5886 | |
| 5887 | pr_cont(" active=%d refcnt=%d%s\n", |
| 5888 | pwq->nr_active, pwq->refcnt, |
| 5889 | !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); |
| 5890 | |
| 5891 | hash_for_each(pool->busy_hash, bkt, worker, hentry) { |
| 5892 | if (worker->current_pwq == pwq) { |
| 5893 | has_in_flight = true; |
| 5894 | break; |
| 5895 | } |
| 5896 | } |
| 5897 | if (has_in_flight) { |
| 5898 | bool comma = false; |
| 5899 | |
| 5900 | pr_info(" in-flight:"); |
| 5901 | hash_for_each(pool->busy_hash, bkt, worker, hentry) { |
| 5902 | if (worker->current_pwq != pwq) |
| 5903 | continue; |
| 5904 | |
| 5905 | pr_cont(" %s", comma ? "," : ""); |
| 5906 | pr_cont_worker_id(worker); |
| 5907 | pr_cont(":%ps", worker->current_func); |
| 5908 | list_for_each_entry(work, &worker->scheduled, entry) |
| 5909 | pr_cont_work(false, work, &pcws); |
| 5910 | pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); |
| 5911 | comma = true; |
| 5912 | } |
| 5913 | pr_cont("\n"); |
| 5914 | } |
| 5915 | |
| 5916 | list_for_each_entry(work, &pool->worklist, entry) { |
| 5917 | if (get_work_pwq(work) == pwq) { |
| 5918 | has_pending = true; |
| 5919 | break; |
| 5920 | } |
| 5921 | } |
| 5922 | if (has_pending) { |
| 5923 | bool comma = false; |
| 5924 | |
| 5925 | pr_info(" pending:"); |
| 5926 | list_for_each_entry(work, &pool->worklist, entry) { |
| 5927 | if (get_work_pwq(work) != pwq) |
| 5928 | continue; |
| 5929 | |
| 5930 | pr_cont_work(comma, work, &pcws); |
| 5931 | comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); |
| 5932 | } |
| 5933 | pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); |
| 5934 | pr_cont("\n"); |
| 5935 | } |
| 5936 | |
| 5937 | if (!list_empty(&pwq->inactive_works)) { |
| 5938 | bool comma = false; |
| 5939 | |
| 5940 | pr_info(" inactive:"); |
| 5941 | list_for_each_entry(work, &pwq->inactive_works, entry) { |
| 5942 | pr_cont_work(comma, work, &pcws); |
| 5943 | comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); |
| 5944 | } |
| 5945 | pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); |
| 5946 | pr_cont("\n"); |
| 5947 | } |
| 5948 | } |
| 5949 | |
| 5950 | /** |
| 5951 | * show_one_workqueue - dump state of specified workqueue |
| 5952 | * @wq: workqueue whose state will be printed |
| 5953 | */ |
| 5954 | void show_one_workqueue(struct workqueue_struct *wq) |
| 5955 | { |
| 5956 | struct pool_workqueue *pwq; |
| 5957 | bool idle = true; |
| 5958 | unsigned long flags; |
| 5959 | |
| 5960 | for_each_pwq(pwq, wq) { |
| 5961 | if (!pwq_is_empty(pwq)) { |
| 5962 | idle = false; |
| 5963 | break; |
| 5964 | } |
| 5965 | } |
| 5966 | if (idle) /* Nothing to print for idle workqueue */ |
| 5967 | return; |
| 5968 | |
| 5969 | pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); |
| 5970 | |
| 5971 | for_each_pwq(pwq, wq) { |
| 5972 | raw_spin_lock_irqsave(&pwq->pool->lock, flags); |
| 5973 | if (!pwq_is_empty(pwq)) { |
| 5974 | /* |
| 5975 | * Defer printing to avoid deadlocks in console |
| 5976 | * drivers that queue work while holding locks |
| 5977 | * also taken in their write paths. |
| 5978 | */ |
| 5979 | printk_deferred_enter(); |
| 5980 | show_pwq(pwq); |
| 5981 | printk_deferred_exit(); |
| 5982 | } |
| 5983 | raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); |
| 5984 | /* |
| 5985 | * We could be printing a lot from atomic context, e.g. |
| 5986 | * sysrq-t -> show_all_workqueues(). Avoid triggering |
| 5987 | * hard lockup. |
| 5988 | */ |
| 5989 | touch_nmi_watchdog(); |
| 5990 | } |
| 5991 | |
| 5992 | } |
| 5993 | |
| 5994 | /** |
| 5995 | * show_one_worker_pool - dump state of specified worker pool |
| 5996 | * @pool: worker pool whose state will be printed |
| 5997 | */ |
| 5998 | static void show_one_worker_pool(struct worker_pool *pool) |
| 5999 | { |
| 6000 | struct worker *worker; |
| 6001 | bool first = true; |
| 6002 | unsigned long flags; |
| 6003 | unsigned long hung = 0; |
| 6004 | |
| 6005 | raw_spin_lock_irqsave(&pool->lock, flags); |
| 6006 | if (pool->nr_workers == pool->nr_idle) |
| 6007 | goto next_pool; |
| 6008 | |
| 6009 | /* How long the first pending work is waiting for a worker. */ |
| 6010 | if (!list_empty(&pool->worklist)) |
| 6011 | hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; |
| 6012 | |
| 6013 | /* |
| 6014 | * Defer printing to avoid deadlocks in console drivers that |
| 6015 | * queue work while holding locks also taken in their write |
| 6016 | * paths. |
| 6017 | */ |
| 6018 | printk_deferred_enter(); |
| 6019 | pr_info("pool %d:", pool->id); |
| 6020 | pr_cont_pool_info(pool); |
| 6021 | pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); |
| 6022 | if (pool->manager) |
| 6023 | pr_cont(" manager: %d", |
| 6024 | task_pid_nr(pool->manager->task)); |
| 6025 | list_for_each_entry(worker, &pool->idle_list, entry) { |
| 6026 | pr_cont(" %s", first ? "idle: " : ""); |
| 6027 | pr_cont_worker_id(worker); |
| 6028 | first = false; |
| 6029 | } |
| 6030 | pr_cont("\n"); |
| 6031 | printk_deferred_exit(); |
| 6032 | next_pool: |
| 6033 | raw_spin_unlock_irqrestore(&pool->lock, flags); |
| 6034 | /* |
| 6035 | * We could be printing a lot from atomic context, e.g. |
| 6036 | * sysrq-t -> show_all_workqueues(). Avoid triggering |
| 6037 | * hard lockup. |
| 6038 | */ |
| 6039 | touch_nmi_watchdog(); |
| 6040 | |
| 6041 | } |
| 6042 | |
| 6043 | /** |
| 6044 | * show_all_workqueues - dump workqueue state |
| 6045 | * |
| 6046 | * Called from a sysrq handler and prints out all busy workqueues and pools. |
| 6047 | */ |
| 6048 | void show_all_workqueues(void) |
| 6049 | { |
| 6050 | struct workqueue_struct *wq; |
| 6051 | struct worker_pool *pool; |
| 6052 | int pi; |
| 6053 | |
| 6054 | rcu_read_lock(); |
| 6055 | |
| 6056 | pr_info("Showing busy workqueues and worker pools:\n"); |
| 6057 | |
| 6058 | list_for_each_entry_rcu(wq, &workqueues, list) |
| 6059 | show_one_workqueue(wq); |
| 6060 | |
| 6061 | for_each_pool(pool, pi) |
| 6062 | show_one_worker_pool(pool); |
| 6063 | |
| 6064 | rcu_read_unlock(); |
| 6065 | } |
| 6066 | |
| 6067 | /** |
| 6068 | * show_freezable_workqueues - dump freezable workqueue state |
| 6069 | * |
| 6070 | * Called from try_to_freeze_tasks() and prints out all freezable workqueues |
| 6071 | * still busy. |
| 6072 | */ |
| 6073 | void show_freezable_workqueues(void) |
| 6074 | { |
| 6075 | struct workqueue_struct *wq; |
| 6076 | |
| 6077 | rcu_read_lock(); |
| 6078 | |
| 6079 | pr_info("Showing freezable workqueues that are still busy:\n"); |
| 6080 | |
| 6081 | list_for_each_entry_rcu(wq, &workqueues, list) { |
| 6082 | if (!(wq->flags & WQ_FREEZABLE)) |
| 6083 | continue; |
| 6084 | show_one_workqueue(wq); |
| 6085 | } |
| 6086 | |
| 6087 | rcu_read_unlock(); |
| 6088 | } |
| 6089 | |
| 6090 | /* used to show worker information through /proc/PID/{comm,stat,status} */ |
| 6091 | void wq_worker_comm(char *buf, size_t size, struct task_struct *task) |
| 6092 | { |
| 6093 | int off; |
| 6094 | |
| 6095 | /* always show the actual comm */ |
| 6096 | off = strscpy(buf, task->comm, size); |
| 6097 | if (off < 0) |
| 6098 | return; |
| 6099 | |
| 6100 | /* stabilize PF_WQ_WORKER and worker pool association */ |
| 6101 | mutex_lock(&wq_pool_attach_mutex); |
| 6102 | |
| 6103 | if (task->flags & PF_WQ_WORKER) { |
| 6104 | struct worker *worker = kthread_data(task); |
| 6105 | struct worker_pool *pool = worker->pool; |
| 6106 | |
| 6107 | if (pool) { |
| 6108 | raw_spin_lock_irq(&pool->lock); |
| 6109 | /* |
| 6110 | * ->desc tracks information (wq name or |
| 6111 | * set_worker_desc()) for the latest execution. If |
| 6112 | * current, prepend '+', otherwise '-'. |
| 6113 | */ |
| 6114 | if (worker->desc[0] != '\0') { |
| 6115 | if (worker->current_work) |
| 6116 | scnprintf(buf + off, size - off, "+%s", |
| 6117 | worker->desc); |
| 6118 | else |
| 6119 | scnprintf(buf + off, size - off, "-%s", |
| 6120 | worker->desc); |
| 6121 | } |
| 6122 | raw_spin_unlock_irq(&pool->lock); |
| 6123 | } |
| 6124 | } |
| 6125 | |
| 6126 | mutex_unlock(&wq_pool_attach_mutex); |
| 6127 | } |
| 6128 | |
| 6129 | #ifdef CONFIG_SMP |
| 6130 | |
| 6131 | /* |
| 6132 | * CPU hotplug. |
| 6133 | * |
| 6134 | * There are two challenges in supporting CPU hotplug. Firstly, there |
| 6135 | * are a lot of assumptions on strong associations among work, pwq and |
| 6136 | * pool which make migrating pending and scheduled works very |
| 6137 | * difficult to implement without impacting hot paths. Secondly, |
| 6138 | * worker pools serve mix of short, long and very long running works making |
| 6139 | * blocked draining impractical. |
| 6140 | * |
| 6141 | * This is solved by allowing the pools to be disassociated from the CPU |
| 6142 | * running as an unbound one and allowing it to be reattached later if the |
| 6143 | * cpu comes back online. |
| 6144 | */ |
| 6145 | |
| 6146 | static void unbind_workers(int cpu) |
| 6147 | { |
| 6148 | struct worker_pool *pool; |
| 6149 | struct worker *worker; |
| 6150 | |
| 6151 | for_each_cpu_worker_pool(pool, cpu) { |
| 6152 | mutex_lock(&wq_pool_attach_mutex); |
| 6153 | raw_spin_lock_irq(&pool->lock); |
| 6154 | |
| 6155 | /* |
| 6156 | * We've blocked all attach/detach operations. Make all workers |
| 6157 | * unbound and set DISASSOCIATED. Before this, all workers |
| 6158 | * must be on the cpu. After this, they may become diasporas. |
| 6159 | * And the preemption disabled section in their sched callbacks |
| 6160 | * are guaranteed to see WORKER_UNBOUND since the code here |
| 6161 | * is on the same cpu. |
| 6162 | */ |
| 6163 | for_each_pool_worker(worker, pool) |
| 6164 | worker->flags |= WORKER_UNBOUND; |
| 6165 | |
| 6166 | pool->flags |= POOL_DISASSOCIATED; |
| 6167 | |
| 6168 | /* |
| 6169 | * The handling of nr_running in sched callbacks are disabled |
| 6170 | * now. Zap nr_running. After this, nr_running stays zero and |
| 6171 | * need_more_worker() and keep_working() are always true as |
| 6172 | * long as the worklist is not empty. This pool now behaves as |
| 6173 | * an unbound (in terms of concurrency management) pool which |
| 6174 | * are served by workers tied to the pool. |
| 6175 | */ |
| 6176 | pool->nr_running = 0; |
| 6177 | |
| 6178 | /* |
| 6179 | * With concurrency management just turned off, a busy |
| 6180 | * worker blocking could lead to lengthy stalls. Kick off |
| 6181 | * unbound chain execution of currently pending work items. |
| 6182 | */ |
| 6183 | kick_pool(pool); |
| 6184 | |
| 6185 | raw_spin_unlock_irq(&pool->lock); |
| 6186 | |
| 6187 | for_each_pool_worker(worker, pool) |
| 6188 | unbind_worker(worker); |
| 6189 | |
| 6190 | mutex_unlock(&wq_pool_attach_mutex); |
| 6191 | } |
| 6192 | } |
| 6193 | |
| 6194 | /** |
| 6195 | * rebind_workers - rebind all workers of a pool to the associated CPU |
| 6196 | * @pool: pool of interest |
| 6197 | * |
| 6198 | * @pool->cpu is coming online. Rebind all workers to the CPU. |
| 6199 | */ |
| 6200 | static void rebind_workers(struct worker_pool *pool) |
| 6201 | { |
| 6202 | struct worker *worker; |
| 6203 | |
| 6204 | lockdep_assert_held(&wq_pool_attach_mutex); |
| 6205 | |
| 6206 | /* |
| 6207 | * Restore CPU affinity of all workers. As all idle workers should |
| 6208 | * be on the run-queue of the associated CPU before any local |
| 6209 | * wake-ups for concurrency management happen, restore CPU affinity |
| 6210 | * of all workers first and then clear UNBOUND. As we're called |
| 6211 | * from CPU_ONLINE, the following shouldn't fail. |
| 6212 | */ |
| 6213 | for_each_pool_worker(worker, pool) { |
| 6214 | kthread_set_per_cpu(worker->task, pool->cpu); |
| 6215 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, |
| 6216 | pool_allowed_cpus(pool)) < 0); |
| 6217 | } |
| 6218 | |
| 6219 | raw_spin_lock_irq(&pool->lock); |
| 6220 | |
| 6221 | pool->flags &= ~POOL_DISASSOCIATED; |
| 6222 | |
| 6223 | for_each_pool_worker(worker, pool) { |
| 6224 | unsigned int worker_flags = worker->flags; |
| 6225 | |
| 6226 | /* |
| 6227 | * We want to clear UNBOUND but can't directly call |
| 6228 | * worker_clr_flags() or adjust nr_running. Atomically |
| 6229 | * replace UNBOUND with another NOT_RUNNING flag REBOUND. |
| 6230 | * @worker will clear REBOUND using worker_clr_flags() when |
| 6231 | * it initiates the next execution cycle thus restoring |
| 6232 | * concurrency management. Note that when or whether |
| 6233 | * @worker clears REBOUND doesn't affect correctness. |
| 6234 | * |
| 6235 | * WRITE_ONCE() is necessary because @worker->flags may be |
| 6236 | * tested without holding any lock in |
| 6237 | * wq_worker_running(). Without it, NOT_RUNNING test may |
| 6238 | * fail incorrectly leading to premature concurrency |
| 6239 | * management operations. |
| 6240 | */ |
| 6241 | WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); |
| 6242 | worker_flags |= WORKER_REBOUND; |
| 6243 | worker_flags &= ~WORKER_UNBOUND; |
| 6244 | WRITE_ONCE(worker->flags, worker_flags); |
| 6245 | } |
| 6246 | |
| 6247 | raw_spin_unlock_irq(&pool->lock); |
| 6248 | } |
| 6249 | |
| 6250 | /** |
| 6251 | * restore_unbound_workers_cpumask - restore cpumask of unbound workers |
| 6252 | * @pool: unbound pool of interest |
| 6253 | * @cpu: the CPU which is coming up |
| 6254 | * |
| 6255 | * An unbound pool may end up with a cpumask which doesn't have any online |
| 6256 | * CPUs. When a worker of such pool get scheduled, the scheduler resets |
| 6257 | * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any |
| 6258 | * online CPU before, cpus_allowed of all its workers should be restored. |
| 6259 | */ |
| 6260 | static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) |
| 6261 | { |
| 6262 | static cpumask_t cpumask; |
| 6263 | struct worker *worker; |
| 6264 | |
| 6265 | lockdep_assert_held(&wq_pool_attach_mutex); |
| 6266 | |
| 6267 | /* is @cpu allowed for @pool? */ |
| 6268 | if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) |
| 6269 | return; |
| 6270 | |
| 6271 | cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); |
| 6272 | |
| 6273 | /* as we're called from CPU_ONLINE, the following shouldn't fail */ |
| 6274 | for_each_pool_worker(worker, pool) |
| 6275 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); |
| 6276 | } |
| 6277 | |
| 6278 | int workqueue_prepare_cpu(unsigned int cpu) |
| 6279 | { |
| 6280 | struct worker_pool *pool; |
| 6281 | |
| 6282 | for_each_cpu_worker_pool(pool, cpu) { |
| 6283 | if (pool->nr_workers) |
| 6284 | continue; |
| 6285 | if (!create_worker(pool)) |
| 6286 | return -ENOMEM; |
| 6287 | } |
| 6288 | return 0; |
| 6289 | } |
| 6290 | |
| 6291 | int workqueue_online_cpu(unsigned int cpu) |
| 6292 | { |
| 6293 | struct worker_pool *pool; |
| 6294 | struct workqueue_struct *wq; |
| 6295 | int pi; |
| 6296 | |
| 6297 | mutex_lock(&wq_pool_mutex); |
| 6298 | |
| 6299 | for_each_pool(pool, pi) { |
| 6300 | /* BH pools aren't affected by hotplug */ |
| 6301 | if (pool->flags & POOL_BH) |
| 6302 | continue; |
| 6303 | |
| 6304 | mutex_lock(&wq_pool_attach_mutex); |
| 6305 | if (pool->cpu == cpu) |
| 6306 | rebind_workers(pool); |
| 6307 | else if (pool->cpu < 0) |
| 6308 | restore_unbound_workers_cpumask(pool, cpu); |
| 6309 | mutex_unlock(&wq_pool_attach_mutex); |
| 6310 | } |
| 6311 | |
| 6312 | /* update pod affinity of unbound workqueues */ |
| 6313 | list_for_each_entry(wq, &workqueues, list) { |
| 6314 | struct workqueue_attrs *attrs = wq->unbound_attrs; |
| 6315 | |
| 6316 | if (attrs) { |
| 6317 | const struct wq_pod_type *pt = wqattrs_pod_type(attrs); |
| 6318 | int tcpu; |
| 6319 | |
| 6320 | for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) |
| 6321 | wq_update_pod(wq, tcpu, cpu, true); |
| 6322 | |
| 6323 | mutex_lock(&wq->mutex); |
| 6324 | wq_update_node_max_active(wq, -1); |
| 6325 | mutex_unlock(&wq->mutex); |
| 6326 | } |
| 6327 | } |
| 6328 | |
| 6329 | mutex_unlock(&wq_pool_mutex); |
| 6330 | return 0; |
| 6331 | } |
| 6332 | |
| 6333 | int workqueue_offline_cpu(unsigned int cpu) |
| 6334 | { |
| 6335 | struct workqueue_struct *wq; |
| 6336 | |
| 6337 | /* unbinding per-cpu workers should happen on the local CPU */ |
| 6338 | if (WARN_ON(cpu != smp_processor_id())) |
| 6339 | return -1; |
| 6340 | |
| 6341 | unbind_workers(cpu); |
| 6342 | |
| 6343 | /* update pod affinity of unbound workqueues */ |
| 6344 | mutex_lock(&wq_pool_mutex); |
| 6345 | list_for_each_entry(wq, &workqueues, list) { |
| 6346 | struct workqueue_attrs *attrs = wq->unbound_attrs; |
| 6347 | |
| 6348 | if (attrs) { |
| 6349 | const struct wq_pod_type *pt = wqattrs_pod_type(attrs); |
| 6350 | int tcpu; |
| 6351 | |
| 6352 | for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) |
| 6353 | wq_update_pod(wq, tcpu, cpu, false); |
| 6354 | |
| 6355 | mutex_lock(&wq->mutex); |
| 6356 | wq_update_node_max_active(wq, cpu); |
| 6357 | mutex_unlock(&wq->mutex); |
| 6358 | } |
| 6359 | } |
| 6360 | mutex_unlock(&wq_pool_mutex); |
| 6361 | |
| 6362 | return 0; |
| 6363 | } |
| 6364 | |
| 6365 | struct work_for_cpu { |
| 6366 | struct work_struct work; |
| 6367 | long (*fn)(void *); |
| 6368 | void *arg; |
| 6369 | long ret; |
| 6370 | }; |
| 6371 | |
| 6372 | static void work_for_cpu_fn(struct work_struct *work) |
| 6373 | { |
| 6374 | struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); |
| 6375 | |
| 6376 | wfc->ret = wfc->fn(wfc->arg); |
| 6377 | } |
| 6378 | |
| 6379 | /** |
| 6380 | * work_on_cpu_key - run a function in thread context on a particular cpu |
| 6381 | * @cpu: the cpu to run on |
| 6382 | * @fn: the function to run |
| 6383 | * @arg: the function arg |
| 6384 | * @key: The lock class key for lock debugging purposes |
| 6385 | * |
| 6386 | * It is up to the caller to ensure that the cpu doesn't go offline. |
| 6387 | * The caller must not hold any locks which would prevent @fn from completing. |
| 6388 | * |
| 6389 | * Return: The value @fn returns. |
| 6390 | */ |
| 6391 | long work_on_cpu_key(int cpu, long (*fn)(void *), |
| 6392 | void *arg, struct lock_class_key *key) |
| 6393 | { |
| 6394 | struct work_for_cpu wfc = { .fn = fn, .arg = arg }; |
| 6395 | |
| 6396 | INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); |
| 6397 | schedule_work_on(cpu, &wfc.work); |
| 6398 | flush_work(&wfc.work); |
| 6399 | destroy_work_on_stack(&wfc.work); |
| 6400 | return wfc.ret; |
| 6401 | } |
| 6402 | EXPORT_SYMBOL_GPL(work_on_cpu_key); |
| 6403 | |
| 6404 | /** |
| 6405 | * work_on_cpu_safe_key - run a function in thread context on a particular cpu |
| 6406 | * @cpu: the cpu to run on |
| 6407 | * @fn: the function to run |
| 6408 | * @arg: the function argument |
| 6409 | * @key: The lock class key for lock debugging purposes |
| 6410 | * |
| 6411 | * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold |
| 6412 | * any locks which would prevent @fn from completing. |
| 6413 | * |
| 6414 | * Return: The value @fn returns. |
| 6415 | */ |
| 6416 | long work_on_cpu_safe_key(int cpu, long (*fn)(void *), |
| 6417 | void *arg, struct lock_class_key *key) |
| 6418 | { |
| 6419 | long ret = -ENODEV; |
| 6420 | |
| 6421 | cpus_read_lock(); |
| 6422 | if (cpu_online(cpu)) |
| 6423 | ret = work_on_cpu_key(cpu, fn, arg, key); |
| 6424 | cpus_read_unlock(); |
| 6425 | return ret; |
| 6426 | } |
| 6427 | EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); |
| 6428 | #endif /* CONFIG_SMP */ |
| 6429 | |
| 6430 | #ifdef CONFIG_FREEZER |
| 6431 | |
| 6432 | /** |
| 6433 | * freeze_workqueues_begin - begin freezing workqueues |
| 6434 | * |
| 6435 | * Start freezing workqueues. After this function returns, all freezable |
| 6436 | * workqueues will queue new works to their inactive_works list instead of |
| 6437 | * pool->worklist. |
| 6438 | * |
| 6439 | * CONTEXT: |
| 6440 | * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. |
| 6441 | */ |
| 6442 | void freeze_workqueues_begin(void) |
| 6443 | { |
| 6444 | struct workqueue_struct *wq; |
| 6445 | |
| 6446 | mutex_lock(&wq_pool_mutex); |
| 6447 | |
| 6448 | WARN_ON_ONCE(workqueue_freezing); |
| 6449 | workqueue_freezing = true; |
| 6450 | |
| 6451 | list_for_each_entry(wq, &workqueues, list) { |
| 6452 | mutex_lock(&wq->mutex); |
| 6453 | wq_adjust_max_active(wq); |
| 6454 | mutex_unlock(&wq->mutex); |
| 6455 | } |
| 6456 | |
| 6457 | mutex_unlock(&wq_pool_mutex); |
| 6458 | } |
| 6459 | |
| 6460 | /** |
| 6461 | * freeze_workqueues_busy - are freezable workqueues still busy? |
| 6462 | * |
| 6463 | * Check whether freezing is complete. This function must be called |
| 6464 | * between freeze_workqueues_begin() and thaw_workqueues(). |
| 6465 | * |
| 6466 | * CONTEXT: |
| 6467 | * Grabs and releases wq_pool_mutex. |
| 6468 | * |
| 6469 | * Return: |
| 6470 | * %true if some freezable workqueues are still busy. %false if freezing |
| 6471 | * is complete. |
| 6472 | */ |
| 6473 | bool freeze_workqueues_busy(void) |
| 6474 | { |
| 6475 | bool busy = false; |
| 6476 | struct workqueue_struct *wq; |
| 6477 | struct pool_workqueue *pwq; |
| 6478 | |
| 6479 | mutex_lock(&wq_pool_mutex); |
| 6480 | |
| 6481 | WARN_ON_ONCE(!workqueue_freezing); |
| 6482 | |
| 6483 | list_for_each_entry(wq, &workqueues, list) { |
| 6484 | if (!(wq->flags & WQ_FREEZABLE)) |
| 6485 | continue; |
| 6486 | /* |
| 6487 | * nr_active is monotonically decreasing. It's safe |
| 6488 | * to peek without lock. |
| 6489 | */ |
| 6490 | rcu_read_lock(); |
| 6491 | for_each_pwq(pwq, wq) { |
| 6492 | WARN_ON_ONCE(pwq->nr_active < 0); |
| 6493 | if (pwq->nr_active) { |
| 6494 | busy = true; |
| 6495 | rcu_read_unlock(); |
| 6496 | goto out_unlock; |
| 6497 | } |
| 6498 | } |
| 6499 | rcu_read_unlock(); |
| 6500 | } |
| 6501 | out_unlock: |
| 6502 | mutex_unlock(&wq_pool_mutex); |
| 6503 | return busy; |
| 6504 | } |
| 6505 | |
| 6506 | /** |
| 6507 | * thaw_workqueues - thaw workqueues |
| 6508 | * |
| 6509 | * Thaw workqueues. Normal queueing is restored and all collected |
| 6510 | * frozen works are transferred to their respective pool worklists. |
| 6511 | * |
| 6512 | * CONTEXT: |
| 6513 | * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. |
| 6514 | */ |
| 6515 | void thaw_workqueues(void) |
| 6516 | { |
| 6517 | struct workqueue_struct *wq; |
| 6518 | |
| 6519 | mutex_lock(&wq_pool_mutex); |
| 6520 | |
| 6521 | if (!workqueue_freezing) |
| 6522 | goto out_unlock; |
| 6523 | |
| 6524 | workqueue_freezing = false; |
| 6525 | |
| 6526 | /* restore max_active and repopulate worklist */ |
| 6527 | list_for_each_entry(wq, &workqueues, list) { |
| 6528 | mutex_lock(&wq->mutex); |
| 6529 | wq_adjust_max_active(wq); |
| 6530 | mutex_unlock(&wq->mutex); |
| 6531 | } |
| 6532 | |
| 6533 | out_unlock: |
| 6534 | mutex_unlock(&wq_pool_mutex); |
| 6535 | } |
| 6536 | #endif /* CONFIG_FREEZER */ |
| 6537 | |
| 6538 | static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) |
| 6539 | { |
| 6540 | LIST_HEAD(ctxs); |
| 6541 | int ret = 0; |
| 6542 | struct workqueue_struct *wq; |
| 6543 | struct apply_wqattrs_ctx *ctx, *n; |
| 6544 | |
| 6545 | lockdep_assert_held(&wq_pool_mutex); |
| 6546 | |
| 6547 | list_for_each_entry(wq, &workqueues, list) { |
| 6548 | if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) |
| 6549 | continue; |
| 6550 | |
| 6551 | ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); |
| 6552 | if (IS_ERR(ctx)) { |
| 6553 | ret = PTR_ERR(ctx); |
| 6554 | break; |
| 6555 | } |
| 6556 | |
| 6557 | list_add_tail(&ctx->list, &ctxs); |
| 6558 | } |
| 6559 | |
| 6560 | list_for_each_entry_safe(ctx, n, &ctxs, list) { |
| 6561 | if (!ret) |
| 6562 | apply_wqattrs_commit(ctx); |
| 6563 | apply_wqattrs_cleanup(ctx); |
| 6564 | } |
| 6565 | |
| 6566 | if (!ret) { |
| 6567 | mutex_lock(&wq_pool_attach_mutex); |
| 6568 | cpumask_copy(wq_unbound_cpumask, unbound_cpumask); |
| 6569 | mutex_unlock(&wq_pool_attach_mutex); |
| 6570 | } |
| 6571 | return ret; |
| 6572 | } |
| 6573 | |
| 6574 | /** |
| 6575 | * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask |
| 6576 | * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask |
| 6577 | * |
| 6578 | * This function can be called from cpuset code to provide a set of isolated |
| 6579 | * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold |
| 6580 | * either cpus_read_lock or cpus_write_lock. |
| 6581 | */ |
| 6582 | int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) |
| 6583 | { |
| 6584 | cpumask_var_t cpumask; |
| 6585 | int ret = 0; |
| 6586 | |
| 6587 | if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) |
| 6588 | return -ENOMEM; |
| 6589 | |
| 6590 | lockdep_assert_cpus_held(); |
| 6591 | mutex_lock(&wq_pool_mutex); |
| 6592 | |
| 6593 | /* Save the current isolated cpumask & export it via sysfs */ |
| 6594 | cpumask_copy(wq_isolated_cpumask, exclude_cpumask); |
| 6595 | |
| 6596 | /* |
| 6597 | * If the operation fails, it will fall back to |
| 6598 | * wq_requested_unbound_cpumask which is initially set to |
| 6599 | * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten |
| 6600 | * by any subsequent write to workqueue/cpumask sysfs file. |
| 6601 | */ |
| 6602 | if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) |
| 6603 | cpumask_copy(cpumask, wq_requested_unbound_cpumask); |
| 6604 | if (!cpumask_equal(cpumask, wq_unbound_cpumask)) |
| 6605 | ret = workqueue_apply_unbound_cpumask(cpumask); |
| 6606 | |
| 6607 | mutex_unlock(&wq_pool_mutex); |
| 6608 | free_cpumask_var(cpumask); |
| 6609 | return ret; |
| 6610 | } |
| 6611 | |
| 6612 | static int parse_affn_scope(const char *val) |
| 6613 | { |
| 6614 | int i; |
| 6615 | |
| 6616 | for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { |
| 6617 | if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) |
| 6618 | return i; |
| 6619 | } |
| 6620 | return -EINVAL; |
| 6621 | } |
| 6622 | |
| 6623 | static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) |
| 6624 | { |
| 6625 | struct workqueue_struct *wq; |
| 6626 | int affn, cpu; |
| 6627 | |
| 6628 | affn = parse_affn_scope(val); |
| 6629 | if (affn < 0) |
| 6630 | return affn; |
| 6631 | if (affn == WQ_AFFN_DFL) |
| 6632 | return -EINVAL; |
| 6633 | |
| 6634 | cpus_read_lock(); |
| 6635 | mutex_lock(&wq_pool_mutex); |
| 6636 | |
| 6637 | wq_affn_dfl = affn; |
| 6638 | |
| 6639 | list_for_each_entry(wq, &workqueues, list) { |
| 6640 | for_each_online_cpu(cpu) { |
| 6641 | wq_update_pod(wq, cpu, cpu, true); |
| 6642 | } |
| 6643 | } |
| 6644 | |
| 6645 | mutex_unlock(&wq_pool_mutex); |
| 6646 | cpus_read_unlock(); |
| 6647 | |
| 6648 | return 0; |
| 6649 | } |
| 6650 | |
| 6651 | static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) |
| 6652 | { |
| 6653 | return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); |
| 6654 | } |
| 6655 | |
| 6656 | static const struct kernel_param_ops wq_affn_dfl_ops = { |
| 6657 | .set = wq_affn_dfl_set, |
| 6658 | .get = wq_affn_dfl_get, |
| 6659 | }; |
| 6660 | |
| 6661 | module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); |
| 6662 | |
| 6663 | #ifdef CONFIG_SYSFS |
| 6664 | /* |
| 6665 | * Workqueues with WQ_SYSFS flag set is visible to userland via |
| 6666 | * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the |
| 6667 | * following attributes. |
| 6668 | * |
| 6669 | * per_cpu RO bool : whether the workqueue is per-cpu or unbound |
| 6670 | * max_active RW int : maximum number of in-flight work items |
| 6671 | * |
| 6672 | * Unbound workqueues have the following extra attributes. |
| 6673 | * |
| 6674 | * nice RW int : nice value of the workers |
| 6675 | * cpumask RW mask : bitmask of allowed CPUs for the workers |
| 6676 | * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) |
| 6677 | * affinity_strict RW bool : worker CPU affinity is strict |
| 6678 | */ |
| 6679 | struct wq_device { |
| 6680 | struct workqueue_struct *wq; |
| 6681 | struct device dev; |
| 6682 | }; |
| 6683 | |
| 6684 | static struct workqueue_struct *dev_to_wq(struct device *dev) |
| 6685 | { |
| 6686 | struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); |
| 6687 | |
| 6688 | return wq_dev->wq; |
| 6689 | } |
| 6690 | |
| 6691 | static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, |
| 6692 | char *buf) |
| 6693 | { |
| 6694 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6695 | |
| 6696 | return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); |
| 6697 | } |
| 6698 | static DEVICE_ATTR_RO(per_cpu); |
| 6699 | |
| 6700 | static ssize_t max_active_show(struct device *dev, |
| 6701 | struct device_attribute *attr, char *buf) |
| 6702 | { |
| 6703 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6704 | |
| 6705 | return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); |
| 6706 | } |
| 6707 | |
| 6708 | static ssize_t max_active_store(struct device *dev, |
| 6709 | struct device_attribute *attr, const char *buf, |
| 6710 | size_t count) |
| 6711 | { |
| 6712 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6713 | int val; |
| 6714 | |
| 6715 | if (sscanf(buf, "%d", &val) != 1 || val <= 0) |
| 6716 | return -EINVAL; |
| 6717 | |
| 6718 | workqueue_set_max_active(wq, val); |
| 6719 | return count; |
| 6720 | } |
| 6721 | static DEVICE_ATTR_RW(max_active); |
| 6722 | |
| 6723 | static struct attribute *wq_sysfs_attrs[] = { |
| 6724 | &dev_attr_per_cpu.attr, |
| 6725 | &dev_attr_max_active.attr, |
| 6726 | NULL, |
| 6727 | }; |
| 6728 | ATTRIBUTE_GROUPS(wq_sysfs); |
| 6729 | |
| 6730 | static void apply_wqattrs_lock(void) |
| 6731 | { |
| 6732 | /* CPUs should stay stable across pwq creations and installations */ |
| 6733 | cpus_read_lock(); |
| 6734 | mutex_lock(&wq_pool_mutex); |
| 6735 | } |
| 6736 | |
| 6737 | static void apply_wqattrs_unlock(void) |
| 6738 | { |
| 6739 | mutex_unlock(&wq_pool_mutex); |
| 6740 | cpus_read_unlock(); |
| 6741 | } |
| 6742 | |
| 6743 | static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, |
| 6744 | char *buf) |
| 6745 | { |
| 6746 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6747 | int written; |
| 6748 | |
| 6749 | mutex_lock(&wq->mutex); |
| 6750 | written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); |
| 6751 | mutex_unlock(&wq->mutex); |
| 6752 | |
| 6753 | return written; |
| 6754 | } |
| 6755 | |
| 6756 | /* prepare workqueue_attrs for sysfs store operations */ |
| 6757 | static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) |
| 6758 | { |
| 6759 | struct workqueue_attrs *attrs; |
| 6760 | |
| 6761 | lockdep_assert_held(&wq_pool_mutex); |
| 6762 | |
| 6763 | attrs = alloc_workqueue_attrs(); |
| 6764 | if (!attrs) |
| 6765 | return NULL; |
| 6766 | |
| 6767 | copy_workqueue_attrs(attrs, wq->unbound_attrs); |
| 6768 | return attrs; |
| 6769 | } |
| 6770 | |
| 6771 | static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, |
| 6772 | const char *buf, size_t count) |
| 6773 | { |
| 6774 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6775 | struct workqueue_attrs *attrs; |
| 6776 | int ret = -ENOMEM; |
| 6777 | |
| 6778 | apply_wqattrs_lock(); |
| 6779 | |
| 6780 | attrs = wq_sysfs_prep_attrs(wq); |
| 6781 | if (!attrs) |
| 6782 | goto out_unlock; |
| 6783 | |
| 6784 | if (sscanf(buf, "%d", &attrs->nice) == 1 && |
| 6785 | attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) |
| 6786 | ret = apply_workqueue_attrs_locked(wq, attrs); |
| 6787 | else |
| 6788 | ret = -EINVAL; |
| 6789 | |
| 6790 | out_unlock: |
| 6791 | apply_wqattrs_unlock(); |
| 6792 | free_workqueue_attrs(attrs); |
| 6793 | return ret ?: count; |
| 6794 | } |
| 6795 | |
| 6796 | static ssize_t wq_cpumask_show(struct device *dev, |
| 6797 | struct device_attribute *attr, char *buf) |
| 6798 | { |
| 6799 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6800 | int written; |
| 6801 | |
| 6802 | mutex_lock(&wq->mutex); |
| 6803 | written = scnprintf(buf, PAGE_SIZE, "%*pb\n", |
| 6804 | cpumask_pr_args(wq->unbound_attrs->cpumask)); |
| 6805 | mutex_unlock(&wq->mutex); |
| 6806 | return written; |
| 6807 | } |
| 6808 | |
| 6809 | static ssize_t wq_cpumask_store(struct device *dev, |
| 6810 | struct device_attribute *attr, |
| 6811 | const char *buf, size_t count) |
| 6812 | { |
| 6813 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6814 | struct workqueue_attrs *attrs; |
| 6815 | int ret = -ENOMEM; |
| 6816 | |
| 6817 | apply_wqattrs_lock(); |
| 6818 | |
| 6819 | attrs = wq_sysfs_prep_attrs(wq); |
| 6820 | if (!attrs) |
| 6821 | goto out_unlock; |
| 6822 | |
| 6823 | ret = cpumask_parse(buf, attrs->cpumask); |
| 6824 | if (!ret) |
| 6825 | ret = apply_workqueue_attrs_locked(wq, attrs); |
| 6826 | |
| 6827 | out_unlock: |
| 6828 | apply_wqattrs_unlock(); |
| 6829 | free_workqueue_attrs(attrs); |
| 6830 | return ret ?: count; |
| 6831 | } |
| 6832 | |
| 6833 | static ssize_t wq_affn_scope_show(struct device *dev, |
| 6834 | struct device_attribute *attr, char *buf) |
| 6835 | { |
| 6836 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6837 | int written; |
| 6838 | |
| 6839 | mutex_lock(&wq->mutex); |
| 6840 | if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) |
| 6841 | written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", |
| 6842 | wq_affn_names[WQ_AFFN_DFL], |
| 6843 | wq_affn_names[wq_affn_dfl]); |
| 6844 | else |
| 6845 | written = scnprintf(buf, PAGE_SIZE, "%s\n", |
| 6846 | wq_affn_names[wq->unbound_attrs->affn_scope]); |
| 6847 | mutex_unlock(&wq->mutex); |
| 6848 | |
| 6849 | return written; |
| 6850 | } |
| 6851 | |
| 6852 | static ssize_t wq_affn_scope_store(struct device *dev, |
| 6853 | struct device_attribute *attr, |
| 6854 | const char *buf, size_t count) |
| 6855 | { |
| 6856 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6857 | struct workqueue_attrs *attrs; |
| 6858 | int affn, ret = -ENOMEM; |
| 6859 | |
| 6860 | affn = parse_affn_scope(buf); |
| 6861 | if (affn < 0) |
| 6862 | return affn; |
| 6863 | |
| 6864 | apply_wqattrs_lock(); |
| 6865 | attrs = wq_sysfs_prep_attrs(wq); |
| 6866 | if (attrs) { |
| 6867 | attrs->affn_scope = affn; |
| 6868 | ret = apply_workqueue_attrs_locked(wq, attrs); |
| 6869 | } |
| 6870 | apply_wqattrs_unlock(); |
| 6871 | free_workqueue_attrs(attrs); |
| 6872 | return ret ?: count; |
| 6873 | } |
| 6874 | |
| 6875 | static ssize_t wq_affinity_strict_show(struct device *dev, |
| 6876 | struct device_attribute *attr, char *buf) |
| 6877 | { |
| 6878 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6879 | |
| 6880 | return scnprintf(buf, PAGE_SIZE, "%d\n", |
| 6881 | wq->unbound_attrs->affn_strict); |
| 6882 | } |
| 6883 | |
| 6884 | static ssize_t wq_affinity_strict_store(struct device *dev, |
| 6885 | struct device_attribute *attr, |
| 6886 | const char *buf, size_t count) |
| 6887 | { |
| 6888 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 6889 | struct workqueue_attrs *attrs; |
| 6890 | int v, ret = -ENOMEM; |
| 6891 | |
| 6892 | if (sscanf(buf, "%d", &v) != 1) |
| 6893 | return -EINVAL; |
| 6894 | |
| 6895 | apply_wqattrs_lock(); |
| 6896 | attrs = wq_sysfs_prep_attrs(wq); |
| 6897 | if (attrs) { |
| 6898 | attrs->affn_strict = (bool)v; |
| 6899 | ret = apply_workqueue_attrs_locked(wq, attrs); |
| 6900 | } |
| 6901 | apply_wqattrs_unlock(); |
| 6902 | free_workqueue_attrs(attrs); |
| 6903 | return ret ?: count; |
| 6904 | } |
| 6905 | |
| 6906 | static struct device_attribute wq_sysfs_unbound_attrs[] = { |
| 6907 | __ATTR(nice, 0644, wq_nice_show, wq_nice_store), |
| 6908 | __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), |
| 6909 | __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), |
| 6910 | __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), |
| 6911 | __ATTR_NULL, |
| 6912 | }; |
| 6913 | |
| 6914 | static struct bus_type wq_subsys = { |
| 6915 | .name = "workqueue", |
| 6916 | .dev_groups = wq_sysfs_groups, |
| 6917 | }; |
| 6918 | |
| 6919 | /** |
| 6920 | * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask |
| 6921 | * @cpumask: the cpumask to set |
| 6922 | * |
| 6923 | * The low-level workqueues cpumask is a global cpumask that limits |
| 6924 | * the affinity of all unbound workqueues. This function check the @cpumask |
| 6925 | * and apply it to all unbound workqueues and updates all pwqs of them. |
| 6926 | * |
| 6927 | * Return: 0 - Success |
| 6928 | * -EINVAL - Invalid @cpumask |
| 6929 | * -ENOMEM - Failed to allocate memory for attrs or pwqs. |
| 6930 | */ |
| 6931 | static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) |
| 6932 | { |
| 6933 | int ret = -EINVAL; |
| 6934 | |
| 6935 | /* |
| 6936 | * Not excluding isolated cpus on purpose. |
| 6937 | * If the user wishes to include them, we allow that. |
| 6938 | */ |
| 6939 | cpumask_and(cpumask, cpumask, cpu_possible_mask); |
| 6940 | if (!cpumask_empty(cpumask)) { |
| 6941 | apply_wqattrs_lock(); |
| 6942 | cpumask_copy(wq_requested_unbound_cpumask, cpumask); |
| 6943 | if (cpumask_equal(cpumask, wq_unbound_cpumask)) { |
| 6944 | ret = 0; |
| 6945 | goto out_unlock; |
| 6946 | } |
| 6947 | |
| 6948 | ret = workqueue_apply_unbound_cpumask(cpumask); |
| 6949 | |
| 6950 | out_unlock: |
| 6951 | apply_wqattrs_unlock(); |
| 6952 | } |
| 6953 | |
| 6954 | return ret; |
| 6955 | } |
| 6956 | |
| 6957 | static ssize_t __wq_cpumask_show(struct device *dev, |
| 6958 | struct device_attribute *attr, char *buf, cpumask_var_t mask) |
| 6959 | { |
| 6960 | int written; |
| 6961 | |
| 6962 | mutex_lock(&wq_pool_mutex); |
| 6963 | written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); |
| 6964 | mutex_unlock(&wq_pool_mutex); |
| 6965 | |
| 6966 | return written; |
| 6967 | } |
| 6968 | |
| 6969 | static ssize_t wq_unbound_cpumask_show(struct device *dev, |
| 6970 | struct device_attribute *attr, char *buf) |
| 6971 | { |
| 6972 | return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); |
| 6973 | } |
| 6974 | |
| 6975 | static ssize_t wq_requested_cpumask_show(struct device *dev, |
| 6976 | struct device_attribute *attr, char *buf) |
| 6977 | { |
| 6978 | return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); |
| 6979 | } |
| 6980 | |
| 6981 | static ssize_t wq_isolated_cpumask_show(struct device *dev, |
| 6982 | struct device_attribute *attr, char *buf) |
| 6983 | { |
| 6984 | return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); |
| 6985 | } |
| 6986 | |
| 6987 | static ssize_t wq_unbound_cpumask_store(struct device *dev, |
| 6988 | struct device_attribute *attr, const char *buf, size_t count) |
| 6989 | { |
| 6990 | cpumask_var_t cpumask; |
| 6991 | int ret; |
| 6992 | |
| 6993 | if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) |
| 6994 | return -ENOMEM; |
| 6995 | |
| 6996 | ret = cpumask_parse(buf, cpumask); |
| 6997 | if (!ret) |
| 6998 | ret = workqueue_set_unbound_cpumask(cpumask); |
| 6999 | |
| 7000 | free_cpumask_var(cpumask); |
| 7001 | return ret ? ret : count; |
| 7002 | } |
| 7003 | |
| 7004 | static struct device_attribute wq_sysfs_cpumask_attrs[] = { |
| 7005 | __ATTR(cpumask, 0644, wq_unbound_cpumask_show, |
| 7006 | wq_unbound_cpumask_store), |
| 7007 | __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), |
| 7008 | __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), |
| 7009 | __ATTR_NULL, |
| 7010 | }; |
| 7011 | |
| 7012 | static int __init wq_sysfs_init(void) |
| 7013 | { |
| 7014 | struct device *dev_root; |
| 7015 | int err; |
| 7016 | |
| 7017 | err = subsys_virtual_register(&wq_subsys, NULL); |
| 7018 | if (err) |
| 7019 | return err; |
| 7020 | |
| 7021 | dev_root = bus_get_dev_root(&wq_subsys); |
| 7022 | if (dev_root) { |
| 7023 | struct device_attribute *attr; |
| 7024 | |
| 7025 | for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { |
| 7026 | err = device_create_file(dev_root, attr); |
| 7027 | if (err) |
| 7028 | break; |
| 7029 | } |
| 7030 | put_device(dev_root); |
| 7031 | } |
| 7032 | return err; |
| 7033 | } |
| 7034 | core_initcall(wq_sysfs_init); |
| 7035 | |
| 7036 | static void wq_device_release(struct device *dev) |
| 7037 | { |
| 7038 | struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); |
| 7039 | |
| 7040 | kfree(wq_dev); |
| 7041 | } |
| 7042 | |
| 7043 | /** |
| 7044 | * workqueue_sysfs_register - make a workqueue visible in sysfs |
| 7045 | * @wq: the workqueue to register |
| 7046 | * |
| 7047 | * Expose @wq in sysfs under /sys/bus/workqueue/devices. |
| 7048 | * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set |
| 7049 | * which is the preferred method. |
| 7050 | * |
| 7051 | * Workqueue user should use this function directly iff it wants to apply |
| 7052 | * workqueue_attrs before making the workqueue visible in sysfs; otherwise, |
| 7053 | * apply_workqueue_attrs() may race against userland updating the |
| 7054 | * attributes. |
| 7055 | * |
| 7056 | * Return: 0 on success, -errno on failure. |
| 7057 | */ |
| 7058 | int workqueue_sysfs_register(struct workqueue_struct *wq) |
| 7059 | { |
| 7060 | struct wq_device *wq_dev; |
| 7061 | int ret; |
| 7062 | |
| 7063 | /* |
| 7064 | * Adjusting max_active breaks ordering guarantee. Disallow exposing |
| 7065 | * ordered workqueues. |
| 7066 | */ |
| 7067 | if (WARN_ON(wq->flags & __WQ_ORDERED)) |
| 7068 | return -EINVAL; |
| 7069 | |
| 7070 | wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); |
| 7071 | if (!wq_dev) |
| 7072 | return -ENOMEM; |
| 7073 | |
| 7074 | wq_dev->wq = wq; |
| 7075 | wq_dev->dev.bus = &wq_subsys; |
| 7076 | wq_dev->dev.release = wq_device_release; |
| 7077 | dev_set_name(&wq_dev->dev, "%s", wq->name); |
| 7078 | |
| 7079 | /* |
| 7080 | * unbound_attrs are created separately. Suppress uevent until |
| 7081 | * everything is ready. |
| 7082 | */ |
| 7083 | dev_set_uevent_suppress(&wq_dev->dev, true); |
| 7084 | |
| 7085 | ret = device_register(&wq_dev->dev); |
| 7086 | if (ret) { |
| 7087 | put_device(&wq_dev->dev); |
| 7088 | wq->wq_dev = NULL; |
| 7089 | return ret; |
| 7090 | } |
| 7091 | |
| 7092 | if (wq->flags & WQ_UNBOUND) { |
| 7093 | struct device_attribute *attr; |
| 7094 | |
| 7095 | for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { |
| 7096 | ret = device_create_file(&wq_dev->dev, attr); |
| 7097 | if (ret) { |
| 7098 | device_unregister(&wq_dev->dev); |
| 7099 | wq->wq_dev = NULL; |
| 7100 | return ret; |
| 7101 | } |
| 7102 | } |
| 7103 | } |
| 7104 | |
| 7105 | dev_set_uevent_suppress(&wq_dev->dev, false); |
| 7106 | kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); |
| 7107 | return 0; |
| 7108 | } |
| 7109 | |
| 7110 | /** |
| 7111 | * workqueue_sysfs_unregister - undo workqueue_sysfs_register() |
| 7112 | * @wq: the workqueue to unregister |
| 7113 | * |
| 7114 | * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. |
| 7115 | */ |
| 7116 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq) |
| 7117 | { |
| 7118 | struct wq_device *wq_dev = wq->wq_dev; |
| 7119 | |
| 7120 | if (!wq->wq_dev) |
| 7121 | return; |
| 7122 | |
| 7123 | wq->wq_dev = NULL; |
| 7124 | device_unregister(&wq_dev->dev); |
| 7125 | } |
| 7126 | #else /* CONFIG_SYSFS */ |
| 7127 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } |
| 7128 | #endif /* CONFIG_SYSFS */ |
| 7129 | |
| 7130 | /* |
| 7131 | * Workqueue watchdog. |
| 7132 | * |
| 7133 | * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal |
| 7134 | * flush dependency, a concurrency managed work item which stays RUNNING |
| 7135 | * indefinitely. Workqueue stalls can be very difficult to debug as the |
| 7136 | * usual warning mechanisms don't trigger and internal workqueue state is |
| 7137 | * largely opaque. |
| 7138 | * |
| 7139 | * Workqueue watchdog monitors all worker pools periodically and dumps |
| 7140 | * state if some pools failed to make forward progress for a while where |
| 7141 | * forward progress is defined as the first item on ->worklist changing. |
| 7142 | * |
| 7143 | * This mechanism is controlled through the kernel parameter |
| 7144 | * "workqueue.watchdog_thresh" which can be updated at runtime through the |
| 7145 | * corresponding sysfs parameter file. |
| 7146 | */ |
| 7147 | #ifdef CONFIG_WQ_WATCHDOG |
| 7148 | |
| 7149 | static unsigned long wq_watchdog_thresh = 30; |
| 7150 | static struct timer_list wq_watchdog_timer; |
| 7151 | |
| 7152 | static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; |
| 7153 | static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; |
| 7154 | |
| 7155 | /* |
| 7156 | * Show workers that might prevent the processing of pending work items. |
| 7157 | * The only candidates are CPU-bound workers in the running state. |
| 7158 | * Pending work items should be handled by another idle worker |
| 7159 | * in all other situations. |
| 7160 | */ |
| 7161 | static void show_cpu_pool_hog(struct worker_pool *pool) |
| 7162 | { |
| 7163 | struct worker *worker; |
| 7164 | unsigned long flags; |
| 7165 | int bkt; |
| 7166 | |
| 7167 | raw_spin_lock_irqsave(&pool->lock, flags); |
| 7168 | |
| 7169 | hash_for_each(pool->busy_hash, bkt, worker, hentry) { |
| 7170 | if (task_is_running(worker->task)) { |
| 7171 | /* |
| 7172 | * Defer printing to avoid deadlocks in console |
| 7173 | * drivers that queue work while holding locks |
| 7174 | * also taken in their write paths. |
| 7175 | */ |
| 7176 | printk_deferred_enter(); |
| 7177 | |
| 7178 | pr_info("pool %d:\n", pool->id); |
| 7179 | sched_show_task(worker->task); |
| 7180 | |
| 7181 | printk_deferred_exit(); |
| 7182 | } |
| 7183 | } |
| 7184 | |
| 7185 | raw_spin_unlock_irqrestore(&pool->lock, flags); |
| 7186 | } |
| 7187 | |
| 7188 | static void show_cpu_pools_hogs(void) |
| 7189 | { |
| 7190 | struct worker_pool *pool; |
| 7191 | int pi; |
| 7192 | |
| 7193 | pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); |
| 7194 | |
| 7195 | rcu_read_lock(); |
| 7196 | |
| 7197 | for_each_pool(pool, pi) { |
| 7198 | if (pool->cpu_stall) |
| 7199 | show_cpu_pool_hog(pool); |
| 7200 | |
| 7201 | } |
| 7202 | |
| 7203 | rcu_read_unlock(); |
| 7204 | } |
| 7205 | |
| 7206 | static void wq_watchdog_reset_touched(void) |
| 7207 | { |
| 7208 | int cpu; |
| 7209 | |
| 7210 | wq_watchdog_touched = jiffies; |
| 7211 | for_each_possible_cpu(cpu) |
| 7212 | per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; |
| 7213 | } |
| 7214 | |
| 7215 | static void wq_watchdog_timer_fn(struct timer_list *unused) |
| 7216 | { |
| 7217 | unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; |
| 7218 | bool lockup_detected = false; |
| 7219 | bool cpu_pool_stall = false; |
| 7220 | unsigned long now = jiffies; |
| 7221 | struct worker_pool *pool; |
| 7222 | int pi; |
| 7223 | |
| 7224 | if (!thresh) |
| 7225 | return; |
| 7226 | |
| 7227 | rcu_read_lock(); |
| 7228 | |
| 7229 | for_each_pool(pool, pi) { |
| 7230 | unsigned long pool_ts, touched, ts; |
| 7231 | |
| 7232 | pool->cpu_stall = false; |
| 7233 | if (list_empty(&pool->worklist)) |
| 7234 | continue; |
| 7235 | |
| 7236 | /* |
| 7237 | * If a virtual machine is stopped by the host it can look to |
| 7238 | * the watchdog like a stall. |
| 7239 | */ |
| 7240 | kvm_check_and_clear_guest_paused(); |
| 7241 | |
| 7242 | /* get the latest of pool and touched timestamps */ |
| 7243 | if (pool->cpu >= 0) |
| 7244 | touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); |
| 7245 | else |
| 7246 | touched = READ_ONCE(wq_watchdog_touched); |
| 7247 | pool_ts = READ_ONCE(pool->watchdog_ts); |
| 7248 | |
| 7249 | if (time_after(pool_ts, touched)) |
| 7250 | ts = pool_ts; |
| 7251 | else |
| 7252 | ts = touched; |
| 7253 | |
| 7254 | /* did we stall? */ |
| 7255 | if (time_after(now, ts + thresh)) { |
| 7256 | lockup_detected = true; |
| 7257 | if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) { |
| 7258 | pool->cpu_stall = true; |
| 7259 | cpu_pool_stall = true; |
| 7260 | } |
| 7261 | pr_emerg("BUG: workqueue lockup - pool"); |
| 7262 | pr_cont_pool_info(pool); |
| 7263 | pr_cont(" stuck for %us!\n", |
| 7264 | jiffies_to_msecs(now - pool_ts) / 1000); |
| 7265 | } |
| 7266 | |
| 7267 | |
| 7268 | } |
| 7269 | |
| 7270 | rcu_read_unlock(); |
| 7271 | |
| 7272 | if (lockup_detected) |
| 7273 | show_all_workqueues(); |
| 7274 | |
| 7275 | if (cpu_pool_stall) |
| 7276 | show_cpu_pools_hogs(); |
| 7277 | |
| 7278 | wq_watchdog_reset_touched(); |
| 7279 | mod_timer(&wq_watchdog_timer, jiffies + thresh); |
| 7280 | } |
| 7281 | |
| 7282 | notrace void wq_watchdog_touch(int cpu) |
| 7283 | { |
| 7284 | if (cpu >= 0) |
| 7285 | per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; |
| 7286 | |
| 7287 | wq_watchdog_touched = jiffies; |
| 7288 | } |
| 7289 | |
| 7290 | static void wq_watchdog_set_thresh(unsigned long thresh) |
| 7291 | { |
| 7292 | wq_watchdog_thresh = 0; |
| 7293 | del_timer_sync(&wq_watchdog_timer); |
| 7294 | |
| 7295 | if (thresh) { |
| 7296 | wq_watchdog_thresh = thresh; |
| 7297 | wq_watchdog_reset_touched(); |
| 7298 | mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); |
| 7299 | } |
| 7300 | } |
| 7301 | |
| 7302 | static int wq_watchdog_param_set_thresh(const char *val, |
| 7303 | const struct kernel_param *kp) |
| 7304 | { |
| 7305 | unsigned long thresh; |
| 7306 | int ret; |
| 7307 | |
| 7308 | ret = kstrtoul(val, 0, &thresh); |
| 7309 | if (ret) |
| 7310 | return ret; |
| 7311 | |
| 7312 | if (system_wq) |
| 7313 | wq_watchdog_set_thresh(thresh); |
| 7314 | else |
| 7315 | wq_watchdog_thresh = thresh; |
| 7316 | |
| 7317 | return 0; |
| 7318 | } |
| 7319 | |
| 7320 | static const struct kernel_param_ops wq_watchdog_thresh_ops = { |
| 7321 | .set = wq_watchdog_param_set_thresh, |
| 7322 | .get = param_get_ulong, |
| 7323 | }; |
| 7324 | |
| 7325 | module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, |
| 7326 | 0644); |
| 7327 | |
| 7328 | static void wq_watchdog_init(void) |
| 7329 | { |
| 7330 | timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); |
| 7331 | wq_watchdog_set_thresh(wq_watchdog_thresh); |
| 7332 | } |
| 7333 | |
| 7334 | #else /* CONFIG_WQ_WATCHDOG */ |
| 7335 | |
| 7336 | static inline void wq_watchdog_init(void) { } |
| 7337 | |
| 7338 | #endif /* CONFIG_WQ_WATCHDOG */ |
| 7339 | |
| 7340 | static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) |
| 7341 | { |
| 7342 | if (!cpumask_intersects(wq_unbound_cpumask, mask)) { |
| 7343 | pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", |
| 7344 | cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); |
| 7345 | return; |
| 7346 | } |
| 7347 | |
| 7348 | cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); |
| 7349 | } |
| 7350 | |
| 7351 | static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) |
| 7352 | { |
| 7353 | BUG_ON(init_worker_pool(pool)); |
| 7354 | pool->cpu = cpu; |
| 7355 | cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); |
| 7356 | cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); |
| 7357 | pool->attrs->nice = nice; |
| 7358 | pool->attrs->affn_strict = true; |
| 7359 | pool->node = cpu_to_node(cpu); |
| 7360 | |
| 7361 | /* alloc pool ID */ |
| 7362 | mutex_lock(&wq_pool_mutex); |
| 7363 | BUG_ON(worker_pool_assign_id(pool)); |
| 7364 | mutex_unlock(&wq_pool_mutex); |
| 7365 | } |
| 7366 | |
| 7367 | /** |
| 7368 | * workqueue_init_early - early init for workqueue subsystem |
| 7369 | * |
| 7370 | * This is the first step of three-staged workqueue subsystem initialization and |
| 7371 | * invoked as soon as the bare basics - memory allocation, cpumasks and idr are |
| 7372 | * up. It sets up all the data structures and system workqueues and allows early |
| 7373 | * boot code to create workqueues and queue/cancel work items. Actual work item |
| 7374 | * execution starts only after kthreads can be created and scheduled right |
| 7375 | * before early initcalls. |
| 7376 | */ |
| 7377 | void __init workqueue_init_early(void) |
| 7378 | { |
| 7379 | struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; |
| 7380 | int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; |
| 7381 | int i, cpu; |
| 7382 | |
| 7383 | BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); |
| 7384 | |
| 7385 | BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); |
| 7386 | BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); |
| 7387 | BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); |
| 7388 | |
| 7389 | cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); |
| 7390 | restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); |
| 7391 | restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); |
| 7392 | if (!cpumask_empty(&wq_cmdline_cpumask)) |
| 7393 | restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); |
| 7394 | |
| 7395 | cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); |
| 7396 | |
| 7397 | pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); |
| 7398 | |
| 7399 | wq_update_pod_attrs_buf = alloc_workqueue_attrs(); |
| 7400 | BUG_ON(!wq_update_pod_attrs_buf); |
| 7401 | |
| 7402 | /* |
| 7403 | * If nohz_full is enabled, set power efficient workqueue as unbound. |
| 7404 | * This allows workqueue items to be moved to HK CPUs. |
| 7405 | */ |
| 7406 | if (housekeeping_enabled(HK_TYPE_TICK)) |
| 7407 | wq_power_efficient = true; |
| 7408 | |
| 7409 | /* initialize WQ_AFFN_SYSTEM pods */ |
| 7410 | pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); |
| 7411 | pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); |
| 7412 | pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); |
| 7413 | BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); |
| 7414 | |
| 7415 | BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); |
| 7416 | |
| 7417 | pt->nr_pods = 1; |
| 7418 | cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); |
| 7419 | pt->pod_node[0] = NUMA_NO_NODE; |
| 7420 | pt->cpu_pod[0] = 0; |
| 7421 | |
| 7422 | /* initialize BH and CPU pools */ |
| 7423 | for_each_possible_cpu(cpu) { |
| 7424 | struct worker_pool *pool; |
| 7425 | |
| 7426 | i = 0; |
| 7427 | for_each_bh_worker_pool(pool, cpu) { |
| 7428 | init_cpu_worker_pool(pool, cpu, std_nice[i++]); |
| 7429 | pool->flags |= POOL_BH; |
| 7430 | } |
| 7431 | |
| 7432 | i = 0; |
| 7433 | for_each_cpu_worker_pool(pool, cpu) |
| 7434 | init_cpu_worker_pool(pool, cpu, std_nice[i++]); |
| 7435 | } |
| 7436 | |
| 7437 | /* create default unbound and ordered wq attrs */ |
| 7438 | for (i = 0; i < NR_STD_WORKER_POOLS; i++) { |
| 7439 | struct workqueue_attrs *attrs; |
| 7440 | |
| 7441 | BUG_ON(!(attrs = alloc_workqueue_attrs())); |
| 7442 | attrs->nice = std_nice[i]; |
| 7443 | unbound_std_wq_attrs[i] = attrs; |
| 7444 | |
| 7445 | /* |
| 7446 | * An ordered wq should have only one pwq as ordering is |
| 7447 | * guaranteed by max_active which is enforced by pwqs. |
| 7448 | */ |
| 7449 | BUG_ON(!(attrs = alloc_workqueue_attrs())); |
| 7450 | attrs->nice = std_nice[i]; |
| 7451 | attrs->ordered = true; |
| 7452 | ordered_wq_attrs[i] = attrs; |
| 7453 | } |
| 7454 | |
| 7455 | system_wq = alloc_workqueue("events", 0, 0); |
| 7456 | system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); |
| 7457 | system_long_wq = alloc_workqueue("events_long", 0, 0); |
| 7458 | system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, |
| 7459 | WQ_MAX_ACTIVE); |
| 7460 | system_freezable_wq = alloc_workqueue("events_freezable", |
| 7461 | WQ_FREEZABLE, 0); |
| 7462 | system_power_efficient_wq = alloc_workqueue("events_power_efficient", |
| 7463 | WQ_POWER_EFFICIENT, 0); |
| 7464 | system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", |
| 7465 | WQ_FREEZABLE | WQ_POWER_EFFICIENT, |
| 7466 | 0); |
| 7467 | system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0); |
| 7468 | system_bh_highpri_wq = alloc_workqueue("events_bh_highpri", |
| 7469 | WQ_BH | WQ_HIGHPRI, 0); |
| 7470 | BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || |
| 7471 | !system_unbound_wq || !system_freezable_wq || |
| 7472 | !system_power_efficient_wq || |
| 7473 | !system_freezable_power_efficient_wq || |
| 7474 | !system_bh_wq || !system_bh_highpri_wq); |
| 7475 | } |
| 7476 | |
| 7477 | static void __init wq_cpu_intensive_thresh_init(void) |
| 7478 | { |
| 7479 | unsigned long thresh; |
| 7480 | unsigned long bogo; |
| 7481 | |
| 7482 | pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); |
| 7483 | BUG_ON(IS_ERR(pwq_release_worker)); |
| 7484 | |
| 7485 | /* if the user set it to a specific value, keep it */ |
| 7486 | if (wq_cpu_intensive_thresh_us != ULONG_MAX) |
| 7487 | return; |
| 7488 | |
| 7489 | /* |
| 7490 | * The default of 10ms is derived from the fact that most modern (as of |
| 7491 | * 2023) processors can do a lot in 10ms and that it's just below what |
| 7492 | * most consider human-perceivable. However, the kernel also runs on a |
| 7493 | * lot slower CPUs including microcontrollers where the threshold is way |
| 7494 | * too low. |
| 7495 | * |
| 7496 | * Let's scale up the threshold upto 1 second if BogoMips is below 4000. |
| 7497 | * This is by no means accurate but it doesn't have to be. The mechanism |
| 7498 | * is still useful even when the threshold is fully scaled up. Also, as |
| 7499 | * the reports would usually be applicable to everyone, some machines |
| 7500 | * operating on longer thresholds won't significantly diminish their |
| 7501 | * usefulness. |
| 7502 | */ |
| 7503 | thresh = 10 * USEC_PER_MSEC; |
| 7504 | |
| 7505 | /* see init/calibrate.c for lpj -> BogoMIPS calculation */ |
| 7506 | bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); |
| 7507 | if (bogo < 4000) |
| 7508 | thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); |
| 7509 | |
| 7510 | pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", |
| 7511 | loops_per_jiffy, bogo, thresh); |
| 7512 | |
| 7513 | wq_cpu_intensive_thresh_us = thresh; |
| 7514 | } |
| 7515 | |
| 7516 | /** |
| 7517 | * workqueue_init - bring workqueue subsystem fully online |
| 7518 | * |
| 7519 | * This is the second step of three-staged workqueue subsystem initialization |
| 7520 | * and invoked as soon as kthreads can be created and scheduled. Workqueues have |
| 7521 | * been created and work items queued on them, but there are no kworkers |
| 7522 | * executing the work items yet. Populate the worker pools with the initial |
| 7523 | * workers and enable future kworker creations. |
| 7524 | */ |
| 7525 | void __init workqueue_init(void) |
| 7526 | { |
| 7527 | struct workqueue_struct *wq; |
| 7528 | struct worker_pool *pool; |
| 7529 | int cpu, bkt; |
| 7530 | |
| 7531 | wq_cpu_intensive_thresh_init(); |
| 7532 | |
| 7533 | mutex_lock(&wq_pool_mutex); |
| 7534 | |
| 7535 | /* |
| 7536 | * Per-cpu pools created earlier could be missing node hint. Fix them |
| 7537 | * up. Also, create a rescuer for workqueues that requested it. |
| 7538 | */ |
| 7539 | for_each_possible_cpu(cpu) { |
| 7540 | for_each_bh_worker_pool(pool, cpu) |
| 7541 | pool->node = cpu_to_node(cpu); |
| 7542 | for_each_cpu_worker_pool(pool, cpu) |
| 7543 | pool->node = cpu_to_node(cpu); |
| 7544 | } |
| 7545 | |
| 7546 | list_for_each_entry(wq, &workqueues, list) { |
| 7547 | WARN(init_rescuer(wq), |
| 7548 | "workqueue: failed to create early rescuer for %s", |
| 7549 | wq->name); |
| 7550 | } |
| 7551 | |
| 7552 | mutex_unlock(&wq_pool_mutex); |
| 7553 | |
| 7554 | /* |
| 7555 | * Create the initial workers. A BH pool has one pseudo worker that |
| 7556 | * represents the shared BH execution context and thus doesn't get |
| 7557 | * affected by hotplug events. Create the BH pseudo workers for all |
| 7558 | * possible CPUs here. |
| 7559 | */ |
| 7560 | for_each_possible_cpu(cpu) |
| 7561 | for_each_bh_worker_pool(pool, cpu) |
| 7562 | BUG_ON(!create_worker(pool)); |
| 7563 | |
| 7564 | for_each_online_cpu(cpu) { |
| 7565 | for_each_cpu_worker_pool(pool, cpu) { |
| 7566 | pool->flags &= ~POOL_DISASSOCIATED; |
| 7567 | BUG_ON(!create_worker(pool)); |
| 7568 | } |
| 7569 | } |
| 7570 | |
| 7571 | hash_for_each(unbound_pool_hash, bkt, pool, hash_node) |
| 7572 | BUG_ON(!create_worker(pool)); |
| 7573 | |
| 7574 | wq_online = true; |
| 7575 | wq_watchdog_init(); |
| 7576 | } |
| 7577 | |
| 7578 | /* |
| 7579 | * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to |
| 7580 | * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique |
| 7581 | * and consecutive pod ID. The rest of @pt is initialized accordingly. |
| 7582 | */ |
| 7583 | static void __init init_pod_type(struct wq_pod_type *pt, |
| 7584 | bool (*cpus_share_pod)(int, int)) |
| 7585 | { |
| 7586 | int cur, pre, cpu, pod; |
| 7587 | |
| 7588 | pt->nr_pods = 0; |
| 7589 | |
| 7590 | /* init @pt->cpu_pod[] according to @cpus_share_pod() */ |
| 7591 | pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); |
| 7592 | BUG_ON(!pt->cpu_pod); |
| 7593 | |
| 7594 | for_each_possible_cpu(cur) { |
| 7595 | for_each_possible_cpu(pre) { |
| 7596 | if (pre >= cur) { |
| 7597 | pt->cpu_pod[cur] = pt->nr_pods++; |
| 7598 | break; |
| 7599 | } |
| 7600 | if (cpus_share_pod(cur, pre)) { |
| 7601 | pt->cpu_pod[cur] = pt->cpu_pod[pre]; |
| 7602 | break; |
| 7603 | } |
| 7604 | } |
| 7605 | } |
| 7606 | |
| 7607 | /* init the rest to match @pt->cpu_pod[] */ |
| 7608 | pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); |
| 7609 | pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); |
| 7610 | BUG_ON(!pt->pod_cpus || !pt->pod_node); |
| 7611 | |
| 7612 | for (pod = 0; pod < pt->nr_pods; pod++) |
| 7613 | BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); |
| 7614 | |
| 7615 | for_each_possible_cpu(cpu) { |
| 7616 | cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); |
| 7617 | pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); |
| 7618 | } |
| 7619 | } |
| 7620 | |
| 7621 | static bool __init cpus_dont_share(int cpu0, int cpu1) |
| 7622 | { |
| 7623 | return false; |
| 7624 | } |
| 7625 | |
| 7626 | static bool __init cpus_share_smt(int cpu0, int cpu1) |
| 7627 | { |
| 7628 | #ifdef CONFIG_SCHED_SMT |
| 7629 | return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); |
| 7630 | #else |
| 7631 | return false; |
| 7632 | #endif |
| 7633 | } |
| 7634 | |
| 7635 | static bool __init cpus_share_numa(int cpu0, int cpu1) |
| 7636 | { |
| 7637 | return cpu_to_node(cpu0) == cpu_to_node(cpu1); |
| 7638 | } |
| 7639 | |
| 7640 | /** |
| 7641 | * workqueue_init_topology - initialize CPU pods for unbound workqueues |
| 7642 | * |
| 7643 | * This is the third step of three-staged workqueue subsystem initialization and |
| 7644 | * invoked after SMP and topology information are fully initialized. It |
| 7645 | * initializes the unbound CPU pods accordingly. |
| 7646 | */ |
| 7647 | void __init workqueue_init_topology(void) |
| 7648 | { |
| 7649 | struct workqueue_struct *wq; |
| 7650 | int cpu; |
| 7651 | |
| 7652 | init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); |
| 7653 | init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); |
| 7654 | init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); |
| 7655 | init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); |
| 7656 | |
| 7657 | wq_topo_initialized = true; |
| 7658 | |
| 7659 | mutex_lock(&wq_pool_mutex); |
| 7660 | |
| 7661 | /* |
| 7662 | * Workqueues allocated earlier would have all CPUs sharing the default |
| 7663 | * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU |
| 7664 | * combinations to apply per-pod sharing. |
| 7665 | */ |
| 7666 | list_for_each_entry(wq, &workqueues, list) { |
| 7667 | for_each_online_cpu(cpu) |
| 7668 | wq_update_pod(wq, cpu, cpu, true); |
| 7669 | if (wq->flags & WQ_UNBOUND) { |
| 7670 | mutex_lock(&wq->mutex); |
| 7671 | wq_update_node_max_active(wq, -1); |
| 7672 | mutex_unlock(&wq->mutex); |
| 7673 | } |
| 7674 | } |
| 7675 | |
| 7676 | mutex_unlock(&wq_pool_mutex); |
| 7677 | } |
| 7678 | |
| 7679 | void __warn_flushing_systemwide_wq(void) |
| 7680 | { |
| 7681 | pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); |
| 7682 | dump_stack(); |
| 7683 | } |
| 7684 | EXPORT_SYMBOL(__warn_flushing_systemwide_wq); |
| 7685 | |
| 7686 | static int __init workqueue_unbound_cpus_setup(char *str) |
| 7687 | { |
| 7688 | if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { |
| 7689 | cpumask_clear(&wq_cmdline_cpumask); |
| 7690 | pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); |
| 7691 | } |
| 7692 | |
| 7693 | return 1; |
| 7694 | } |
| 7695 | __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); |