179 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3=======================================4The padata parallel execution mechanism5=======================================6 7:Date: May 20208 9Padata is a mechanism by which the kernel can farm jobs out to be done in10parallel on multiple CPUs while optionally retaining their ordering.11 12It was originally developed for IPsec, which needs to perform encryption and13decryption on large numbers of packets without reordering those packets. This14is currently the sole consumer of padata's serialized job support.15 16Padata also supports multithreaded jobs, splitting up the job evenly while load17balancing and coordinating between threads.18 19Running Serialized Jobs20=======================21 22Initializing23------------24 25The first step in using padata to run serialized jobs is to set up a26padata_instance structure for overall control of how jobs are to be run::27 28 #include <linux/padata.h>29 30 struct padata_instance *padata_alloc(const char *name);31 32'name' simply identifies the instance.33 34Then, complete padata initialization by allocating a padata_shell::35 36 struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);37 38A padata_shell is used to submit a job to padata and allows a series of such39jobs to be serialized independently. A padata_instance may have one or more40padata_shells associated with it, each allowing a separate series of jobs.41 42Modifying cpumasks43------------------44 45The CPUs used to run jobs can be changed in two ways, programmatically with46padata_set_cpumask() or via sysfs. The former is defined::47 48 int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,49 cpumask_var_t cpumask);50 51Here cpumask_type is one of PADATA_CPU_PARALLEL or PADATA_CPU_SERIAL, where a52parallel cpumask describes which processors will be used to execute jobs53submitted to this instance in parallel and a serial cpumask defines which54processors are allowed to be used as the serialization callback processor.55cpumask specifies the new cpumask to use.56 57There may be sysfs files for an instance's cpumasks. For example, pcrypt's58live in /sys/kernel/pcrypt/<instance-name>. Within an instance's directory59there are two files, parallel_cpumask and serial_cpumask, and either cpumask60may be changed by echoing a bitmask into the file, for example::61 62 echo f > /sys/kernel/pcrypt/pencrypt/parallel_cpumask63 64Reading one of these files shows the user-supplied cpumask, which may be65different from the 'usable' cpumask.66 67Padata maintains two pairs of cpumasks internally, the user-supplied cpumasks68and the 'usable' cpumasks. (Each pair consists of a parallel and a serial69cpumask.) The user-supplied cpumasks default to all possible CPUs on instance70allocation and may be changed as above. The usable cpumasks are always a71subset of the user-supplied cpumasks and contain only the online CPUs in the72user-supplied masks; these are the cpumasks padata actually uses. So it is73legal to supply a cpumask to padata that contains offline CPUs. Once an74offline CPU in the user-supplied cpumask comes online, padata is going to use75it.76 77Changing the CPU masks are expensive operations, so it should not be done with78great frequency.79 80Running A Job81-------------82 83Actually submitting work to the padata instance requires the creation of a84padata_priv structure, which represents one job::85 86 struct padata_priv {87 /* Other stuff here... */88 void (*parallel)(struct padata_priv *padata);89 void (*serial)(struct padata_priv *padata);90 };91 92This structure will almost certainly be embedded within some larger93structure specific to the work to be done. Most of its fields are private to94padata, but the structure should be zeroed at initialisation time, and the95parallel() and serial() functions should be provided. Those functions will96be called in the process of getting the work done as we will see97momentarily.98 99The submission of the job is done with::100 101 int padata_do_parallel(struct padata_shell *ps,102 struct padata_priv *padata, int *cb_cpu);103 104The ps and padata structures must be set up as described above; cb_cpu105points to the preferred CPU to be used for the final callback when the job is106done; it must be in the current instance's CPU mask (if not the cb_cpu pointer107is updated to point to the CPU actually chosen). The return value from108padata_do_parallel() is zero on success, indicating that the job is in109progress. -EBUSY means that somebody, somewhere else is messing with the110instance's CPU mask, while -EINVAL is a complaint about cb_cpu not being in the111serial cpumask, no online CPUs in the parallel or serial cpumasks, or a stopped112instance.113 114Each job submitted to padata_do_parallel() will, in turn, be passed to115exactly one call to the above-mentioned parallel() function, on one CPU, so116true parallelism is achieved by submitting multiple jobs. parallel() runs with117software interrupts disabled and thus cannot sleep. The parallel()118function gets the padata_priv structure pointer as its lone parameter;119information about the actual work to be done is probably obtained by using120container_of() to find the enclosing structure.121 122Note that parallel() has no return value; the padata subsystem assumes that123parallel() will take responsibility for the job from this point. The job124need not be completed during this call, but, if parallel() leaves work125outstanding, it should be prepared to be called again with a new job before126the previous one completes.127 128Serializing Jobs129----------------130 131When a job does complete, parallel() (or whatever function actually finishes132the work) should inform padata of the fact with a call to::133 134 void padata_do_serial(struct padata_priv *padata);135 136At some point in the future, padata_do_serial() will trigger a call to the137serial() function in the padata_priv structure. That call will happen on138the CPU requested in the initial call to padata_do_parallel(); it, too, is139run with local software interrupts disabled.140Note that this call may be deferred for a while since the padata code takes141pains to ensure that jobs are completed in the order in which they were142submitted.143 144Destroying145----------146 147Cleaning up a padata instance predictably involves calling the two free148functions that correspond to the allocation in reverse::149 150 void padata_free_shell(struct padata_shell *ps);151 void padata_free(struct padata_instance *pinst);152 153It is the user's responsibility to ensure all outstanding jobs are complete154before any of the above are called.155 156Running Multithreaded Jobs157==========================158 159A multithreaded job has a main thread and zero or more helper threads, with the160main thread participating in the job and then waiting until all helpers have161finished. padata splits the job into units called chunks, where a chunk is a162piece of the job that one thread completes in one call to the thread function.163 164A user has to do three things to run a multithreaded job. First, describe the165job by defining a padata_mt_job structure, which is explained in the Interface166section. This includes a pointer to the thread function, which padata will167call each time it assigns a job chunk to a thread. Then, define the thread168function, which accepts three arguments, ``start``, ``end``, and ``arg``, where169the first two delimit the range that the thread operates on and the last is a170pointer to the job's shared state, if any. Prepare the shared state, which is171typically allocated on the main thread's stack. Last, call172padata_do_multithreaded(), which will return once the job is finished.173 174Interface175=========176 177.. kernel-doc:: include/linux/padata.h178.. kernel-doc:: kernel/padata.c179