1 /* SPDX-License-Identifier: GPL-2.0-only */ 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> 7 */ 8 9 #ifndef PADATA_H 10 #define PADATA_H 11 12 #include <linux/compiler_types.h> 13 #include <linux/workqueue.h> 14 #include <linux/spinlock.h> 15 #include <linux/list.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 - Represents one job 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 */ 33 struct padata_priv { 34 struct list_head list; 35 struct parallel_data *pd; 36 int cb_cpu; 37 int cpu; 38 unsigned int seq_nr; 39 int info; 40 void (*parallel)(struct padata_priv *padata); 41 void (*serial)(struct padata_priv *padata); 42 }; 43 44 /** 45 * struct padata_list - one per work type per CPU 46 * 47 * @list: List head. 48 * @lock: List lock. 49 */ 50 struct 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 */ 62 struct 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 * @work: work struct for parallelization. 74 * @num_obj: Number of objects that are processed by this cpu. 75 */ 76 struct padata_parallel_queue { 77 struct padata_list parallel; 78 struct padata_list reorder; 79 struct work_struct work; 80 atomic_t num_obj; 81 }; 82 83 /** 84 * struct padata_cpumask - The cpumasks for the parallel/serial workers 85 * 86 * @pcpu: cpumask for the parallel workers. 87 * @cbcpu: cpumask for the serial (callback) workers. 88 */ 89 struct padata_cpumask { 90 cpumask_var_t pcpu; 91 cpumask_var_t cbcpu; 92 }; 93 94 /** 95 * struct parallel_data - Internal control structure, covers everything 96 * that depends on the cpumask in use. 97 * 98 * @ps: padata_shell object. 99 * @pqueue: percpu padata queues used for parallelization. 100 * @squeue: percpu padata queues used for serialuzation. 101 * @refcnt: Number of objects holding a reference on this parallel_data. 102 * @seq_nr: Sequence number of the parallelized data object. 103 * @processed: Number of already processed objects. 104 * @cpu: Next CPU to be processed. 105 * @cpumask: The cpumasks in use for parallel and serial workers. 106 * @reorder_work: work struct for reordering. 107 * @lock: Reorder lock. 108 */ 109 struct parallel_data { 110 struct padata_shell *ps; 111 struct padata_parallel_queue __percpu *pqueue; 112 struct padata_serial_queue __percpu *squeue; 113 atomic_t refcnt; 114 atomic_t seq_nr; 115 unsigned int processed; 116 int cpu; 117 struct padata_cpumask cpumask; 118 struct work_struct reorder_work; 119 spinlock_t ____cacheline_aligned lock; 120 }; 121 122 /** 123 * struct padata_shell - Wrapper around struct parallel_data, its 124 * purpose is to allow the underlying control structure to be replaced 125 * on the fly using RCU. 126 * 127 * @pinst: padat instance. 128 * @pd: Actual parallel_data structure which may be substituted on the fly. 129 * @opd: Pointer to old pd to be freed by padata_replace. 130 * @list: List entry in padata_instance list. 131 */ 132 struct padata_shell { 133 struct padata_instance *pinst; 134 struct parallel_data __rcu *pd; 135 struct parallel_data *opd; 136 struct list_head list; 137 }; 138 139 /** 140 * struct padata_instance - The overall control structure. 141 * 142 * @node: Used by CPU hotplug. 143 * @parallel_wq: The workqueue used for parallel work. 144 * @serial_wq: The workqueue used for serial work. 145 * @pslist: List of padata_shell objects attached to this instance. 146 * @cpumask: User supplied cpumasks for parallel and serial works. 147 * @rcpumask: Actual cpumasks based on user cpumask and cpu_online_mask. 148 * @kobj: padata instance kernel object. 149 * @lock: padata instance lock. 150 * @flags: padata flags. 151 */ 152 struct padata_instance { 153 struct hlist_node node; 154 struct workqueue_struct *parallel_wq; 155 struct workqueue_struct *serial_wq; 156 struct list_head pslist; 157 struct padata_cpumask cpumask; 158 struct padata_cpumask rcpumask; 159 struct kobject kobj; 160 struct mutex lock; 161 u8 flags; 162 #define PADATA_INIT 1 163 #define PADATA_RESET 2 164 #define PADATA_INVALID 4 165 }; 166 167 extern struct padata_instance *padata_alloc_possible(const char *name); 168 extern void padata_free(struct padata_instance *pinst); 169 extern struct padata_shell *padata_alloc_shell(struct padata_instance *pinst); 170 extern void padata_free_shell(struct padata_shell *ps); 171 extern int padata_do_parallel(struct padata_shell *ps, 172 struct padata_priv *padata, int *cb_cpu); 173 extern void padata_do_serial(struct padata_priv *padata); 174 extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type, 175 cpumask_var_t cpumask); 176 extern int padata_start(struct padata_instance *pinst); 177 extern void padata_stop(struct padata_instance *pinst); 178 #endif 179