padata.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * padata.c - generic interface to process data streams in parallel
  4. *
  5. * See Documentation/core-api/padata.rst for more information.
  6. *
  7. * Copyright (C) 2008, 2009 secunet Security Networks AG
  8. * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
  9. *
  10. * Copyright (c) 2020 Oracle and/or its affiliates.
  11. * Author: Daniel Jordan <daniel.m.jordan@oracle.com>
  12. */
  13. #include <linux/completion.h>
  14. #include <linux/export.h>
  15. #include <linux/cpumask.h>
  16. #include <linux/err.h>
  17. #include <linux/cpu.h>
  18. #include <linux/padata.h>
  19. #include <linux/mutex.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/rcupdate.h>
  24. #define PADATA_WORK_ONSTACK 1 /* Work's memory is on stack */
  25. struct padata_work {
  26. struct work_struct pw_work;
  27. struct list_head pw_list; /* padata_free_works linkage */
  28. void *pw_data;
  29. };
  30. static DEFINE_SPINLOCK(padata_works_lock);
  31. static struct padata_work *padata_works;
  32. static LIST_HEAD(padata_free_works);
  33. struct padata_mt_job_state {
  34. spinlock_t lock;
  35. struct completion completion;
  36. struct padata_mt_job *job;
  37. int nworks;
  38. int nworks_fini;
  39. unsigned long chunk_size;
  40. };
  41. static void padata_free_pd(struct parallel_data *pd);
  42. static void __init padata_mt_helper(struct work_struct *work);
  43. static inline void padata_get_pd(struct parallel_data *pd)
  44. {
  45. refcount_inc(&pd->refcnt);
  46. }
  47. static inline void padata_put_pd_cnt(struct parallel_data *pd, int cnt)
  48. {
  49. if (refcount_sub_and_test(cnt, &pd->refcnt))
  50. padata_free_pd(pd);
  51. }
  52. static inline void padata_put_pd(struct parallel_data *pd)
  53. {
  54. padata_put_pd_cnt(pd, 1);
  55. }
  56. static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
  57. {
  58. int cpu, target_cpu;
  59. target_cpu = cpumask_first(pd->cpumask.pcpu);
  60. for (cpu = 0; cpu < cpu_index; cpu++)
  61. target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu);
  62. return target_cpu;
  63. }
  64. static int padata_cpu_hash(struct parallel_data *pd, unsigned int seq_nr)
  65. {
  66. /*
  67. * Hash the sequence numbers to the cpus by taking
  68. * seq_nr mod. number of cpus in use.
  69. */
  70. int cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu);
  71. return padata_index_to_cpu(pd, cpu_index);
  72. }
  73. static struct padata_work *padata_work_alloc(void)
  74. {
  75. struct padata_work *pw;
  76. lockdep_assert_held(&padata_works_lock);
  77. if (list_empty(&padata_free_works))
  78. return NULL; /* No more work items allowed to be queued. */
  79. pw = list_first_entry(&padata_free_works, struct padata_work, pw_list);
  80. list_del(&pw->pw_list);
  81. return pw;
  82. }
  83. /*
  84. * This function is marked __ref because this function may be optimized in such
  85. * a way that it directly refers to work_fn's address, which causes modpost to
  86. * complain when work_fn is marked __init. This scenario was observed with clang
  87. * LTO, where padata_work_init() was optimized to refer directly to
  88. * padata_mt_helper() because the calls to padata_work_init() with other work_fn
  89. * values were eliminated or inlined.
  90. */
  91. static void __ref padata_work_init(struct padata_work *pw, work_func_t work_fn,
  92. void *data, int flags)
  93. {
  94. if (flags & PADATA_WORK_ONSTACK)
  95. INIT_WORK_ONSTACK(&pw->pw_work, work_fn);
  96. else
  97. INIT_WORK(&pw->pw_work, work_fn);
  98. pw->pw_data = data;
  99. }
  100. static int __init padata_work_alloc_mt(int nworks, void *data,
  101. struct list_head *head)
  102. {
  103. int i;
  104. spin_lock_bh(&padata_works_lock);
  105. /* Start at 1 because the current task participates in the job. */
  106. for (i = 1; i < nworks; ++i) {
  107. struct padata_work *pw = padata_work_alloc();
  108. if (!pw)
  109. break;
  110. padata_work_init(pw, padata_mt_helper, data, 0);
  111. list_add(&pw->pw_list, head);
  112. }
  113. spin_unlock_bh(&padata_works_lock);
  114. return i;
  115. }
  116. static void padata_work_free(struct padata_work *pw)
  117. {
  118. lockdep_assert_held(&padata_works_lock);
  119. list_add(&pw->pw_list, &padata_free_works);
  120. }
  121. static void __init padata_works_free(struct list_head *works)
  122. {
  123. struct padata_work *cur, *next;
  124. if (list_empty(works))
  125. return;
  126. spin_lock_bh(&padata_works_lock);
  127. list_for_each_entry_safe(cur, next, works, pw_list) {
  128. list_del(&cur->pw_list);
  129. padata_work_free(cur);
  130. }
  131. spin_unlock_bh(&padata_works_lock);
  132. }
  133. static void padata_parallel_worker(struct work_struct *parallel_work)
  134. {
  135. struct padata_work *pw = container_of(parallel_work, struct padata_work,
  136. pw_work);
  137. struct padata_priv *padata = pw->pw_data;
  138. local_bh_disable();
  139. padata->parallel(padata);
  140. spin_lock(&padata_works_lock);
  141. padata_work_free(pw);
  142. spin_unlock(&padata_works_lock);
  143. local_bh_enable();
  144. }
  145. /**
  146. * padata_do_parallel - padata parallelization function
  147. *
  148. * @ps: padatashell
  149. * @padata: object to be parallelized
  150. * @cb_cpu: pointer to the CPU that the serialization callback function should
  151. * run on. If it's not in the serial cpumask of @pinst
  152. * (i.e. cpumask.cbcpu), this function selects a fallback CPU and if
  153. * none found, returns -EINVAL.
  154. *
  155. * The parallelization callback function will run with BHs off.
  156. * Note: Every object which is parallelized by padata_do_parallel
  157. * must be seen by padata_do_serial.
  158. *
  159. * Return: 0 on success or else negative error code.
  160. */
  161. int padata_do_parallel(struct padata_shell *ps,
  162. struct padata_priv *padata, int *cb_cpu)
  163. {
  164. struct padata_instance *pinst = ps->pinst;
  165. int i, cpu, cpu_index, err;
  166. struct parallel_data *pd;
  167. struct padata_work *pw;
  168. rcu_read_lock_bh();
  169. pd = rcu_dereference_bh(ps->pd);
  170. err = -EINVAL;
  171. if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
  172. goto out;
  173. if (!cpumask_test_cpu(*cb_cpu, pd->cpumask.cbcpu)) {
  174. if (cpumask_empty(pd->cpumask.cbcpu))
  175. goto out;
  176. /* Select an alternate fallback CPU and notify the caller. */
  177. cpu_index = *cb_cpu % cpumask_weight(pd->cpumask.cbcpu);
  178. cpu = cpumask_first(pd->cpumask.cbcpu);
  179. for (i = 0; i < cpu_index; i++)
  180. cpu = cpumask_next(cpu, pd->cpumask.cbcpu);
  181. *cb_cpu = cpu;
  182. }
  183. err = -EBUSY;
  184. if ((pinst->flags & PADATA_RESET))
  185. goto out;
  186. padata_get_pd(pd);
  187. padata->pd = pd;
  188. padata->cb_cpu = *cb_cpu;
  189. spin_lock(&padata_works_lock);
  190. padata->seq_nr = ++pd->seq_nr;
  191. pw = padata_work_alloc();
  192. spin_unlock(&padata_works_lock);
  193. if (!pw) {
  194. /* Maximum works limit exceeded, run in the current task. */
  195. padata->parallel(padata);
  196. }
  197. rcu_read_unlock_bh();
  198. if (pw) {
  199. padata_work_init(pw, padata_parallel_worker, padata, 0);
  200. queue_work(pinst->parallel_wq, &pw->pw_work);
  201. }
  202. return 0;
  203. out:
  204. rcu_read_unlock_bh();
  205. return err;
  206. }
  207. EXPORT_SYMBOL(padata_do_parallel);
  208. /*
  209. * padata_find_next - Find the next object that needs serialization.
  210. *
  211. * Return:
  212. * * A pointer to the control struct of the next object that needs
  213. * serialization, if present in one of the percpu reorder queues.
  214. * * NULL, if the next object that needs serialization will
  215. * be parallel processed by another cpu and is not yet present in
  216. * the cpu's reorder queue.
  217. */
  218. static struct padata_priv *padata_find_next(struct parallel_data *pd,
  219. bool remove_object)
  220. {
  221. struct padata_priv *padata;
  222. struct padata_list *reorder;
  223. int cpu = pd->cpu;
  224. reorder = per_cpu_ptr(pd->reorder_list, cpu);
  225. spin_lock(&reorder->lock);
  226. if (list_empty(&reorder->list)) {
  227. spin_unlock(&reorder->lock);
  228. return NULL;
  229. }
  230. padata = list_entry(reorder->list.next, struct padata_priv, list);
  231. /*
  232. * Checks the rare case where two or more parallel jobs have hashed to
  233. * the same CPU and one of the later ones finishes first.
  234. */
  235. if (padata->seq_nr != pd->processed) {
  236. spin_unlock(&reorder->lock);
  237. return NULL;
  238. }
  239. if (remove_object) {
  240. list_del_init(&padata->list);
  241. ++pd->processed;
  242. /* When sequence wraps around, reset to the first CPU. */
  243. if (unlikely(pd->processed == 0))
  244. pd->cpu = cpumask_first(pd->cpumask.pcpu);
  245. else
  246. pd->cpu = cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1, false);
  247. }
  248. spin_unlock(&reorder->lock);
  249. return padata;
  250. }
  251. static void padata_reorder(struct parallel_data *pd)
  252. {
  253. struct padata_instance *pinst = pd->ps->pinst;
  254. int cb_cpu;
  255. struct padata_priv *padata;
  256. struct padata_serial_queue *squeue;
  257. struct padata_list *reorder;
  258. /*
  259. * We need to ensure that only one cpu can work on dequeueing of
  260. * the reorder queue the time. Calculating in which percpu reorder
  261. * queue the next object will arrive takes some time. A spinlock
  262. * would be highly contended. Also it is not clear in which order
  263. * the objects arrive to the reorder queues. So a cpu could wait to
  264. * get the lock just to notice that there is nothing to do at the
  265. * moment. Therefore we use a trylock and let the holder of the lock
  266. * care for all the objects enqueued during the holdtime of the lock.
  267. */
  268. if (!spin_trylock_bh(&pd->lock))
  269. return;
  270. while (1) {
  271. padata = padata_find_next(pd, true);
  272. /*
  273. * If the next object that needs serialization is parallel
  274. * processed by another cpu and is still on it's way to the
  275. * cpu's reorder queue, nothing to do for now.
  276. */
  277. if (!padata)
  278. break;
  279. cb_cpu = padata->cb_cpu;
  280. squeue = per_cpu_ptr(pd->squeue, cb_cpu);
  281. spin_lock(&squeue->serial.lock);
  282. list_add_tail(&padata->list, &squeue->serial.list);
  283. spin_unlock(&squeue->serial.lock);
  284. queue_work_on(cb_cpu, pinst->serial_wq, &squeue->work);
  285. }
  286. spin_unlock_bh(&pd->lock);
  287. /*
  288. * The next object that needs serialization might have arrived to
  289. * the reorder queues in the meantime.
  290. *
  291. * Ensure reorder queue is read after pd->lock is dropped so we see
  292. * new objects from another task in padata_do_serial. Pairs with
  293. * smp_mb in padata_do_serial.
  294. */
  295. smp_mb();
  296. reorder = per_cpu_ptr(pd->reorder_list, pd->cpu);
  297. if (!list_empty(&reorder->list) && padata_find_next(pd, false)) {
  298. /*
  299. * Other context(eg. the padata_serial_worker) can finish the request.
  300. * To avoid UAF issue, add pd ref here, and put pd ref after reorder_work finish.
  301. */
  302. padata_get_pd(pd);
  303. if (!queue_work(pinst->serial_wq, &pd->reorder_work))
  304. padata_put_pd(pd);
  305. }
  306. }
  307. static void invoke_padata_reorder(struct work_struct *work)
  308. {
  309. struct parallel_data *pd;
  310. local_bh_disable();
  311. pd = container_of(work, struct parallel_data, reorder_work);
  312. padata_reorder(pd);
  313. local_bh_enable();
  314. /* Pairs with putting the reorder_work in the serial_wq */
  315. padata_put_pd(pd);
  316. }
  317. static void padata_serial_worker(struct work_struct *serial_work)
  318. {
  319. struct padata_serial_queue *squeue;
  320. struct parallel_data *pd;
  321. LIST_HEAD(local_list);
  322. int cnt;
  323. local_bh_disable();
  324. squeue = container_of(serial_work, struct padata_serial_queue, work);
  325. pd = squeue->pd;
  326. spin_lock(&squeue->serial.lock);
  327. list_replace_init(&squeue->serial.list, &local_list);
  328. spin_unlock(&squeue->serial.lock);
  329. cnt = 0;
  330. while (!list_empty(&local_list)) {
  331. struct padata_priv *padata;
  332. padata = list_entry(local_list.next,
  333. struct padata_priv, list);
  334. list_del_init(&padata->list);
  335. padata->serial(padata);
  336. cnt++;
  337. }
  338. local_bh_enable();
  339. padata_put_pd_cnt(pd, cnt);
  340. }
  341. /**
  342. * padata_do_serial - padata serialization function
  343. *
  344. * @padata: object to be serialized.
  345. *
  346. * padata_do_serial must be called for every parallelized object.
  347. * The serialization callback function will run with BHs off.
  348. */
  349. void padata_do_serial(struct padata_priv *padata)
  350. {
  351. struct parallel_data *pd = padata->pd;
  352. int hashed_cpu = padata_cpu_hash(pd, padata->seq_nr);
  353. struct padata_list *reorder = per_cpu_ptr(pd->reorder_list, hashed_cpu);
  354. struct padata_priv *cur;
  355. struct list_head *pos;
  356. spin_lock(&reorder->lock);
  357. /* Sort in ascending order of sequence number. */
  358. list_for_each_prev(pos, &reorder->list) {
  359. cur = list_entry(pos, struct padata_priv, list);
  360. /* Compare by difference to consider integer wrap around */
  361. if ((signed int)(cur->seq_nr - padata->seq_nr) < 0)
  362. break;
  363. }
  364. list_add(&padata->list, pos);
  365. spin_unlock(&reorder->lock);
  366. /*
  367. * Ensure the addition to the reorder list is ordered correctly
  368. * with the trylock of pd->lock in padata_reorder. Pairs with smp_mb
  369. * in padata_reorder.
  370. */
  371. smp_mb();
  372. padata_reorder(pd);
  373. }
  374. EXPORT_SYMBOL(padata_do_serial);
  375. static int padata_setup_cpumasks(struct padata_instance *pinst)
  376. {
  377. struct workqueue_attrs *attrs;
  378. int err;
  379. attrs = alloc_workqueue_attrs();
  380. if (!attrs)
  381. return -ENOMEM;
  382. /* Restrict parallel_wq workers to pd->cpumask.pcpu. */
  383. cpumask_copy(attrs->cpumask, pinst->cpumask.pcpu);
  384. err = apply_workqueue_attrs(pinst->parallel_wq, attrs);
  385. free_workqueue_attrs(attrs);
  386. return err;
  387. }
  388. static void __init padata_mt_helper(struct work_struct *w)
  389. {
  390. struct padata_work *pw = container_of(w, struct padata_work, pw_work);
  391. struct padata_mt_job_state *ps = pw->pw_data;
  392. struct padata_mt_job *job = ps->job;
  393. bool done;
  394. spin_lock(&ps->lock);
  395. while (job->size > 0) {
  396. unsigned long start, size, end;
  397. start = job->start;
  398. /* So end is chunk size aligned if enough work remains. */
  399. size = roundup(start + 1, ps->chunk_size) - start;
  400. size = min(size, job->size);
  401. end = start + size;
  402. job->start = end;
  403. job->size -= size;
  404. spin_unlock(&ps->lock);
  405. job->thread_fn(start, end, job->fn_arg);
  406. spin_lock(&ps->lock);
  407. }
  408. ++ps->nworks_fini;
  409. done = (ps->nworks_fini == ps->nworks);
  410. spin_unlock(&ps->lock);
  411. if (done)
  412. complete(&ps->completion);
  413. }
  414. /**
  415. * padata_do_multithreaded - run a multithreaded job
  416. * @job: Description of the job.
  417. *
  418. * See the definition of struct padata_mt_job for more details.
  419. */
  420. void __init padata_do_multithreaded(struct padata_mt_job *job)
  421. {
  422. /* In case threads finish at different times. */
  423. static const unsigned long load_balance_factor = 4;
  424. struct padata_work my_work, *pw;
  425. struct padata_mt_job_state ps;
  426. LIST_HEAD(works);
  427. int nworks, nid;
  428. static atomic_t last_used_nid __initdata;
  429. if (job->size == 0)
  430. return;
  431. /* Ensure at least one thread when size < min_chunk. */
  432. nworks = max(job->size / max(job->min_chunk, job->align), 1ul);
  433. nworks = min(nworks, job->max_threads);
  434. if (nworks == 1) {
  435. /* Single thread, no coordination needed, cut to the chase. */
  436. job->thread_fn(job->start, job->start + job->size, job->fn_arg);
  437. return;
  438. }
  439. spin_lock_init(&ps.lock);
  440. init_completion(&ps.completion);
  441. ps.job = job;
  442. ps.nworks = padata_work_alloc_mt(nworks, &ps, &works);
  443. ps.nworks_fini = 0;
  444. /*
  445. * Chunk size is the amount of work a helper does per call to the
  446. * thread function. Load balance large jobs between threads by
  447. * increasing the number of chunks, guarantee at least the minimum
  448. * chunk size from the caller, and honor the caller's alignment.
  449. * Ensure chunk_size is at least 1 to prevent divide-by-0
  450. * panic in padata_mt_helper().
  451. */
  452. ps.chunk_size = job->size / (ps.nworks * load_balance_factor);
  453. ps.chunk_size = max(ps.chunk_size, job->min_chunk);
  454. ps.chunk_size = max(ps.chunk_size, 1ul);
  455. ps.chunk_size = roundup(ps.chunk_size, job->align);
  456. /*
  457. * chunk_size can be 0 if the caller sets min_chunk to 0. So force it
  458. * to at least 1 to prevent divide-by-0 panic in padata_mt_helper().`
  459. */
  460. if (!ps.chunk_size)
  461. ps.chunk_size = 1U;
  462. list_for_each_entry(pw, &works, pw_list)
  463. if (job->numa_aware) {
  464. int old_node = atomic_read(&last_used_nid);
  465. do {
  466. nid = next_node_in(old_node, node_states[N_CPU]);
  467. } while (!atomic_try_cmpxchg(&last_used_nid, &old_node, nid));
  468. queue_work_node(nid, system_unbound_wq, &pw->pw_work);
  469. } else {
  470. queue_work(system_unbound_wq, &pw->pw_work);
  471. }
  472. /* Use the current thread, which saves starting a workqueue worker. */
  473. padata_work_init(&my_work, padata_mt_helper, &ps, PADATA_WORK_ONSTACK);
  474. padata_mt_helper(&my_work.pw_work);
  475. /* Wait for all the helpers to finish. */
  476. wait_for_completion(&ps.completion);
  477. destroy_work_on_stack(&my_work.pw_work);
  478. padata_works_free(&works);
  479. }
  480. static void __padata_list_init(struct padata_list *pd_list)
  481. {
  482. INIT_LIST_HEAD(&pd_list->list);
  483. spin_lock_init(&pd_list->lock);
  484. }
  485. /* Initialize all percpu queues used by serial workers */
  486. static void padata_init_squeues(struct parallel_data *pd)
  487. {
  488. int cpu;
  489. struct padata_serial_queue *squeue;
  490. for_each_cpu(cpu, pd->cpumask.cbcpu) {
  491. squeue = per_cpu_ptr(pd->squeue, cpu);
  492. squeue->pd = pd;
  493. __padata_list_init(&squeue->serial);
  494. INIT_WORK(&squeue->work, padata_serial_worker);
  495. }
  496. }
  497. /* Initialize per-CPU reorder lists */
  498. static void padata_init_reorder_list(struct parallel_data *pd)
  499. {
  500. int cpu;
  501. struct padata_list *list;
  502. for_each_cpu(cpu, pd->cpumask.pcpu) {
  503. list = per_cpu_ptr(pd->reorder_list, cpu);
  504. __padata_list_init(list);
  505. }
  506. }
  507. /* Allocate and initialize the internal cpumask dependend resources. */
  508. static struct parallel_data *padata_alloc_pd(struct padata_shell *ps)
  509. {
  510. struct padata_instance *pinst = ps->pinst;
  511. struct parallel_data *pd;
  512. pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
  513. if (!pd)
  514. goto err;
  515. pd->reorder_list = alloc_percpu(struct padata_list);
  516. if (!pd->reorder_list)
  517. goto err_free_pd;
  518. pd->squeue = alloc_percpu(struct padata_serial_queue);
  519. if (!pd->squeue)
  520. goto err_free_reorder_list;
  521. pd->ps = ps;
  522. if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
  523. goto err_free_squeue;
  524. if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL))
  525. goto err_free_pcpu;
  526. cpumask_and(pd->cpumask.pcpu, pinst->cpumask.pcpu, cpu_online_mask);
  527. cpumask_and(pd->cpumask.cbcpu, pinst->cpumask.cbcpu, cpu_online_mask);
  528. padata_init_reorder_list(pd);
  529. padata_init_squeues(pd);
  530. pd->seq_nr = -1;
  531. refcount_set(&pd->refcnt, 1);
  532. spin_lock_init(&pd->lock);
  533. pd->cpu = cpumask_first(pd->cpumask.pcpu);
  534. INIT_WORK(&pd->reorder_work, invoke_padata_reorder);
  535. return pd;
  536. err_free_pcpu:
  537. free_cpumask_var(pd->cpumask.pcpu);
  538. err_free_squeue:
  539. free_percpu(pd->squeue);
  540. err_free_reorder_list:
  541. free_percpu(pd->reorder_list);
  542. err_free_pd:
  543. kfree(pd);
  544. err:
  545. return NULL;
  546. }
  547. static void padata_free_pd(struct parallel_data *pd)
  548. {
  549. free_cpumask_var(pd->cpumask.pcpu);
  550. free_cpumask_var(pd->cpumask.cbcpu);
  551. free_percpu(pd->reorder_list);
  552. free_percpu(pd->squeue);
  553. kfree(pd);
  554. }
  555. static void __padata_start(struct padata_instance *pinst)
  556. {
  557. pinst->flags |= PADATA_INIT;
  558. }
  559. static void __padata_stop(struct padata_instance *pinst)
  560. {
  561. if (!(pinst->flags & PADATA_INIT))
  562. return;
  563. pinst->flags &= ~PADATA_INIT;
  564. synchronize_rcu();
  565. }
  566. /* Replace the internal control structure with a new one. */
  567. static int padata_replace_one(struct padata_shell *ps)
  568. {
  569. struct parallel_data *pd_new;
  570. pd_new = padata_alloc_pd(ps);
  571. if (!pd_new)
  572. return -ENOMEM;
  573. ps->opd = rcu_dereference_protected(ps->pd, 1);
  574. rcu_assign_pointer(ps->pd, pd_new);
  575. return 0;
  576. }
  577. static int padata_replace(struct padata_instance *pinst)
  578. {
  579. struct padata_shell *ps;
  580. int err = 0;
  581. pinst->flags |= PADATA_RESET;
  582. list_for_each_entry(ps, &pinst->pslist, list) {
  583. err = padata_replace_one(ps);
  584. if (err)
  585. break;
  586. }
  587. synchronize_rcu();
  588. list_for_each_entry_continue_reverse(ps, &pinst->pslist, list)
  589. padata_put_pd(ps->opd);
  590. pinst->flags &= ~PADATA_RESET;
  591. return err;
  592. }
  593. /* If cpumask contains no active cpu, we mark the instance as invalid. */
  594. static bool padata_validate_cpumask(struct padata_instance *pinst,
  595. const struct cpumask *cpumask)
  596. {
  597. if (!cpumask_intersects(cpumask, cpu_online_mask)) {
  598. pinst->flags |= PADATA_INVALID;
  599. return false;
  600. }
  601. pinst->flags &= ~PADATA_INVALID;
  602. return true;
  603. }
  604. static int __padata_set_cpumasks(struct padata_instance *pinst,
  605. cpumask_var_t pcpumask,
  606. cpumask_var_t cbcpumask)
  607. {
  608. int valid;
  609. int err;
  610. valid = padata_validate_cpumask(pinst, pcpumask);
  611. if (!valid) {
  612. __padata_stop(pinst);
  613. goto out_replace;
  614. }
  615. valid = padata_validate_cpumask(pinst, cbcpumask);
  616. if (!valid)
  617. __padata_stop(pinst);
  618. out_replace:
  619. cpumask_copy(pinst->cpumask.pcpu, pcpumask);
  620. cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
  621. err = padata_setup_cpumasks(pinst) ?: padata_replace(pinst);
  622. if (valid)
  623. __padata_start(pinst);
  624. return err;
  625. }
  626. /**
  627. * padata_set_cpumask - Sets specified by @cpumask_type cpumask to the value
  628. * equivalent to @cpumask.
  629. * @pinst: padata instance
  630. * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
  631. * to parallel and serial cpumasks respectively.
  632. * @cpumask: the cpumask to use
  633. *
  634. * Return: 0 on success or negative error code
  635. */
  636. int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
  637. cpumask_var_t cpumask)
  638. {
  639. struct cpumask *serial_mask, *parallel_mask;
  640. int err = -EINVAL;
  641. cpus_read_lock();
  642. mutex_lock(&pinst->lock);
  643. switch (cpumask_type) {
  644. case PADATA_CPU_PARALLEL:
  645. serial_mask = pinst->cpumask.cbcpu;
  646. parallel_mask = cpumask;
  647. break;
  648. case PADATA_CPU_SERIAL:
  649. parallel_mask = pinst->cpumask.pcpu;
  650. serial_mask = cpumask;
  651. break;
  652. default:
  653. goto out;
  654. }
  655. err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
  656. out:
  657. mutex_unlock(&pinst->lock);
  658. cpus_read_unlock();
  659. return err;
  660. }
  661. EXPORT_SYMBOL(padata_set_cpumask);
  662. #ifdef CONFIG_HOTPLUG_CPU
  663. static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
  664. {
  665. int err = 0;
  666. if (cpumask_test_cpu(cpu, cpu_online_mask)) {
  667. err = padata_replace(pinst);
  668. if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
  669. padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  670. __padata_start(pinst);
  671. }
  672. return err;
  673. }
  674. static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
  675. {
  676. int err = 0;
  677. if (!cpumask_test_cpu(cpu, cpu_online_mask)) {
  678. if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
  679. !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  680. __padata_stop(pinst);
  681. err = padata_replace(pinst);
  682. }
  683. return err;
  684. }
  685. static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
  686. {
  687. return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
  688. cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
  689. }
  690. static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
  691. {
  692. struct padata_instance *pinst;
  693. int ret;
  694. pinst = hlist_entry_safe(node, struct padata_instance, cpu_online_node);
  695. if (!pinst_has_cpu(pinst, cpu))
  696. return 0;
  697. mutex_lock(&pinst->lock);
  698. ret = __padata_add_cpu(pinst, cpu);
  699. mutex_unlock(&pinst->lock);
  700. return ret;
  701. }
  702. static int padata_cpu_dead(unsigned int cpu, struct hlist_node *node)
  703. {
  704. struct padata_instance *pinst;
  705. int ret;
  706. pinst = hlist_entry_safe(node, struct padata_instance, cpu_dead_node);
  707. if (!pinst_has_cpu(pinst, cpu))
  708. return 0;
  709. mutex_lock(&pinst->lock);
  710. ret = __padata_remove_cpu(pinst, cpu);
  711. mutex_unlock(&pinst->lock);
  712. return ret;
  713. }
  714. static enum cpuhp_state hp_online;
  715. #endif
  716. static void __padata_free(struct padata_instance *pinst)
  717. {
  718. #ifdef CONFIG_HOTPLUG_CPU
  719. cpuhp_state_remove_instance_nocalls(CPUHP_PADATA_DEAD,
  720. &pinst->cpu_dead_node);
  721. cpuhp_state_remove_instance_nocalls(hp_online, &pinst->cpu_online_node);
  722. #endif
  723. WARN_ON(!list_empty(&pinst->pslist));
  724. free_cpumask_var(pinst->cpumask.pcpu);
  725. free_cpumask_var(pinst->cpumask.cbcpu);
  726. destroy_workqueue(pinst->serial_wq);
  727. destroy_workqueue(pinst->parallel_wq);
  728. kfree(pinst);
  729. }
  730. #define kobj2pinst(_kobj) \
  731. container_of(_kobj, struct padata_instance, kobj)
  732. #define attr2pentry(_attr) \
  733. container_of(_attr, struct padata_sysfs_entry, attr)
  734. static void padata_sysfs_release(struct kobject *kobj)
  735. {
  736. struct padata_instance *pinst = kobj2pinst(kobj);
  737. __padata_free(pinst);
  738. }
  739. struct padata_sysfs_entry {
  740. struct attribute attr;
  741. ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
  742. ssize_t (*store)(struct padata_instance *, struct attribute *,
  743. const char *, size_t);
  744. };
  745. static ssize_t show_cpumask(struct padata_instance *pinst,
  746. struct attribute *attr, char *buf)
  747. {
  748. struct cpumask *cpumask;
  749. ssize_t len;
  750. mutex_lock(&pinst->lock);
  751. if (!strcmp(attr->name, "serial_cpumask"))
  752. cpumask = pinst->cpumask.cbcpu;
  753. else
  754. cpumask = pinst->cpumask.pcpu;
  755. len = snprintf(buf, PAGE_SIZE, "%*pb\n",
  756. nr_cpu_ids, cpumask_bits(cpumask));
  757. mutex_unlock(&pinst->lock);
  758. return len < PAGE_SIZE ? len : -EINVAL;
  759. }
  760. static ssize_t store_cpumask(struct padata_instance *pinst,
  761. struct attribute *attr,
  762. const char *buf, size_t count)
  763. {
  764. cpumask_var_t new_cpumask;
  765. ssize_t ret;
  766. int mask_type;
  767. if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
  768. return -ENOMEM;
  769. ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
  770. nr_cpumask_bits);
  771. if (ret < 0)
  772. goto out;
  773. mask_type = !strcmp(attr->name, "serial_cpumask") ?
  774. PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
  775. ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
  776. if (!ret)
  777. ret = count;
  778. out:
  779. free_cpumask_var(new_cpumask);
  780. return ret;
  781. }
  782. #define PADATA_ATTR_RW(_name, _show_name, _store_name) \
  783. static struct padata_sysfs_entry _name##_attr = \
  784. __ATTR(_name, 0644, _show_name, _store_name)
  785. #define PADATA_ATTR_RO(_name, _show_name) \
  786. static struct padata_sysfs_entry _name##_attr = \
  787. __ATTR(_name, 0400, _show_name, NULL)
  788. PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
  789. PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
  790. /*
  791. * Padata sysfs provides the following objects:
  792. * serial_cpumask [RW] - cpumask for serial workers
  793. * parallel_cpumask [RW] - cpumask for parallel workers
  794. */
  795. static struct attribute *padata_default_attrs[] = {
  796. &serial_cpumask_attr.attr,
  797. &parallel_cpumask_attr.attr,
  798. NULL,
  799. };
  800. ATTRIBUTE_GROUPS(padata_default);
  801. static ssize_t padata_sysfs_show(struct kobject *kobj,
  802. struct attribute *attr, char *buf)
  803. {
  804. struct padata_instance *pinst;
  805. struct padata_sysfs_entry *pentry;
  806. ssize_t ret = -EIO;
  807. pinst = kobj2pinst(kobj);
  808. pentry = attr2pentry(attr);
  809. if (pentry->show)
  810. ret = pentry->show(pinst, attr, buf);
  811. return ret;
  812. }
  813. static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
  814. const char *buf, size_t count)
  815. {
  816. struct padata_instance *pinst;
  817. struct padata_sysfs_entry *pentry;
  818. ssize_t ret = -EIO;
  819. pinst = kobj2pinst(kobj);
  820. pentry = attr2pentry(attr);
  821. if (pentry->store)
  822. ret = pentry->store(pinst, attr, buf, count);
  823. return ret;
  824. }
  825. static const struct sysfs_ops padata_sysfs_ops = {
  826. .show = padata_sysfs_show,
  827. .store = padata_sysfs_store,
  828. };
  829. static const struct kobj_type padata_attr_type = {
  830. .sysfs_ops = &padata_sysfs_ops,
  831. .default_groups = padata_default_groups,
  832. .release = padata_sysfs_release,
  833. };
  834. /**
  835. * padata_alloc - allocate and initialize a padata instance
  836. * @name: used to identify the instance
  837. *
  838. * Return: new instance on success, NULL on error
  839. */
  840. struct padata_instance *padata_alloc(const char *name)
  841. {
  842. struct padata_instance *pinst;
  843. pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
  844. if (!pinst)
  845. goto err;
  846. pinst->parallel_wq = alloc_workqueue("%s_parallel", WQ_UNBOUND, 0,
  847. name);
  848. if (!pinst->parallel_wq)
  849. goto err_free_inst;
  850. cpus_read_lock();
  851. pinst->serial_wq = alloc_workqueue("%s_serial", WQ_MEM_RECLAIM |
  852. WQ_CPU_INTENSIVE, 1, name);
  853. if (!pinst->serial_wq)
  854. goto err_put_cpus;
  855. if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
  856. goto err_free_serial_wq;
  857. if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
  858. free_cpumask_var(pinst->cpumask.pcpu);
  859. goto err_free_serial_wq;
  860. }
  861. INIT_LIST_HEAD(&pinst->pslist);
  862. cpumask_copy(pinst->cpumask.pcpu, cpu_possible_mask);
  863. cpumask_copy(pinst->cpumask.cbcpu, cpu_possible_mask);
  864. if (padata_setup_cpumasks(pinst))
  865. goto err_free_masks;
  866. __padata_start(pinst);
  867. kobject_init(&pinst->kobj, &padata_attr_type);
  868. mutex_init(&pinst->lock);
  869. #ifdef CONFIG_HOTPLUG_CPU
  870. cpuhp_state_add_instance_nocalls_cpuslocked(hp_online,
  871. &pinst->cpu_online_node);
  872. cpuhp_state_add_instance_nocalls_cpuslocked(CPUHP_PADATA_DEAD,
  873. &pinst->cpu_dead_node);
  874. #endif
  875. cpus_read_unlock();
  876. return pinst;
  877. err_free_masks:
  878. free_cpumask_var(pinst->cpumask.pcpu);
  879. free_cpumask_var(pinst->cpumask.cbcpu);
  880. err_free_serial_wq:
  881. destroy_workqueue(pinst->serial_wq);
  882. err_put_cpus:
  883. cpus_read_unlock();
  884. destroy_workqueue(pinst->parallel_wq);
  885. err_free_inst:
  886. kfree(pinst);
  887. err:
  888. return NULL;
  889. }
  890. EXPORT_SYMBOL(padata_alloc);
  891. /**
  892. * padata_free - free a padata instance
  893. *
  894. * @pinst: padata instance to free
  895. */
  896. void padata_free(struct padata_instance *pinst)
  897. {
  898. kobject_put(&pinst->kobj);
  899. }
  900. EXPORT_SYMBOL(padata_free);
  901. /**
  902. * padata_alloc_shell - Allocate and initialize padata shell.
  903. *
  904. * @pinst: Parent padata_instance object.
  905. *
  906. * Return: new shell on success, NULL on error
  907. */
  908. struct padata_shell *padata_alloc_shell(struct padata_instance *pinst)
  909. {
  910. struct parallel_data *pd;
  911. struct padata_shell *ps;
  912. ps = kzalloc(sizeof(*ps), GFP_KERNEL);
  913. if (!ps)
  914. goto out;
  915. ps->pinst = pinst;
  916. cpus_read_lock();
  917. pd = padata_alloc_pd(ps);
  918. cpus_read_unlock();
  919. if (!pd)
  920. goto out_free_ps;
  921. mutex_lock(&pinst->lock);
  922. RCU_INIT_POINTER(ps->pd, pd);
  923. list_add(&ps->list, &pinst->pslist);
  924. mutex_unlock(&pinst->lock);
  925. return ps;
  926. out_free_ps:
  927. kfree(ps);
  928. out:
  929. return NULL;
  930. }
  931. EXPORT_SYMBOL(padata_alloc_shell);
  932. /**
  933. * padata_free_shell - free a padata shell
  934. *
  935. * @ps: padata shell to free
  936. */
  937. void padata_free_shell(struct padata_shell *ps)
  938. {
  939. struct parallel_data *pd;
  940. if (!ps)
  941. return;
  942. /*
  943. * Wait for all _do_serial calls to finish to avoid touching
  944. * freed pd's and ps's.
  945. */
  946. synchronize_rcu();
  947. mutex_lock(&ps->pinst->lock);
  948. list_del(&ps->list);
  949. pd = rcu_dereference_protected(ps->pd, 1);
  950. padata_put_pd(pd);
  951. mutex_unlock(&ps->pinst->lock);
  952. kfree(ps);
  953. }
  954. EXPORT_SYMBOL(padata_free_shell);
  955. void __init padata_init(void)
  956. {
  957. unsigned int i, possible_cpus;
  958. #ifdef CONFIG_HOTPLUG_CPU
  959. int ret;
  960. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online",
  961. padata_cpu_online, NULL);
  962. if (ret < 0)
  963. goto err;
  964. hp_online = ret;
  965. ret = cpuhp_setup_state_multi(CPUHP_PADATA_DEAD, "padata:dead",
  966. NULL, padata_cpu_dead);
  967. if (ret < 0)
  968. goto remove_online_state;
  969. #endif
  970. possible_cpus = num_possible_cpus();
  971. padata_works = kmalloc_array(possible_cpus, sizeof(struct padata_work),
  972. GFP_KERNEL);
  973. if (!padata_works)
  974. goto remove_dead_state;
  975. for (i = 0; i < possible_cpus; ++i)
  976. list_add(&padata_works[i].pw_list, &padata_free_works);
  977. return;
  978. remove_dead_state:
  979. #ifdef CONFIG_HOTPLUG_CPU
  980. cpuhp_remove_multi_state(CPUHP_PADATA_DEAD);
  981. remove_online_state:
  982. cpuhp_remove_multi_state(hp_online);
  983. err:
  984. #endif
  985. pr_warn("padata: initialization failed\n");
  986. }