padata.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * padata.h - header for the padata parallelization interface
  3. *
  4. * Copyright (C) 2008, 2009 secunet Security Networks AG
  5. * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #ifndef PADATA_H
  21. #define PADATA_H
  22. #include <linux/workqueue.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/list.h>
  25. #include <linux/timer.h>
  26. #include <linux/notifier.h>
  27. #include <linux/kobject.h>
  28. #define PADATA_CPU_SERIAL 0x01
  29. #define PADATA_CPU_PARALLEL 0x02
  30. /**
  31. * struct padata_priv - Embedded to the users data structure.
  32. *
  33. * @list: List entry, to attach to the padata lists.
  34. * @pd: Pointer to the internal control structure.
  35. * @cb_cpu: Callback cpu for serializatioon.
  36. * @seq_nr: Sequence number of the parallelized data object.
  37. * @info: Used to pass information from the parallel to the serial function.
  38. * @parallel: Parallel execution function.
  39. * @serial: Serial complete function.
  40. */
  41. struct padata_priv {
  42. struct list_head list;
  43. struct parallel_data *pd;
  44. int cb_cpu;
  45. int seq_nr;
  46. int info;
  47. void (*parallel)(struct padata_priv *padata);
  48. void (*serial)(struct padata_priv *padata);
  49. };
  50. /**
  51. * struct padata_list
  52. *
  53. * @list: List head.
  54. * @lock: List lock.
  55. */
  56. struct padata_list {
  57. struct list_head list;
  58. spinlock_t lock;
  59. };
  60. /**
  61. * struct padata_serial_queue - The percpu padata serial queue
  62. *
  63. * @serial: List to wait for serialization after reordering.
  64. * @work: work struct for serialization.
  65. * @pd: Backpointer to the internal control structure.
  66. */
  67. struct padata_serial_queue {
  68. struct padata_list serial;
  69. struct work_struct work;
  70. struct parallel_data *pd;
  71. };
  72. /**
  73. * struct padata_parallel_queue - The percpu padata parallel queue
  74. *
  75. * @parallel: List to wait for parallelization.
  76. * @reorder: List to wait for reordering after parallel processing.
  77. * @serial: List to wait for serialization after reordering.
  78. * @pwork: work struct for parallelization.
  79. * @swork: work struct for serialization.
  80. * @pd: Backpointer to the internal control structure.
  81. * @work: work struct for parallelization.
  82. * @num_obj: Number of objects that are processed by this cpu.
  83. * @cpu_index: Index of the cpu.
  84. */
  85. struct padata_parallel_queue {
  86. struct padata_list parallel;
  87. struct padata_list reorder;
  88. struct parallel_data *pd;
  89. struct work_struct work;
  90. atomic_t num_obj;
  91. int cpu_index;
  92. };
  93. /**
  94. * struct parallel_data - Internal control structure, covers everything
  95. * that depends on the cpumask in use.
  96. *
  97. * @pinst: padata instance.
  98. * @pqueue: percpu padata queues used for parallelization.
  99. * @squeue: percpu padata queues used for serialuzation.
  100. * @seq_nr: The sequence number that will be attached to the next object.
  101. * @reorder_objects: Number of objects waiting in the reorder queues.
  102. * @refcnt: Number of objects holding a reference on this parallel_data.
  103. * @max_seq_nr: Maximal used sequence number.
  104. * @cpumask: Contains two cpumasks: pcpu and cbcpu for
  105. * parallel and serial workers respectively.
  106. * @lock: Reorder lock.
  107. * @processed: Number of already processed objects.
  108. * @timer: Reorder timer.
  109. */
  110. struct parallel_data {
  111. struct padata_instance *pinst;
  112. struct padata_parallel_queue *pqueue;
  113. struct padata_serial_queue *squeue;
  114. atomic_t seq_nr;
  115. atomic_t reorder_objects;
  116. atomic_t refcnt;
  117. unsigned int max_seq_nr;
  118. struct {
  119. cpumask_var_t pcpu;
  120. cpumask_var_t cbcpu;
  121. } cpumask;
  122. spinlock_t lock ____cacheline_aligned;
  123. unsigned int processed;
  124. struct timer_list timer;
  125. };
  126. /**
  127. * struct padata_instance - The overall control structure.
  128. *
  129. * @cpu_notifier: cpu hotplug notifier.
  130. * @wq: The workqueue in use.
  131. * @pd: The internal control structure.
  132. * @cpumask: User supplied cpumask. Contains two cpumasks: pcpu and
  133. * cbcpu for parallel and serial works respectivly.
  134. * @cpumask_change_notifier: Notifiers chain for user-defined notify
  135. * callbacks that will be called when either @pcpu or @cbcpu
  136. * or both cpumasks change.
  137. * @kobj: padata instance kernel object.
  138. * @lock: padata instance lock.
  139. * @flags: padata flags.
  140. */
  141. struct padata_instance {
  142. struct notifier_block cpu_notifier;
  143. struct workqueue_struct *wq;
  144. struct parallel_data *pd;
  145. struct {
  146. cpumask_var_t pcpu;
  147. cpumask_var_t cbcpu;
  148. } cpumask;
  149. struct blocking_notifier_head cpumask_change_notifier;
  150. struct kobject kobj;
  151. struct mutex lock;
  152. u8 flags;
  153. #define PADATA_INIT 1
  154. #define PADATA_RESET 2
  155. #define PADATA_INVALID 4
  156. };
  157. extern struct padata_instance *padata_alloc_possible(
  158. struct workqueue_struct *wq);
  159. extern struct padata_instance *padata_alloc(struct workqueue_struct *wq,
  160. const struct cpumask *pcpumask,
  161. const struct cpumask *cbcpumask);
  162. extern void padata_free(struct padata_instance *pinst);
  163. extern int padata_do_parallel(struct padata_instance *pinst,
  164. struct padata_priv *padata, int cb_cpu);
  165. extern void padata_do_serial(struct padata_priv *padata);
  166. extern int padata_get_cpumask(struct padata_instance *pinst,
  167. int cpumask_type, struct cpumask *out_mask);
  168. extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
  169. cpumask_var_t cpumask);
  170. extern int padata_set_cpumasks(struct padata_instance *pinst,
  171. cpumask_var_t pcpumask,
  172. cpumask_var_t cbcpumask);
  173. extern int padata_add_cpu(struct padata_instance *pinst, int cpu, int mask);
  174. extern int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask);
  175. extern int padata_start(struct padata_instance *pinst);
  176. extern void padata_stop(struct padata_instance *pinst);
  177. extern int padata_register_cpumask_notifier(struct padata_instance *pinst,
  178. struct notifier_block *nblock);
  179. extern int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
  180. struct notifier_block *nblock);
  181. #endif