Skip to content

Commit 9abffc6

Browse files
Sebastian Andrzej Siewiorherbertx
authored andcommitted
crypto: mcryptd - protect the per-CPU queue with a lock
mcryptd_enqueue_request() grabs the per-CPU queue struct and protects access to it with disabled preemption. Then it schedules a worker on the same CPU. The worker in mcryptd_queue_worker() guards access to the same per-CPU variable with disabled preemption. If we take CPU-hotplug into account then it is possible that between queue_work_on() and the actual invocation of the worker the CPU goes down and the worker will be scheduled on _another_ CPU. And here the preempt_disable() protection does not work anymore. The easiest thing is to add a spin_lock() to guard access to the list. Another detail: mcryptd_queue_worker() is not processing more than MCRYPTD_BATCH invocation in a row. If there are still items left, then it will invoke queue_work() to proceed with more later. *I* would suggest to simply drop that check because it does not use a system workqueue and the workqueue is already marked as "CPU_INTENSIVE". And if preemption is required then the scheduler should do it. However if queue_work() is used then the work item is marked as CPU unbound. That means it will try to run on the local CPU but it may run on another CPU as well. Especially with CONFIG_DEBUG_WQ_FORCE_RR_CPU=y. Again, the preempt_disable() won't work here but lock which was introduced will help. In order to keep work-item on the local CPU (and avoid RR) I changed it to queue_work_on(). Cc: stable@vger.kernel.org Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 11edb55 commit 9abffc6

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

crypto/mcryptd.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static int mcryptd_init_queue(struct mcryptd_queue *queue,
8181
pr_debug("cpu_queue #%d %p\n", cpu, queue->cpu_queue);
8282
crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
8383
INIT_WORK(&cpu_queue->work, mcryptd_queue_worker);
84+
spin_lock_init(&cpu_queue->q_lock);
8485
}
8586
return 0;
8687
}
@@ -104,15 +105,16 @@ static int mcryptd_enqueue_request(struct mcryptd_queue *queue,
104105
int cpu, err;
105106
struct mcryptd_cpu_queue *cpu_queue;
106107

107-
cpu = get_cpu();
108-
cpu_queue = this_cpu_ptr(queue->cpu_queue);
109-
rctx->tag.cpu = cpu;
108+
cpu_queue = raw_cpu_ptr(queue->cpu_queue);
109+
spin_lock(&cpu_queue->q_lock);
110+
cpu = smp_processor_id();
111+
rctx->tag.cpu = smp_processor_id();
110112

111113
err = crypto_enqueue_request(&cpu_queue->queue, request);
112114
pr_debug("enqueue request: cpu %d cpu_queue %p request %p\n",
113115
cpu, cpu_queue, request);
116+
spin_unlock(&cpu_queue->q_lock);
114117
queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
115-
put_cpu();
116118

117119
return err;
118120
}
@@ -161,16 +163,11 @@ static void mcryptd_queue_worker(struct work_struct *work)
161163
cpu_queue = container_of(work, struct mcryptd_cpu_queue, work);
162164
i = 0;
163165
while (i < MCRYPTD_BATCH || single_task_running()) {
164-
/*
165-
* preempt_disable/enable is used to prevent
166-
* being preempted by mcryptd_enqueue_request()
167-
*/
168-
local_bh_disable();
169-
preempt_disable();
166+
167+
spin_lock_bh(&cpu_queue->q_lock);
170168
backlog = crypto_get_backlog(&cpu_queue->queue);
171169
req = crypto_dequeue_request(&cpu_queue->queue);
172-
preempt_enable();
173-
local_bh_enable();
170+
spin_unlock_bh(&cpu_queue->q_lock);
174171

175172
if (!req) {
176173
mcryptd_opportunistic_flush();
@@ -185,7 +182,7 @@ static void mcryptd_queue_worker(struct work_struct *work)
185182
++i;
186183
}
187184
if (cpu_queue->queue.qlen)
188-
queue_work(kcrypto_wq, &cpu_queue->work);
185+
queue_work_on(smp_processor_id(), kcrypto_wq, &cpu_queue->work);
189186
}
190187

191188
void mcryptd_flusher(struct work_struct *__work)

include/crypto/mcryptd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static inline struct mcryptd_ahash *__mcryptd_ahash_cast(
2727

2828
struct mcryptd_cpu_queue {
2929
struct crypto_queue queue;
30+
spinlock_t q_lock;
3031
struct work_struct work;
3132
};
3233

0 commit comments

Comments
 (0)