padata.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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_online_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_online_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_online_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_online_mask)) {
  554. pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
  555. pinst->cpumask.cbcpu);
  556. if (!pd)
  557. return -ENOMEM;
  558. padata_replace(pinst, pd);
  559. if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
  560. padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  561. __padata_start(pinst);
  562. }
  563. return 0;
  564. }
  565. /**
  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. cpumask_clear_cpu(cpu, pd->cpumask.cbcpu);
  606. cpumask_clear_cpu(cpu, pd->cpumask.pcpu);
  607. }
  608. return 0;
  609. }
  610. /**
  611. * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
  612. * padata cpumasks.
  613. *
  614. * @pinst: padata instance
  615. * @cpu: cpu to remove
  616. * @mask: bitmask specifying from which cpumask @cpu should be removed
  617. * The @mask may be any combination of the following flags:
  618. * PADATA_CPU_SERIAL - serial cpumask
  619. * PADATA_CPU_PARALLEL - parallel cpumask
  620. */
  621. int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask)
  622. {
  623. int err;
  624. if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
  625. return -EINVAL;
  626. mutex_lock(&pinst->lock);
  627. get_online_cpus();
  628. if (mask & PADATA_CPU_SERIAL)
  629. cpumask_clear_cpu(cpu, pinst->cpumask.cbcpu);
  630. if (mask & PADATA_CPU_PARALLEL)
  631. cpumask_clear_cpu(cpu, pinst->cpumask.pcpu);
  632. err = __padata_remove_cpu(pinst, cpu);
  633. put_online_cpus();
  634. mutex_unlock(&pinst->lock);
  635. return err;
  636. }
  637. EXPORT_SYMBOL(padata_remove_cpu);
  638. /**
  639. * padata_start - start the parallel processing
  640. *
  641. * @pinst: padata instance to start
  642. */
  643. int padata_start(struct padata_instance *pinst)
  644. {
  645. int err = 0;
  646. mutex_lock(&pinst->lock);
  647. if (pinst->flags & PADATA_INVALID)
  648. err =-EINVAL;
  649. __padata_start(pinst);
  650. mutex_unlock(&pinst->lock);
  651. return err;
  652. }
  653. EXPORT_SYMBOL(padata_start);
  654. /**
  655. * padata_stop - stop the parallel processing
  656. *
  657. * @pinst: padata instance to stop
  658. */
  659. void padata_stop(struct padata_instance *pinst)
  660. {
  661. mutex_lock(&pinst->lock);
  662. __padata_stop(pinst);
  663. mutex_unlock(&pinst->lock);
  664. }
  665. EXPORT_SYMBOL(padata_stop);
  666. #ifdef CONFIG_HOTPLUG_CPU
  667. static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
  668. {
  669. return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
  670. cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
  671. }
  672. static int padata_cpu_callback(struct notifier_block *nfb,
  673. unsigned long action, void *hcpu)
  674. {
  675. int err;
  676. struct padata_instance *pinst;
  677. int cpu = (unsigned long)hcpu;
  678. pinst = container_of(nfb, struct padata_instance, cpu_notifier);
  679. switch (action) {
  680. case CPU_ONLINE:
  681. case CPU_ONLINE_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. if (!pinst_has_cpu(pinst, cpu))
  693. break;
  694. mutex_lock(&pinst->lock);
  695. err = __padata_remove_cpu(pinst, cpu);
  696. mutex_unlock(&pinst->lock);
  697. if (err)
  698. return notifier_from_errno(err);
  699. break;
  700. case CPU_UP_CANCELED:
  701. case CPU_UP_CANCELED_FROZEN:
  702. if (!pinst_has_cpu(pinst, cpu))
  703. break;
  704. mutex_lock(&pinst->lock);
  705. __padata_remove_cpu(pinst, cpu);
  706. mutex_unlock(&pinst->lock);
  707. case CPU_DOWN_FAILED:
  708. case CPU_DOWN_FAILED_FROZEN:
  709. if (!pinst_has_cpu(pinst, cpu))
  710. break;
  711. mutex_lock(&pinst->lock);
  712. __padata_add_cpu(pinst, cpu);
  713. mutex_unlock(&pinst->lock);
  714. }
  715. return NOTIFY_OK;
  716. }
  717. #endif
  718. static void __padata_free(struct padata_instance *pinst)
  719. {
  720. #ifdef CONFIG_HOTPLUG_CPU
  721. unregister_hotcpu_notifier(&pinst->cpu_notifier);
  722. #endif
  723. padata_stop(pinst);
  724. padata_free_pd(pinst->pd);
  725. free_cpumask_var(pinst->cpumask.pcpu);
  726. free_cpumask_var(pinst->cpumask.cbcpu);
  727. kfree(pinst);
  728. }
  729. #define kobj2pinst(_kobj) \
  730. container_of(_kobj, struct padata_instance, kobj)
  731. #define attr2pentry(_attr) \
  732. container_of(_attr, struct padata_sysfs_entry, attr)
  733. static void padata_sysfs_release(struct kobject *kobj)
  734. {
  735. struct padata_instance *pinst = kobj2pinst(kobj);
  736. __padata_free(pinst);
  737. }
  738. struct padata_sysfs_entry {
  739. struct attribute attr;
  740. ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
  741. ssize_t (*store)(struct padata_instance *, struct attribute *,
  742. const char *, size_t);
  743. };
  744. static ssize_t show_cpumask(struct padata_instance *pinst,
  745. struct attribute *attr, char *buf)
  746. {
  747. struct cpumask *cpumask;
  748. ssize_t len;
  749. mutex_lock(&pinst->lock);
  750. if (!strcmp(attr->name, "serial_cpumask"))
  751. cpumask = pinst->cpumask.cbcpu;
  752. else
  753. cpumask = pinst->cpumask.pcpu;
  754. len = bitmap_scnprintf(buf, PAGE_SIZE, cpumask_bits(cpumask),
  755. nr_cpu_ids);
  756. if (PAGE_SIZE - len < 2)
  757. len = -EINVAL;
  758. else
  759. len += sprintf(buf + len, "\n");
  760. mutex_unlock(&pinst->lock);
  761. return len;
  762. }
  763. static ssize_t store_cpumask(struct padata_instance *pinst,
  764. struct attribute *attr,
  765. const char *buf, size_t count)
  766. {
  767. cpumask_var_t new_cpumask;
  768. ssize_t ret;
  769. int mask_type;
  770. if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
  771. return -ENOMEM;
  772. ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
  773. nr_cpumask_bits);
  774. if (ret < 0)
  775. goto out;
  776. mask_type = !strcmp(attr->name, "serial_cpumask") ?
  777. PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
  778. ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
  779. if (!ret)
  780. ret = count;
  781. out:
  782. free_cpumask_var(new_cpumask);
  783. return ret;
  784. }
  785. #define PADATA_ATTR_RW(_name, _show_name, _store_name) \
  786. static struct padata_sysfs_entry _name##_attr = \
  787. __ATTR(_name, 0644, _show_name, _store_name)
  788. #define PADATA_ATTR_RO(_name, _show_name) \
  789. static struct padata_sysfs_entry _name##_attr = \
  790. __ATTR(_name, 0400, _show_name, NULL)
  791. PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
  792. PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
  793. /*
  794. * Padata sysfs provides the following objects:
  795. * serial_cpumask [RW] - cpumask for serial workers
  796. * parallel_cpumask [RW] - cpumask for parallel workers
  797. */
  798. static struct attribute *padata_default_attrs[] = {
  799. &serial_cpumask_attr.attr,
  800. &parallel_cpumask_attr.attr,
  801. NULL,
  802. };
  803. static ssize_t padata_sysfs_show(struct kobject *kobj,
  804. struct attribute *attr, char *buf)
  805. {
  806. struct padata_instance *pinst;
  807. struct padata_sysfs_entry *pentry;
  808. ssize_t ret = -EIO;
  809. pinst = kobj2pinst(kobj);
  810. pentry = attr2pentry(attr);
  811. if (pentry->show)
  812. ret = pentry->show(pinst, attr, buf);
  813. return ret;
  814. }
  815. static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
  816. const char *buf, size_t count)
  817. {
  818. struct padata_instance *pinst;
  819. struct padata_sysfs_entry *pentry;
  820. ssize_t ret = -EIO;
  821. pinst = kobj2pinst(kobj);
  822. pentry = attr2pentry(attr);
  823. if (pentry->show)
  824. ret = pentry->store(pinst, attr, buf, count);
  825. return ret;
  826. }
  827. static const struct sysfs_ops padata_sysfs_ops = {
  828. .show = padata_sysfs_show,
  829. .store = padata_sysfs_store,
  830. };
  831. static struct kobj_type padata_attr_type = {
  832. .sysfs_ops = &padata_sysfs_ops,
  833. .default_attrs = padata_default_attrs,
  834. .release = padata_sysfs_release,
  835. };
  836. /**
  837. * padata_alloc_possible - Allocate and initialize padata instance.
  838. * Use the cpu_possible_mask for serial and
  839. * parallel workers.
  840. *
  841. * @wq: workqueue to use for the allocated padata instance
  842. */
  843. struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq)
  844. {
  845. return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask);
  846. }
  847. EXPORT_SYMBOL(padata_alloc_possible);
  848. /**
  849. * padata_alloc - allocate and initialize a padata instance and specify
  850. * cpumasks for serial and parallel workers.
  851. *
  852. * @wq: workqueue to use for the allocated padata instance
  853. * @pcpumask: cpumask that will be used for padata parallelization
  854. * @cbcpumask: cpumask that will be used for padata serialization
  855. */
  856. struct padata_instance *padata_alloc(struct workqueue_struct *wq,
  857. const struct cpumask *pcpumask,
  858. const struct cpumask *cbcpumask)
  859. {
  860. struct padata_instance *pinst;
  861. struct parallel_data *pd = NULL;
  862. pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
  863. if (!pinst)
  864. goto err;
  865. get_online_cpus();
  866. if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
  867. goto err_free_inst;
  868. if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
  869. free_cpumask_var(pinst->cpumask.pcpu);
  870. goto err_free_inst;
  871. }
  872. if (!padata_validate_cpumask(pinst, pcpumask) ||
  873. !padata_validate_cpumask(pinst, cbcpumask))
  874. goto err_free_masks;
  875. pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
  876. if (!pd)
  877. goto err_free_masks;
  878. rcu_assign_pointer(pinst->pd, pd);
  879. pinst->wq = wq;
  880. cpumask_copy(pinst->cpumask.pcpu, pcpumask);
  881. cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
  882. pinst->flags = 0;
  883. #ifdef CONFIG_HOTPLUG_CPU
  884. pinst->cpu_notifier.notifier_call = padata_cpu_callback;
  885. pinst->cpu_notifier.priority = 0;
  886. register_hotcpu_notifier(&pinst->cpu_notifier);
  887. #endif
  888. put_online_cpus();
  889. BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier);
  890. kobject_init(&pinst->kobj, &padata_attr_type);
  891. mutex_init(&pinst->lock);
  892. return pinst;
  893. err_free_masks:
  894. free_cpumask_var(pinst->cpumask.pcpu);
  895. free_cpumask_var(pinst->cpumask.cbcpu);
  896. err_free_inst:
  897. kfree(pinst);
  898. put_online_cpus();
  899. err:
  900. return NULL;
  901. }
  902. EXPORT_SYMBOL(padata_alloc);
  903. /**
  904. * padata_free - free a padata instance
  905. *
  906. * @padata_inst: padata instance to free
  907. */
  908. void padata_free(struct padata_instance *pinst)
  909. {
  910. kobject_put(&pinst->kobj);
  911. }
  912. EXPORT_SYMBOL(padata_free);