Skip to content

Commit ff73c2c

Browse files
committed
fscrypt: avoid data race on fscrypt_mode::logged_impl_name
The access to logged_impl_name is technically a data race, which tools like KCSAN could complain about in the future. See: https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE Fix by using xchg(), which also ensures that only one thread does the logging. This also required switching from bool to int, to avoid a build error on the RISC-V architecture which doesn't implement xchg on bytes. Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent 065ab4c commit ff73c2c

2 files changed

Lines changed: 3 additions & 5 deletions

File tree

‎fs/crypto/fscrypt_private.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ struct fscrypt_mode {
435435
const char *cipher_str;
436436
int keysize;
437437
int ivsize;
438-
bool logged_impl_name;
438+
int logged_impl_name;
439439
};
440440

441441
static inline bool

‎fs/crypto/keysetup.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,13 @@ struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode,
8181
mode->cipher_str, PTR_ERR(tfm));
8282
return tfm;
8383
}
84-
if (unlikely(!mode->logged_impl_name)) {
84+
if (!xchg(&mode->logged_impl_name, 1)) {
8585
/*
8686
* fscrypt performance can vary greatly depending on which
8787
* crypto algorithm implementation is used. Help people debug
8888
* performance problems by logging the ->cra_driver_name the
89-
* first time a mode is used. Note that multiple threads can
90-
* race here, but it doesn't really matter.
89+
* first time a mode is used.
9190
*/
92-
mode->logged_impl_name = true;
9391
pr_info("fscrypt: %s using implementation \"%s\"\n",
9492
mode->friendly_name,
9593
crypto_skcipher_alg(tfm)->base.cra_driver_name);

0 commit comments

Comments
 (0)