padata.h 6.1 KB

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