padata.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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_cpumask: Sets specified by @cpumask_type cpumask to the value
  494. * equivalent to @cpumask.
  495. *
  496. * @pinst: padata instance
  497. * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
  498. * to parallel and serial cpumasks respectively.
  499. * @cpumask: the cpumask to use
  500. */
  501. int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
  502. cpumask_var_t cpumask)
  503. {
  504. struct cpumask *serial_mask, *parallel_mask;
  505. int err = -EINVAL;
  506. mutex_lock(&pinst->lock);
  507. get_online_cpus();
  508. switch (cpumask_type) {
  509. case PADATA_CPU_PARALLEL:
  510. serial_mask = pinst->cpumask.cbcpu;
  511. parallel_mask = cpumask;
  512. break;
  513. case PADATA_CPU_SERIAL:
  514. parallel_mask = pinst->cpumask.pcpu;
  515. serial_mask = cpumask;
  516. break;
  517. default:
  518. goto out;
  519. }
  520. err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
  521. out:
  522. put_online_cpus();
  523. mutex_unlock(&pinst->lock);
  524. return err;
  525. }
  526. EXPORT_SYMBOL(padata_set_cpumask);
  527. /**
  528. * padata_start - start the parallel processing
  529. *
  530. * @pinst: padata instance to start
  531. */
  532. int padata_start(struct padata_instance *pinst)
  533. {
  534. int err = 0;
  535. mutex_lock(&pinst->lock);
  536. if (pinst->flags & PADATA_INVALID)
  537. err = -EINVAL;
  538. __padata_start(pinst);
  539. mutex_unlock(&pinst->lock);
  540. return err;
  541. }
  542. EXPORT_SYMBOL(padata_start);
  543. /**
  544. * padata_stop - stop the parallel processing
  545. *
  546. * @pinst: padata instance to stop
  547. */
  548. void padata_stop(struct padata_instance *pinst)
  549. {
  550. mutex_lock(&pinst->lock);
  551. __padata_stop(pinst);
  552. mutex_unlock(&pinst->lock);
  553. }
  554. EXPORT_SYMBOL(padata_stop);
  555. #ifdef CONFIG_HOTPLUG_CPU
  556. static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
  557. {
  558. struct parallel_data *pd;
  559. if (cpumask_test_cpu(cpu, cpu_online_mask)) {
  560. pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
  561. pinst->cpumask.cbcpu);
  562. if (!pd)
  563. return -ENOMEM;
  564. padata_replace(pinst, pd);
  565. if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
  566. padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  567. __padata_start(pinst);
  568. }
  569. return 0;
  570. }
  571. static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
  572. {
  573. struct parallel_data *pd = NULL;
  574. if (cpumask_test_cpu(cpu, cpu_online_mask)) {
  575. if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
  576. !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  577. __padata_stop(pinst);
  578. pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
  579. pinst->cpumask.cbcpu);
  580. if (!pd)
  581. return -ENOMEM;
  582. padata_replace(pinst, pd);
  583. cpumask_clear_cpu(cpu, pd->cpumask.cbcpu);
  584. cpumask_clear_cpu(cpu, pd->cpumask.pcpu);
  585. }
  586. return 0;
  587. }
  588. /**
  589. * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
  590. * padata cpumasks.
  591. *
  592. * @pinst: padata instance
  593. * @cpu: cpu to remove
  594. * @mask: bitmask specifying from which cpumask @cpu should be removed
  595. * The @mask may be any combination of the following flags:
  596. * PADATA_CPU_SERIAL - serial cpumask
  597. * PADATA_CPU_PARALLEL - parallel cpumask
  598. */
  599. int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask)
  600. {
  601. int err;
  602. if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
  603. return -EINVAL;
  604. mutex_lock(&pinst->lock);
  605. get_online_cpus();
  606. if (mask & PADATA_CPU_SERIAL)
  607. cpumask_clear_cpu(cpu, pinst->cpumask.cbcpu);
  608. if (mask & PADATA_CPU_PARALLEL)
  609. cpumask_clear_cpu(cpu, pinst->cpumask.pcpu);
  610. err = __padata_remove_cpu(pinst, cpu);
  611. put_online_cpus();
  612. mutex_unlock(&pinst->lock);
  613. return err;
  614. }
  615. EXPORT_SYMBOL(padata_remove_cpu);
  616. static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
  617. {
  618. return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
  619. cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
  620. }
  621. static int padata_cpu_callback(struct notifier_block *nfb,
  622. unsigned long action, void *hcpu)
  623. {
  624. int err;
  625. struct padata_instance *pinst;
  626. int cpu = (unsigned long)hcpu;
  627. pinst = container_of(nfb, struct padata_instance, cpu_notifier);
  628. switch (action) {
  629. case CPU_ONLINE:
  630. case CPU_ONLINE_FROZEN:
  631. case CPU_DOWN_FAILED:
  632. case CPU_DOWN_FAILED_FROZEN:
  633. if (!pinst_has_cpu(pinst, cpu))
  634. break;
  635. mutex_lock(&pinst->lock);
  636. err = __padata_add_cpu(pinst, cpu);
  637. mutex_unlock(&pinst->lock);
  638. if (err)
  639. return notifier_from_errno(err);
  640. break;
  641. case CPU_DOWN_PREPARE:
  642. case CPU_DOWN_PREPARE_FROZEN:
  643. case CPU_UP_CANCELED:
  644. case CPU_UP_CANCELED_FROZEN:
  645. if (!pinst_has_cpu(pinst, cpu))
  646. break;
  647. mutex_lock(&pinst->lock);
  648. err = __padata_remove_cpu(pinst, cpu);
  649. mutex_unlock(&pinst->lock);
  650. if (err)
  651. return notifier_from_errno(err);
  652. break;
  653. }
  654. return NOTIFY_OK;
  655. }
  656. #endif
  657. static void __padata_free(struct padata_instance *pinst)
  658. {
  659. #ifdef CONFIG_HOTPLUG_CPU
  660. unregister_hotcpu_notifier(&pinst->cpu_notifier);
  661. #endif
  662. padata_stop(pinst);
  663. padata_free_pd(pinst->pd);
  664. free_cpumask_var(pinst->cpumask.pcpu);
  665. free_cpumask_var(pinst->cpumask.cbcpu);
  666. kfree(pinst);
  667. }
  668. #define kobj2pinst(_kobj) \
  669. container_of(_kobj, struct padata_instance, kobj)
  670. #define attr2pentry(_attr) \
  671. container_of(_attr, struct padata_sysfs_entry, attr)
  672. static void padata_sysfs_release(struct kobject *kobj)
  673. {
  674. struct padata_instance *pinst = kobj2pinst(kobj);
  675. __padata_free(pinst);
  676. }
  677. struct padata_sysfs_entry {
  678. struct attribute attr;
  679. ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
  680. ssize_t (*store)(struct padata_instance *, struct attribute *,
  681. const char *, size_t);
  682. };
  683. static ssize_t show_cpumask(struct padata_instance *pinst,
  684. struct attribute *attr, char *buf)
  685. {
  686. struct cpumask *cpumask;
  687. ssize_t len;
  688. mutex_lock(&pinst->lock);
  689. if (!strcmp(attr->name, "serial_cpumask"))
  690. cpumask = pinst->cpumask.cbcpu;
  691. else
  692. cpumask = pinst->cpumask.pcpu;
  693. len = snprintf(buf, PAGE_SIZE, "%*pb\n",
  694. nr_cpu_ids, cpumask_bits(cpumask));
  695. mutex_unlock(&pinst->lock);
  696. return len < PAGE_SIZE ? len : -EINVAL;
  697. }
  698. static ssize_t store_cpumask(struct padata_instance *pinst,
  699. struct attribute *attr,
  700. const char *buf, size_t count)
  701. {
  702. cpumask_var_t new_cpumask;
  703. ssize_t ret;
  704. int mask_type;
  705. if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
  706. return -ENOMEM;
  707. ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
  708. nr_cpumask_bits);
  709. if (ret < 0)
  710. goto out;
  711. mask_type = !strcmp(attr->name, "serial_cpumask") ?
  712. PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
  713. ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
  714. if (!ret)
  715. ret = count;
  716. out:
  717. free_cpumask_var(new_cpumask);
  718. return ret;
  719. }
  720. #define PADATA_ATTR_RW(_name, _show_name, _store_name) \
  721. static struct padata_sysfs_entry _name##_attr = \
  722. __ATTR(_name, 0644, _show_name, _store_name)
  723. #define PADATA_ATTR_RO(_name, _show_name) \
  724. static struct padata_sysfs_entry _name##_attr = \
  725. __ATTR(_name, 0400, _show_name, NULL)
  726. PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
  727. PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
  728. /*
  729. * Padata sysfs provides the following objects:
  730. * serial_cpumask [RW] - cpumask for serial workers
  731. * parallel_cpumask [RW] - cpumask for parallel workers
  732. */
  733. static struct attribute *padata_default_attrs[] = {
  734. &serial_cpumask_attr.attr,
  735. &parallel_cpumask_attr.attr,
  736. NULL,
  737. };
  738. static ssize_t padata_sysfs_show(struct kobject *kobj,
  739. struct attribute *attr, char *buf)
  740. {
  741. struct padata_instance *pinst;
  742. struct padata_sysfs_entry *pentry;
  743. ssize_t ret = -EIO;
  744. pinst = kobj2pinst(kobj);
  745. pentry = attr2pentry(attr);
  746. if (pentry->show)
  747. ret = pentry->show(pinst, attr, buf);
  748. return ret;
  749. }
  750. static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
  751. const char *buf, size_t count)
  752. {
  753. struct padata_instance *pinst;
  754. struct padata_sysfs_entry *pentry;
  755. ssize_t ret = -EIO;
  756. pinst = kobj2pinst(kobj);
  757. pentry = attr2pentry(attr);
  758. if (pentry->show)
  759. ret = pentry->store(pinst, attr, buf, count);
  760. return ret;
  761. }
  762. static const struct sysfs_ops padata_sysfs_ops = {
  763. .show = padata_sysfs_show,
  764. .store = padata_sysfs_store,
  765. };
  766. static struct kobj_type padata_attr_type = {
  767. .sysfs_ops = &padata_sysfs_ops,
  768. .default_attrs = padata_default_attrs,
  769. .release = padata_sysfs_release,
  770. };
  771. /**
  772. * padata_alloc_possible - Allocate and initialize padata instance.
  773. * Use the cpu_possible_mask for serial and
  774. * parallel workers.
  775. *
  776. * @wq: workqueue to use for the allocated padata instance
  777. */
  778. struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq)
  779. {
  780. return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask);
  781. }
  782. EXPORT_SYMBOL(padata_alloc_possible);
  783. /**
  784. * padata_alloc - allocate and initialize a padata instance and specify
  785. * cpumasks for serial and parallel workers.
  786. *
  787. * @wq: workqueue to use for the allocated padata instance
  788. * @pcpumask: cpumask that will be used for padata parallelization
  789. * @cbcpumask: cpumask that will be used for padata serialization
  790. */
  791. struct padata_instance *padata_alloc(struct workqueue_struct *wq,
  792. const struct cpumask *pcpumask,
  793. const struct cpumask *cbcpumask)
  794. {
  795. struct padata_instance *pinst;
  796. struct parallel_data *pd = NULL;
  797. pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
  798. if (!pinst)
  799. goto err;
  800. get_online_cpus();
  801. if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
  802. goto err_free_inst;
  803. if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
  804. free_cpumask_var(pinst->cpumask.pcpu);
  805. goto err_free_inst;
  806. }
  807. if (!padata_validate_cpumask(pinst, pcpumask) ||
  808. !padata_validate_cpumask(pinst, cbcpumask))
  809. goto err_free_masks;
  810. pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
  811. if (!pd)
  812. goto err_free_masks;
  813. rcu_assign_pointer(pinst->pd, pd);
  814. pinst->wq = wq;
  815. cpumask_copy(pinst->cpumask.pcpu, pcpumask);
  816. cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
  817. pinst->flags = 0;
  818. put_online_cpus();
  819. BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier);
  820. kobject_init(&pinst->kobj, &padata_attr_type);
  821. mutex_init(&pinst->lock);
  822. #ifdef CONFIG_HOTPLUG_CPU
  823. pinst->cpu_notifier.notifier_call = padata_cpu_callback;
  824. pinst->cpu_notifier.priority = 0;
  825. register_hotcpu_notifier(&pinst->cpu_notifier);
  826. #endif
  827. return pinst;
  828. err_free_masks:
  829. free_cpumask_var(pinst->cpumask.pcpu);
  830. free_cpumask_var(pinst->cpumask.cbcpu);
  831. err_free_inst:
  832. kfree(pinst);
  833. put_online_cpus();
  834. err:
  835. return NULL;
  836. }
  837. /**
  838. * padata_free - free a padata instance
  839. *
  840. * @padata_inst: padata instance to free
  841. */
  842. void padata_free(struct padata_instance *pinst)
  843. {
  844. kobject_put(&pinst->kobj);
  845. }
  846. EXPORT_SYMBOL(padata_free);