Blame view

Documentation/padata.txt 7.3 KB
7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
1
  =======================================
4047f8b1f   Jonathan Corbet   Add a document de...
2
  The padata parallel execution mechanism
7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
3
4
5
  =======================================
  
  :Last updated: for 2.6.36
4047f8b1f   Jonathan Corbet   Add a document de...
6
7
8
9
10
11
12
13
14
  
  Padata is a mechanism by which the kernel can farm work out to be done in
  parallel on multiple CPUs while retaining the ordering of tasks.  It was
  developed for use with the IPsec code, which needs to be able to perform
  encryption and decryption on large numbers of packets without reordering
  those packets.  The crypto developers made a point of writing padata in a
  sufficiently general fashion that it could be put to other uses as well.
  
  The first step in using padata is to set up a padata_instance structure for
7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
15
  overall control of how tasks are to be run::
4047f8b1f   Jonathan Corbet   Add a document de...
16
17
  
      #include <linux/padata.h>
b128a3040   Daniel Jordan   padata: allocate ...
18
      struct padata_instance *padata_alloc(const char *name,
313910d3b   Steffen Klassert   padata: update AP...
19
20
  					 const struct cpumask *pcpumask,
  					 const struct cpumask *cbcpumask);
4047f8b1f   Jonathan Corbet   Add a document de...
21

b128a3040   Daniel Jordan   padata: allocate ...
22
  'name' simply identifies the instance.
313910d3b   Steffen Klassert   padata: update AP...
23
24
  The pcpumask describes which processors will be used to execute work
  submitted to this instance in parallel. The cbcpumask defines which
2b24706a7   Randy Dunlap   Documentation/pad...
25
  processors are allowed to be used as the serialization callback processor.
313910d3b   Steffen Klassert   padata: update AP...
26
27
28
29
  The workqueue wq is where the work will actually be done; it should be
  a multithreaded queue, naturally.
  
  To allocate a padata instance with the cpu_possible_mask for both
7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
30
  cpumasks this helper function can be used::
313910d3b   Steffen Klassert   padata: update AP...
31
32
33
34
35
  
      struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq);
  
  Note: Padata maintains two kinds of cpumasks internally. The user supplied
  cpumasks, submitted by padata_alloc/padata_alloc_possible and the 'usable'
2b24706a7   Randy Dunlap   Documentation/pad...
36
37
38
39
  cpumasks. The usable cpumasks are always a subset of active CPUs in the
  user supplied cpumasks; these are the cpumasks padata actually uses. So
  it is legal to supply a cpumask to padata that contains offline CPUs.
  Once an offline CPU in the user supplied cpumask comes online, padata
313910d3b   Steffen Klassert   padata: update AP...
40
  is going to use it.
4047f8b1f   Jonathan Corbet   Add a document de...
41

7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
42
  There are functions for enabling and disabling the instance::
4047f8b1f   Jonathan Corbet   Add a document de...
43

2197f9a16   Steffen Klassert   padata: update do...
44
      int padata_start(struct padata_instance *pinst);
4047f8b1f   Jonathan Corbet   Add a document de...
45
      void padata_stop(struct padata_instance *pinst);
2197f9a16   Steffen Klassert   padata: update do...
46
47
48
  These functions are setting or clearing the "PADATA_INIT" flag;
  if that flag is not set, other functions will refuse to work.
  padata_start returns zero on success (flag set) or -EINVAL if the
2b24706a7   Randy Dunlap   Documentation/pad...
49
  padata cpumask contains no active CPU (flag not set).
2197f9a16   Steffen Klassert   padata: update do...
50
51
  padata_stop clears the flag and blocks until the padata instance
  is unused.
4047f8b1f   Jonathan Corbet   Add a document de...
52

7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
53
  The list of CPUs to be used can be adjusted with these functions::
4047f8b1f   Jonathan Corbet   Add a document de...
54

313910d3b   Steffen Klassert   padata: update AP...
55
56
57
58
      int padata_set_cpumasks(struct padata_instance *pinst,
  			    cpumask_var_t pcpumask,
  			    cpumask_var_t cbcpumask);
      int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
4047f8b1f   Jonathan Corbet   Add a document de...
59
  			   cpumask_var_t cpumask);
313910d3b   Steffen Klassert   padata: update AP...
60
61
62
63
64
65
66
67
      int padata_add_cpu(struct padata_instance *pinst, int cpu, int mask);
      int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask);
  
  Changing the CPU masks are expensive operations, though, so it should not be
  done with great frequency.
  
  It's possible to change both cpumasks of a padata instance with
  padata_set_cpumasks by specifying the cpumasks for parallel execution (pcpumask)
2b24706a7   Randy Dunlap   Documentation/pad...
68
  and for the serial callback function (cbcpumask). padata_set_cpumask is used to
313910d3b   Steffen Klassert   padata: update AP...
69
70
  change just one of the cpumasks. Here cpumask_type is one of PADATA_CPU_SERIAL,
  PADATA_CPU_PARALLEL and cpumask specifies the new cpumask to use.
2b24706a7   Randy Dunlap   Documentation/pad...
71
72
  To simply add or remove one CPU from a certain cpumask the functions
  padata_add_cpu/padata_remove_cpu are used. cpu specifies the CPU to add or
