The padata parallel execution mechanism¶
| Date: | May 2020 |
|---|
Padata is a mechanism by which the kernel can farm jobs out to be done inparallel on multiple CPUs while optionally retaining their ordering.
It was originally developed for IPsec, which needs to perform encryption anddecryption on large numbers of packets without reordering those packets. Thisis currently the sole consumer of padata’s serialized job support.
Padata also supports multithreaded jobs, splitting up the job evenly while loadbalancing and coordinating between threads.
Running Serialized Jobs¶
Initializing¶
The first step in using padata to run serialized jobs is to set up apadata_instance structure for overall control of how jobs are to be run:
#include <linux/padata.h>struct padata_instance *padata_alloc(const char *name);
‘name’ simply identifies the instance.
Then, complete padata initialization by allocating a padata_shell:
struct padata_shell *padata_alloc_shell(struct padata_instance *pinst);
A padata_shell is used to submit a job to padata and allows a series of suchjobs to be serialized independently. A padata_instance may have one or morepadata_shells associated with it, each allowing a separate series of jobs.
Modifying cpumasks¶
The CPUs used to run jobs can be changed in two ways, programatically withpadata_set_cpumask() or via sysfs. The former is defined:
int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type, cpumask_var_t cpumask);
Here cpumask_type is one of PADATA_CPU_PARALLEL or PADATA_CPU_SERIAL, where aparallel cpumask describes which processors will be used to execute jobssubmitted to this instance in parallel and a serial cpumask defines whichprocessors are allowed to be used as the serialization callback processor.cpumask specifies the new cpumask to use.
There may be sysfs files for an instance’s cpumasks. For example, pcrypt’slive in /sys/kernel/pcrypt/<instance-name>. Within an instance’s directorythere are two files, parallel_cpumask and serial_cpumask, and either cpumaskmay be changed by echoing a bitmask into the file, for example:
echo f > /sys/kernel/pcrypt/pencrypt/parallel_cpumask
Reading one of these files shows the user-supplied cpumask, which may bedifferent from the ‘usable’ cpumask.
Padata maintains two pairs of cpumasks internally, the user-supplied cpumasksand the ‘usable’ cpumasks. (Each pair consists of a parallel and a serialcpumask.) The user-supplied cpumasks default to all possible CPUs on instanceallocation and may be changed as above. The usable cpumasks are always asubset of the user-supplied cpumasks and contain only the online CPUs in theuser-supplied masks; these are the cpumasks padata actually uses. So it islegal to supply a cpumask to padata that contains offline CPUs. Once anoffline CPU in the user-supplied cpumask comes online, padata is going to useit.
Changing the CPU masks are expensive operations, so it should not be done withgreat frequency.
Running A Job¶
Actually submitting work to the padata instance requires the creation of apadata_priv structure, which represents one job:
struct padata_priv { /* Other stuff here... */ void (*parallel)(struct padata_priv *padata); void (*serial)(struct padata_priv *padata);};This structure will almost certainly be embedded within some largerstructure specific to the work to be done. Most of its fields are private topadata, but the structure should be zeroed at initialisation time, and theparallel() and serial() functions should be provided. Those functions willbe called in the process of getting the work done as we will seemomentarily.
The submission of the job is done with:
int padata_do_parallel(struct padata_shell *ps, struct padata_priv *padata, int *cb_cpu);
The ps and padata structures must be set up as described above; cb_cpupoints to the preferred CPU to be used for the final callback when the job isdone; it must be in the current instance’s CPU mask (if not the cb_cpu pointeris updated to point to the CPU actually chosen). The return value frompadata_do_parallel() is zero on success, indicating that the job is inprogress. -EBUSY means that somebody, somewhere else is messing with theinstance’s CPU mask, while -EINVAL is a complaint about cb_cpu not being in theserial cpumask, no online CPUs in the parallel or serial cpumasks, or a stoppedinstance.
Each job submitted topadata_do_parallel() will, in turn, be passed toexactly one call to the above-mentioned parallel() function, on one CPU, sotrue parallelism is achieved by submitting multiple jobs. parallel() runs withsoftware interrupts disabled and thus cannot sleep. The parallel()function gets the padata_priv structure pointer as its lone parameter;information about the actual work to be done is probably obtained by usingcontainer_of() to find the enclosing structure.
Note that parallel() has no return value; the padata subsystem assumes thatparallel() will take responsibility for the job from this point. The jobneed not be completed during this call, but, if parallel() leaves workoutstanding, it should be prepared to be called again with a new job beforethe previous one completes.
Serializing Jobs¶
When a job does complete, parallel() (or whatever function actually finishesthe work) should inform padata of the fact with a call to:
void padata_do_serial(struct padata_priv *padata);
At some point in the future,padata_do_serial() will trigger a call to theserial() function in the padata_priv structure. That call will happen onthe CPU requested in the initial call topadata_do_parallel(); it, too, isrun with local software interrupts disabled.Note that this call may be deferred for a while since the padata code takespains to ensure that jobs are completed in the order in which they weresubmitted.
Destroying¶
Cleaning up a padata instance predictably involves calling the two freefunctions that correspond to the allocation in reverse:
void padata_free_shell(struct padata_shell *ps);void padata_free(struct padata_instance *pinst);
It is the user’s responsibility to ensure all outstanding jobs are completebefore any of the above are called.
Running Multithreaded Jobs¶
A multithreaded job has a main thread and zero or more helper threads, with themain thread participating in the job and then waiting until all helpers havefinished. padata splits the job into units called chunks, where a chunk is apiece of the job that one thread completes in one call to the thread function.
A user has to do three things to run a multithreaded job. First, describe thejob by defining a padata_mt_job structure, which is explained in the Interfacesection. This includes a pointer to the thread function, which padata willcall each time it assigns a job chunk to a thread. Then, define the threadfunction, which accepts three arguments,start,end, andarg, wherethe first two delimit the range that the thread operates on and the last is apointer to the job’s shared state, if any. Prepare the shared state, which istypically allocated on the main thread’s stack. Last, callpadata_do_multithreaded(), which will return once the job is finished.
Interface¶
- struct
padata_priv¶ Represents one job
Definition
struct padata_priv { struct list_head list; struct parallel_data *pd; int cb_cpu; unsigned int seq_nr; int info; void (*parallel)(struct padata_priv *padata); void (*serial)(struct padata_priv *padata);};Members
list- List entry, to attach to the padata lists.
pd- Pointer to the internal control structure.
cb_cpu- Callback cpu for serializatioon.
seq_nr- Sequence number of the parallelized data object.
info- Used to pass information from the parallel to the serial function.
parallel- Parallel execution function.
serial- Serial complete function.
- struct
padata_list¶ one per work type per CPU
Definition
struct padata_list { struct list_head list; spinlock_t lock;};Members
list- List head.
lock- List lock.
- struct
padata_serial_queue¶ The percpu padata serial queue
Definition
struct padata_serial_queue { struct padata_list serial; struct work_struct work; struct parallel_data *pd;};Members
serial- List to wait for serialization after reordering.
work- work struct for serialization.
pd- Backpointer to the internal control structure.
- struct
padata_cpumask¶ The cpumasks for the parallel/serial workers
Definition
struct padata_cpumask { cpumask_var_t pcpu; cpumask_var_t cbcpu;};Members
pcpu- cpumask for the parallel workers.
cbcpu- cpumask for the serial (callback) workers.
- struct
parallel_data¶ Internal control structure, covers everything that depends on the cpumask in use.
Definition
struct parallel_data { struct padata_shell *ps; struct padata_list __percpu *reorder_list; struct padata_serial_queue __percpu *squeue; atomic_t refcnt; unsigned int seq_nr; unsigned int processed; int cpu; struct padata_cpumask cpumask; struct work_struct reorder_work; spinlock_t ____cacheline_aligned lock;};Members
ps- padata_shell object.
reorder_list- percpu reorder lists
squeue- percpu padata queues used for serialuzation.
refcnt- Number of objects holding a reference on this parallel_data.
seq_nr- Sequence number of the parallelized data object.
processed- Number of already processed objects.
cpu- Next CPU to be processed.
cpumask- The cpumasks in use for parallel and serial workers.
reorder_work- work struct for reordering.
lock- Reorder lock.
- struct
padata_shell¶ Wrapper around struct parallel_data, its purpose is to allow the underlying control structure to be replaced on the fly using RCU.
Definition
struct padata_shell { struct padata_instance *pinst; struct parallel_data __rcu *pd; struct parallel_data *opd; struct list_head list;};Members
pinst- padat instance.
pd- Actual parallel_data structure which may be substituted on the fly.
opd- Pointer to old pd to be freed by padata_replace.
list- List entry in padata_instance list.
- struct
padata_mt_job¶ represents one multithreaded job
Definition
struct padata_mt_job { void (*thread_fn)(unsigned long start, unsigned long end, void *arg); void *fn_arg; unsigned long start; unsigned long size; unsigned long align; unsigned long min_chunk; int max_threads;};Members
thread_fn- Called for each chunk of work that a padata thread does.
fn_arg- The thread function argument.
start- The start of the job (units are job-specific).
size- size of this node’s work (units are job-specific).
align- Ranges passed to the thread function fall on this boundary, with thepossible exceptions of the beginning and end of the job.
min_chunk- The minimum chunk size in job-specific units. This allowsthe client to communicate the minimum amount of work that’sappropriate for one worker thread to do at once.
max_threads- Max threads to use for the job, actual number may be lessdepending on task size and minimum chunk size.
- struct
padata_instance¶ The overall control structure.
Definition
struct padata_instance { struct hlist_node cpu_online_node; struct hlist_node cpu_dead_node; struct workqueue_struct *parallel_wq; struct workqueue_struct *serial_wq; struct list_head pslist; struct padata_cpumask cpumask; struct kobject kobj; struct mutex lock; u8 flags;#define PADATA_INIT 1;#define PADATA_RESET 2;#define PADATA_INVALID 4;};Members
cpu_online_node- Linkage for CPU online callback.
cpu_dead_node- Linkage for CPU offline callback.
parallel_wq- The workqueue used for parallel work.
serial_wq- The workqueue used for serial work.
pslist- List of padata_shell objects attached to this instance.
cpumask- User supplied cpumasks for parallel and serial works.
kobj- padata instance kernel object.
lock- padata instance lock.
flags- padata flags.
- int
padata_do_parallel(structpadata_shell * ps, structpadata_priv * padata, int * cb_cpu)¶ padata parallelization function
Parameters
structpadata_shell*ps- padatashell
structpadata_priv*padata- object to be parallelized
int*cb_cpu- pointer to the CPU that the serialization callback function shouldrun on. If it’s not in the serial cpumask ofpinst(i.e. cpumask.cbcpu), this function selects a fallback CPU and ifnone found, returns -EINVAL.
Description
The parallelization callback function will run with BHs off.
Note
Every object which is parallelized by padata_do_parallelmust be seen by padata_do_serial.
Return
0 on success or else negative error code.
- void
padata_do_serial(structpadata_priv * padata)¶ padata serialization function
Parameters
structpadata_priv*padata- object to be serialized.
Description
padata_do_serial must be called for every parallelized object.The serialization callback function will run with BHs off.
- void
padata_do_multithreaded(structpadata_mt_job * job)¶ run a multithreaded job
Parameters
structpadata_mt_job*job- Description of the job.
Description
See the definition of struct padata_mt_job for more details.
- int
padata_set_cpumask(structpadata_instance * pinst, int cpumask_type, cpumask_var_t cpumask)¶ Sets specified bycpumask_type cpumask to the value equivalent tocpumask.
Parameters
structpadata_instance*pinst- padata instance
intcpumask_type- PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL correspondingto parallel and serial cpumasks respectively.
cpumask_var_tcpumask- the cpumask to use
Return
0 on success or negative error code
- structpadata_instance *
padata_alloc(const char * name)¶ allocate and initialize a padata instance
Parameters
constchar*name- used to identify the instance
Return
new instance on success, NULL on error
- void
padata_free(structpadata_instance * pinst)¶ free a padata instance
Parameters
structpadata_instance*pinst- padata instance to free
- structpadata_shell *
padata_alloc_shell(structpadata_instance * pinst)¶ Allocate and initialize padata shell.
Parameters
structpadata_instance*pinst- Parent padata_instance object.
Return
new shell on success, NULL on error
- void
padata_free_shell(structpadata_shell * ps)¶ free a padata shell
Parameters
structpadata_shell*ps- padata shell to free