Skip to content

Commit c0dbceb

Browse files
herbertxgregkh
authored andcommitted
crypto: api - Use work queue in crypto_destroy_instance
[ Upstream commit 9ae4577 ] The function crypto_drop_spawn expects to be called in process context. However, when an instance is unregistered while it still has active users, the last user may cause the instance to be freed in atomic context. Fix this by delaying the freeing to a work queue. Fixes: 6bfd480 ("[CRYPTO] api: Added spawns") Reported-by: Florent Revest <[email protected]> Reported-by: [email protected] Reported-by: [email protected] Signed-off-by: Herbert Xu <[email protected]> Tested-by: Florent Revest <[email protected]> Acked-by: Florent Revest <[email protected]> Signed-off-by: Herbert Xu <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 2de34b2 commit c0dbceb

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

crypto/algapi.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/rtnetlink.h>
1818
#include <linux/slab.h>
1919
#include <linux/string.h>
20+
#include <linux/workqueue.h>
2021

2122
#include "internal.h"
2223

@@ -74,15 +75,26 @@ static void crypto_free_instance(struct crypto_instance *inst)
7475
inst->alg.cra_type->free(inst);
7576
}
7677

77-
static void crypto_destroy_instance(struct crypto_alg *alg)
78+
static void crypto_destroy_instance_workfn(struct work_struct *w)
7879
{
79-
struct crypto_instance *inst = (void *)alg;
80+
struct crypto_instance *inst = container_of(w, struct crypto_instance,
81+
free_work);
8082
struct crypto_template *tmpl = inst->tmpl;
8183

8284
crypto_free_instance(inst);
8385
crypto_tmpl_put(tmpl);
8486
}
8587

88+
static void crypto_destroy_instance(struct crypto_alg *alg)
89+
{
90+
struct crypto_instance *inst = container_of(alg,
91+
struct crypto_instance,
92+
alg);
93+
94+
INIT_WORK(&inst->free_work, crypto_destroy_instance_workfn);
95+
schedule_work(&inst->free_work);
96+
}
97+
8698
/*
8799
* This function adds a spawn to the list secondary_spawns which
88100
* will be used at the end of crypto_remove_spawns to unregister

include/crypto/algapi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/cache.h>
1313
#include <linux/crypto.h>
1414
#include <linux/types.h>
15+
#include <linux/workqueue.h>
1516

1617
/*
1718
* Maximum values for blocksize and alignmask, used to allocate
@@ -82,6 +83,8 @@ struct crypto_instance {
8283
struct crypto_spawn *spawns;
8384
};
8485

86+
struct work_struct free_work;
87+
8588
void *__ctx[] CRYPTO_MINALIGN_ATTR;
8689
};
8790

0 commit comments

Comments
 (0)