313910d3b   Steffen Klassert   padata: update AP...
73
74
75
  remove and mask is one of PADATA_CPU_SERIAL, PADATA_CPU_PARALLEL.
  
  If a user is interested in padata cpumask changes, he can register to
7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
76
  the padata cpumask change notifier::
313910d3b   Steffen Klassert   padata: update AP...
77
78
79
  
      int padata_register_cpumask_notifier(struct padata_instance *pinst,
  					 struct notifier_block *nblock);
7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
80
  To unregister from that notifier::
313910d3b   Steffen Klassert   padata: update AP...
81
82
83
84
85
  
      int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
  					   struct notifier_block *nblock);
  
  The padata cpumask change notifier notifies about changes of the usable
2b24706a7   Randy Dunlap   Documentation/pad...
86
  cpumasks, i.e. the subset of active CPUs in the user supplied cpumask.
313910d3b   Steffen Klassert   padata: update AP...
87

7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
88
  Padata calls the notifier chain with::
313910d3b   Steffen Klassert   padata: update AP...
89
90
91
92
  
      blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
  				 notification_mask,
  				 &pd_new->cpumask);
4047f8b1f   Jonathan Corbet   Add a document de...
93

313910d3b   Steffen Klassert   padata: update AP...
94
95
  Here cpumask_change_notifier is registered notifier, notification_mask
  is one of PADATA_CPU_SERIAL, PADATA_CPU_PARALLEL and cpumask is a pointer
2b24706a7   Randy Dunlap   Documentation/pad...
96
  to a struct padata_cpumask that contains the new cpumask information.
4047f8b1f   Jonathan Corbet   Add a document de...
97
98
  
  Actually submitting work to the padata instance requires the creation of a
7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
99
  padata_priv structure::
4047f8b1f   Jonathan Corbet   Add a document de...
100
101
102
103
104
105
106
107
  
      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 larger
2b24706a7   Randy Dunlap   Documentation/pad...
108
  structure specific to the work to be done.  Most of its fields are private to
313910d3b   Steffen Klassert   padata: update AP...
109
  padata, but the structure should be zeroed at initialisation time, and the
4047f8b1f   Jonathan Corbet   Add a document de...
110
111
112
  parallel() and serial() functions should be provided.  Those functions will
  be called in the process of getting the work done as we will see
  momentarily.
7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
113
  The submission of work is done with::
4047f8b1f   Jonathan Corbet   Add a document de...
114
115
116
117
118
119
120
  
      int padata_do_parallel(struct padata_instance *pinst,
  		           struct padata_priv *padata, int cb_cpu);
  
  The pinst and padata structures must be set up as described above; cb_cpu
  specifies which CPU will be used for the final callback when the work is
  done; it must be in the current instance's CPU mask.  The return value from
2197f9a16   Steffen Klassert   padata: update do...
121
122
123
124
  padata_do_parallel() is zero on success, indicating that the work is in
  progress. -EBUSY means that somebody, somewhere else is messing with the
  instance's CPU mask, while -EINVAL is a complaint about cb_cpu not being
  in that CPU mask or about a not running instance.
4047f8b1f   Jonathan Corbet   Add a document de...
125
126
127
  
  Each task submitted to padata_do_parallel() will, in turn, be passed to
  exactly one call to the above-mentioned parallel() function, on one CPU, so
b128a3040   Daniel Jordan   padata: allocate ...
128
  true parallelism is achieved by submitting multiple tasks.  parallel() runs with
4047f8b1f   Jonathan Corbet   Add a document de...
129
130
131
132
133
134
135
136
137
138
139
  software 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 using
  container_of() to find the enclosing structure.
  
  Note that parallel() has no return value; the padata subsystem assumes that
  parallel() will take responsibility for the task from this point.  The work
  need not be completed during this call, but, if parallel() leaves work
  outstanding, it should be prepared to be called again with a new job before
  the previous one completes.  When a task does complete, parallel() (or
  whatever function actually finishes the job) should inform padata of the
7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
140
  fact with a call to::
4047f8b1f   Jonathan Corbet   Add a document de...
141
142
143
144
145
146
  
      void padata_do_serial(struct padata_priv *padata);
  
  At some point in the future, padata_do_serial() will trigger a call to the
  serial() function in the padata_priv structure.  That call will happen on
  the CPU requested in the initial call to padata_do_parallel(); it, too, is
b128a3040   Daniel Jordan   padata: allocate ...
147
  run with local software interrupts disabled.
4047f8b1f   Jonathan Corbet   Add a document de...
148
149
150
151
152
  Note that this call may be deferred for a while since the padata code takes
  pains to ensure that tasks are completed in the order in which they were
  submitted.
  
  The one remaining function in the padata API should be called to clean up
7576b2b98   Mauro Carvalho Chehab   padata.txt: stand...
153
  when a padata instance is no longer needed::
4047f8b1f   Jonathan Corbet   Add a document de...
154
155
156
157
  
      void padata_free(struct padata_instance *pinst);
  
  This function will busy-wait while any remaining tasks are completed, so it
b128a3040   Daniel Jordan   padata: allocate ...
158
  might be best not to call it while there is work outstanding.