blob: 269d7d0d8335c54cead614cacad4d96e8f692c6a [file] [log] [blame]
Jonathan Corbet4047f8b2010-05-12 14:23:48 -06001The padata parallel execution mechanism
2Last updated for 2.6.34
3
4Padata is a mechanism by which the kernel can farm work out to be done in
5parallel on multiple CPUs while retaining the ordering of tasks. It was
6developed for use with the IPsec code, which needs to be able to perform
7encryption and decryption on large numbers of packets without reordering
8those packets. The crypto developers made a point of writing padata in a
9sufficiently general fashion that it could be put to other uses as well.
10
11The first step in using padata is to set up a padata_instance structure for
12overall control of how tasks are to be run:
13
14 #include <linux/padata.h>
15
16 struct padata_instance *padata_alloc(const struct cpumask *cpumask,
17 struct workqueue_struct *wq);
18
19The cpumask describes which processors will be used to execute work
20submitted to this instance. The workqueue wq is where the work will
21actually be done; it should be a multithreaded queue, naturally.
22
23There are functions for enabling and disabling the instance:
24
25 void padata_start(struct padata_instance *pinst);
26 void padata_stop(struct padata_instance *pinst);
27
28These functions literally do nothing beyond setting or clearing the
29"padata_start() was called" flag; if that flag is not set, other functions
30will refuse to work.
31
32The list of CPUs to be used can be adjusted with these functions:
33
34 int padata_set_cpumask(struct padata_instance *pinst,
35 cpumask_var_t cpumask);
36 int padata_add_cpu(struct padata_instance *pinst, int cpu);
37 int padata_remove_cpu(struct padata_instance *pinst, int cpu);
38
39Changing the CPU mask has the look of an expensive operation, though, so it
40probably should not be done with great frequency.
41
42Actually submitting work to the padata instance requires the creation of a
43padata_priv structure:
44
45 struct padata_priv {
46 /* Other stuff here... */
47 void (*parallel)(struct padata_priv *padata);
48 void (*serial)(struct padata_priv *padata);
49 };
50
51This structure will almost certainly be embedded within some larger
52structure specific to the work to be done. Most its fields are private to
53padata, but the structure should be zeroed at initialization time, and the
54parallel() and serial() functions should be provided. Those functions will
55be called in the process of getting the work done as we will see
56momentarily.
57
58The submission of work is done with:
59
60 int padata_do_parallel(struct padata_instance *pinst,
61 struct padata_priv *padata, int cb_cpu);
62
63The pinst and padata structures must be set up as described above; cb_cpu
64specifies which CPU will be used for the final callback when the work is
65done; it must be in the current instance's CPU mask. The return value from
66padata_do_parallel() is a little strange; zero is an error return
67indicating that the caller forgot the padata_start() formalities. -EBUSY
68means that somebody, somewhere else is messing with the instance's CPU
69mask, while -EINVAL is a complaint about cb_cpu not being in that CPU mask.
70If all goes well, this function will return -EINPROGRESS, indicating that
71the work is in progress.
72
73Each task submitted to padata_do_parallel() will, in turn, be passed to
74exactly one call to the above-mentioned parallel() function, on one CPU, so
75true parallelism is achieved by submitting multiple tasks. Despite the
76fact that the workqueue is used to make these calls, parallel() is run with
77software interrupts disabled and thus cannot sleep. The parallel()
78function gets the padata_priv structure pointer as its lone parameter;
79information about the actual work to be done is probably obtained by using
80container_of() to find the enclosing structure.
81
82Note that parallel() has no return value; the padata subsystem assumes that
83parallel() will take responsibility for the task from this point. The work
84need not be completed during this call, but, if parallel() leaves work
85outstanding, it should be prepared to be called again with a new job before
86the previous one completes. When a task does complete, parallel() (or
87whatever function actually finishes the job) should inform padata of the
88fact with a call to:
89
90 void padata_do_serial(struct padata_priv *padata);
91
92At some point in the future, padata_do_serial() will trigger a call to the
93serial() function in the padata_priv structure. That call will happen on
94the CPU requested in the initial call to padata_do_parallel(); it, too, is
95done through the workqueue, but with local software interrupts disabled.
96Note that this call may be deferred for a while since the padata code takes
97pains to ensure that tasks are completed in the order in which they were
98submitted.
99
100The one remaining function in the padata API should be called to clean up
101when a padata instance is no longer needed:
102
103 void padata_free(struct padata_instance *pinst);
104
105This function will busy-wait while any remaining tasks are completed, so it
106might be best not to call it while there is work outstanding. Shutting
107down the workqueue, if necessary, should be done separately.