padata.c 28 KB

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