xref: /openbmc/linux/include/linux/padata.h (revision 35267cea)
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_cpumask - The cpumasks for the parallel/serial workers
71  *
72  * @pcpu: cpumask for the parallel workers.
73  * @cbcpu: cpumask for the serial (callback) workers.
74  */
75 struct padata_cpumask {
76 	cpumask_var_t	pcpu;
77 	cpumask_var_t	cbcpu;
78 };
79 
80 /**
81  * struct parallel_data - Internal control structure, covers everything
82  * that depends on the cpumask in use.
83  *
84  * @ps: padata_shell object.
85  * @reorder_list: percpu reorder lists
86  * @squeue: percpu padata queues used for serialuzation.
87  * @refcnt: Number of objects holding a reference on this parallel_data.
88  * @seq_nr: Sequence number of the parallelized data object.
89  * @processed: Number of already processed objects.
90  * @cpu: Next CPU to be processed.
91  * @cpumask: The cpumasks in use for parallel and serial workers.
92  * @reorder_work: work struct for reordering.
93  * @lock: Reorder lock.
94  */
95 struct parallel_data {
96 	struct padata_shell		*ps;
97 	struct padata_list		__percpu *reorder_list;
98 	struct padata_serial_queue	__percpu *squeue;
99 	atomic_t			refcnt;
100 	unsigned int			seq_nr;
101 	unsigned int			processed;
102 	int				cpu;
103 	struct padata_cpumask		cpumask;
104 	struct work_struct		reorder_work;
105 	spinlock_t                      ____cacheline_aligned lock;
106 };
107 
108 /**
109  * struct padata_shell - Wrapper around struct parallel_data, its
110  * purpose is to allow the underlying control structure to be replaced
111  * on the fly using RCU.
112  *
113  * @pinst: padat instance.
114  * @pd: Actual parallel_data structure which may be substituted on the fly.
115  * @opd: Pointer to old pd to be freed by padata_replace.
116  * @list: List entry in padata_instance list.
117  */
118 struct padata_shell {
119 	struct padata_instance		*pinst;
120 	struct parallel_data __rcu	*pd;
121 	struct parallel_data		*opd;
122 	struct list_head		list;
123 };
124 
125 /**
126  * struct padata_mt_job - represents one multithreaded job
127  *
128  * @thread_fn: Called for each chunk of work that a padata thread does.
129  * @fn_arg: The thread function argument.
130  * @start: The start of the job (units are job-specific).
131  * @size: size of this node's work (units are job-specific).
132  * @align: Ranges passed to the thread function fall on this boundary, with the
133  *         possible exceptions of the beginning and end of the job.
134  * @min_chunk: The minimum chunk size in job-specific units.  This allows
135  *             the client to communicate the minimum amount of work that's
136  *             appropriate for one worker thread to do at once.
137  * @max_threads: Max threads to use for the job, actual number may be less
138  *               depending on task size and minimum chunk size.
139  */
140 struct padata_mt_job {
141 	void (*thread_fn)(unsigned long start, unsigned long end, void *arg);
142 	void			*fn_arg;
143 	unsigned long		start;
144 	unsigned long		size;
145 	unsigned long		align;
146 	unsigned long		min_chunk;
147 	int			max_threads;
148 };
149 
150 /**
151  * struct padata_instance - The overall control structure.
152  *
153  * @cpu_online_node: Linkage for CPU online callback.
154  * @cpu_dead_node: Linkage for CPU offline callback.
155  * @parallel_wq: The workqueue used for parallel work.
156  * @serial_wq: The workqueue used for serial work.
157  * @pslist: List of padata_shell objects attached to this instance.
158  * @cpumask: User supplied cpumasks for parallel and serial works.
159  * @kobj: padata instance kernel object.
160  * @lock: padata instance lock.
161  * @flags: padata flags.
162  */
163 struct padata_instance {
164 	struct hlist_node		cpu_online_node;
165 	struct hlist_node		cpu_dead_node;
166 	struct workqueue_struct		*parallel_wq;
167 	struct workqueue_struct		*serial_wq;
168 	struct list_head		pslist;
169 	struct padata_cpumask		cpumask;
170 	struct kobject                   kobj;
171 	struct mutex			 lock;
172 	u8				 flags;
173 #define	PADATA_INIT	1
174 #define	PADATA_RESET	2
175 #define	PADATA_INVALID	4
176 };
177 
178 #ifdef CONFIG_PADATA
179 extern void __init padata_init(void);
180 #else
181 static inline void __init padata_init(void) {}
182 #endif
183 
184 extern struct padata_instance *padata_alloc(const char *name);
185 extern void padata_free(struct padata_instance *pinst);
186 extern struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);
187 extern void padata_free_shell(struct padata_shell *ps);
188 extern int padata_do_parallel(struct padata_shell *ps,
189 			      struct padata_priv *padata, int *cb_cpu);
190 extern void padata_do_serial(struct padata_priv *padata);
191 extern void __init padata_do_multithreaded(struct padata_mt_job *job);
192 extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
193 			      cpumask_var_t cpumask);
194 #endif
195