xref: /openbmc/linux/include/linux/padata.h (revision 04b3c795)
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 
12 #ifndef PADATA_H
13 #define PADATA_H
14 
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>
20 
21 #define PADATA_CPU_SERIAL   0x01
22 #define PADATA_CPU_PARALLEL 0x02
23 
24 /**
25  * struct padata_priv - Represents one job
26  *
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.
34  */
35 struct padata_priv {
36 	struct list_head	list;
37 	struct parallel_data	*pd;
38 	int			cb_cpu;
39 	unsigned int		seq_nr;
40 	int			info;
41 	void                    (*parallel)(struct padata_priv *padata);
42 	void                    (*serial)(struct padata_priv *padata);
43 };
44 
45 /**
46  * struct padata_list - one per work type per CPU
47  *
48  * @list: List head.
49  * @lock: List lock.
50  */
51 struct padata_list {
52 	struct list_head        list;
53 	spinlock_t              lock;
54 };
55 
56 /**
57 * struct padata_serial_queue - The percpu padata serial queue
58 *
59 * @serial: List to wait for serialization after reordering.
60 * @work: work struct for serialization.
61 * @pd: Backpointer to the internal control structure.
62 */
63 struct padata_serial_queue {
64        struct padata_list    serial;
65        struct work_struct    work;
66        struct parallel_data *pd;
67 };
68 
69 /**
70  * struct padata_parallel_queue - The percpu padata parallel queue
71  *
72  * @reorder: List to wait for reordering after parallel processing.
73  * @num_obj: Number of objects that are processed by this cpu.
74  */
75 struct padata_parallel_queue {
76        struct padata_list    reorder;
77        atomic_t              num_obj;
78 };
79 
80 /**
81  * struct padata_cpumask - The cpumasks for the parallel/serial workers
82  *
83  * @pcpu: cpumask for the parallel workers.
84  * @cbcpu: cpumask for the serial (callback) workers.
85  */
86 struct padata_cpumask {
87 	cpumask_var_t	pcpu;
88 	cpumask_var_t	cbcpu;
89 };
90 
91 /**
92  * struct parallel_data - Internal control structure, covers everything
93  * that depends on the cpumask in use.
94  *
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.
105  */
106 struct parallel_data {
107 	struct padata_shell		*ps;
108 	struct padata_parallel_queue	__percpu *pqueue;
109 	struct padata_serial_queue	__percpu *squeue;
110 	atomic_t			refcnt;
111 	unsigned int			seq_nr;
112 	unsigned int			processed;
113 	int				cpu;
114 	struct padata_cpumask		cpumask;
115 	struct work_struct		reorder_work;
116 	spinlock_t                      ____cacheline_aligned lock;
117 };
118 
119 /**
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.
123  *
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.
128  */
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;
134 };
135 
136 /**
137  * struct padata_mt_job - represents one multithreaded job
138  *
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.
150  */
151 struct padata_mt_job {
152 	void (*thread_fn)(unsigned long start, unsigned long end, void *arg);
153 	void			*fn_arg;
154 	unsigned long		start;
155 	unsigned long		size;
156 	unsigned long		align;
157 	unsigned long		min_chunk;
158 	int			max_threads;
159 };
160 
161 /**
162  * struct padata_instance - The overall control structure.
163  *
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.
174  */
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;
183 	struct kobject                   kobj;
184 	struct mutex			 lock;
185 	u8				 flags;
186 #define	PADATA_INIT	1
187 #define	PADATA_RESET	2
188 #define	PADATA_INVALID	4
189 };
190 
191 #ifdef CONFIG_PADATA
192 extern void __init padata_init(void);
193 #else
194 static inline void __init padata_init(void) {}
195 #endif
196 
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);
209 #endif
210