1.. SPDX-License-Identifier: GPL-2.0 2 3======================================= 4The padata parallel execution mechanism 5======================================= 6 7:Date: December 2019 8 9Padata is a mechanism by which the kernel can farm jobs out to be done in 10parallel on multiple CPUs while retaining their ordering. It was developed for 11use with the IPsec code, which needs to be able to perform encryption and 12decryption on large numbers of packets without reordering those packets. The 13crypto developers made a point of writing padata in a sufficiently general 14fashion that it could be put to other uses as well. 15 16Usage 17===== 18 19Initializing 20------------ 21 22The first step in using padata is to set up a padata_instance structure for 23overall control of how jobs are to be run:: 24 25 #include <linux/padata.h> 26 27 struct padata_instance *padata_alloc_possible(const char *name); 28 29'name' simply identifies the instance. 30 31There are functions for enabling and disabling the instance:: 32 33 int padata_start(struct padata_instance *pinst); 34 void padata_stop(struct padata_instance *pinst); 35 36These functions are setting or clearing the "PADATA_INIT" flag; if that flag is 37not set, other functions will refuse to work. padata_start() returns zero on 38success (flag set) or -EINVAL if the padata cpumask contains no active CPU 39(flag not set). padata_stop() clears the flag and blocks until the padata 40instance is unused. 41 42Finally, complete padata initialization by allocating a padata_shell:: 43 44 struct padata_shell *padata_alloc_shell(struct padata_instance *pinst); 45 46A padata_shell is used to submit a job to padata and allows a series of such 47jobs to be serialized independently. A padata_instance may have one or more 48padata_shells associated with it, each allowing a separate series of jobs. 49 50Modifying cpumasks 51------------------ 52 53The CPUs used to run jobs can be changed in two ways, programatically with 54padata_set_cpumask() or via sysfs. The former is defined:: 55 56 int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type, 57 cpumask_var_t cpumask); 58 59Here cpumask_type is one of PADATA_CPU_PARALLEL or PADATA_CPU_SERIAL, where a 60parallel cpumask describes which processors will be used to execute jobs 61submitted to this instance in parallel and a serial cpumask defines which 62processors are allowed to be used as the serialization callback processor. 63cpumask specifies the new cpumask to use. 64 65There may be sysfs files for an instance's cpumasks. For example, pcrypt's 66live in /sys/kernel/pcrypt/<instance-name>. Within an instance's directory 67there are two files, parallel_cpumask and serial_cpumask, and either cpumask 68may be changed by echoing a bitmask into the file, for example:: 69 70 echo f > /sys/kernel/pcrypt/pencrypt/parallel_cpumask 71 72Reading one of these files shows the user-supplied cpumask, which may be 73different from the 'usable' cpumask. 74 75Padata maintains two pairs of cpumasks internally, the user-supplied cpumasks 76and the 'usable' cpumasks. (Each pair consists of a parallel and a serial 77cpumask.) The user-supplied cpumasks default to all possible CPUs on instance 78allocation and may be changed as above. The usable cpumasks are always a 79subset of the user-supplied cpumasks and contain only the online CPUs in the 80user-supplied masks; these are the cpumasks padata actually uses. So it is 81legal to supply a cpumask to padata that contains offline CPUs. Once an 82offline CPU in the user-supplied cpumask comes online, padata is going to use 83it. 84 85Changing the CPU masks are expensive operations, so it should not be done with 86great frequency. 87 88Running A Job 89------------- 90 91Actually submitting work to the padata instance requires the creation of a 92padata_priv structure, which represents one job:: 93 94 struct padata_priv { 95 /* Other stuff here... */ 96 void (*parallel)(struct padata_priv *padata); 97 void (*serial)(struct padata_priv *padata); 98 }; 99 100This structure will almost certainly be embedded within some larger 101structure specific to the work to be done. Most of its fields are private to 102padata, but the structure should be zeroed at initialisation time, and the 103parallel() and serial() functions should be provided. Those functions will 104be called in the process of getting the work done as we will see 105momentarily. 106 107The submission of the job is done with:: 108 109 int padata_do_parallel(struct padata_shell *ps, 110 struct padata_priv *padata, int *cb_cpu); 111 112The ps and padata structures must be set up as described above; cb_cpu 113points to the preferred CPU to be used for the final callback when the job is 114done; it must be in the current instance's CPU mask (if not the cb_cpu pointer 115is updated to point to the CPU actually chosen). The return value from 116padata_do_parallel() is zero on success, indicating that the job is in 117progress. -EBUSY means that somebody, somewhere else is messing with the 118instance's CPU mask, while -EINVAL is a complaint about cb_cpu not being in the 119serial cpumask, no online CPUs in the parallel or serial cpumasks, or a stopped 120instance. 121 122Each job submitted to padata_do_parallel() will, in turn, be passed to 123exactly one call to the above-mentioned parallel() function, on one CPU, so 124true parallelism is achieved by submitting multiple jobs. parallel() runs with 125software interrupts disabled and thus cannot sleep. The parallel() 126function gets the padata_priv structure pointer as its lone parameter; 127information about the actual work to be done is probably obtained by using 128container_of() to find the enclosing structure. 129 130Note that parallel() has no return value; the padata subsystem assumes that 131parallel() will take responsibility for the job from this point. The job 132need not be completed during this call, but, if parallel() leaves work 133outstanding, it should be prepared to be called again with a new job before 134the previous one completes. 135 136Serializing Jobs 137---------------- 138 139When a job does complete, parallel() (or whatever function actually finishes 140the work) should inform padata of the fact with a call to:: 141 142 void padata_do_serial(struct padata_priv *padata); 143 144At some point in the future, padata_do_serial() will trigger a call to the 145serial() function in the padata_priv structure. That call will happen on 146the CPU requested in the initial call to padata_do_parallel(); it, too, is 147run with local software interrupts disabled. 148Note that this call may be deferred for a while since the padata code takes 149pains to ensure that jobs are completed in the order in which they were 150submitted. 151 152Destroying 153---------- 154 155Cleaning up a padata instance predictably involves calling the three free 156functions that correspond to the allocation in reverse:: 157 158 void padata_free_shell(struct padata_shell *ps); 159 void padata_stop(struct padata_instance *pinst); 160 void padata_free(struct padata_instance *pinst); 161 162It is the user's responsibility to ensure all outstanding jobs are complete 163before any of the above are called. 164 165Interface 166========= 167 168.. kernel-doc:: include/linux/padata.h 169.. kernel-doc:: kernel/padata.c 170