padata.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * padata.c - generic interface to process data streams in parallel
  4. *
  5. * See Documentation/padata.txt for an api documentation.
  6. *
  7. * Copyright (C) 2008, 2009 secunet Security Networks AG
  8. * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  22. */
  23. #include <linux/export.h>
  24. #include <linux/cpumask.h>
  25. #include <linux/err.h>
  26. #include <linux/cpu.h>
  27. #include <linux/padata.h>
  28. #include <linux/mutex.h>
  29. #include <linux/sched.h>
  30. #include <linux/slab.h>
  31. #include <linux/sysfs.h>
  32. #include <linux/rcupdate.h>
  33. #include <linux/module.h>
  34. #define MAX_OBJ_NUM 1000
  35. static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
  36. {
  37. int cpu, target_cpu;
  38. target_cpu = cpumask_first(pd->cpumask.pcpu);
  39. for (cpu = 0; cpu < cpu_index; cpu++)
  40. target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu);
  41. return target_cpu;
  42. }
  43. static int padata_cpu_hash(struct parallel_data *pd)
  44. {
  45. unsigned int seq_nr;
  46. int cpu_index;
  47. /*
  48. * Hash the sequence numbers to the cpus by taking
  49. * seq_nr mod. number of cpus in use.
  50. */
  51. seq_nr = atomic_inc_return(&pd->seq_nr);
  52. cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu);
  53. return padata_index_to_cpu(pd, cpu_index);
  54. }
  55. static void padata_parallel_worker(struct work_struct *parallel_work)
  56. {
  57. struct padata_parallel_queue *pqueue;
  58. LIST_HEAD(local_list);
  59. local_bh_disable();
  60. pqueue = container_of(parallel_work,
  61. struct padata_parallel_queue, work);
  62. spin_lock(&pqueue->parallel.lock);
  63. list_replace_init(&pqueue->parallel.list, &local_list);
  64. spin_unlock(&pqueue->parallel.lock);
  65. while (!list_empty(&local_list)) {
  66. struct padata_priv *padata;
  67. padata = list_entry(local_list.next,
  68. struct padata_priv, list);
  69. list_del_init(&padata->list);
  70. padata->parallel(padata);
  71. }
  72. local_bh_enable();
  73. }
  74. /**
  75. * padata_do_parallel - padata parallelization function
  76. *
  77. * @pinst: padata instance
  78. * @padata: object to be parallelized
  79. * @cb_cpu: cpu the serialization callback function will run on,
  80. * must be in the serial cpumask of padata(i.e. cpumask.cbcpu).
  81. *
  82. * The parallelization callback function will run with BHs off.
  83. * Note: Every object which is parallelized by padata_do_parallel
  84. * must be seen by padata_do_serial.
  85. */
  86. int padata_do_parallel(struct padata_instance *pinst,
  87. struct padata_priv *padata, int cb_cpu)
  88. {
  89. int target_cpu, err;
  90. struct padata_parallel_queue *queue;
  91. struct parallel_data *pd;
  92. rcu_read_lock_bh();
  93. pd = rcu_dereference_bh(pinst->pd);
  94. err = -EINVAL;
  95. if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
  96. goto out;
  97. if (!cpumask_test_cpu(cb_cpu, pd->cpumask.cbcpu))
  98. goto out;
  99. err = -EBUSY;
  100. if ((pinst->flags & PADATA_RESET))
  101. goto out;
  102. if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM)
  103. goto out;
  104. err = 0;
  105. atomic_inc(&pd->refcnt);
  106. padata->pd = pd;
  107. padata->cb_cpu = cb_cpu;
  108. target_cpu = padata_cpu_hash(pd);
  109. padata->cpu = target_cpu;
  110. queue = per_cpu_ptr(pd->pqueue, target_cpu);
  111. spin_lock(&queue->parallel.lock);
  112. list_add_tail(&padata->list, &queue->parallel.list);
  113. spin_unlock(&queue->parallel.lock);
  114. queue_work_on(target_cpu, pinst->wq, &queue->work);
  115. out:
  116. rcu_read_unlock_bh();
  117. return err;
  118. }
  119. EXPORT_SYMBOL(padata_do_parallel);
  120. /*
  121. * padata_get_next - Get the next object that needs serialization.
  122. *
  123. * Return values are:
  124. *
  125. * A pointer to the control struct of the next object that needs
  126. * serialization, if present in one of the percpu reorder queues.
  127. *
  128. * -EINPROGRESS, if the next object that needs serialization will
  129. * be parallel processed by another cpu and is not yet present in
  130. * the cpu's reorder queue.
  131. *
  132. * -ENODATA, if this cpu has to do the parallel processing for
  133. * the next object.
  134. */
  135. static struct padata_priv *padata_get_next(struct parallel_data *pd)
  136. {
  137. int cpu, num_cpus;
  138. unsigned int next_nr, next_index;
  139. struct padata_parallel_queue *next_queue;
  140. struct padata_priv *padata;
  141. struct padata_list *reorder;
  142. num_cpus = cpumask_weight(pd->cpumask.pcpu);
  143. /*
  144. * Calculate the percpu reorder queue and the sequence
  145. * number of the next object.
  146. */
  147. next_nr = pd->processed;
  148. next_index = next_nr % num_cpus;
  149. cpu = padata_index_to_cpu(pd, next_index);
  150. next_queue = per_cpu_ptr(pd->pqueue, cpu);
  151. reorder = &next_queue->reorder;
  152. spin_lock(&reorder->lock);
  153. if (!list_empty(&reorder->list)) {
  154. padata = list_entry(reorder->list.next,
  155. struct padata_priv, list);
  156. list_del_init(&padata->list);
  157. atomic_dec(&pd->reorder_objects);
  158. pd->processed++;
  159. spin_unlock(&reorder->lock);
  160. goto out;
  161. }
  162. spin_unlock(&reorder->lock);
  163. if (__this_cpu_read(pd->pqueue->cpu_index) == next_queue->cpu_index) {
  164. padata = ERR_PTR(-ENODATA);
  165. goto out;
  166. }
  167. padata = ERR_PTR(-EINPROGRESS);
  168. out:
  169. return padata;
  170. }
  171. static void padata_reorder(struct parallel_data *pd)
  172. {
  173. int cb_cpu;
  174. struct padata_priv *padata;
  175. struct padata_serial_queue *squeue;
  176. struct padata_instance *pinst = pd->pinst;
  177. /*
  178. * We need to ensure that only one cpu can work on dequeueing of
  179. * the reorder queue the time. Calculating in which percpu reorder
  180. * queue the next object will arrive takes some time. A spinlock
  181. * would be highly contended. Also it is not clear in which order
  182. * the objects arrive to the reorder queues. So a cpu could wait to
  183. * get the lock just to notice that there is nothing to do at the
  184. * moment. Therefore we use a trylock and let the holder of the lock
  185. * care for all the objects enqueued during the holdtime of the lock.
  186. */
  187. if (!spin_trylock_bh(&pd->lock))
  188. return;
  189. while (1) {
  190. padata = padata_get_next(pd);
  191. /*
  192. * If the next object that needs serialization is parallel
  193. * processed by another cpu and is still on it's way to the
  194. * cpu's reorder queue, nothing to do for now.
  195. */
  196. if (PTR_ERR(padata) == -EINPROGRESS)
  197. break;
  198. /*
  199. * This cpu has to do the parallel processing of the next
  200. * object. It's waiting in the cpu's parallelization queue,
  201. * so exit immediately.
  202. */
  203. if (PTR_ERR(padata) == -ENODATA) {
  204. del_timer(&pd->timer);
  205. spin_unlock_bh(&pd->lock);
  206. return;
  207. }
  208. cb_cpu = padata->cb_cpu;
  209. squeue = per_cpu_ptr(pd->squeue, cb_cpu);
  210. spin_lock(&squeue->serial.lock);
  211. list_add_tail(&padata->list, &squeue->serial.list);
  212. spin_unlock(&squeue->serial.lock);
  213. queue_work_on(cb_cpu, pinst->wq, &squeue->work);
  214. }
  215. spin_unlock_bh(&pd->lock);
  216. /*
  217. * The next object that needs serialization might have arrived to
  218. * the reorder queues in the meantime, we will be called again
  219. * from the timer function if no one else cares for it.
  220. */
  221. if (atomic_read(&pd->reorder_objects)
  222. && !(pinst->flags & PADATA_RESET))
  223. mod_timer(&pd->timer, jiffies + HZ);
  224. else
  225. del_timer(&pd->timer);
  226. return;
  227. }
  228. static void invoke_padata_reorder(struct work_struct *work)
  229. {
  230. struct padata_parallel_queue *pqueue;
  231. struct parallel_data *pd;
  232. local_bh_disable();
  233. pqueue = container_of(work, struct padata_parallel_queue, reorder_work);
  234. pd = pqueue->pd;
  235. padata_reorder(pd);
  236. local_bh_enable();
  237. }
  238. static void padata_reorder_timer(struct timer_list *t)
  239. {
  240. struct parallel_data *pd = from_timer(pd, t, timer);
  241. unsigned int weight;
  242. int target_cpu, cpu;
  243. cpu = get_cpu();
  244. /* We don't lock pd here to not interfere with parallel processing
  245. * padata_reorder() calls on other CPUs. We just need any CPU out of
  246. * the cpumask.pcpu set. It would be nice if it's the right one but
  247. * it doesn't matter if we're off to the next one by using an outdated
  248. * pd->processed value.
  249. */
  250. weight = cpumask_weight(pd->cpumask.pcpu);
  251. target_cpu = padata_index_to_cpu(pd, pd->processed % weight);
  252. /* ensure to call the reorder callback on the correct CPU */
  253. if (cpu != target_cpu) {
  254. struct padata_parallel_queue *pqueue;
  255. struct padata_instance *pinst;
  256. /* The timer function is serialized wrt itself -- no locking
  257. * needed.
  258. */
  259. pinst = pd->pinst;
  260. pqueue = per_cpu_ptr(pd->pqueue, target_cpu);
  261. queue_work_on(target_cpu, pinst->wq, &pqueue->reorder_work);
  262. } else {
  263. padata_reorder(pd);
  264. }
  265. put_cpu();
  266. }
  267. static void padata_serial_worker(struct work_struct *serial_work)
  268. {
  269. struct padata_serial_queue *squeue;
  270. struct parallel_data *pd;
  271. LIST_HEAD(local_list);
  272. local_bh_disable();
  273. squeue = container_of(serial_work, struct padata_serial_queue, work);
  274. pd = squeue->pd;
  275. spin_lock(&squeue->serial.lock);
  276. list_replace_init(&squeue->serial.list, &local_list);
  277. spin_unlock(&squeue->serial.lock);
  278. while (!list_empty(&local_list)) {
  279. struct padata_priv *padata;
  280. padata = list_entry(local_list.next,
  281. struct padata_priv, list);
  282. list_del_init(&padata->list);
  283. padata->serial(padata);
  284. atomic_dec(&pd->refcnt);
  285. }
  286. local_bh_enable();
  287. }
  288. /**
  289. * padata_do_serial - padata serialization function
  290. *
  291. * @padata: object to be serialized.
  292. *
  293. * padata_do_serial must be called for every parallelized object.
  294. * The serialization callback function will run with BHs off.
  295. */
  296. void padata_do_serial(struct padata_priv *padata)
  297. {
  298. int cpu;
  299. struct padata_parallel_queue *pqueue;
  300. struct parallel_data *pd;
  301. int reorder_via_wq = 0;
  302. pd = padata->pd;
  303. cpu = get_cpu();
  304. /* We need to run on the same CPU padata_do_parallel(.., padata, ..)
  305. * was called on -- or, at least, enqueue the padata object into the
  306. * correct per-cpu queue.
  307. */
  308. if (cpu != padata->cpu) {
  309. reorder_via_wq = 1;
  310. cpu = padata->cpu;
  311. }
  312. pqueue = per_cpu_ptr(pd->pqueue, cpu);
  313. spin_lock(&pqueue->reorder.lock);
  314. atomic_inc(&pd->reorder_objects);
  315. list_add_tail(&padata->list, &pqueue->reorder.list);
  316. spin_unlock(&pqueue->reorder.lock);
  317. put_cpu();
  318. /* If we're running on the wrong CPU, call padata_reorder() via a
  319. * kernel worker.
  320. */
  321. if (reorder_via_wq)
  322. queue_work_on(cpu, pd->pinst->wq, &pqueue->reorder_work);
  323. else
  324. padata_reorder(pd);
  325. }
  326. EXPORT_SYMBOL(padata_do_serial);
  327. static int padata_setup_cpumasks(struct parallel_data *pd,
  328. const struct cpumask *pcpumask,
  329. const struct cpumask *cbcpumask)
  330. {
  331. if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
  332. return -ENOMEM;
  333. cpumask_and(pd->cpumask.pcpu, pcpumask, cpu_online_mask);
  334. if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL)) {
  335. free_cpumask_var(pd->cpumask.pcpu);
  336. return -ENOMEM;
  337. }
  338. cpumask_and(pd->cpumask.cbcpu, cbcpumask, cpu_online_mask);
  339. return 0;
  340. }
  341. static void __padata_list_init(struct padata_list *pd_list)
  342. {
  343. INIT_LIST_HEAD(&pd_list->list);
  344. spin_lock_init(&pd_list->lock);
  345. }
  346. /* Initialize all percpu queues used by serial workers */
  347. static void padata_init_squeues(struct parallel_data *pd)
  348. {
  349. int cpu;
  350. struct padata_serial_queue *squeue;
  351. for_each_cpu(cpu, pd->cpumask.cbcpu) {
  352. squeue = per_cpu_ptr(pd->squeue, cpu);
  353. squeue->pd = pd;
  354. __padata_list_init(&squeue->serial);
  355. INIT_WORK(&squeue->work, padata_serial_worker);
  356. }
  357. }
  358. /* Initialize all percpu queues used by parallel workers */
  359. static void padata_init_pqueues(struct parallel_data *pd)
  360. {
  361. int cpu_index, cpu;
  362. struct padata_parallel_queue *pqueue;
  363. cpu_index = 0;
  364. for_each_possible_cpu(cpu) {
  365. pqueue = per_cpu_ptr(pd->pqueue, cpu);
  366. if (!cpumask_test_cpu(cpu, pd->cpumask.pcpu)) {
  367. pqueue->cpu_index = -1;
  368. continue;
  369. }
  370. pqueue->pd = pd;
  371. pqueue->cpu_index = cpu_index;
  372. cpu_index++;
  373. __padata_list_init(&pqueue->reorder);
  374. __padata_list_init(&pqueue->parallel);
  375. INIT_WORK(&pqueue->work, padata_parallel_worker);
  376. INIT_WORK(&pqueue->reorder_work, invoke_padata_reorder);
  377. atomic_set(&pqueue->num_obj, 0);
  378. }
  379. }
  380. /* Allocate and initialize the internal cpumask dependend resources. */
  381. static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
  382. const struct cpumask *pcpumask,
  383. const struct cpumask *cbcpumask)
  384. {
  385. struct parallel_data *pd;
  386. pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
  387. if (!pd)
  388. goto err;
  389. pd->pqueue = alloc_percpu(struct padata_parallel_queue);
  390. if (!pd->pqueue)
  391. goto err_free_pd;
  392. pd->squeue = alloc_percpu(struct padata_serial_queue);
  393. if (!pd->squeue)
  394. goto err_free_pqueue;
  395. if (padata_setup_cpumasks(pd, pcpumask, cbcpumask) < 0)
  396. goto err_free_squeue;
  397. padata_init_pqueues(pd);
  398. padata_init_squeues(pd);
  399. timer_setup(&pd->timer, padata_reorder_timer, 0);
  400. atomic_set(&pd->seq_nr, -1);
  401. atomic_set(&pd->reorder_objects, 0);
  402. atomic_set(&pd->refcnt, 0);
  403. pd->pinst = pinst;
  404. spin_lock_init(&pd->lock);
  405. return pd;
  406. err_free_squeue:
  407. free_percpu(pd->squeue);
  408. err_free_pqueue:
  409. free_percpu(pd->pqueue);
  410. err_free_pd:
  411. kfree(pd);
  412. err:
  413. return NULL;
  414. }
  415. static void padata_free_pd(struct parallel_data *pd)
  416. {
  417. free_cpumask_var(pd->cpumask.pcpu);
  418. free_cpumask_var(pd->cpumask.cbcpu);
  419. free_percpu(pd->pqueue);
  420. free_percpu(pd->squeue);
  421. kfree(pd);
  422. }
  423. /* Flush all objects out of the padata queues. */
  424. static void padata_flush_queues(struct parallel_data *pd)
  425. {
  426. int cpu;
  427. struct padata_parallel_queue *pqueue;
  428. struct padata_serial_queue *squeue;
  429. for_each_cpu(cpu, pd->cpumask.pcpu) {
  430. pqueue = per_cpu_ptr(pd->pqueue, cpu);
  431. flush_work(&pqueue->work);
  432. }
  433. del_timer_sync(&pd->timer);
  434. if (atomic_read(&pd->reorder_objects))
  435. padata_reorder(pd);
  436. for_each_cpu(cpu, pd->cpumask.cbcpu) {
  437. squeue = per_cpu_ptr(pd->squeue, cpu);
  438. flush_work(&squeue->work);
  439. }
  440. BUG_ON(atomic_read(&pd->refcnt) != 0);
  441. }
  442. static void __padata_start(struct padata_instance *pinst)
  443. {
  444. pinst->flags |= PADATA_INIT;
  445. }
  446. static void __padata_stop(struct padata_instance *pinst)
  447. {
  448. if (!(pinst->flags & PADATA_INIT))
  449. return;
  450. pinst->flags &= ~PADATA_INIT;
  451. synchronize_rcu();
  452. get_online_cpus();
  453. padata_flush_queues(pinst->pd);
  454. put_online_cpus();
  455. }
  456. /* Replace the internal control structure with a new one. */
  457. static void padata_replace(struct padata_instance *pinst,
  458. struct parallel_data *pd_new)
  459. {
  460. struct parallel_data *pd_old = pinst->pd;
  461. int notification_mask = 0;
  462. pinst->flags |= PADATA_RESET;
  463. rcu_assign_pointer(pinst->pd, pd_new);
  464. synchronize_rcu();
  465. if (!cpumask_equal(pd_old->cpumask.pcpu, pd_new->cpumask.pcpu))
  466. notification_mask |= PADATA_CPU_PARALLEL;
  467. if (!cpumask_equal(pd_old->cpumask.cbcpu, pd_new->cpumask.cbcpu))
  468. notification_mask |= PADATA_CPU_SERIAL;
  469. padata_flush_queues(pd_old);
  470. padata_free_pd(pd_old);
  471. if (notification_mask)
  472. blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
  473. notification_mask,
  474. &pd_new->cpumask);
  475. pinst->flags &= ~PADATA_RESET;
  476. }
  477. /**
  478. * padata_register_cpumask_notifier - Registers a notifier that will be called
  479. * if either pcpu or cbcpu or both cpumasks change.
  480. *
  481. * @pinst: A poineter to padata instance
  482. * @nblock: A pointer to notifier block.
  483. */
  484. int padata_register_cpumask_notifier(struct padata_instance *pinst,
  485. struct notifier_block *nblock)
  486. {
  487. return blocking_notifier_chain_register(&pinst->cpumask_change_notifier,
  488. nblock);
  489. }
  490. EXPORT_SYMBOL(padata_register_cpumask_notifier);
  491. /**
  492. * padata_unregister_cpumask_notifier - Unregisters cpumask notifier
  493. * registered earlier using padata_register_cpumask_notifier
  494. *
  495. * @pinst: A pointer to data instance.
  496. * @nlock: A pointer to notifier block.
  497. */
  498. int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
  499. struct notifier_block *nblock)
  500. {
  501. return blocking_notifier_chain_unregister(
  502. &pinst->cpumask_change_notifier,
  503. nblock);
  504. }
  505. EXPORT_SYMBOL(padata_unregister_cpumask_notifier);
  506. /* If cpumask contains no active cpu, we mark the instance as invalid. */
  507. static bool padata_validate_cpumask(struct padata_instance *pinst,
  508. const struct cpumask *cpumask)
  509. {
  510. if (!cpumask_intersects(cpumask, cpu_online_mask)) {
  511. pinst->flags |= PADATA_INVALID;
  512. return false;
  513. }
  514. pinst->flags &= ~PADATA_INVALID;
  515. return true;
  516. }
  517. static int __padata_set_cpumasks(struct padata_instance *pinst,
  518. cpumask_var_t pcpumask,
  519. cpumask_var_t cbcpumask)
  520. {
  521. int valid;
  522. struct parallel_data *pd;
  523. valid = padata_validate_cpumask(pinst, pcpumask);
  524. if (!valid) {
  525. __padata_stop(pinst);
  526. goto out_replace;
  527. }
  528. valid = padata_validate_cpumask(pinst, cbcpumask);
  529. if (!valid)
  530. __padata_stop(pinst);
  531. out_replace:
  532. pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
  533. if (!pd)
  534. return -ENOMEM;
  535. cpumask_copy(pinst->cpumask.pcpu, pcpumask);
  536. cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
  537. padata_replace(pinst, pd);
  538. if (valid)
  539. __padata_start(pinst);
  540. return 0;
  541. }
  542. /**
  543. * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value
  544. * equivalent to @cpumask.
  545. *
  546. * @pinst: padata instance
  547. * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
  548. * to parallel and serial cpumasks respectively.
  549. * @cpumask: the cpumask to use
  550. */
  551. int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
  552. cpumask_var_t cpumask)
  553. {
  554. struct cpumask *serial_mask, *parallel_mask;
  555. int err = -EINVAL;
  556. mutex_lock(&pinst->lock);
  557. get_online_cpus();
  558. switch (cpumask_type) {
  559. case PADATA_CPU_PARALLEL:
  560. serial_mask = pinst->cpumask.cbcpu;
  561. parallel_mask = cpumask;
  562. break;
  563. case PADATA_CPU_SERIAL:
  564. parallel_mask = pinst->cpumask.pcpu;
  565. serial_mask = cpumask;
  566. break;
  567. default:
  568. goto out;
  569. }
  570. err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
  571. out:
  572. put_online_cpus();
  573. mutex_unlock(&pinst->lock);
  574. return err;
  575. }
  576. EXPORT_SYMBOL(padata_set_cpumask);
  577. /**
  578. * padata_start - start the parallel processing
  579. *
  580. * @pinst: padata instance to start
  581. */
  582. int padata_start(struct padata_instance *pinst)
  583. {
  584. int err = 0;
  585. mutex_lock(&pinst->lock);
  586. if (pinst->flags & PADATA_INVALID)
  587. err = -EINVAL;
  588. __padata_start(pinst);
  589. mutex_unlock(&pinst->lock);
  590. return err;
  591. }
  592. EXPORT_SYMBOL(padata_start);
  593. /**
  594. * padata_stop - stop the parallel processing
  595. *
  596. * @pinst: padata instance to stop
  597. */
  598. void padata_stop(struct padata_instance *pinst)
  599. {
  600. mutex_lock(&pinst->lock);
  601. __padata_stop(pinst);
  602. mutex_unlock(&pinst->lock);
  603. }
  604. EXPORT_SYMBOL(padata_stop);
  605. #ifdef CONFIG_HOTPLUG_CPU
  606. static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
  607. {
  608. struct parallel_data *pd;
  609. if (cpumask_test_cpu(cpu, cpu_online_mask)) {
  610. pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
  611. pinst->cpumask.cbcpu);
  612. if (!pd)
  613. return -ENOMEM;
  614. padata_replace(pinst, pd);
  615. if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
  616. padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  617. __padata_start(pinst);
  618. }
  619. return 0;
  620. }
  621. static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
  622. {
  623. struct parallel_data *pd = NULL;
  624. if (cpumask_test_cpu(cpu, cpu_online_mask)) {
  625. if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
  626. !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  627. __padata_stop(pinst);
  628. pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
  629. pinst->cpumask.cbcpu);
  630. if (!pd)
  631. return -ENOMEM;
  632. padata_replace(pinst, pd);
  633. cpumask_clear_cpu(cpu, pd->cpumask.cbcpu);
  634. cpumask_clear_cpu(cpu, pd->cpumask.pcpu);
  635. }
  636. return 0;
  637. }
  638. /**
  639. * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
  640. * padata cpumasks.
  641. *
  642. * @pinst: padata instance
  643. * @cpu: cpu to remove
  644. * @mask: bitmask specifying from which cpumask @cpu should be removed
  645. * The @mask may be any combination of the following flags:
  646. * PADATA_CPU_SERIAL - serial cpumask
  647. * PADATA_CPU_PARALLEL - parallel cpumask
  648. */
  649. int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask)
  650. {
  651. int err;
  652. if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
  653. return -EINVAL;
  654. mutex_lock(&pinst->lock);
  655. get_online_cpus();
  656. if (mask & PADATA_CPU_SERIAL)
  657. cpumask_clear_cpu(cpu, pinst->cpumask.cbcpu);
  658. if (mask & PADATA_CPU_PARALLEL)
  659. cpumask_clear_cpu(cpu, pinst->cpumask.pcpu);
  660. err = __padata_remove_cpu(pinst, cpu);
  661. put_online_cpus();
  662. mutex_unlock(&pinst->lock);
  663. return err;
  664. }
  665. EXPORT_SYMBOL(padata_remove_cpu);
  666. static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
  667. {
  668. return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
  669. cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
  670. }
  671. static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
  672. {
  673. struct padata_instance *pinst;
  674. int ret;
  675. pinst = hlist_entry_safe(node, struct padata_instance, node);
  676. if (!pinst_has_cpu(pinst, cpu))
  677. return 0;
  678. mutex_lock(&pinst->lock);
  679. ret = __padata_add_cpu(pinst, cpu);
  680. mutex_unlock(&pinst->lock);
  681. return ret;
  682. }
  683. static int padata_cpu_prep_down(unsigned int cpu, struct hlist_node *node)
  684. {
  685. struct padata_instance *pinst;
  686. int ret;
  687. pinst = hlist_entry_safe(node, struct padata_instance, node);
  688. if (!pinst_has_cpu(pinst, cpu))
  689. return 0;
  690. mutex_lock(&pinst->lock);
  691. ret = __padata_remove_cpu(pinst, cpu);
  692. mutex_unlock(&pinst->lock);
  693. return ret;
  694. }
  695. static enum cpuhp_state hp_online;
  696. #endif
  697. static void __padata_free(struct padata_instance *pinst)
  698. {
  699. #ifdef CONFIG_HOTPLUG_CPU
  700. cpuhp_state_remove_instance_nocalls(hp_online, &pinst->node);
  701. #endif
  702. padata_stop(pinst);
  703. padata_free_pd(pinst->pd);
  704. free_cpumask_var(pinst->cpumask.pcpu);
  705. free_cpumask_var(pinst->cpumask.cbcpu);
  706. kfree(pinst);
  707. }
  708. #define kobj2pinst(_kobj) \
  709. container_of(_kobj, struct padata_instance, kobj)
  710. #define attr2pentry(_attr) \
  711. container_of(_attr, struct padata_sysfs_entry, attr)
  712. static void padata_sysfs_release(struct kobject *kobj)
  713. {
  714. struct padata_instance *pinst = kobj2pinst(kobj);
  715. __padata_free(pinst);
  716. }
  717. struct padata_sysfs_entry {
  718. struct attribute attr;
  719. ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
  720. ssize_t (*store)(struct padata_instance *, struct attribute *,
  721. const char *, size_t);
  722. };
  723. static ssize_t show_cpumask(struct padata_instance *pinst,
  724. struct attribute *attr, char *buf)
  725. {
  726. struct cpumask *cpumask;
  727. ssize_t len;
  728. mutex_lock(&pinst->lock);
  729. if (!strcmp(attr->name, "serial_cpumask"))
  730. cpumask = pinst->cpumask.cbcpu;
  731. else
  732. cpumask = pinst->cpumask.pcpu;
  733. len = snprintf(buf, PAGE_SIZE, "%*pb\n",
  734. nr_cpu_ids, cpumask_bits(cpumask));
  735. mutex_unlock(&pinst->lock);
  736. return len < PAGE_SIZE ? len : -EINVAL;
  737. }
  738. static ssize_t store_cpumask(struct padata_instance *pinst,
  739. struct attribute *attr,
  740. const char *buf, size_t count)
  741. {
  742. cpumask_var_t new_cpumask;
  743. ssize_t ret;
  744. int mask_type;
  745. if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
  746. return -ENOMEM;
  747. ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
  748. nr_cpumask_bits);
  749. if (ret < 0)
  750. goto out;
  751. mask_type = !strcmp(attr->name, "serial_cpumask") ?
  752. PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
  753. ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
  754. if (!ret)
  755. ret = count;
  756. out:
  757. free_cpumask_var(new_cpumask);
  758. return ret;
  759. }
  760. #define PADATA_ATTR_RW(_name, _show_name, _store_name) \
  761. static struct padata_sysfs_entry _name##_attr = \
  762. __ATTR(_name, 0644, _show_name, _store_name)
  763. #define PADATA_ATTR_RO(_name, _show_name) \
  764. static struct padata_sysfs_entry _name##_attr = \
  765. __ATTR(_name, 0400, _show_name, NULL)
  766. PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
  767. PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
  768. /*
  769. * Padata sysfs provides the following objects:
  770. * serial_cpumask [RW] - cpumask for serial workers
  771. * parallel_cpumask [RW] - cpumask for parallel workers
  772. */
  773. static struct attribute *padata_default_attrs[] = {
  774. &serial_cpumask_attr.attr,
  775. &parallel_cpumask_attr.attr,
  776. NULL,
  777. };
  778. static ssize_t padata_sysfs_show(struct kobject *kobj,
  779. struct attribute *attr, char *buf)
  780. {
  781. struct padata_instance *pinst;
  782. struct padata_sysfs_entry *pentry;
  783. ssize_t ret = -EIO;
  784. pinst = kobj2pinst(kobj);
  785. pentry = attr2pentry(attr);
  786. if (pentry->show)
  787. ret = pentry->show(pinst, attr, buf);
  788. return ret;
  789. }
  790. static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
  791. const char *buf, size_t count)
  792. {
  793. struct padata_instance *pinst;
  794. struct padata_sysfs_entry *pentry;
  795. ssize_t ret = -EIO;
  796. pinst = kobj2pinst(kobj);
  797. pentry = attr2pentry(attr);
  798. if (pentry->show)
  799. ret = pentry->store(pinst, attr, buf, count);
  800. return ret;
  801. }
  802. static const struct sysfs_ops padata_sysfs_ops = {
  803. .show = padata_sysfs_show,
  804. .store = padata_sysfs_store,
  805. };
  806. static struct kobj_type padata_attr_type = {
  807. .sysfs_ops = &padata_sysfs_ops,
  808. .default_attrs = padata_default_attrs,
  809. .release = padata_sysfs_release,
  810. };
  811. /**
  812. * padata_alloc - allocate and initialize a padata instance and specify
  813. * cpumasks for serial and parallel workers.
  814. *
  815. * @wq: workqueue to use for the allocated padata instance
  816. * @pcpumask: cpumask that will be used for padata parallelization
  817. * @cbcpumask: cpumask that will be used for padata serialization
  818. *
  819. * Must be called from a cpus_read_lock() protected region
  820. */
  821. static struct padata_instance *padata_alloc(struct workqueue_struct *wq,
  822. const struct cpumask *pcpumask,
  823. const struct cpumask *cbcpumask)
  824. {
  825. struct padata_instance *pinst;
  826. struct parallel_data *pd = NULL;
  827. pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
  828. if (!pinst)
  829. goto err;
  830. if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
  831. goto err_free_inst;
  832. if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
  833. free_cpumask_var(pinst->cpumask.pcpu);
  834. goto err_free_inst;
  835. }
  836. if (!padata_validate_cpumask(pinst, pcpumask) ||
  837. !padata_validate_cpumask(pinst, cbcpumask))
  838. goto err_free_masks;
  839. pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
  840. if (!pd)
  841. goto err_free_masks;
  842. rcu_assign_pointer(pinst->pd, pd);
  843. pinst->wq = wq;
  844. cpumask_copy(pinst->cpumask.pcpu, pcpumask);
  845. cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
  846. pinst->flags = 0;
  847. BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier);
  848. kobject_init(&pinst->kobj, &padata_attr_type);
  849. mutex_init(&pinst->lock);
  850. #ifdef CONFIG_HOTPLUG_CPU
  851. cpuhp_state_add_instance_nocalls_cpuslocked(hp_online, &pinst->node);
  852. #endif
  853. return pinst;
  854. err_free_masks:
  855. free_cpumask_var(pinst->cpumask.pcpu);
  856. free_cpumask_var(pinst->cpumask.cbcpu);
  857. err_free_inst:
  858. kfree(pinst);
  859. err:
  860. return NULL;
  861. }
  862. /**
  863. * padata_alloc_possible - Allocate and initialize padata instance.
  864. * Use the cpu_possible_mask for serial and
  865. * parallel workers.
  866. *
  867. * @wq: workqueue to use for the allocated padata instance
  868. *
  869. * Must be called from a cpus_read_lock() protected region
  870. */
  871. struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq)
  872. {
  873. lockdep_assert_cpus_held();
  874. return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask);
  875. }
  876. EXPORT_SYMBOL(padata_alloc_possible);
  877. /**
  878. * padata_free - free a padata instance
  879. *
  880. * @padata_inst: padata instance to free
  881. */
  882. void padata_free(struct padata_instance *pinst)
  883. {
  884. kobject_put(&pinst->kobj);
  885. }
  886. EXPORT_SYMBOL(padata_free);
  887. #ifdef CONFIG_HOTPLUG_CPU
  888. static __init int padata_driver_init(void)
  889. {
  890. int ret;
  891. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online",
  892. padata_cpu_online,
  893. padata_cpu_prep_down);
  894. if (ret < 0)
  895. return ret;
  896. hp_online = ret;
  897. return 0;
  898. }
  899. module_init(padata_driver_init);
  900. static __exit void padata_driver_exit(void)
  901. {
  902. cpuhp_remove_multi_state(hp_online);
  903. }
  904. module_exit(padata_driver_exit);
  905. #endif