xref: /linux-6.15/include/linux/padata.h (revision bfde23ce)
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/workqueue.h>
13 #include <linux/spinlock.h>
14 #include <linux/list.h>
15 #include <linux/notifier.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 -  Embedded to the users data structure.
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  * @cpu: Cpu for parallelization.
28  * @seq_nr: Sequence number of the parallelized data object.
29  * @info: Used to pass information from the parallel to the serial function.
30  * @parallel: Parallel execution function.
31  * @serial: Serial complete function.
32  */
33 struct padata_priv {
34 	struct list_head	list;
35 	struct parallel_data	*pd;
36 	int			cb_cpu;
37 	int			cpu;
38 	unsigned int		seq_nr;
39 	int			info;
40 	void                    (*parallel)(struct padata_priv *padata);
41 	void                    (*serial)(struct padata_priv *padata);
42 };
43 
44 /**
45  * struct padata_list
46  *
47  * @list: List head.
48  * @lock: List lock.
49  */
50 struct padata_list {
51 	struct list_head        list;
52 	spinlock_t              lock;
53 };
54 
55 /**
56 * struct padata_serial_queue - The percpu padata serial queue
57 *
58 * @serial: List to wait for serialization after reordering.
59 * @work: work struct for serialization.
60 * @pd: Backpointer to the internal control structure.
61 */
62 struct padata_serial_queue {
63        struct padata_list    serial;
64        struct work_struct    work;
65        struct parallel_data *pd;
66 };
67 
68 /**
69  * struct padata_parallel_queue - The percpu padata parallel queue
70  *
71  * @parallel: List to wait for parallelization.
72  * @reorder: List to wait for reordering after parallel processing.
73  * @serial: List to wait for serialization after reordering.
74  * @pwork: work struct for parallelization.
75  * @swork: work struct for serialization.
76  * @work: work struct for parallelization.
77  * @num_obj: Number of objects that are processed by this cpu.
78  * @cpu_index: Index of the cpu.
79  */
80 struct padata_parallel_queue {
81        struct padata_list    parallel;
82        struct padata_list    reorder;
83        struct work_struct    work;
84        atomic_t              num_obj;
85        int                   cpu_index;
86 };
87 
88 /**
89  * struct padata_cpumask - The cpumasks for the parallel/serial workers
90  *
91  * @pcpu: cpumask for the parallel workers.
92  * @cbcpu: cpumask for the serial (callback) workers.
93  */
94 struct padata_cpumask {
95 	cpumask_var_t	pcpu;
96 	cpumask_var_t	cbcpu;
97 };
98 
99 /**
100  * struct parallel_data - Internal control structure, covers everything
101  * that depends on the cpumask in use.
102  *
103  * @pinst: padata instance.
104  * @pqueue: percpu padata queues used for parallelization.
105  * @squeue: percpu padata queues used for serialuzation.
106  * @reorder_objects: Number of objects waiting in the reorder queues.
107  * @refcnt: Number of objects holding a reference on this parallel_data.
108  * @max_seq_nr:  Maximal used sequence number.
109  * @processed: Number of already processed objects.
110  * @cpu: Next CPU to be processed.
111  * @cpumask: The cpumasks in use for parallel and serial workers.
112  * @reorder_work: work struct for reordering.
113  * @lock: Reorder lock.
114  */
115 struct parallel_data {
116 	struct padata_instance		*pinst;
117 	struct padata_parallel_queue	__percpu *pqueue;
118 	struct padata_serial_queue	__percpu *squeue;
119 	atomic_t			reorder_objects;
120 	atomic_t			refcnt;
121 	atomic_t			seq_nr;
122 	unsigned int			processed;
123 	int				cpu;
124 	struct padata_cpumask		cpumask;
125 	struct work_struct		reorder_work;
126 	spinlock_t                      lock ____cacheline_aligned;
127 };
128 
129 /**
130  * struct padata_instance - The overall control structure.
131  *
132  * @cpu_notifier: cpu hotplug notifier.
133  * @parallel_wq: The workqueue used for parallel work.
134  * @serial_wq: The workqueue used for serial work.
135  * @pd: The internal control structure.
136  * @cpumask: User supplied cpumasks for parallel and serial works.
137  * @cpumask_change_notifier: Notifiers chain for user-defined notify
138  *            callbacks that will be called when either @pcpu or @cbcpu
139  *            or both cpumasks change.
140  * @kobj: padata instance kernel object.
141  * @lock: padata instance lock.
142  * @flags: padata flags.
143  */
144 struct padata_instance {
145 	struct hlist_node		 node;
146 	struct workqueue_struct		*parallel_wq;
147 	struct workqueue_struct		*serial_wq;
148 	struct parallel_data		*pd;
149 	struct padata_cpumask		cpumask;
150 	struct blocking_notifier_head	 cpumask_change_notifier;
151 	struct kobject                   kobj;
152 	struct mutex			 lock;
153 	u8				 flags;
154 #define	PADATA_INIT	1
155 #define	PADATA_RESET	2
156 #define	PADATA_INVALID	4
157 };
158 
159 extern struct padata_instance *padata_alloc_possible(const char *name);
160 extern void padata_free(struct padata_instance *pinst);
161 extern int padata_do_parallel(struct padata_instance *pinst,
162 			      struct padata_priv *padata, int *cb_cpu);
163 extern void padata_do_serial(struct padata_priv *padata);
164 extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
165 			      cpumask_var_t cpumask);
166 extern int padata_start(struct padata_instance *pinst);
167 extern void padata_stop(struct padata_instance *pinst);
168 extern int padata_register_cpumask_notifier(struct padata_instance *pinst,
169 					    struct notifier_block *nblock);
170 extern int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
171 					      struct notifier_block *nblock);
172 #endif
173