1=======================================
2The padata parallel execution mechanism
3=======================================
4
5:Last updated: for 2.6.36
6
7Padata is a mechanism by which the kernel can farm work out to be done in
8parallel on multiple CPUs while retaining the ordering of tasks.  It was
9developed for use with the IPsec code, which needs to be able to perform
10encryption and decryption on large numbers of packets without reordering
11those packets.  The crypto developers made a point of writing padata in a
12sufficiently general fashion that it could be put to other uses as well.
13
14The first step in using padata is to set up a padata_instance structure for
15overall control of how tasks are to be run::
16
17    #include <linux/padata.h>
18
19    struct padata_instance *padata_alloc(struct workqueue_struct *wq,
20					 const struct cpumask *pcpumask,
21					 const struct cpumask *cbcpumask);
22
23The pcpumask describes which processors will be used to execute work
24submitted to this instance in parallel. The cbcpumask defines which
25processors are allowed to be used as the serialization callback processor.
26The workqueue wq is where the work will actually be done; it should be
27a multithreaded queue, naturally.
28
29To allocate a padata instance with the cpu_possible_mask for both
30cpumasks this helper function can be used::
31
32    struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq);
33
34Note: Padata maintains two kinds of cpumasks internally. The user supplied
35cpumasks, submitted by padata_alloc/padata_alloc_possible and the 'usable'
36cpumasks. The usable cpumasks are always a subset of active CPUs in the
37user supplied cpumasks; these are the cpumasks padata actually uses. So
38it is legal to supply a cpumask to padata that contains offline CPUs.
39Once an offline CPU in the user supplied cpumask comes online, padata
40is going to use it.
41
42There are functions for enabling and disabling the instance::
43
44    int padata_start(struct padata_instance *pinst);
45    void padata_stop(struct padata_instance *pinst);
46
47These functions are setting or clearing the "PADATA_INIT" flag;
48if that flag is not set, other functions will refuse to work.
49padata_start returns zero on success (flag set) or -EINVAL if the
50padata cpumask contains no active CPU (flag not set).
51padata_stop clears the flag and blocks until the padata instance
52is unused.
53
54The list of CPUs to be used can be adjusted with these functions::
55
56    int padata_set_cpumasks(struct padata_instance *pinst,
57			    cpumask_var_t pcpumask,
58			    cpumask_var_t cbcpumask);
59    int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
60			   cpumask_var_t cpumask);
61    int padata_add_cpu(struct padata_instance *pinst, int cpu, int mask);
62    int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask);
63
64Changing the CPU masks are expensive operations, though, so it should not be
65done with great frequency.
66
67It's possible to change both cpumasks of a padata instance with
68padata_set_cpumasks by specifying the cpumasks for parallel execution (pcpumask)
69and for the serial callback function (cbcpumask). padata_set_cpumask is used to
70change just one of the cpumasks. Here cpumask_type is one of PADATA_CPU_SERIAL,
71PADATA_CPU_PARALLEL and cpumask specifies the new cpumask to use.
72To simply add or remove one CPU from a certain cpumask the functions
73padata_add_cpu/padata_remove_cpu are used. cpu specifies the CPU to add or
74remove and mask is one of PADATA_CPU_SERIAL, PADATA_CPU_PARALLEL.
75
76If a user is interested in padata cpumask changes, he can register to
77the padata cpumask change notifier::
78
79    int padata_register_cpumask_notifier(struct padata_instance *pinst,
80					 struct notifier_block *nblock);
81
82To unregister from that notifier::
83
84    int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
85					   struct notifier_block *nblock);
86
87The padata cpumask change notifier notifies about changes of the usable
88cpumasks, i.e. the subset of active CPUs in the user supplied cpumask.
89
90Padata calls the notifier chain with::
91
92    blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
93				 notification_mask,
94				 &pd_new->cpumask);
95
96Here cpumask_change_notifier is registered notifier, notification_mask
97is one of PADATA_CPU_SERIAL, PADATA_CPU_PARALLEL and cpumask is a pointer
98to a struct padata_cpumask that contains the new cpumask information.
99
100Actually submitting work to the padata instance requires the creation of a
101padata_priv structure::
102
103    struct padata_priv {
104        /* Other stuff here... */
105	void                    (*parallel)(struct padata_priv *padata);
106	void                    (*serial)(struct padata_priv *padata);
107    };
108
109This structure will almost certainly be embedded within some larger
110structure specific to the work to be done.  Most of its fields are private to
111padata, but the structure should be zeroed at initialisation time, and the
112parallel() and serial() functions should be provided.  Those functions will
113be called in the process of getting the work done as we will see
114momentarily.
115
116The submission of work is done with::
117
118    int padata_do_parallel(struct padata_instance *pinst,
119		           struct padata_priv *padata, int cb_cpu);
120
121The pinst and padata structures must be set up as described above; cb_cpu
122specifies which CPU will be used for the final callback when the work is
123done; it must be in the current instance's CPU mask.  The return value from
124padata_do_parallel() is zero on success, indicating that the work is in
125progress. -EBUSY means that somebody, somewhere else is messing with the
126instance's CPU mask, while -EINVAL is a complaint about cb_cpu not being
127in that CPU mask or about a not running instance.
128
129Each task submitted to padata_do_parallel() will, in turn, be passed to
130exactly one call to the above-mentioned parallel() function, on one CPU, so
131true parallelism is achieved by submitting multiple tasks.  Despite the
132fact that the workqueue is used to make these calls, parallel() is run with
133software interrupts disabled and thus cannot sleep.  The parallel()
134function gets the padata_priv structure pointer as its lone parameter;
135information about the actual work to be done is probably obtained by using
136container_of() to find the enclosing structure.
137
138Note that parallel() has no return value; the padata subsystem assumes that
139parallel() will take responsibility for the task from this point.  The work
140need not be completed during this call, but, if parallel() leaves work
141outstanding, it should be prepared to be called again with a new job before
142the previous one completes.  When a task does complete, parallel() (or
143whatever function actually finishes the job) should inform padata of the
144fact with a call to::
145
146    void padata_do_serial(struct padata_priv *padata);
147
148At some point in the future, padata_do_serial() will trigger a call to the
149serial() function in the padata_priv structure.  That call will happen on
150the CPU requested in the initial call to padata_do_parallel(); it, too, is
151done through the workqueue, but with local software interrupts disabled.
152Note that this call may be deferred for a while since the padata code takes
153pains to ensure that tasks are completed in the order in which they were
154submitted.
155
156The one remaining function in the padata API should be called to clean up
157when a padata instance is no longer needed::
158
159    void padata_free(struct padata_instance *pinst);
160
161This function will busy-wait while any remaining tasks are completed, so it
162might be best not to call it while there is work outstanding.  Shutting
163down the workqueue, if necessary, should be done separately.
164