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