blob: 23717eeaad23a59a71358a7dea78a0a91548eb45 [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
12#include <linux/workqueue.h>
13#include <linux/spinlock.h>
14#include <linux/list.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015#include <linux/notifier.h>
16#include <linux/kobject.h>
17
18#define PADATA_CPU_SERIAL 0x01
19#define PADATA_CPU_PARALLEL 0x02
20
21/**
22 * struct padata_priv - Embedded to the users data structure.
23 *
24 * @list: List entry, to attach to the padata lists.
25 * @pd: Pointer to the internal control structure.
26 * @cb_cpu: Callback cpu for serializatioon.
27 * @cpu: Cpu for parallelization.
28 * @seq_nr: Sequence number of the parallelized data object.
29 * @info: Used to pass information from the parallel to the serial function.
30 * @parallel: Parallel execution function.
31 * @serial: Serial complete function.
32 */
33struct padata_priv {
34 struct list_head list;
35 struct parallel_data *pd;
36 int cb_cpu;
37 int cpu;
David Brazdil0f672f62019-12-10 10:32:29 +000038 unsigned int seq_nr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039 int info;
40 void (*parallel)(struct padata_priv *padata);
41 void (*serial)(struct padata_priv *padata);
42};
43
44/**
45 * struct padata_list
46 *
47 * @list: List head.
48 * @lock: List lock.
49 */
50struct padata_list {
51 struct list_head list;
52 spinlock_t lock;
53};
54
55/**
56* struct padata_serial_queue - The percpu padata serial queue
57*
58* @serial: List to wait for serialization after reordering.
59* @work: work struct for serialization.
60* @pd: Backpointer to the internal control structure.
61*/
62struct padata_serial_queue {
63 struct padata_list serial;
64 struct work_struct work;
65 struct parallel_data *pd;
66};
67
68/**
69 * struct padata_parallel_queue - The percpu padata parallel queue
70 *
71 * @parallel: List to wait for parallelization.
72 * @reorder: List to wait for reordering after parallel processing.
73 * @serial: List to wait for serialization after reordering.
74 * @pwork: work struct for parallelization.
75 * @swork: work struct for serialization.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 * @work: work struct for parallelization.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077 * @num_obj: Number of objects that are processed by this cpu.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078 */
79struct padata_parallel_queue {
80 struct padata_list parallel;
81 struct padata_list reorder;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082 struct work_struct work;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083 atomic_t num_obj;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084};
85
86/**
87 * struct padata_cpumask - The cpumasks for the parallel/serial workers
88 *
89 * @pcpu: cpumask for the parallel workers.
90 * @cbcpu: cpumask for the serial (callback) workers.
91 */
92struct padata_cpumask {
93 cpumask_var_t pcpu;
94 cpumask_var_t cbcpu;
95};
96
97/**
98 * struct parallel_data - Internal control structure, covers everything
99 * that depends on the cpumask in use.
100 *
101 * @pinst: padata instance.
102 * @pqueue: percpu padata queues used for parallelization.
103 * @squeue: percpu padata queues used for serialuzation.
104 * @reorder_objects: Number of objects waiting in the reorder queues.
105 * @refcnt: Number of objects holding a reference on this parallel_data.
106 * @max_seq_nr: Maximal used sequence number.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000107 * @processed: Number of already processed objects.
David Brazdil0f672f62019-12-10 10:32:29 +0000108 * @cpu: Next CPU to be processed.
109 * @cpumask: The cpumasks in use for parallel and serial workers.
110 * @reorder_work: work struct for reordering.
111 * @lock: Reorder lock.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112 */
113struct parallel_data {
114 struct padata_instance *pinst;
115 struct padata_parallel_queue __percpu *pqueue;
116 struct padata_serial_queue __percpu *squeue;
117 atomic_t reorder_objects;
118 atomic_t refcnt;
119 atomic_t seq_nr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120 unsigned int processed;
David Brazdil0f672f62019-12-10 10:32:29 +0000121 int cpu;
122 struct padata_cpumask cpumask;
123 struct work_struct reorder_work;
124 spinlock_t lock ____cacheline_aligned;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125};
126
127/**
128 * struct padata_instance - The overall control structure.
129 *
130 * @cpu_notifier: cpu hotplug notifier.
David Brazdil0f672f62019-12-10 10:32:29 +0000131 * @parallel_wq: The workqueue used for parallel work.
132 * @serial_wq: The workqueue used for serial work.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000133 * @pd: The internal control structure.
134 * @cpumask: User supplied cpumasks for parallel and serial works.
135 * @cpumask_change_notifier: Notifiers chain for user-defined notify
136 * callbacks that will be called when either @pcpu or @cbcpu
137 * or both cpumasks change.
138 * @kobj: padata instance kernel object.
139 * @lock: padata instance lock.
140 * @flags: padata flags.
141 */
142struct padata_instance {
143 struct hlist_node node;
David Brazdil0f672f62019-12-10 10:32:29 +0000144 struct workqueue_struct *parallel_wq;
145 struct workqueue_struct *serial_wq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000146 struct parallel_data *pd;
147 struct padata_cpumask cpumask;
148 struct blocking_notifier_head cpumask_change_notifier;
149 struct kobject kobj;
150 struct mutex lock;
151 u8 flags;
152#define PADATA_INIT 1
153#define PADATA_RESET 2
154#define PADATA_INVALID 4
155};
156
David Brazdil0f672f62019-12-10 10:32:29 +0000157extern struct padata_instance *padata_alloc_possible(const char *name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158extern void padata_free(struct padata_instance *pinst);
159extern int padata_do_parallel(struct padata_instance *pinst,
David Brazdil0f672f62019-12-10 10:32:29 +0000160 struct padata_priv *padata, int *cb_cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000161extern void padata_do_serial(struct padata_priv *padata);
162extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
163 cpumask_var_t cpumask);
164extern int padata_start(struct padata_instance *pinst);
165extern void padata_stop(struct padata_instance *pinst);
166extern int padata_register_cpumask_notifier(struct padata_instance *pinst,
167 struct notifier_block *nblock);
168extern int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
169 struct notifier_block *nblock);
170#endif