blob: fa35dcfbd13f27b7294dada5bd4508274d060d71 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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 <steffen.klassert@secunet.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9#ifndef PADATA_H
10#define PADATA_H
11
Olivier Deprez0e641232021-09-23 10:07:05 +020012#include <linux/compiler_types.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013#include <linux/workqueue.h>
14#include <linux/spinlock.h>
15#include <linux/list.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <linux/notifier.h>
17#include <linux/kobject.h>
18
19#define PADATA_CPU_SERIAL 0x01
20#define PADATA_CPU_PARALLEL 0x02
21
22/**
23 * struct padata_priv - Embedded to the users data structure.
24 *
25 * @list: List entry, to attach to the padata lists.
26 * @pd: Pointer to the internal control structure.
27 * @cb_cpu: Callback cpu for serializatioon.
28 * @cpu: Cpu for parallelization.
29 * @seq_nr: Sequence number of the parallelized data object.
30 * @info: Used to pass information from the parallel to the serial function.
31 * @parallel: Parallel execution function.
32 * @serial: Serial complete function.
33 */
34struct padata_priv {
35 struct list_head list;
36 struct parallel_data *pd;
37 int cb_cpu;
38 int cpu;
David Brazdil0f672f62019-12-10 10:32:29 +000039 unsigned int seq_nr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040 int info;
41 void (*parallel)(struct padata_priv *padata);
42 void (*serial)(struct padata_priv *padata);
43};
44
45/**
46 * struct padata_list
47 *
48 * @list: List head.
49 * @lock: List lock.
50 */
51struct padata_list {
52 struct list_head list;
53 spinlock_t lock;
54};
55
56/**
57* struct padata_serial_queue - The percpu padata serial queue
58*
59* @serial: List to wait for serialization after reordering.
60* @work: work struct for serialization.
61* @pd: Backpointer to the internal control structure.
62*/
63struct padata_serial_queue {
64 struct padata_list serial;
65 struct work_struct work;
66 struct parallel_data *pd;
67};
68
69/**
70 * struct padata_parallel_queue - The percpu padata parallel queue
71 *
72 * @parallel: List to wait for parallelization.
73 * @reorder: List to wait for reordering after parallel processing.
74 * @serial: List to wait for serialization after reordering.
75 * @pwork: work struct for parallelization.
76 * @swork: work struct for serialization.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077 * @work: work struct for parallelization.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078 * @num_obj: Number of objects that are processed by this cpu.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079 */
80struct padata_parallel_queue {
81 struct padata_list parallel;
82 struct padata_list reorder;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083 struct work_struct work;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 atomic_t num_obj;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085};
86
87/**
88 * struct padata_cpumask - The cpumasks for the parallel/serial workers
89 *
90 * @pcpu: cpumask for the parallel workers.
91 * @cbcpu: cpumask for the serial (callback) workers.
92 */
93struct padata_cpumask {
94 cpumask_var_t pcpu;
95 cpumask_var_t cbcpu;
96};
97
98/**
99 * struct parallel_data - Internal control structure, covers everything
100 * that depends on the cpumask in use.
101 *
Olivier Deprez0e641232021-09-23 10:07:05 +0200102 * @sh: padata_shell object.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103 * @pqueue: percpu padata queues used for parallelization.
104 * @squeue: percpu padata queues used for serialuzation.
105 * @reorder_objects: Number of objects waiting in the reorder queues.
106 * @refcnt: Number of objects holding a reference on this parallel_data.
107 * @max_seq_nr: Maximal used sequence number.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000108 * @processed: Number of already processed objects.
David Brazdil0f672f62019-12-10 10:32:29 +0000109 * @cpu: Next CPU to be processed.
110 * @cpumask: The cpumasks in use for parallel and serial workers.
111 * @reorder_work: work struct for reordering.
112 * @lock: Reorder lock.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000113 */
114struct parallel_data {
Olivier Deprez0e641232021-09-23 10:07:05 +0200115 struct padata_shell *ps;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116 struct padata_parallel_queue __percpu *pqueue;
117 struct padata_serial_queue __percpu *squeue;
118 atomic_t reorder_objects;
119 atomic_t refcnt;
120 atomic_t seq_nr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121 unsigned int processed;
David Brazdil0f672f62019-12-10 10:32:29 +0000122 int cpu;
123 struct padata_cpumask cpumask;
124 struct work_struct reorder_work;
125 spinlock_t lock ____cacheline_aligned;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126};
127
128/**
Olivier Deprez0e641232021-09-23 10:07:05 +0200129 * struct padata_shell - Wrapper around struct parallel_data, its
130 * purpose is to allow the underlying control structure to be replaced
131 * on the fly using RCU.
132 *
133 * @pinst: padat instance.
134 * @pd: Actual parallel_data structure which may be substituted on the fly.
135 * @opd: Pointer to old pd to be freed by padata_replace.
136 * @list: List entry in padata_instance list.
137 */
138struct padata_shell {
139 struct padata_instance *pinst;
140 struct parallel_data __rcu *pd;
141 struct parallel_data *opd;
142 struct list_head list;
143};
144
145/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000146 * struct padata_instance - The overall control structure.
147 *
Olivier Deprez0e641232021-09-23 10:07:05 +0200148 * @cpu_online_node: Linkage for CPU online callback.
149 * @cpu_dead_node: Linkage for CPU offline callback.
David Brazdil0f672f62019-12-10 10:32:29 +0000150 * @parallel_wq: The workqueue used for parallel work.
151 * @serial_wq: The workqueue used for serial work.
Olivier Deprez0e641232021-09-23 10:07:05 +0200152 * @pslist: List of padata_shell objects attached to this instance.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153 * @cpumask: User supplied cpumasks for parallel and serial works.
Olivier Deprez0e641232021-09-23 10:07:05 +0200154 * @rcpumask: Actual cpumasks based on user cpumask and cpu_online_mask.
155 * @omask: Temporary storage used to compute the notification mask.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000156 * @cpumask_change_notifier: Notifiers chain for user-defined notify
157 * callbacks that will be called when either @pcpu or @cbcpu
158 * or both cpumasks change.
159 * @kobj: padata instance kernel object.
160 * @lock: padata instance lock.
161 * @flags: padata flags.
162 */
163struct padata_instance {
Olivier Deprez0e641232021-09-23 10:07:05 +0200164 struct hlist_node cpu_online_node;
165 struct hlist_node cpu_dead_node;
David Brazdil0f672f62019-12-10 10:32:29 +0000166 struct workqueue_struct *parallel_wq;
167 struct workqueue_struct *serial_wq;
Olivier Deprez0e641232021-09-23 10:07:05 +0200168 struct list_head pslist;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169 struct padata_cpumask cpumask;
Olivier Deprez0e641232021-09-23 10:07:05 +0200170 struct padata_cpumask rcpumask;
171 cpumask_var_t omask;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172 struct blocking_notifier_head cpumask_change_notifier;
173 struct kobject kobj;
174 struct mutex lock;
175 u8 flags;
176#define PADATA_INIT 1
177#define PADATA_RESET 2
178#define PADATA_INVALID 4
179};
180
David Brazdil0f672f62019-12-10 10:32:29 +0000181extern struct padata_instance *padata_alloc_possible(const char *name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000182extern void padata_free(struct padata_instance *pinst);
Olivier Deprez0e641232021-09-23 10:07:05 +0200183extern struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);
184extern void padata_free_shell(struct padata_shell *ps);
185extern int padata_do_parallel(struct padata_shell *ps,
David Brazdil0f672f62019-12-10 10:32:29 +0000186 struct padata_priv *padata, int *cb_cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000187extern void padata_do_serial(struct padata_priv *padata);
188extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
189 cpumask_var_t cpumask);
190extern int padata_start(struct padata_instance *pinst);
191extern void padata_stop(struct padata_instance *pinst);
192extern int padata_register_cpumask_notifier(struct padata_instance *pinst,
193 struct notifier_block *nblock);
194extern int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
195 struct notifier_block *nblock);
196#endif