Skip to content

Commit 350ef88

Browse files
minipliherbertx
authored andcommittedOct 7, 2017
padata: ensure padata_do_serial() runs on the correct CPU
If the algorithm we're parallelizing is asynchronous we might change CPUs between padata_do_parallel() and padata_do_serial(). However, we don't expect this to happen as we need to enqueue the padata object into the per-cpu reorder queue we took it from, i.e. the same-cpu's parallel queue. Ensure we're not switching CPUs for a given padata object by tracking the CPU within the padata object. If the serial callback gets called on the wrong CPU, defer invoking padata_reorder() via a kernel worker on the CPU we're expected to run on. Signed-off-by: Mathias Krause <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent cf5868c commit 350ef88

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
 

‎include/linux/padata.h

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @list: List entry, to attach to the padata lists.
3838
* @pd: Pointer to the internal control structure.
3939
* @cb_cpu: Callback cpu for serializatioon.
40+
* @cpu: Cpu for parallelization.
4041
* @seq_nr: Sequence number of the parallelized data object.
4142
* @info: Used to pass information from the parallel to the serial function.
4243
* @parallel: Parallel execution function.
@@ -46,6 +47,7 @@ struct padata_priv {
4647
struct list_head list;
4748
struct parallel_data *pd;
4849
int cb_cpu;
50+
int cpu;
4951
int info;
5052
void (*parallel)(struct padata_priv *padata);
5153
void (*serial)(struct padata_priv *padata);

‎kernel/padata.c

+19-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ int padata_do_parallel(struct padata_instance *pinst,
131131
padata->cb_cpu = cb_cpu;
132132

133133
target_cpu = padata_cpu_hash(pd);
134+
padata->cpu = target_cpu;
134135
queue = per_cpu_ptr(pd->pqueue, target_cpu);
135136

136137
spin_lock(&queue->parallel.lock);
@@ -363,10 +364,21 @@ void padata_do_serial(struct padata_priv *padata)
363364
int cpu;
364365
struct padata_parallel_queue *pqueue;
365366
struct parallel_data *pd;
367+
int reorder_via_wq = 0;
366368

367369
pd = padata->pd;
368370

369371
cpu = get_cpu();
372+
373+
/* We need to run on the same CPU padata_do_parallel(.., padata, ..)
374+
* was called on -- or, at least, enqueue the padata object into the
375+
* correct per-cpu queue.
376+
*/
377+
if (cpu != padata->cpu) {
378+
reorder_via_wq = 1;
379+
cpu = padata->cpu;
380+
}
381+
370382
pqueue = per_cpu_ptr(pd->pqueue, cpu);
371383

372384
spin_lock(&pqueue->reorder.lock);
@@ -376,7 +388,13 @@ void padata_do_serial(struct padata_priv *padata)
376388

377389
put_cpu();
378390

379-
padata_reorder(pd);
391+
/* If we're running on the wrong CPU, call padata_reorder() via a
392+
* kernel worker.
393+
*/
394+
if (reorder_via_wq)
395+
queue_work_on(cpu, pd->pinst->wq, &pqueue->reorder_work);
396+
else
397+
padata_reorder(pd);
380398
}
381399
EXPORT_SYMBOL(padata_do_serial);
382400

0 commit comments

Comments
 (0)
Please sign in to comment.