xref: /openbmc/linux/include/linux/padata.h (revision 3c2214b6)
1a61127c2SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
216295becSSteffen Klassert /*
316295becSSteffen Klassert  * padata.h - header for the padata parallelization interface
416295becSSteffen Klassert  *
516295becSSteffen Klassert  * Copyright (C) 2008, 2009 secunet Security Networks AG
616295becSSteffen Klassert  * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
716295becSSteffen Klassert  */
816295becSSteffen Klassert 
916295becSSteffen Klassert #ifndef PADATA_H
1016295becSSteffen Klassert #define PADATA_H
1116295becSSteffen Klassert 
12bbefa1ddSHerbert Xu #include <linux/compiler_types.h>
1316295becSSteffen Klassert #include <linux/workqueue.h>
1416295becSSteffen Klassert #include <linux/spinlock.h>
1516295becSSteffen Klassert #include <linux/list.h>
165e017dc3SDan Kruchinin #include <linux/kobject.h>
17e15bacbeSDan Kruchinin 
18e15bacbeSDan Kruchinin #define PADATA_CPU_SERIAL   0x01
19e15bacbeSDan Kruchinin #define PADATA_CPU_PARALLEL 0x02
2016295becSSteffen Klassert 
210198ffd1SSteffen Klassert /**
22bfcdcef8SDaniel Jordan  * struct padata_priv - Represents one job
230198ffd1SSteffen Klassert  *
240198ffd1SSteffen Klassert  * @list: List entry, to attach to the padata lists.
250198ffd1SSteffen Klassert  * @pd: Pointer to the internal control structure.
260198ffd1SSteffen Klassert  * @cb_cpu: Callback cpu for serializatioon.
27350ef88eSMathias Krause  * @cpu: Cpu for parallelization.
280198ffd1SSteffen Klassert  * @seq_nr: Sequence number of the parallelized data object.
290198ffd1SSteffen Klassert  * @info: Used to pass information from the parallel to the serial function.
300198ffd1SSteffen Klassert  * @parallel: Parallel execution function.
310198ffd1SSteffen Klassert  * @serial: Serial complete function.
320198ffd1SSteffen Klassert  */
3316295becSSteffen Klassert struct padata_priv {
3416295becSSteffen Klassert 	struct list_head	list;
3516295becSSteffen Klassert 	struct parallel_data	*pd;
3616295becSSteffen Klassert 	int			cb_cpu;
37350ef88eSMathias Krause 	int			cpu;
38bfde23ceSDaniel Jordan 	unsigned int		seq_nr;
3916295becSSteffen Klassert 	int			info;
4016295becSSteffen Klassert 	void                    (*parallel)(struct padata_priv *padata);
4116295becSSteffen Klassert 	void                    (*serial)(struct padata_priv *padata);
4216295becSSteffen Klassert };
4316295becSSteffen Klassert 
440198ffd1SSteffen Klassert /**
45bfcdcef8SDaniel Jordan  * struct padata_list - one per work type per CPU
460198ffd1SSteffen Klassert  *
470198ffd1SSteffen Klassert  * @list: List head.
480198ffd1SSteffen Klassert  * @lock: List lock.
490198ffd1SSteffen Klassert  */
5016295becSSteffen Klassert struct padata_list {
5116295becSSteffen Klassert 	struct list_head        list;
5216295becSSteffen Klassert 	spinlock_t              lock;
5316295becSSteffen Klassert };
5416295becSSteffen Klassert 
550198ffd1SSteffen Klassert /**
56e15bacbeSDan Kruchinin * struct padata_serial_queue - The percpu padata serial queue
57e15bacbeSDan Kruchinin *
58e15bacbeSDan Kruchinin * @serial: List to wait for serialization after reordering.
59e15bacbeSDan Kruchinin * @work: work struct for serialization.
60e15bacbeSDan Kruchinin * @pd: Backpointer to the internal control structure.
61e15bacbeSDan Kruchinin */
62e15bacbeSDan Kruchinin struct padata_serial_queue {
63e15bacbeSDan Kruchinin        struct padata_list    serial;
64e15bacbeSDan Kruchinin        struct work_struct    work;
65e15bacbeSDan Kruchinin        struct parallel_data *pd;
66e15bacbeSDan Kruchinin };
67e15bacbeSDan Kruchinin 
68e15bacbeSDan Kruchinin /**
69e15bacbeSDan Kruchinin  * struct padata_parallel_queue - The percpu padata parallel queue
700198ffd1SSteffen Klassert  *
710198ffd1SSteffen Klassert  * @parallel: List to wait for parallelization.
720198ffd1SSteffen Klassert  * @reorder: List to wait for reordering after parallel processing.
73e15bacbeSDan Kruchinin  * @work: work struct for parallelization.
74e15bacbeSDan Kruchinin  * @num_obj: Number of objects that are processed by this cpu.
750198ffd1SSteffen Klassert  */
76e15bacbeSDan Kruchinin struct padata_parallel_queue {
7716295becSSteffen Klassert        struct padata_list    parallel;
7816295becSSteffen Klassert        struct padata_list    reorder;
79e15bacbeSDan Kruchinin        struct work_struct    work;
80e15bacbeSDan Kruchinin        atomic_t              num_obj;
8116295becSSteffen Klassert };
8216295becSSteffen Klassert 
83c635696cSSteffen Klassert /**
84c635696cSSteffen Klassert  * struct padata_cpumask - The cpumasks for the parallel/serial workers
85c635696cSSteffen Klassert  *
86c635696cSSteffen Klassert  * @pcpu: cpumask for the parallel workers.
87c635696cSSteffen Klassert  * @cbcpu: cpumask for the serial (callback) workers.
88c635696cSSteffen Klassert  */
89c635696cSSteffen Klassert struct padata_cpumask {
90c635696cSSteffen Klassert 	cpumask_var_t	pcpu;
91c635696cSSteffen Klassert 	cpumask_var_t	cbcpu;
92c635696cSSteffen Klassert };
93e15bacbeSDan Kruchinin 
940198ffd1SSteffen Klassert /**
950198ffd1SSteffen Klassert  * struct parallel_data - Internal control structure, covers everything
960198ffd1SSteffen Klassert  * that depends on the cpumask in use.
970198ffd1SSteffen Klassert  *
98bfcdcef8SDaniel Jordan  * @ps: padata_shell object.
99e15bacbeSDan Kruchinin  * @pqueue: percpu padata queues used for parallelization.
100e15bacbeSDan Kruchinin  * @squeue: percpu padata queues used for serialuzation.
1010198ffd1SSteffen Klassert  * @refcnt: Number of objects holding a reference on this parallel_data.
102bfcdcef8SDaniel Jordan  * @seq_nr: Sequence number of the parallelized data object.
103bfde23ceSDaniel Jordan  * @processed: Number of already processed objects.
1046fc4dbcfSHerbert Xu  * @cpu: Next CPU to be processed.
105c635696cSSteffen Klassert  * @cpumask: The cpumasks in use for parallel and serial workers.
1066fc4dbcfSHerbert Xu  * @reorder_work: work struct for reordering.
1070198ffd1SSteffen Klassert  * @lock: Reorder lock.
1080198ffd1SSteffen Klassert  */
10916295becSSteffen Klassert struct parallel_data {
110bbefa1ddSHerbert Xu 	struct padata_shell		*ps;
11157a2ce5fSNamhyung Kim 	struct padata_parallel_queue	__percpu *pqueue;
11257a2ce5fSNamhyung Kim 	struct padata_serial_queue	__percpu *squeue;
11316295becSSteffen Klassert 	atomic_t			refcnt;
1140b6b098eSMathias Krause 	atomic_t			seq_nr;
115bfde23ceSDaniel Jordan 	unsigned int			processed;
1166fc4dbcfSHerbert Xu 	int				cpu;
117c635696cSSteffen Klassert 	struct padata_cpumask		cpumask;
1186fc4dbcfSHerbert Xu 	struct work_struct		reorder_work;
119bfcdcef8SDaniel Jordan 	spinlock_t                      ____cacheline_aligned lock;
12016295becSSteffen Klassert };
12116295becSSteffen Klassert 
1220198ffd1SSteffen Klassert /**
123bbefa1ddSHerbert Xu  * struct padata_shell - Wrapper around struct parallel_data, its
124bbefa1ddSHerbert Xu  * purpose is to allow the underlying control structure to be replaced
125bbefa1ddSHerbert Xu  * on the fly using RCU.
126bbefa1ddSHerbert Xu  *
127bbefa1ddSHerbert Xu  * @pinst: padat instance.
128bbefa1ddSHerbert Xu  * @pd: Actual parallel_data structure which may be substituted on the fly.
129bbefa1ddSHerbert Xu  * @opd: Pointer to old pd to be freed by padata_replace.
130bbefa1ddSHerbert Xu  * @list: List entry in padata_instance list.
131bbefa1ddSHerbert Xu  */
132bbefa1ddSHerbert Xu struct padata_shell {
133bbefa1ddSHerbert Xu 	struct padata_instance		*pinst;
134bbefa1ddSHerbert Xu 	struct parallel_data __rcu	*pd;
135bbefa1ddSHerbert Xu 	struct parallel_data		*opd;
136bbefa1ddSHerbert Xu 	struct list_head		list;
137bbefa1ddSHerbert Xu };
138bbefa1ddSHerbert Xu 
139bbefa1ddSHerbert Xu /**
1400198ffd1SSteffen Klassert  * struct padata_instance - The overall control structure.
1410198ffd1SSteffen Klassert  *
1423c2214b6SDaniel Jordan  * @cpu_online_node: Linkage for CPU online callback.
1433c2214b6SDaniel Jordan  * @cpu_dead_node: Linkage for CPU offline callback.
14445d153c0SDaniel Jordan  * @parallel_wq: The workqueue used for parallel work.
14545d153c0SDaniel Jordan  * @serial_wq: The workqueue used for serial work.
146bbefa1ddSHerbert Xu  * @pslist: List of padata_shell objects attached to this instance.
147c635696cSSteffen Klassert  * @cpumask: User supplied cpumasks for parallel and serial works.
148bbefa1ddSHerbert Xu  * @rcpumask: Actual cpumasks based on user cpumask and cpu_online_mask.
1495e017dc3SDan Kruchinin  * @kobj: padata instance kernel object.
1500198ffd1SSteffen Klassert  * @lock: padata instance lock.
1510198ffd1SSteffen Klassert  * @flags: padata flags.
1520198ffd1SSteffen Klassert  */
15316295becSSteffen Klassert struct padata_instance {
1543c2214b6SDaniel Jordan 	struct hlist_node		cpu_online_node;
1553c2214b6SDaniel Jordan 	struct hlist_node		cpu_dead_node;
15645d153c0SDaniel Jordan 	struct workqueue_struct		*parallel_wq;
15745d153c0SDaniel Jordan 	struct workqueue_struct		*serial_wq;
158bbefa1ddSHerbert Xu 	struct list_head		pslist;
159c635696cSSteffen Klassert 	struct padata_cpumask		cpumask;
160bbefa1ddSHerbert Xu 	struct padata_cpumask		rcpumask;
1615e017dc3SDan Kruchinin 	struct kobject                   kobj;
16216295becSSteffen Klassert 	struct mutex			 lock;
16316295becSSteffen Klassert 	u8				 flags;
16416295becSSteffen Klassert #define	PADATA_INIT	1
16516295becSSteffen Klassert #define	PADATA_RESET	2
1664c879170SSteffen Klassert #define	PADATA_INVALID	4
16716295becSSteffen Klassert };
16816295becSSteffen Klassert 
169b128a304SDaniel Jordan extern struct padata_instance *padata_alloc_possible(const char *name);
17016295becSSteffen Klassert extern void padata_free(struct padata_instance *pinst);
171bbefa1ddSHerbert Xu extern struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);
172bbefa1ddSHerbert Xu extern void padata_free_shell(struct padata_shell *ps);
173bbefa1ddSHerbert Xu extern int padata_do_parallel(struct padata_shell *ps,
174e6ce0e08SDaniel Jordan 			      struct padata_priv *padata, int *cb_cpu);
17516295becSSteffen Klassert extern void padata_do_serial(struct padata_priv *padata);
176e15bacbeSDan Kruchinin extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
17716295becSSteffen Klassert 			      cpumask_var_t cpumask);
1784c879170SSteffen Klassert extern int padata_start(struct padata_instance *pinst);
17916295becSSteffen Klassert extern void padata_stop(struct padata_instance *pinst);
18016295becSSteffen Klassert #endif
181