padata.c 27 KB

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