blob: b37ba1eaace3d905063836b1a715ecf697cd317f [file] [log] [blame]
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -03001=======================================
Jonathan Corbet4047f8b2010-05-12 14:23:48 -06002The padata parallel execution mechanism
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -03003=======================================
4
5:Last updated: for 2.6.36
Jonathan Corbet4047f8b2010-05-12 14:23:48 -06006
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
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -030015overall control of how tasks are to be run::
Jonathan Corbet4047f8b2010-05-12 14:23:48 -060016
17 #include <linux/padata.h>
18
Daniel Jordanb128a302019-09-05 21:40:21 -040019 struct padata_instance *padata_alloc(const char *name,
Steffen Klassert313910d2010-07-27 07:20:47 +020020 const struct cpumask *pcpumask,
21 const struct cpumask *cbcpumask);
Jonathan Corbet4047f8b2010-05-12 14:23:48 -060022
Daniel Jordanb128a302019-09-05 21:40:21 -040023'name' simply identifies the instance.
24
Steffen Klassert313910d2010-07-27 07:20:47 +020025The pcpumask describes which processors will be used to execute work
26submitted to this instance in parallel. The cbcpumask defines which
Randy Dunlap2b247062010-08-10 18:02:53 -070027processors are allowed to be used as the serialization callback processor.
Steffen Klassert313910d2010-07-27 07:20:47 +020028The workqueue wq is where the work will actually be done; it should be
29a multithreaded queue, naturally.
30
31To allocate a padata instance with the cpu_possible_mask for both
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -030032cpumasks this helper function can be used::
Steffen Klassert313910d2010-07-27 07:20:47 +020033
34 struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq);
35
36Note: Padata maintains two kinds of cpumasks internally. The user supplied
37cpumasks, submitted by padata_alloc/padata_alloc_possible and the 'usable'
Randy Dunlap2b247062010-08-10 18:02:53 -070038cpumasks. The usable cpumasks are always a subset of active CPUs in the
39user supplied cpumasks; these are the cpumasks padata actually uses. So
40it is legal to supply a cpumask to padata that contains offline CPUs.
41Once an offline CPU in the user supplied cpumask comes online, padata
Steffen Klassert313910d2010-07-27 07:20:47 +020042is going to use it.
Jonathan Corbet4047f8b2010-05-12 14:23:48 -060043
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -030044There are functions for enabling and disabling the instance::
Jonathan Corbet4047f8b2010-05-12 14:23:48 -060045
Steffen Klassert2197f9a2010-07-07 15:34:03 +020046 int padata_start(struct padata_instance *pinst);
Jonathan Corbet4047f8b2010-05-12 14:23:48 -060047 void padata_stop(struct padata_instance *pinst);
48
Steffen Klassert2197f9a2010-07-07 15:34:03 +020049These functions are setting or clearing the "PADATA_INIT" flag;
50if that flag is not set, other functions will refuse to work.
51padata_start returns zero on success (flag set) or -EINVAL if the
Randy Dunlap2b247062010-08-10 18:02:53 -070052padata cpumask contains no active CPU (flag not set).
Steffen Klassert2197f9a2010-07-07 15:34:03 +020053padata_stop clears the flag and blocks until the padata instance
54is unused.
Jonathan Corbet4047f8b2010-05-12 14:23:48 -060055
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -030056The list of CPUs to be used can be adjusted with these functions::
Jonathan Corbet4047f8b2010-05-12 14:23:48 -060057
Steffen Klassert313910d2010-07-27 07:20:47 +020058 int padata_set_cpumasks(struct padata_instance *pinst,
59 cpumask_var_t pcpumask,
60 cpumask_var_t cbcpumask);
61 int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
Jonathan Corbet4047f8b2010-05-12 14:23:48 -060062 cpumask_var_t cpumask);
Steffen Klassert313910d2010-07-27 07:20:47 +020063 int padata_add_cpu(struct padata_instance *pinst, int cpu, int mask);
64 int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask);
Jonathan Corbet4047f8b2010-05-12 14:23:48 -060065
Steffen Klassert313910d2010-07-27 07:20:47 +020066Changing the CPU masks are expensive operations, though, so it should not be
67done with great frequency.
68
69It's possible to change both cpumasks of a padata instance with
70padata_set_cpumasks by specifying the cpumasks for parallel execution (pcpumask)
Randy Dunlap2b247062010-08-10 18:02:53 -070071and for the serial callback function (cbcpumask). padata_set_cpumask is used to
Steffen Klassert313910d2010-07-27 07:20:47 +020072change just one of the cpumasks. Here cpumask_type is one of PADATA_CPU_SERIAL,
73PADATA_CPU_PARALLEL and cpumask specifies the new cpumask to use.
Randy Dunlap2b247062010-08-10 18:02:53 -070074To simply add or remove one CPU from a certain cpumask the functions
75padata_add_cpu/padata_remove_cpu are used. cpu specifies the CPU to add or
Steffen Klassert313910d2010-07-27 07:20:47 +020076remove and mask is one of PADATA_CPU_SERIAL, PADATA_CPU_PARALLEL.
77
78If a user is interested in padata cpumask changes, he can register to
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -030079the padata cpumask change notifier::
Steffen Klassert313910d2010-07-27 07:20:47 +020080
81 int padata_register_cpumask_notifier(struct padata_instance *pinst,
82 struct notifier_block *nblock);
83
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -030084To unregister from that notifier::
Steffen Klassert313910d2010-07-27 07:20:47 +020085
86 int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
87 struct notifier_block *nblock);
88
89The padata cpumask change notifier notifies about changes of the usable
Randy Dunlap2b247062010-08-10 18:02:53 -070090cpumasks, i.e. the subset of active CPUs in the user supplied cpumask.
Steffen Klassert313910d2010-07-27 07:20:47 +020091
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -030092Padata calls the notifier chain with::
Steffen Klassert313910d2010-07-27 07:20:47 +020093
94 blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
95 notification_mask,
96 &pd_new->cpumask);
97
98Here cpumask_change_notifier is registered notifier, notification_mask
99is one of PADATA_CPU_SERIAL, PADATA_CPU_PARALLEL and cpumask is a pointer
Randy Dunlap2b247062010-08-10 18:02:53 -0700100to a struct padata_cpumask that contains the new cpumask information.
Jonathan Corbet4047f8b2010-05-12 14:23:48 -0600101
102Actually submitting work to the padata instance requires the creation of a
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -0300103padata_priv structure::
Jonathan Corbet4047f8b2010-05-12 14:23:48 -0600104
105 struct padata_priv {
106 /* Other stuff here... */
107 void (*parallel)(struct padata_priv *padata);
108 void (*serial)(struct padata_priv *padata);
109 };
110
111This structure will almost certainly be embedded within some larger
Randy Dunlap2b247062010-08-10 18:02:53 -0700112structure specific to the work to be done. Most of its fields are private to
Steffen Klassert313910d2010-07-27 07:20:47 +0200113padata, but the structure should be zeroed at initialisation time, and the
Jonathan Corbet4047f8b2010-05-12 14:23:48 -0600114parallel() and serial() functions should be provided. Those functions will
115be called in the process of getting the work done as we will see
116momentarily.
117
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -0300118The submission of work is done with::
Jonathan Corbet4047f8b2010-05-12 14:23:48 -0600119
120 int padata_do_parallel(struct padata_instance *pinst,
121 struct padata_priv *padata, int cb_cpu);
122
123The pinst and padata structures must be set up as described above; cb_cpu
124specifies which CPU will be used for the final callback when the work is
125done; it must be in the current instance's CPU mask. The return value from
Steffen Klassert2197f9a2010-07-07 15:34:03 +0200126padata_do_parallel() is zero on success, indicating that the work is in
127progress. -EBUSY means that somebody, somewhere else is messing with the
128instance's CPU mask, while -EINVAL is a complaint about cb_cpu not being
129in that CPU mask or about a not running instance.
Jonathan Corbet4047f8b2010-05-12 14:23:48 -0600130
131Each task submitted to padata_do_parallel() will, in turn, be passed to
132exactly one call to the above-mentioned parallel() function, on one CPU, so
Daniel Jordanb128a302019-09-05 21:40:21 -0400133true parallelism is achieved by submitting multiple tasks. parallel() runs with
Jonathan Corbet4047f8b2010-05-12 14:23:48 -0600134software interrupts disabled and thus cannot sleep. The parallel()
135function gets the padata_priv structure pointer as its lone parameter;
136information about the actual work to be done is probably obtained by using
137container_of() to find the enclosing structure.
138
139Note that parallel() has no return value; the padata subsystem assumes that
140parallel() will take responsibility for the task from this point. The work
141need not be completed during this call, but, if parallel() leaves work
142outstanding, it should be prepared to be called again with a new job before
143the previous one completes. When a task does complete, parallel() (or
144whatever function actually finishes the job) should inform padata of the
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -0300145fact with a call to::
Jonathan Corbet4047f8b2010-05-12 14:23:48 -0600146
147 void padata_do_serial(struct padata_priv *padata);
148
149At some point in the future, padata_do_serial() will trigger a call to the
150serial() function in the padata_priv structure. That call will happen on
151the CPU requested in the initial call to padata_do_parallel(); it, too, is
Daniel Jordanb128a302019-09-05 21:40:21 -0400152run with local software interrupts disabled.
Jonathan Corbet4047f8b2010-05-12 14:23:48 -0600153Note that this call may be deferred for a while since the padata code takes
154pains to ensure that tasks are completed in the order in which they were
155submitted.
156
157The one remaining function in the padata API should be called to clean up
Mauro Carvalho Chehab7576b2b2017-05-16 10:06:48 -0300158when a padata instance is no longer needed::
Jonathan Corbet4047f8b2010-05-12 14:23:48 -0600159
160 void padata_free(struct padata_instance *pinst);
161
162This function will busy-wait while any remaining tasks are completed, so it
Daniel Jordanb128a302019-09-05 21:40:21 -0400163might be best not to call it while there is work outstanding.