Linux 6.10-rc4
[linux-2.6-block.git] / include / linux / padata.h
CommitLineData
a61127c2 1/* SPDX-License-Identifier: GPL-2.0-only */
16295bec
SK
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 <steffen.klassert@secunet.com>
004ed426
DJ
7 *
8 * Copyright (c) 2020 Oracle and/or its affiliates.
9 * Author: Daniel Jordan <daniel.m.jordan@oracle.com>
16295bec
SK
10 */
11
12#ifndef PADATA_H
13#define PADATA_H
14
d5ee8e75 15#include <linux/refcount.h>
bbefa1dd 16#include <linux/compiler_types.h>
16295bec
SK
17#include <linux/workqueue.h>
18#include <linux/spinlock.h>
19#include <linux/list.h>
5e017dc3 20#include <linux/kobject.h>
e15bacbe
DK
21
22#define PADATA_CPU_SERIAL 0x01
23#define PADATA_CPU_PARALLEL 0x02
16295bec 24
0198ffd1 25/**
bfcdcef8 26 * struct padata_priv - Represents one job
0198ffd1
SK
27 *
28 * @list: List entry, to attach to the padata lists.
29 * @pd: Pointer to the internal control structure.
30 * @cb_cpu: Callback cpu for serializatioon.
31 * @seq_nr: Sequence number of the parallelized data object.
32 * @info: Used to pass information from the parallel to the serial function.
33 * @parallel: Parallel execution function.
34 * @serial: Serial complete function.
35 */
16295bec
SK
36struct padata_priv {
37 struct list_head list;
38 struct parallel_data *pd;
39 int cb_cpu;
bfde23ce 40 unsigned int seq_nr;
16295bec
SK
41 int info;
42 void (*parallel)(struct padata_priv *padata);
43 void (*serial)(struct padata_priv *padata);
44};
45
0198ffd1 46/**
bfcdcef8 47 * struct padata_list - one per work type per CPU
0198ffd1
SK
48 *
49 * @list: List head.
50 * @lock: List lock.
51 */
16295bec
SK
52struct padata_list {
53 struct list_head list;
54 spinlock_t lock;
55};
56
0198ffd1 57/**
e15bacbe
DK
58* struct padata_serial_queue - The percpu padata serial queue
59*
60* @serial: List to wait for serialization after reordering.
61* @work: work struct for serialization.
62* @pd: Backpointer to the internal control structure.
63*/
64struct padata_serial_queue {
65 struct padata_list serial;
66 struct work_struct work;
67 struct parallel_data *pd;
68};
69
c635696c
SK
70/**
71 * struct padata_cpumask - The cpumasks for the parallel/serial workers
72 *
73 * @pcpu: cpumask for the parallel workers.
74 * @cbcpu: cpumask for the serial (callback) workers.
75 */
76struct padata_cpumask {
77 cpumask_var_t pcpu;
78 cpumask_var_t cbcpu;
79};
e15bacbe 80
0198ffd1
SK
81/**
82 * struct parallel_data - Internal control structure, covers everything
83 * that depends on the cpumask in use.
84 *
bfcdcef8 85 * @ps: padata_shell object.
f601c725 86 * @reorder_list: percpu reorder lists
e15bacbe 87 * @squeue: percpu padata queues used for serialuzation.
0198ffd1 88 * @refcnt: Number of objects holding a reference on this parallel_data.
bfcdcef8 89 * @seq_nr: Sequence number of the parallelized data object.
bfde23ce 90 * @processed: Number of already processed objects.
6fc4dbcf 91 * @cpu: Next CPU to be processed.
c635696c 92 * @cpumask: The cpumasks in use for parallel and serial workers.
6fc4dbcf 93 * @reorder_work: work struct for reordering.
0198ffd1 94 * @lock: Reorder lock.
0198ffd1 95 */
16295bec 96struct parallel_data {
bbefa1dd 97 struct padata_shell *ps;
f601c725 98 struct padata_list __percpu *reorder_list;
57a2ce5f 99 struct padata_serial_queue __percpu *squeue;
d5ee8e75 100 refcount_t refcnt;
4611ce22 101 unsigned int seq_nr;
bfde23ce 102 unsigned int processed;
6fc4dbcf 103 int cpu;
c635696c 104 struct padata_cpumask cpumask;
6fc4dbcf 105 struct work_struct reorder_work;
bfcdcef8 106 spinlock_t ____cacheline_aligned lock;
16295bec
SK
107};
108
bbefa1dd
HX
109/**
110 * struct padata_shell - Wrapper around struct parallel_data, its
111 * purpose is to allow the underlying control structure to be replaced
112 * on the fly using RCU.
113 *
114 * @pinst: padat instance.
115 * @pd: Actual parallel_data structure which may be substituted on the fly.
116 * @opd: Pointer to old pd to be freed by padata_replace.
117 * @list: List entry in padata_instance list.
118 */
119struct padata_shell {
120 struct padata_instance *pinst;
121 struct parallel_data __rcu *pd;
122 struct parallel_data *opd;
123 struct list_head list;
124};
125
004ed426
DJ
126/**
127 * struct padata_mt_job - represents one multithreaded job
128 *
129 * @thread_fn: Called for each chunk of work that a padata thread does.
130 * @fn_arg: The thread function argument.
131 * @start: The start of the job (units are job-specific).
132 * @size: size of this node's work (units are job-specific).
133 * @align: Ranges passed to the thread function fall on this boundary, with the
134 * possible exceptions of the beginning and end of the job.
135 * @min_chunk: The minimum chunk size in job-specific units. This allows
136 * the client to communicate the minimum amount of work that's
137 * appropriate for one worker thread to do at once.
138 * @max_threads: Max threads to use for the job, actual number may be less
139 * depending on task size and minimum chunk size.
eb522866 140 * @numa_aware: Distribute jobs to different nodes with CPU in a round robin fashion.
004ed426
DJ
141 */
142struct padata_mt_job {
143 void (*thread_fn)(unsigned long start, unsigned long end, void *arg);
144 void *fn_arg;
145 unsigned long start;
146 unsigned long size;
147 unsigned long align;
148 unsigned long min_chunk;
149 int max_threads;
eb522866 150 bool numa_aware;
004ed426
DJ
151};
152
0198ffd1
SK
153/**
154 * struct padata_instance - The overall control structure.
155 *
3c2214b6
DJ
156 * @cpu_online_node: Linkage for CPU online callback.
157 * @cpu_dead_node: Linkage for CPU offline callback.
45d153c0
DJ
158 * @parallel_wq: The workqueue used for parallel work.
159 * @serial_wq: The workqueue used for serial work.
bbefa1dd 160 * @pslist: List of padata_shell objects attached to this instance.
c635696c 161 * @cpumask: User supplied cpumasks for parallel and serial works.
5e017dc3 162 * @kobj: padata instance kernel object.
0198ffd1
SK
163 * @lock: padata instance lock.
164 * @flags: padata flags.
165 */
16295bec 166struct padata_instance {
3c2214b6
DJ
167 struct hlist_node cpu_online_node;
168 struct hlist_node cpu_dead_node;
45d153c0
DJ
169 struct workqueue_struct *parallel_wq;
170 struct workqueue_struct *serial_wq;
bbefa1dd 171 struct list_head pslist;
c635696c 172 struct padata_cpumask cpumask;
5e017dc3 173 struct kobject kobj;
e15bacbe
DK
174 struct mutex lock;
175 u8 flags;
176#define PADATA_INIT 1
177#define PADATA_RESET 2
178#define PADATA_INVALID 4
16295bec
SK
179};
180
f1b192b1
DJ
181#ifdef CONFIG_PADATA
182extern void __init padata_init(void);
3f257191 183extern struct padata_instance *padata_alloc(const char *name);
16295bec 184extern void padata_free(struct padata_instance *pinst);
bbefa1dd
HX
185extern struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);
186extern void padata_free_shell(struct padata_shell *ps);
187extern int padata_do_parallel(struct padata_shell *ps,
e6ce0e08 188 struct padata_priv *padata, int *cb_cpu);
16295bec 189extern void padata_do_serial(struct padata_priv *padata);
004ed426 190extern void __init padata_do_multithreaded(struct padata_mt_job *job);
e15bacbe 191extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
16295bec 192 cpumask_var_t cpumask);
bd5ed02e
GL
193#else
194static inline void __init padata_init(void) {}
195static inline void __init padata_do_multithreaded(struct padata_mt_job *job)
196{
197 job->thread_fn(job->start, job->start + job->size, job->fn_arg);
198}
199#endif
200
16295bec 201#endif