1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * padata.h - header for the padata parallelization interface
5 * Copyright (C) 2008, 2009 secunet Security Networks AG
8 * Copyright (c) 2020 Oracle and/or its affiliates.
15 #include <linux/compiler_types.h>
16 #include <linux/workqueue.h>
17 #include <linux/spinlock.h>
18 #include <linux/list.h>
19 #include <linux/kobject.h>
21 #define PADATA_CPU_SERIAL 0x01
22 #define PADATA_CPU_PARALLEL 0x02
25 * struct padata_priv - Represents one job
27 * @list: List entry, to attach to the padata lists.
28 * @pd: Pointer to the internal control structure.
29 * @cb_cpu: Callback cpu for serializatioon.
30 * @seq_nr: Sequence number of the parallelized data object.
31 * @info: Used to pass information from the parallel to the serial function.
32 * @parallel: Parallel execution function.
33 * @serial: Serial complete function.
36 struct list_head list;
37 struct parallel_data *pd;
41 void (*parallel)(struct padata_priv *padata);
42 void (*serial)(struct padata_priv *padata);
46 * struct padata_list - one per work type per CPU
52 struct list_head list;
57 * struct padata_serial_queue - The percpu padata serial queue
59 * @serial: List to wait for serialization after reordering.
60 * @work: work struct for serialization.
61 * @pd: Backpointer to the internal control structure.
63 struct padata_serial_queue {
64 struct padata_list serial;
65 struct work_struct work;
66 struct parallel_data *pd;
70 * struct padata_parallel_queue - The percpu padata parallel queue
72 * @reorder: List to wait for reordering after parallel processing.
73 * @num_obj: Number of objects that are processed by this cpu.
75 struct padata_parallel_queue {
76 struct padata_list reorder;
81 * struct padata_cpumask - The cpumasks for the parallel/serial workers
83 * @pcpu: cpumask for the parallel workers.
84 * @cbcpu: cpumask for the serial (callback) workers.
86 struct padata_cpumask {
92 * struct parallel_data - Internal control structure, covers everything
93 * that depends on the cpumask in use.
95 * @ps: padata_shell object.
96 * @pqueue: percpu padata queues used for parallelization.
97 * @squeue: percpu padata queues used for serialuzation.
98 * @refcnt: Number of objects holding a reference on this parallel_data.
99 * @seq_nr: Sequence number of the parallelized data object.
100 * @processed: Number of already processed objects.
101 * @cpu: Next CPU to be processed.
102 * @cpumask: The cpumasks in use for parallel and serial workers.
103 * @reorder_work: work struct for reordering.
104 * @lock: Reorder lock.
106 struct parallel_data {
107 struct padata_shell *ps;
108 struct padata_parallel_queue __percpu *pqueue;
109 struct padata_serial_queue __percpu *squeue;
112 unsigned int processed;
114 struct padata_cpumask cpumask;
115 struct work_struct reorder_work;
116 spinlock_t ____cacheline_aligned lock;
120 * struct padata_shell - Wrapper around struct parallel_data, its
121 * purpose is to allow the underlying control structure to be replaced
122 * on the fly using RCU.
124 * @pinst: padat instance.
125 * @pd: Actual parallel_data structure which may be substituted on the fly.
126 * @opd: Pointer to old pd to be freed by padata_replace.
127 * @list: List entry in padata_instance list.
129 struct padata_shell {
130 struct padata_instance *pinst;
131 struct parallel_data __rcu *pd;
132 struct parallel_data *opd;
133 struct list_head list;
137 * struct padata_mt_job - represents one multithreaded job
139 * @thread_fn: Called for each chunk of work that a padata thread does.
140 * @fn_arg: The thread function argument.
141 * @start: The start of the job (units are job-specific).
142 * @size: size of this node's work (units are job-specific).
143 * @align: Ranges passed to the thread function fall on this boundary, with the
144 * possible exceptions of the beginning and end of the job.
145 * @min_chunk: The minimum chunk size in job-specific units. This allows
146 * the client to communicate the minimum amount of work that's
147 * appropriate for one worker thread to do at once.
148 * @max_threads: Max threads to use for the job, actual number may be less
149 * depending on task size and minimum chunk size.
151 struct padata_mt_job {
152 void (*thread_fn)(unsigned long start, unsigned long end, void *arg);
157 unsigned long min_chunk;
162 * struct padata_instance - The overall control structure.
164 * @cpu_online_node: Linkage for CPU online callback.
165 * @cpu_dead_node: Linkage for CPU offline callback.
166 * @parallel_wq: The workqueue used for parallel work.
167 * @serial_wq: The workqueue used for serial work.
168 * @pslist: List of padata_shell objects attached to this instance.
169 * @cpumask: User supplied cpumasks for parallel and serial works.
170 * @rcpumask: Actual cpumasks based on user cpumask and cpu_online_mask.
171 * @kobj: padata instance kernel object.
172 * @lock: padata instance lock.
173 * @flags: padata flags.
175 struct padata_instance {
176 struct hlist_node cpu_online_node;
177 struct hlist_node cpu_dead_node;
178 struct workqueue_struct *parallel_wq;
179 struct workqueue_struct *serial_wq;
180 struct list_head pslist;
181 struct padata_cpumask cpumask;
182 struct padata_cpumask rcpumask;
186 #define PADATA_INIT 1
187 #define PADATA_RESET 2
188 #define PADATA_INVALID 4
192 extern void __init padata_init(void);
194 static inline void __init padata_init(void) {}
197 extern struct padata_instance *padata_alloc_possible(const char *name);
198 extern void padata_free(struct padata_instance *pinst);
199 extern struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);
200 extern void padata_free_shell(struct padata_shell *ps);
201 extern int padata_do_parallel(struct padata_shell *ps,
202 struct padata_priv *padata, int *cb_cpu);
203 extern void padata_do_serial(struct padata_priv *padata);
204 extern void __init padata_do_multithreaded(struct padata_mt_job *job);
205 extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
206 cpumask_var_t cpumask);
207 extern int padata_start(struct padata_instance *pinst);
208 extern void padata_stop(struct padata_instance *pinst);