padata.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /*
  2. * padata.c - generic interface to process data streams in parallel
  3. *
  4. * Copyright (C) 2008, 2009 secunet Security Networks AG
  5. * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <linux/export.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/err.h>
  23. #include <linux/cpu.h>
  24. #include <linux/padata.h>
  25. #include <linux/mutex.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/rcupdate.h>
  30. #define MAX_OBJ_NUM 1000
  31. static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
  32. {
  33. int cpu, target_cpu;
  34. target_cpu = cpumask_first(pd->cpumask.pcpu);
  35. for (cpu = 0; cpu < cpu_index; cpu++)
  36. target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu);
  37. return target_cpu;
  38. }
  39. static int padata_cpu_hash(struct parallel_data *pd)
  40. {
  41. int cpu_index;
  42. /*
  43. * Hash the sequence numbers to the cpus by taking
  44. * seq_nr mod. number of cpus in use.
  45. */
  46. spin_lock(&pd->seq_lock);
  47. cpu_index = pd->seq_nr % cpumask_weight(pd->cpumask.pcpu);
  48. pd->seq_nr++;
  49. spin_unlock(&pd->seq_lock);
  50. return padata_index_to_cpu(pd, cpu_index);
  51. }
  52. static void padata_parallel_worker(struct work_struct *parallel_work)
  53. {
  54. struct padata_parallel_queue *pqueue;
  55. struct parallel_data *pd;
  56. struct padata_instance *pinst;
  57. LIST_HEAD(local_list);
  58. local_bh_disable();
  59. pqueue = container_of(parallel_work,
  60. struct padata_parallel_queue, work);
  61. pd = pqueue->pd;
  62. pinst = pd->pinst;
  63. spin_lock(&pqueue->parallel.lock);
  64. list_replace_init(&pqueue->parallel.list, &local_list);
  65. spin_unlock(&pqueue->parallel.lock);
  66. while (!list_empty(&local_list)) {
  67. struct padata_priv *padata;
  68. padata = list_entry(local_list.next,
  69. struct padata_priv, list);
  70. list_del_init(&padata->list);
  71. padata->parallel(padata);
  72. }
  73. local_bh_enable();
  74. }
  75. /**
  76. * padata_do_parallel - padata parallelization function
  77. *
  78. * @pinst: padata instance
  79. * @padata: object to be parallelized
  80. * @cb_cpu: cpu the serialization callback function will run on,
  81. * must be in the serial cpumask of padata(i.e. cpumask.cbcpu).
  82. *
  83. * The parallelization callback function will run with BHs off.
  84. * Note: Every object which is parallelized by padata_do_parallel
  85. * must be seen by padata_do_serial.
  86. */
  87. int padata_do_parallel(struct padata_instance *pinst,
  88. struct padata_priv *padata, int cb_cpu)
  89. {
  90. int target_cpu, err;
  91. struct padata_parallel_queue *queue;
  92. struct parallel_data *pd;
  93. rcu_read_lock_bh();
  94. pd = rcu_dereference(pinst->pd);
  95. err = -EINVAL;
  96. if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
  97. goto out;
  98. if (!cpumask_test_cpu(cb_cpu, pd->cpumask.cbcpu))
  99. goto out;
  100. err = -EBUSY;
  101. if ((pinst->flags & PADATA_RESET))
  102. goto out;
  103. if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM)
  104. goto out;
  105. err = 0;
  106. atomic_inc(&pd->refcnt);
  107. padata->pd = pd;
  108. padata->cb_cpu = cb_cpu;
  109. target_cpu = padata_cpu_hash(pd);
  110. queue = per_cpu_ptr(pd->pqueue, target_cpu);
  111. spin_lock(&queue->parallel.lock);
  112. list_add_tail(&padata->list, &queue->parallel.list);
  113. spin_unlock(&queue->parallel.lock);
  114. queue_work_on(target_cpu, pinst->wq, &queue->work);
  115. out:
  116. rcu_read_unlock_bh();
  117. return err;
  118. }
  119. EXPORT_SYMBOL(padata_do_parallel);
  120. /*
  121. * padata_get_next - Get the next object that needs serialization.
  122. *
  123. * Return values are:
  124. *
  125. * A pointer to the control struct of the next object that needs
  126. * serialization, if present in one of the percpu reorder queues.
  127. *
  128. * NULL, if all percpu reorder queues are empty.
  129. *
  130. * -EINPROGRESS, if the next object that needs serialization will
  131. * be parallel processed by another cpu and is not yet present in
  132. * the cpu's reorder queue.
  133. *
  134. * -ENODATA, if this cpu has to do the parallel processing for
  135. * the next object.
  136. */
  137. static struct padata_priv *padata_get_next(struct parallel_data *pd)
  138. {
  139. int cpu, num_cpus;
  140. unsigned int next_nr, next_index;
  141. struct padata_parallel_queue *queue, *next_queue;
  142. struct padata_priv *padata;
  143. struct padata_list *reorder;
  144. num_cpus = cpumask_weight(pd->cpumask.pcpu);
  145. /*
  146. * Calculate the percpu reorder queue and the sequence
  147. * number of the next object.
  148. */
  149. next_nr = pd->processed;
  150. next_index = next_nr % num_cpus;
  151. cpu = padata_index_to_cpu(pd, next_index);
  152. next_queue = per_cpu_ptr(pd->pqueue, cpu);
  153. padata = NULL;
  154. reorder = &next_queue->reorder;
  155. if (!list_empty(&reorder->list)) {
  156. padata = list_entry(reorder->list.next,
  157. struct padata_priv, list);
  158. spin_lock(&reorder->lock);
  159. list_del_init(&padata->list);
  160. atomic_dec(&pd->reorder_objects);
  161. spin_unlock(&reorder->lock);
  162. pd->processed++;
  163. goto out;
  164. }
  165. queue = per_cpu_ptr(pd->pqueue, smp_processor_id());
  166. if (queue->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_active_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_active_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. pd->seq_nr = 0;
  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_active_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_active_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. }
  604. return 0;
  605. }
  606. /**
  607. * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
  608. * padata cpumasks.
  609. *
  610. * @pinst: padata instance
  611. * @cpu: cpu to remove
  612. * @mask: bitmask specifying from which cpumask @cpu should be removed
  613. * The @mask may be any combination of the following flags:
  614. * PADATA_CPU_SERIAL - serial cpumask
  615. * PADATA_CPU_PARALLEL - parallel cpumask
  616. */
  617. int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask)
  618. {
  619. int err;
  620. if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
  621. return -EINVAL;
  622. mutex_lock(&pinst->lock);
  623. get_online_cpus();
  624. if (mask & PADATA_CPU_SERIAL)
  625. cpumask_clear_cpu(cpu, pinst->cpumask.cbcpu);
  626. if (mask & PADATA_CPU_PARALLEL)
  627. cpumask_clear_cpu(cpu, pinst->cpumask.pcpu);
  628. err = __padata_remove_cpu(pinst, cpu);
  629. put_online_cpus();
  630. mutex_unlock(&pinst->lock);
  631. return err;
  632. }
  633. EXPORT_SYMBOL(padata_remove_cpu);
  634. /**
  635. * padata_start - start the parallel processing
  636. *
  637. * @pinst: padata instance to start
  638. */
  639. int padata_start(struct padata_instance *pinst)
  640. {
  641. int err = 0;
  642. mutex_lock(&pinst->lock);
  643. if (pinst->flags & PADATA_INVALID)
  644. err =-EINVAL;
  645. __padata_start(pinst);
  646. mutex_unlock(&pinst->lock);
  647. return err;
  648. }
  649. EXPORT_SYMBOL(padata_start);
  650. /**
  651. * padata_stop - stop the parallel processing
  652. *
  653. * @pinst: padata instance to stop
  654. */
  655. void padata_stop(struct padata_instance *pinst)
  656. {
  657. mutex_lock(&pinst->lock);
  658. __padata_stop(pinst);
  659. mutex_unlock(&pinst->lock);
  660. }
  661. EXPORT_SYMBOL(padata_stop);
  662. #ifdef CONFIG_HOTPLUG_CPU
  663. static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
  664. {
  665. return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
  666. cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
  667. }
  668. static int padata_cpu_callback(struct notifier_block *nfb,
  669. unsigned long action, void *hcpu)
  670. {
  671. int err;
  672. struct padata_instance *pinst;
  673. int cpu = (unsigned long)hcpu;
  674. pinst = container_of(nfb, struct padata_instance, cpu_notifier);
  675. switch (action) {
  676. case CPU_ONLINE:
  677. case CPU_ONLINE_FROZEN:
  678. if (!pinst_has_cpu(pinst, cpu))
  679. break;
  680. mutex_lock(&pinst->lock);
  681. err = __padata_add_cpu(pinst, cpu);
  682. mutex_unlock(&pinst->lock);
  683. if (err)
  684. return notifier_from_errno(err);
  685. break;
  686. case CPU_DOWN_PREPARE:
  687. case CPU_DOWN_PREPARE_FROZEN:
  688. if (!pinst_has_cpu(pinst, cpu))
  689. break;
  690. mutex_lock(&pinst->lock);
  691. err = __padata_remove_cpu(pinst, cpu);
  692. mutex_unlock(&pinst->lock);
  693. if (err)
  694. return notifier_from_errno(err);
  695. break;
  696. case CPU_UP_CANCELED:
  697. case CPU_UP_CANCELED_FROZEN:
  698. if (!pinst_has_cpu(pinst, cpu))
  699. break;
  700. mutex_lock(&pinst->lock);
  701. __padata_remove_cpu(pinst, cpu);
  702. mutex_unlock(&pinst->lock);
  703. case CPU_DOWN_FAILED:
  704. case CPU_DOWN_FAILED_FROZEN:
  705. if (!pinst_has_cpu(pinst, cpu))
  706. break;
  707. mutex_lock(&pinst->lock);
  708. __padata_add_cpu(pinst, cpu);
  709. mutex_unlock(&pinst->lock);
  710. }
  711. return NOTIFY_OK;
  712. }
  713. #endif
  714. static void __padata_free(struct padata_instance *pinst)
  715. {
  716. #ifdef CONFIG_HOTPLUG_CPU
  717. unregister_hotcpu_notifier(&pinst->cpu_notifier);
  718. #endif
  719. padata_stop(pinst);
  720. padata_free_pd(pinst->pd);
  721. free_cpumask_var(pinst->cpumask.pcpu);
  722. free_cpumask_var(pinst->cpumask.cbcpu);
  723. kfree(pinst);
  724. }
  725. #define kobj2pinst(_kobj) \
  726. container_of(_kobj, struct padata_instance, kobj)
  727. #define attr2pentry(_attr) \
  728. container_of(_attr, struct padata_sysfs_entry, attr)
  729. static void padata_sysfs_release(struct kobject *kobj)
  730. {
  731. struct padata_instance *pinst = kobj2pinst(kobj);
  732. __padata_free(pinst);
  733. }
  734. struct padata_sysfs_entry {
  735. struct attribute attr;
  736. ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
  737. ssize_t (*store)(struct padata_instance *, struct attribute *,
  738. const char *, size_t);
  739. };
  740. static ssize_t show_cpumask(struct padata_instance *pinst,
  741. struct attribute *attr, char *buf)
  742. {
  743. struct cpumask *cpumask;
  744. ssize_t len;
  745. mutex_lock(&pinst->lock);
  746. if (!strcmp(attr->name, "serial_cpumask"))
  747. cpumask = pinst->cpumask.cbcpu;
  748. else
  749. cpumask = pinst->cpumask.pcpu;
  750. len = bitmap_scnprintf(buf, PAGE_SIZE, cpumask_bits(cpumask),
  751. nr_cpu_ids);
  752. if (PAGE_SIZE - len < 2)
  753. len = -EINVAL;
  754. else
  755. len += sprintf(buf + len, "\n");
  756. mutex_unlock(&pinst->lock);
  757. return len;
  758. }
  759. static ssize_t store_cpumask(struct padata_instance *pinst,
  760. struct attribute *attr,
  761. const char *buf, size_t count)
  762. {
  763. cpumask_var_t new_cpumask;
  764. ssize_t ret;
  765. int mask_type;
  766. if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
  767. return -ENOMEM;
  768. ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
  769. nr_cpumask_bits);
  770. if (ret < 0)
  771. goto out;
  772. mask_type = !strcmp(attr->name, "serial_cpumask") ?
  773. PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
  774. ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
  775. if (!ret)
  776. ret = count;
  777. out:
  778. free_cpumask_var(new_cpumask);
  779. return ret;
  780. }
  781. #define PADATA_ATTR_RW(_name, _show_name, _store_name) \
  782. static struct padata_sysfs_entry _name##_attr = \
  783. __ATTR(_name, 0644, _show_name, _store_name)
  784. #define PADATA_ATTR_RO(_name, _show_name) \
  785. static struct padata_sysfs_entry _name##_attr = \
  786. __ATTR(_name, 0400, _show_name, NULL)
  787. PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
  788. PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
  789. /*
  790. * Padata sysfs provides the following objects:
  791. * serial_cpumask [RW] - cpumask for serial workers
  792. * parallel_cpumask [RW] - cpumask for parallel workers
  793. */
  794. static struct attribute *padata_default_attrs[] = {
  795. &serial_cpumask_attr.attr,
  796. &parallel_cpumask_attr.attr,
  797. NULL,
  798. };
  799. static ssize_t padata_sysfs_show(struct kobject *kobj,
  800. struct attribute *attr, char *buf)
  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->show(pinst, attr, buf);
  809. return ret;
  810. }
  811. static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
  812. const char *buf, size_t count)
  813. {
  814. struct padata_instance *pinst;
  815. struct padata_sysfs_entry *pentry;
  816. ssize_t ret = -EIO;
  817. pinst = kobj2pinst(kobj);
  818. pentry = attr2pentry(attr);
  819. if (pentry->show)
  820. ret = pentry->store(pinst, attr, buf, count);
  821. return ret;
  822. }
  823. static const struct sysfs_ops padata_sysfs_ops = {
  824. .show = padata_sysfs_show,
  825. .store = padata_sysfs_store,
  826. };
  827. static struct kobj_type padata_attr_type = {
  828. .sysfs_ops = &padata_sysfs_ops,
  829. .default_attrs = padata_default_attrs,
  830. .release = padata_sysfs_release,
  831. };
  832. /**
  833. * padata_alloc_possible - Allocate and initialize padata instance.
  834. * Use the cpu_possible_mask for serial and
  835. * parallel workers.
  836. *
  837. * @wq: workqueue to use for the allocated padata instance
  838. */
  839. struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq)
  840. {
  841. return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask);
  842. }
  843. EXPORT_SYMBOL(padata_alloc_possible);
  844. /**
  845. * padata_alloc - allocate and initialize a padata instance and specify
  846. * cpumasks for serial and parallel workers.
  847. *
  848. * @wq: workqueue to use for the allocated padata instance
  849. * @pcpumask: cpumask that will be used for padata parallelization
  850. * @cbcpumask: cpumask that will be used for padata serialization
  851. */
  852. struct padata_instance *padata_alloc(struct workqueue_struct *wq,
  853. const struct cpumask *pcpumask,
  854. const struct cpumask *cbcpumask)
  855. {
  856. struct padata_instance *pinst;
  857. struct parallel_data *pd = NULL;
  858. pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
  859. if (!pinst)
  860. goto err;
  861. get_online_cpus();
  862. if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
  863. goto err_free_inst;
  864. if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
  865. free_cpumask_var(pinst->cpumask.pcpu);
  866. goto err_free_inst;
  867. }
  868. if (!padata_validate_cpumask(pinst, pcpumask) ||
  869. !padata_validate_cpumask(pinst, cbcpumask))
  870. goto err_free_masks;
  871. pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
  872. if (!pd)
  873. goto err_free_masks;
  874. rcu_assign_pointer(pinst->pd, pd);
  875. pinst->wq = wq;
  876. cpumask_copy(pinst->cpumask.pcpu, pcpumask);
  877. cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
  878. pinst->flags = 0;
  879. #ifdef CONFIG_HOTPLUG_CPU
  880. pinst->cpu_notifier.notifier_call = padata_cpu_callback;
  881. pinst->cpu_notifier.priority = 0;
  882. register_hotcpu_notifier(&pinst->cpu_notifier);
  883. #endif
  884. put_online_cpus();
  885. BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier);
  886. kobject_init(&pinst->kobj, &padata_attr_type);
  887. mutex_init(&pinst->lock);
  888. return pinst;
  889. err_free_masks:
  890. free_cpumask_var(pinst->cpumask.pcpu);
  891. free_cpumask_var(pinst->cpumask.cbcpu);
  892. err_free_inst:
  893. kfree(pinst);
  894. put_online_cpus();
  895. err:
  896. return NULL;
  897. }
  898. EXPORT_SYMBOL(padata_alloc);
  899. /**
  900. * padata_free - free a padata instance
  901. *
  902. * @padata_inst: padata instance to free
  903. */
  904. void padata_free(struct padata_instance *pinst)
  905. {
  906. kobject_put(&pinst->kobj);
  907. }
  908. EXPORT_SYMBOL(padata_free);