xref: /linux-6.15/include/linux/padata.h (revision 4611ce22)
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 
9 #ifndef PADATA_H
10 #define PADATA_H
11 
12 #include <linux/compiler_types.h>
13 #include <linux/workqueue.h>
14 #include <linux/spinlock.h>
15 #include <linux/list.h>
16 #include <linux/kobject.h>
17 
18 #define PADATA_CPU_SERIAL   0x01
19 #define PADATA_CPU_PARALLEL 0x02
20 
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 /**
43  * struct padata_list - one per work type per CPU
44  *
45  * @list: List head.
46  * @lock: List lock.
47  */
48 struct padata_list {
49 	struct list_head        list;
50 	spinlock_t              lock;
51 };
52 
53 /**
54 * struct padata_serial_queue - The percpu padata serial queue
55 *
56 * @serial: List to wait for serialization after reordering.
57 * @work: work struct for serialization.
58 * @pd: Backpointer to the internal control structure.
59 */
60 struct padata_serial_queue {
61        struct padata_list    serial;
62        struct work_struct    work;
63        struct parallel_data *pd;
64 };
65 
66 /**
67  * struct padata_parallel_queue - The percpu padata parallel queue
68  *
69  * @reorder: List to wait for reordering after parallel processing.
70  * @num_obj: Number of objects that are processed by this cpu.
71  */
72 struct padata_parallel_queue {
73        struct padata_list    reorder;
74        atomic_t              num_obj;
75 };
76 
77 /**
78  * struct padata_cpumask - The cpumasks for the parallel/serial workers
79  *
80  * @pcpu: cpumask for the parallel workers.
81  * @cbcpu: cpumask for the serial (callback) workers.
82  */
83 struct padata_cpumask {
84 	cpumask_var_t	pcpu;
85 	cpumask_var_t	cbcpu;
86 };
87 
88 /**
89  * struct parallel_data - Internal control structure, covers everything
90  * that depends on the cpumask in use.
91  *
92  * @ps: padata_shell object.
93  * @pqueue: percpu padata queues used for parallelization.
94  * @squeue: percpu padata queues used for serialuzation.
95  * @refcnt: Number of objects holding a reference on this parallel_data.
96  * @seq_nr: Sequence number of the parallelized data object.
97  * @processed: Number of already processed objects.
98  * @cpu: Next CPU to be processed.
99  * @cpumask: The cpumasks in use for parallel and serial workers.
100  * @reorder_work: work struct for reordering.
101  * @lock: Reorder lock.
102  */
103 struct parallel_data {
104 	struct padata_shell		*ps;
105 	struct padata_parallel_queue	__percpu *pqueue;
106 	struct padata_serial_queue	__percpu *squeue;
107 	atomic_t			refcnt;
108 	unsigned int			seq_nr;
109 	unsigned int			processed;
110 	int				cpu;
111 	struct padata_cpumask		cpumask;
112 	struct work_struct		reorder_work;
113 	spinlock_t                      ____cacheline_aligned lock;
114 };
115 
116 /**
117  * struct padata_shell - Wrapper around struct parallel_data, its
118  * purpose is to allow the underlying control structure to be replaced
119  * on the fly using RCU.
120  *
121  * @pinst: padat instance.
122  * @pd: Actual parallel_data structure which may be substituted on the fly.
123  * @opd: Pointer to old pd to be freed by padata_replace.
124  * @list: List entry in padata_instance list.
125  */
126 struct padata_shell {
127 	struct padata_instance		*pinst;
128 	struct parallel_data __rcu	*pd;
129 	struct parallel_data		*opd;
130 	struct list_head		list;
131 };
132 
133 /**
134  * struct padata_instance - The overall control structure.
135  *
136  * @cpu_online_node: Linkage for CPU online callback.
137  * @cpu_dead_node: Linkage for CPU offline callback.
138  * @parallel_wq: The workqueue used for parallel work.
139  * @serial_wq: The workqueue used for serial work.
140  * @pslist: List of padata_shell objects attached to this instance.
141  * @cpumask: User supplied cpumasks for parallel and serial works.
142  * @rcpumask: Actual cpumasks based on user cpumask and cpu_online_mask.
143  * @kobj: padata instance kernel object.
144  * @lock: padata instance lock.
145  * @flags: padata flags.
146  */
147 struct padata_instance {
148 	struct hlist_node		cpu_online_node;
149 	struct hlist_node		cpu_dead_node;
150 	struct workqueue_struct		*parallel_wq;
151 	struct workqueue_struct		*serial_wq;
152 	struct list_head		pslist;
153 	struct padata_cpumask		cpumask;
154 	struct padata_cpumask		rcpumask;
155 	struct kobject                   kobj;
156 	struct mutex			 lock;
157 	u8				 flags;
158 #define	PADATA_INIT	1
159 #define	PADATA_RESET	2
160 #define	PADATA_INVALID	4
161 };
162 
163 #ifdef CONFIG_PADATA
164 extern void __init padata_init(void);
165 #else
166 static inline void __init padata_init(void) {}
167 #endif
168 
169 extern struct padata_instance *padata_alloc_possible(const char *name);
170 extern void padata_free(struct padata_instance *pinst);
171 extern struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);
172 extern void padata_free_shell(struct padata_shell *ps);
173 extern int padata_do_parallel(struct padata_shell *ps,
174 			      struct padata_priv *padata, int *cb_cpu);
175 extern void padata_do_serial(struct padata_priv *padata);
176 extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
177 			      cpumask_var_t cpumask);
178 extern int padata_start(struct padata_instance *pinst);
179 extern void padata_stop(struct padata_instance *pinst);
180 #endif
181