Skip to content

Commit afc49ea

Browse files
herbertxLee Jones
authored andcommitted
padata: Replace delayed timer with immediate workqueue in padata_reorder
[ Upstream commit 6fc4dbc ] The function padata_reorder will use a timer when it cannot progress while completed jobs are outstanding (pd->reorder_objects > 0). This is suboptimal as if we do end up using the timer then it would have introduced a gratuitous delay of one second. In fact we can easily distinguish between whether completed jobs are outstanding and whether we can make progress. All we have to do is look at the next pqueue list. This patch does that by replacing pd->processed with pd->cpu so that the next pqueue is more accessible. A work queue is used instead of the original try_again to avoid hogging the CPU. Note that we don't bother removing the work queue in padata_flush_queues because the whole premise is broken. You cannot flush async crypto requests so it makes no sense to even try. A subsequent patch will fix it by replacing it with a ref counting scheme. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> [dj: - adjust context - corrected setup_timer -> timer_setup to delete hunk - skip padata_flush_queues() hunk, function already removed in 4.4] Signed-off-by: Daniel Jordan <daniel.m.jordan@oracle.com> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Lee Jones <lee.jones@linaro.org> Change-Id: I41441bf382f3ace8dcc286b6b8b05f8633d1b838
1 parent 048a666 commit afc49ea

2 files changed

Lines changed: 22 additions & 86 deletions

File tree

include/linux/padata.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <linux/workqueue.h>
2525
#include <linux/spinlock.h>
2626
#include <linux/list.h>
27-
#include <linux/timer.h>
2827
#include <linux/notifier.h>
2928
#include <linux/kobject.h>
3029

