blob: d803397a28f70be39443e57816d546baffbda43a [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * padata.h - header for the padata parallelization interface
3 *
4 * Copyright (C) 2008, 2009 secunet Security Networks AG
5 * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#ifndef PADATA_H
22#define PADATA_H
23
24#include <linux/workqueue.h>
25#include <linux/spinlock.h>
26#include <linux/list.h>
27#include <linux/notifier.h>
28#include <linux/kobject.h>
29
30#define PADATA_CPU_SERIAL 0x01
31#define PADATA_CPU_PARALLEL 0x02
32
33/**
34 * struct padata_priv - Embedded to the users data structure.
35 *
36 * @list: List entry, to attach to the padata lists.
37 * @pd: Pointer to the internal control structure.
38 * @cb_cpu: Callback cpu for serializatioon.
39 * @cpu: Cpu for parallelization.
40 * @seq_nr: Sequence number of the parallelized data object.
41 * @info: Used to pass information from the parallel to the serial function.
42 * @parallel: Parallel execution function.
43 * @serial: Serial complete function.
44 */
45struct padata_priv {
46 struct list_head list;
47 struct parallel_data *pd;
48 int cb_cpu;
49 int cpu;
50 int info;
51 void (*parallel)(struct padata_priv *padata);
52 void (*serial)(struct padata_priv *padata);
53};
54
55/**
56 * struct padata_list
57 *
58 * @list: List head.
59 * @lock: List lock.
60 */
61struct padata_list {
62 struct list_head list;
63 spinlock_t lock;
64};
65
66/**
67* struct padata_serial_queue - The percpu padata serial queue
68*
69* @serial: List to wait for serialization after reordering.
70* @work: work struct for serialization.
71* @pd: Backpointer to the internal control structure.
72*/
73struct padata_serial_queue {
74 struct padata_list serial;
75 struct work_struct work;
76 struct parallel_data *pd;
77};
78
79/**
80 * struct padata_parallel_queue - The percpu padata parallel queue
81 *
82 * @parallel: List to wait for parallelization.
83 * @reorder: List to wait for reordering after parallel processing.
84 * @serial: List to wait for serialization after reordering.
85 * @pwork: work struct for parallelization.
86 * @swork: work struct for serialization.
87 * @work: work struct for parallelization.
88 * @num_obj: Number of objects that are processed by this cpu.
89 * @cpu_index: Index of the cpu.
90 */
91struct padata_parallel_queue {
92 struct padata_list parallel;
93 struct padata_list reorder;
94 struct work_struct work;
95 atomic_t num_obj;
96 int cpu_index;
97};
98
99/**
100 * struct padata_cpumask - The cpumasks for the parallel/serial workers
101 *
102 * @pcpu: cpumask for the parallel workers.
103 * @cbcpu: cpumask for the serial (callback) workers.
104 */
105struct padata_cpumask {
106 cpumask_var_t pcpu;
107 cpumask_var_t cbcpu;
108};
109
110/**
111 * struct parallel_data - Internal control structure, covers everything
112 * that depends on the cpumask in use.
113 *
114 * @pinst: padata instance.
115 * @pqueue: percpu padata queues used for parallelization.
116 * @squeue: percpu padata queues used for serialuzation.
117 * @reorder_objects: Number of objects waiting in the reorder queues.
118 * @refcnt: Number of objects holding a reference on this parallel_data.
119 * @max_seq_nr: Maximal used sequence number.
120 * @cpu: Next CPU to be processed.
121 * @cpumask: The cpumasks in use for parallel and serial workers.
122 * @reorder_work: work struct for reordering.
123 * @lock: Reorder lock.
124 */
125struct parallel_data {
126 struct padata_instance *pinst;
127 struct padata_parallel_queue __percpu *pqueue;
128 struct padata_serial_queue __percpu *squeue;
129 atomic_t reorder_objects;
130 atomic_t refcnt;
131 atomic_t seq_nr;
132 int cpu;
133 struct padata_cpumask cpumask;
134 struct work_struct reorder_work;
135 spinlock_t lock ____cacheline_aligned;
136};
137
138/**
139 * struct padata_instance - The overall control structure.
140 *
141 * @cpu_notifier: cpu hotplug notifier.
142 * @wq: The workqueue in use.
143 * @pd: The internal control structure.
144 * @cpumask: User supplied cpumasks for parallel and serial works.
145 * @cpumask_change_notifier: Notifiers chain for user-defined notify
146 * callbacks that will be called when either @pcpu or @cbcpu
147 * or both cpumasks change.
148 * @kobj: padata instance kernel object.
149 * @lock: padata instance lock.
150 * @flags: padata flags.
151 */
152struct padata_instance {
153 struct hlist_node node;
154 struct workqueue_struct *wq;
155 struct parallel_data *pd;
156 struct padata_cpumask cpumask;
157 struct blocking_notifier_head cpumask_change_notifier;
158 struct kobject kobj;
159 struct mutex lock;
160 u8 flags;
161#define PADATA_INIT 1
162#define PADATA_RESET 2
163#define PADATA_INVALID 4
164};
165
166extern struct padata_instance *padata_alloc_possible(
167 struct workqueue_struct *wq);
168extern void padata_free(struct padata_instance *pinst);
169extern int padata_do_parallel(struct padata_instance *pinst,
170 struct padata_priv *padata, int cb_cpu);
171extern void padata_do_serial(struct padata_priv *padata);
172extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
173 cpumask_var_t cpumask);
174extern int padata_start(struct padata_instance *pinst);
175extern void padata_stop(struct padata_instance *pinst);
176extern int padata_register_cpumask_notifier(struct padata_instance *pinst,
177 struct notifier_block *nblock);
178extern int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
179 struct notifier_block *nblock);
180#endif