padata.c 25 KB

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