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