padata.c 27 KB

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