xref: /openbmc/linux/include/linux/padata.h (revision d5ee8e75)
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>
7004ed426SDaniel Jordan  *
8004ed426SDaniel Jordan  * Copyright (c) 2020 Oracle and/or its affiliates.
9004ed426SDaniel Jordan  * Author: Daniel Jordan <daniel.m.jordan@oracle.com>
1016295becSSteffen Klassert  */
1116295becSSteffen Klassert 
1216295becSSteffen Klassert #ifndef PADATA_H
1316295becSSteffen Klassert #define PADATA_H
1416295becSSteffen Klassert 
15*d5ee8e75SXiyu Yang #include <linux/refcount.h>
16bbefa1ddSHerbert Xu #include <linux/compiler_types.h>
1716295becSSteffen Klassert #include <linux/workqueue.h>
1816295becSSteffen Klassert #include <linux/spinlock.h>
1916295becSSteffen Klassert #include <linux/list.h>
205e017dc3SDan Kruchinin #include <linux/kobject.h>
21e15bacbeSDan Kruchinin 
22e15bacbeSDan Kruchinin #define PADATA_CPU_SERIAL   0x01
23e15bacbeSDan Kruchinin #define PADATA_CPU_PARALLEL 0x02
2416295becSSteffen Klassert 
250198ffd1SSteffen Klassert /**
26bfcdcef8SDaniel Jordan  * struct padata_priv - Represents one job
270198ffd1SSteffen Klassert  *
280198ffd1SSteffen Klassert  * @list: List entry, to attach to the padata lists.
290198ffd1SSteffen Klassert  * @pd: Pointer to the internal control structure.
300198ffd1SSteffen Klassert  * @cb_cpu: Callback cpu for serializatioon.
310198ffd1SSteffen Klassert  * @seq_nr: Sequence number of the parallelized data object.
320198ffd1SSteffen Klassert  * @info: Used to pass information from the parallel to the serial function.
330198ffd1SSteffen Klassert  * @parallel: Parallel execution function.
340198ffd1SSteffen Klassert  * @serial: Serial complete function.
350198ffd1SSteffen Klassert  */
3616295becSSteffen Klassert struct padata_priv {
3716295becSSteffen Klassert 	struct list_head	list;
3816295becSSteffen Klassert 	struct parallel_data	*pd;
3916295becSSteffen Klassert 	int			cb_cpu;
40bfde23ceSDaniel Jordan 	unsigned int		seq_nr;
4116295becSSteffen Klassert 	int			info;
4216295becSSteffen Klassert 	void                    (*parallel)(struct padata_priv *padata);
4316295becSSteffen Klassert 	void                    (*serial)(struct padata_priv *padata);
4416295becSSteffen Klassert };
4516295becSSteffen Klassert 
460198ffd1SSteffen Klassert /**
47bfcdcef8SDaniel Jordan  * struct padata_list - one per work type per CPU
480198ffd1SSteffen Klassert  *
490198ffd1SSteffen Klassert  * @list: List head.
500198ffd1SSteffen Klassert  * @lock: List lock.
510198ffd1SSteffen Klassert  */
5216295becSSteffen Klassert struct padata_list {
5316295becSSteffen Klassert 	struct list_head        list;
5416295becSSteffen Klassert 	spinlock_t              lock;
5516295becSSteffen Klassert };
5616295becSSteffen Klassert 
570198ffd1SSteffen Klassert /**
58e15bacbeSDan Kruchinin * struct padata_serial_queue - The percpu padata serial queue
59e15bacbeSDan Kruchinin *
60e15bacbeSDan Kruchinin * @serial: List to wait for serialization after reordering.
61e15bacbeSDan Kruchinin * @work: work struct for serialization.
62e15bacbeSDan Kruchinin * @pd: Backpointer to the internal control structure.
63e15bacbeSDan Kruchinin */
64e15bacbeSDan Kruchinin struct padata_serial_queue {
65e15bacbeSDan Kruchinin        struct padata_list    serial;
66e15bacbeSDan Kruchinin        struct work_struct    work;
67e15bacbeSDan Kruchinin        struct parallel_data *pd;
68e15bacbeSDan Kruchinin };
69e15bacbeSDan Kruchinin 
70e15bacbeSDan Kruchinin /**
71c635696cSSteffen Klassert  * struct padata_cpumask - The cpumasks for the parallel/serial workers
72c635696cSSteffen Klassert  *
73c635696cSSteffen Klassert  * @pcpu: cpumask for the parallel workers.
74c635696cSSteffen Klassert  * @cbcpu: cpumask for the serial (callback) workers.
75c635696cSSteffen Klassert  */
76c635696cSSteffen Klassert struct padata_cpumask {
77c635696cSSteffen Klassert 	cpumask_var_t	pcpu;
78c635696cSSteffen Klassert 	cpumask_var_t	cbcpu;
79c635696cSSteffen Klassert };
80e15bacbeSDan Kruchinin 
810198ffd1SSteffen Klassert /**
820198ffd1SSteffen Klassert  * struct parallel_data - Internal control structure, covers everything
830198ffd1SSteffen Klassert  * that depends on the cpumask in use.
840198ffd1SSteffen Klassert  *
85bfcdcef8SDaniel Jordan  * @ps: padata_shell object.
86f601c725SDaniel Jordan  * @reorder_list: percpu reorder lists
87e15bacbeSDan Kruchinin  * @squeue: percpu padata queues used for serialuzation.
880198ffd1SSteffen Klassert  * @refcnt: Number of objects holding a reference on this parallel_data.
89bfcdcef8SDaniel Jordan  * @seq_nr: Sequence number of the parallelized data object.
90bfde23ceSDaniel Jordan  * @processed: Number of already processed objects.
916fc4dbcfSHerbert Xu  * @cpu: Next CPU to be processed.
92c635696cSSteffen Klassert  * @cpumask: The cpumasks in use for parallel and serial workers.
936fc4dbcfSHerbert Xu  * @reorder_work: work struct for reordering.
940198ffd1SSteffen Klassert  * @lock: Reorder lock.
950198ffd1SSteffen Klassert  */
9616295becSSteffen Klassert struct parallel_data {
97bbefa1ddSHerbert Xu 	struct padata_shell		*ps;
98f601c725SDaniel Jordan 	struct padata_list		__percpu *reorder_list;
9957a2ce5fSNamhyung Kim 	struct padata_serial_queue	__percpu *squeue;
100*d5ee8e75SXiyu Yang 	refcount_t			refcnt;
1014611ce22SDaniel Jordan 	unsigned int			seq_nr;
102bfde23ceSDaniel Jordan 	unsigned int			processed;
1036fc4dbcfSHerbert Xu 	int				cpu;
104c635696cSSteffen Klassert 	struct padata_cpumask		cpumask;
1056fc4dbcfSHerbert Xu 	struct work_struct		reorder_work;
106bfcdcef8SDaniel Jordan 	spinlock_t                      ____cacheline_aligned lock;
10716295becSSteffen Klassert };
10816295becSSteffen Klassert 
1090198ffd1SSteffen Klassert /**
110bbefa1ddSHerbert Xu  * struct padata_shell - Wrapper around struct parallel_data, its
111bbefa1ddSHerbert Xu  * purpose is to allow the underlying control structure to be replaced
112bbefa1ddSHerbert Xu  * on the fly using RCU.
113bbefa1ddSHerbert Xu  *
114bbefa1ddSHerbert Xu  * @pinst: padat instance.
115bbefa1ddSHerbert Xu  * @pd: Actual parallel_data structure which may be substituted on the fly.
116bbefa1ddSHerbert Xu  * @opd: Pointer to old pd to be freed by padata_replace.
117bbefa1ddSHerbert Xu  * @list: List entry in padata_instance list.
118bbefa1ddSHerbert Xu  */
119bbefa1ddSHerbert Xu struct padata_shell {
120bbefa1ddSHerbert Xu 	struct padata_instance		*pinst;
121bbefa1ddSHerbert Xu 	struct parallel_data __rcu	*pd;
122bbefa1ddSHerbert Xu 	struct parallel_data		*opd;
123bbefa1ddSHerbert Xu 	struct list_head		list;
124bbefa1ddSHerbert Xu };
125bbefa1ddSHerbert Xu 
126bbefa1ddSHerbert Xu /**
127004ed426SDaniel Jordan  * struct padata_mt_job - represents one multithreaded job
128004ed426SDaniel Jordan  *
129004ed426SDaniel Jordan  * @thread_fn: Called for each chunk of work that a padata thread does.
130004ed426SDaniel Jordan  * @fn_arg: The thread function argument.
131004ed426SDaniel Jordan  * @start: The start of the job (units are job-specific).
132004ed426SDaniel Jordan  * @size: size of this node's work (units are job-specific).
133004ed426SDaniel Jordan  * @align: Ranges passed to the thread function fall on this boundary, with the
134004ed426SDaniel Jordan  *         possible exceptions of the beginning and end of the job.
135004ed426SDaniel Jordan  * @min_chunk: The minimum chunk size in job-specific units.  This allows
136004ed426SDaniel Jordan  *             the client to communicate the minimum amount of work that's
137004ed426SDaniel Jordan  *             appropriate for one worker thread to do at once.
138004ed426SDaniel Jordan  * @max_threads: Max threads to use for the job, actual number may be less
139004ed426SDaniel Jordan  *               depending on task size and minimum chunk size.
140004ed426SDaniel Jordan  */
141004ed426SDaniel Jordan struct padata_mt_job {
142004ed426SDaniel Jordan 	void (*thread_fn)(unsigned long start, unsigned long end, void *arg);
143004ed426SDaniel Jordan 	void			*fn_arg;
144004ed426SDaniel Jordan 	unsigned long		start;
145004ed426SDaniel Jordan 	unsigned long		size;
146004ed426SDaniel Jordan 	unsigned long		align;
147004ed426SDaniel Jordan 	unsigned long		min_chunk;
148004ed426SDaniel Jordan 	int			max_threads;
149004ed426SDaniel Jordan };
150004ed426SDaniel Jordan 
151004ed426SDaniel Jordan /**
1520198ffd1SSteffen Klassert  * struct padata_instance - The overall control structure.
1530198ffd1SSteffen Klassert  *
1543c2214b6SDaniel Jordan  * @cpu_online_node: Linkage for CPU online callback.
1553c2214b6SDaniel Jordan  * @cpu_dead_node: Linkage for CPU offline callback.
15645d153c0SDaniel Jordan  * @parallel_wq: The workqueue used for parallel work.
15745d153c0SDaniel Jordan  * @serial_wq: The workqueue used for serial work.
158bbefa1ddSHerbert Xu  * @pslist: List of padata_shell objects attached to this instance.
159c635696cSSteffen Klassert  * @cpumask: User supplied cpumasks for parallel and serial works.
1605e017dc3SDan Kruchinin  * @kobj: padata instance kernel object.
1610198ffd1SSteffen Klassert  * @lock: padata instance lock.
1620198ffd1SSteffen Klassert  * @flags: padata flags.
1630198ffd1SSteffen Klassert  */
16416295becSSteffen Klassert struct padata_instance {
1653c2214b6SDaniel Jordan 	struct hlist_node		cpu_online_node;
1663c2214b6SDaniel Jordan 	struct hlist_node		cpu_dead_node;
16745d153c0SDaniel Jordan 	struct workqueue_struct		*parallel_wq;
16845d153c0SDaniel Jordan 	struct workqueue_struct		*serial_wq;
169bbefa1ddSHerbert Xu 	struct list_head		pslist;
170c635696cSSteffen Klassert 	struct padata_cpumask		cpumask;
1715e017dc3SDan Kruchinin 	struct kobject                   kobj;
17216295becSSteffen Klassert 	struct mutex			 lock;
17316295becSSteffen Klassert 	u8				 flags;
17416295becSSteffen Klassert #define	PADATA_INIT	1
17516295becSSteffen Klassert #define	PADATA_RESET	2
1764c879170SSteffen Klassert #define	PADATA_INVALID	4
17716295becSSteffen Klassert };
17816295becSSteffen Klassert 
179f1b192b1SDaniel Jordan #ifdef CONFIG_PADATA
180f1b192b1SDaniel Jordan extern void __init padata_init(void);
181f1b192b1SDaniel Jordan #else
padata_init(void)182f1b192b1SDaniel Jordan static inline void __init padata_init(void) {}
183f1b192b1SDaniel Jordan #endif
184f1b192b1SDaniel Jordan 
1853f257191SDaniel Jordan extern struct padata_instance *padata_alloc(const char *name);
18616295becSSteffen Klassert extern void padata_free(struct padata_instance *pinst);
187bbefa1ddSHerbert Xu extern struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);
188bbefa1ddSHerbert Xu extern void padata_free_shell(struct padata_shell *ps);
189bbefa1ddSHerbert Xu extern int padata_do_parallel(struct padata_shell *ps,
190e6ce0e08SDaniel Jordan 			      struct padata_priv *padata, int *cb_cpu);
19116295becSSteffen Klassert extern void padata_do_serial(struct padata_priv *padata);
192004ed426SDaniel Jordan extern void __init padata_do_multithreaded(struct padata_mt_job *job);
193e15bacbeSDan Kruchinin extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
19416295becSSteffen Klassert 			      cpumask_var_t cpumask);
19516295becSSteffen Klassert #endif
196