padata.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * padata.h - header for the padata parallelization interface
  4. *
  5. * Copyright (C) 2008, 2009 secunet Security Networks AG
  6. * Copyright (C) 2008, 2009 Steffen Klassert <[email protected]>
  7. *
  8. * Copyright (c) 2020 Oracle and/or its affiliates.
  9. * Author: Daniel Jordan <[email protected]>
  10. */
  11. #ifndef PADATA_H
  12. #define PADATA_H
  13. #include <linux/refcount.h>
  14. #include <linux/compiler_types.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/list.h>
  18. #include <linux/kobject.h>
  19. #define PADATA_CPU_SERIAL 0x01
  20. #define PADATA_CPU_PARALLEL 0x02
  21. /**
  22. * struct padata_priv - Represents one job
  23. *
  24. * @list: List entry, to attach to the padata lists.
  25. * @pd: Pointer to the internal control structure.
  26. * @cb_cpu: Callback cpu for serializatioon.
  27. * @seq_nr: Sequence number of the parallelized data object.
  28. * @info: Used to pass information from the parallel to the serial function.
  29. * @parallel: Parallel execution function.
  30. * @serial: Serial complete function.
  31. */
  32. struct padata_priv {
  33. struct list_head list;
  34. struct parallel_data *pd;
  35. int cb_cpu;
  36. unsigned int seq_nr;
  37. int info;
  38. void (*parallel)(struct padata_priv *padata);
  39. void (*serial)(struct padata_priv *padata);
  40. };
  41. /**
  42. * struct padata_list - one per work type per CPU
  43. *
  44. * @list: List head.
  45. * @lock: List lock.
  46. */
  47. struct padata_list {
  48. struct list_head list;
  49. spinlock_t lock;
  50. };
  51. /**
  52. * struct padata_serial_queue - The percpu padata serial queue
  53. *
  54. * @serial: List to wait for serialization after reordering.
  55. * @work: work struct for serialization.
  56. * @pd: Backpointer to the internal control structure.
  57. */
  58. struct padata_serial_queue {
  59. struct padata_list serial;
  60. struct work_struct work;
  61. struct parallel_data *pd;
  62. };
  63. /**
  64. * struct padata_cpumask - The cpumasks for the parallel/serial workers
  65. *
  66. * @pcpu: cpumask for the parallel workers.
  67. * @cbcpu: cpumask for the serial (callback) workers.
  68. */
  69. struct padata_cpumask {
  70. cpumask_var_t pcpu;
  71. cpumask_var_t cbcpu;
  72. };
  73. /**
  74. * struct parallel_data - Internal control structure, covers everything
  75. * that depends on the cpumask in use.
  76. *
  77. * @ps: padata_shell object.
  78. * @reorder_list: percpu reorder lists
  79. * @squeue: percpu padata queues used for serialuzation.
  80. * @refcnt: Number of objects holding a reference on this parallel_data.
  81. * @seq_nr: Sequence number of the parallelized data object.
  82. * @processed: Number of already processed objects.
  83. * @cpu: Next CPU to be processed.
  84. * @cpumask: The cpumasks in use for parallel and serial workers.
  85. * @reorder_work: work struct for reordering.
  86. * @lock: Reorder lock.
  87. */
  88. struct parallel_data {
  89. struct padata_shell *ps;
  90. struct padata_list __percpu *reorder_list;
  91. struct padata_serial_queue __percpu *squeue;
  92. refcount_t refcnt;
  93. unsigned int seq_nr;
  94. unsigned int processed;
  95. int cpu;
  96. struct padata_cpumask cpumask;
  97. struct work_struct reorder_work;
  98. spinlock_t ____cacheline_aligned lock;
  99. };
  100. /**
  101. * struct padata_shell - Wrapper around struct parallel_data, its
  102. * purpose is to allow the underlying control structure to be replaced
  103. * on the fly using RCU.
  104. *
  105. * @pinst: padat instance.
  106. * @pd: Actual parallel_data structure which may be substituted on the fly.
  107. * @opd: Pointer to old pd to be freed by padata_replace.
  108. * @list: List entry in padata_instance list.
  109. */
  110. struct padata_shell {
  111. struct padata_instance *pinst;
  112. struct parallel_data __rcu *pd;
  113. struct parallel_data *opd;
  114. struct list_head list;
  115. };
  116. /**
  117. * struct padata_mt_job - represents one multithreaded job
  118. *
  119. * @thread_fn: Called for each chunk of work that a padata thread does.
  120. * @fn_arg: The thread function argument.
  121. * @start: The start of the job (units are job-specific).
  122. * @size: size of this node's work (units are job-specific).
  123. * @align: Ranges passed to the thread function fall on this boundary, with the
  124. * possible exceptions of the beginning and end of the job.
  125. * @min_chunk: The minimum chunk size in job-specific units. This allows
  126. * the client to communicate the minimum amount of work that's
  127. * appropriate for one worker thread to do at once.
  128. * @max_threads: Max threads to use for the job, actual number may be less
  129. * depending on task size and minimum chunk size.
  130. */
  131. struct padata_mt_job {
  132. void (*thread_fn)(unsigned long start, unsigned long end, void *arg);
  133. void *fn_arg;
  134. unsigned long start;
  135. unsigned long size;
  136. unsigned long align;
  137. unsigned long min_chunk;
  138. int max_threads;
  139. };
  140. /**
  141. * struct padata_instance - The overall control structure.
  142. *
  143. * @cpu_online_node: Linkage for CPU online callback.
  144. * @cpu_dead_node: Linkage for CPU offline callback.
  145. * @parallel_wq: The workqueue used for parallel work.
  146. * @serial_wq: The workqueue used for serial work.
  147. * @pslist: List of padata_shell objects attached to this instance.
  148. * @cpumask: User supplied cpumasks for parallel and serial works.
  149. * @kobj: padata instance kernel object.
  150. * @lock: padata instance lock.
  151. * @flags: padata flags.
  152. */
  153. struct padata_instance {
  154. struct hlist_node cpu_online_node;
  155. struct hlist_node cpu_dead_node;
  156. struct workqueue_struct *parallel_wq;
  157. struct workqueue_struct *serial_wq;
  158. struct list_head pslist;
  159. struct padata_cpumask cpumask;
  160. struct kobject kobj;
  161. struct mutex lock;
  162. u8 flags;
  163. #define PADATA_INIT 1
  164. #define PADATA_RESET 2
  165. #define PADATA_INVALID 4
  166. };
  167. #ifdef CONFIG_PADATA
  168. extern void __init padata_init(void);
  169. #else
  170. static inline void __init padata_init(void) {}
  171. #endif
  172. extern struct padata_instance *padata_alloc(const char *name);
  173. extern void padata_free(struct padata_instance *pinst);
  174. extern struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);
  175. extern void padata_free_shell(struct padata_shell *ps);
  176. extern int padata_do_parallel(struct padata_shell *ps,
  177. struct padata_priv *padata, int *cb_cpu);
  178. extern void padata_do_serial(struct padata_priv *padata);
  179. extern void __init padata_do_multithreaded(struct padata_mt_job *job);
  180. extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
  181. cpumask_var_t cpumask);
  182. #endif