@@ -85,18 +84,14 @@ struct padata_serial_queue {
8584
* @serial: List to wait for serialization after reordering.
8685
* @pwork: work struct for parallelization.
8786
* @swork: work struct for serialization.
88-
* @pd: Backpointer to the internal control structure.
8987
* @work: work struct for parallelization.
90-
* @reorder_work: work struct for reordering.
9188
* @num_obj: Number of objects that are processed by this cpu.
9289
* @cpu_index: Index of the cpu.
9390
*/
9491
struct padata_parallel_queue {
9592
struct padata_list parallel;
9693
struct padata_list reorder;
97-
struct parallel_data *pd;
9894
struct work_struct work;
99-
struct work_struct reorder_work;
10095
atomic_t num_obj;
10196
int cpu_index;
10297
};
@@ -122,10 +117,10 @@ struct padata_cpumask {
122117
* @reorder_objects: Number of objects waiting in the reorder queues.
123118
* @refcnt: Number of objects holding a reference on this parallel_data.
124119
* @max_seq_nr: Maximal used sequence number.
120+
* @cpu: Next CPU to be processed.
125121
* @cpumask: The cpumasks in use for parallel and serial workers.
122+
* @reorder_work: work struct for reordering.
126123
* @lock: Reorder lock.
127-
* @processed: Number of already processed objects.
128-
* @timer: Reorder timer.
129124
*/
130125
struct parallel_data {
131126
struct padata_instance *pinst;
@@ -134,10 +129,10 @@ struct parallel_data {
134129
atomic_t reorder_objects;
135130
atomic_t refcnt;
136131
atomic_t seq_nr;
132+
int cpu;
137133
struct padata_cpumask cpumask;
134+
struct work_struct reorder_work;
138135
spinlock_t lock ____cacheline_aligned;
139-
unsigned int processed;
140-
struct timer_list timer;
141136
};
142137

143138
/**

kernel/padata.c

Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,12 @@ EXPORT_SYMBOL(padata_do_parallel);
165165
*/
166166
static struct padata_priv *padata_get_next(struct parallel_data *pd)
167167
{
168-
int cpu, num_cpus;
169-
unsigned int next_nr, next_index;
170168
struct padata_parallel_queue *next_queue;
171169
struct padata_priv *padata;
172170
struct padata_list *reorder;
171+
int cpu = pd->cpu;
173172

174-
num_cpus = cpumask_weight(pd->cpumask.pcpu);
175-
176-
/*
177-
* Calculate the percpu reorder queue and the sequence
178-
* number of the next object.
179-
*/
180-
next_nr = pd->processed;
181-
next_index = next_nr % num_cpus;
182-
cpu = padata_index_to_cpu(pd, next_index);
183173
next_queue = per_cpu_ptr(pd->pqueue, cpu);
184-
185174
reorder = &next_queue->reorder;
186175

187176
spin_lock(&reorder->lock);
@@ -192,7 +181,8 @@ static struct padata_priv *padata_get_next(struct parallel_data *pd)
192181
list_del_init(&padata->list);
193182
atomic_dec(&pd->reorder_objects);
194183

195-
pd->processed++;
184+
pd->cpu = cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1,
185+
false);
196186

197187
spin_unlock(&reorder->lock);
198188
goto out;
@@ -215,6 +205,7 @@ static void padata_reorder(struct parallel_data *pd)
215205
struct padata_priv *padata;
216206
struct padata_serial_queue *squeue;
217207
struct padata_instance *pinst = pd->pinst;
208+
struct padata_parallel_queue *next_queue;
218209

219210
/*
220211
* We need to ensure that only one cpu can work on dequeueing of
@@ -246,7 +237,6 @@ static void padata_reorder(struct parallel_data *pd)
246237
* so exit immediately.
247238
*/
248239
if (PTR_ERR(padata) == -ENODATA) {
249-
del_timer(&pd->timer);
250240
spin_unlock_bh(&pd->lock);
251241
return;
252242
}
@@ -265,70 +255,29 @@ static void padata_reorder(struct parallel_data *pd)
265255

266256
/*
267257
* The next object that needs serialization might have arrived to
268-
* the reorder queues in the meantime, we will be called again
269-
* from the timer function if no one else cares for it.
258+
* the reorder queues in the meantime.
270259
*
271-
* Ensure reorder_objects is read after pd->lock is dropped so we see
272-
* an increment from another task in padata_do_serial. Pairs with
260+
* Ensure reorder queue is read after pd->lock is dropped so we see
261+
* new objects from another task in padata_do_serial. Pairs with
273262
* smp_mb__after_atomic in padata_do_serial.
274263
*/
275264
smp_mb();
276-
if (atomic_read(&pd->reorder_objects)
277-
&& !(pinst->flags & PADATA_RESET))
278-
mod_timer(&pd->timer, jiffies + HZ);
279-
else
280-
del_timer(&pd->timer);
281265

282-
return;
266+
next_queue = per_cpu_ptr(pd->pqueue, pd->cpu);
267+
if (!list_empty(&next_queue->reorder.list))
268+
queue_work(pinst->wq, &pd->reorder_work);
283269
}
284270

285271
static void invoke_padata_reorder(struct work_struct *work)
286272
{
287-
struct padata_parallel_queue *pqueue;
288273
struct parallel_data *pd;
289274

290275
local_bh_disable();
291-
pqueue = container_of(work, struct padata_parallel_queue, reorder_work);
292-
pd = pqueue->pd;
276+
pd = container_of(work, struct parallel_data, reorder_work);
293277
padata_reorder(pd);
294278
local_bh_enable();
295279
}
296280

297-
static void padata_reorder_timer(unsigned long arg)
298-
{
299-
struct parallel_data *pd = (struct parallel_data *)arg;
300-
unsigned int weight;
301-
int target_cpu, cpu;
302-
303-
cpu = get_cpu();
304-
305-
/* We don't lock pd here to not interfere with parallel processing
306-
* padata_reorder() calls on other CPUs. We just need any CPU out of
307-
* the cpumask.pcpu set. It would be nice if it's the right one but
308-
* it doesn't matter if we're off to the next one by using an outdated
309-
* pd->processed value.
310-
*/
311-
weight = cpumask_weight(pd->cpumask.pcpu);
312-
target_cpu = padata_index_to_cpu(pd, pd->processed % weight);
313-
314-
/* ensure to call the reorder callback on the correct CPU */
315-
if (cpu != target_cpu) {
316-
struct padata_parallel_queue *pqueue;
317-
struct padata_instance *pinst;
318-
319-
/* The timer function is serialized wrt itself -- no locking
320-
* needed.
321-
*/
322-
pinst = pd->pinst;
323-
pqueue = per_cpu_ptr(pd->pqueue, target_cpu);
324-
queue_work_on(target_cpu, pinst->wq, &pqueue->reorder_work);
325-
} else {
326-
padata_reorder(pd);
327-
}
328-
329-
put_cpu();
330-
}
331-
332281
static void padata_serial_worker(struct work_struct *serial_work)
333282
{
334283
struct padata_serial_queue *squeue;
@@ -382,9 +331,8 @@ void padata_do_serial(struct padata_priv *padata)
382331

383332
cpu = get_cpu();
384333

385-
/* We need to run on the same CPU padata_do_parallel(.., padata, ..)
386-
* was called on -- or, at least, enqueue the padata object into the
387-
* correct per-cpu queue.
334+
/* We need to enqueue the padata object into the correct
335+
* per-cpu queue.
388336
*/
389337
if (cpu != padata->cpu) {
390338
reorder_via_wq = 1;
@@ -394,26 +342,20 @@ void padata_do_serial(struct padata_priv *padata)
394342
pqueue = per_cpu_ptr(pd->pqueue, cpu);
395343

396344
spin_lock(&pqueue->reorder.lock);
397-
atomic_inc(&pd->reorder_objects);
398345
list_add_tail(&padata->list, &pqueue->reorder.list);
346+
atomic_inc(&pd->reorder_objects);
399347
spin_unlock(&pqueue->reorder.lock);
400348

401349
/*
402-
* Ensure the atomic_inc of reorder_objects above is ordered correctly
350+
* Ensure the addition to the reorder list is ordered correctly
403351
* with the trylock of pd->lock in padata_reorder. Pairs with smp_mb
404352
* in padata_reorder.
405353
*/
406354
smp_mb__after_atomic();
407355

408356
put_cpu();
409357

410-
/* If we're running on the wrong CPU, call padata_reorder() via a
411-
* kernel worker.
412-
*/
413-
if (reorder_via_wq)
414-
queue_work_on(cpu, pd->pinst->wq, &pqueue->reorder_work);
415-
else
416-
padata_reorder(pd);
358+
padata_reorder(pd);
417359
}
418360
EXPORT_SYMBOL(padata_do_serial);
419361

@@ -469,14 +411,12 @@ static void padata_init_pqueues(struct parallel_data *pd)
469411
continue;
470412
}
471413

472-
pqueue->pd = pd;
473414
pqueue->cpu_index = cpu_index;
474415
cpu_index++;
475416

476417
__padata_list_init(&pqueue->reorder);
477418
__padata_list_init(&pqueue->parallel);
478419
INIT_WORK(&pqueue->work, padata_parallel_worker);
479-
INIT_WORK(&pqueue->reorder_work, invoke_padata_reorder);
480420
atomic_set(&pqueue->num_obj, 0);
481421
}
482422
}
@@ -504,12 +444,13 @@ static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
504444

505445
padata_init_pqueues(pd);
506446
padata_init_squeues(pd);
507-
setup_timer(&pd->timer, padata_reorder_timer, (unsigned long)pd);
508447
atomic_set(&pd->seq_nr, -1);
509448
atomic_set(&pd->reorder_objects, 0);
510449
atomic_set(&pd->refcnt, 1);
511450
pd->pinst = pinst;
512451
spin_lock_init(&pd->lock);
452+
pd->cpu = cpumask_first(pcpumask);
453+
INIT_WORK(&pd->reorder_work, invoke_padata_reorder);
513454

514455
return pd;
515456

0 commit comments

Comments
 (0)