padata.c 25 KB

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