padata.c 25 KB

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