On Tue, Aug 08, 2023 at 01:08:31PM -0400, Sweet Tea Dorminy wrote: > btrfs sometimes frees extents while holding a mutex, which makes it > impossible to free an inlinecrypt prepared key since that requires > taking a semaphore. Therefore, we will need to offload prepared key > freeing into an asynchronous process (rcu is insufficient since that can > run in softirq context which is also incompatible with taking a > semaphore). In order to avoid use-after-free on the filesystem > superblock for keys being freed during shutdown, we need to cache the > list of devices that the key has been loaded into, so that we can later > remove it without reference to the superblock. > > Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@xxxxxxxxxx> > --- > fs/crypto/fscrypt_private.h | 13 +++++++++++-- > fs/crypto/inline_crypt.c | 20 +++++++++----------- > fs/crypto/keysetup.c | 2 +- > 3 files changed, 21 insertions(+), 14 deletions(-) > > diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h > index 03be2c136c0e..aba83509c735 100644 > --- a/fs/crypto/fscrypt_private.h > +++ b/fs/crypto/fscrypt_private.h > @@ -205,6 +205,16 @@ struct fscrypt_prepared_key { > struct crypto_skcipher *tfm; > #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT > struct blk_crypto_key *blk_key; > + > + /* > + * The list of devices that have this block key. > + */ > + struct block_device **devices; > + > + /* > + * The number of devices in @ci_devices. > + */ > + size_t device_count; > #endif > enum fscrypt_prepared_key_type type; > }; Well, this is basically reverting commit 22e9947a4b2b, but doing it in a slightly different way. I worry about potentially bringing back problems from doing work after the filesystem has already been unmounted. Can't you just make the filesystem flush its eviction work items when it is being unmounted? - Eric