Skip to content

Commit

Permalink
fscrypt: avoid data race on fscrypt_mode::logged_impl_name
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ebiggers committed Nov 6, 2019
1 parent 065ab4c commit ff73c2c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
2 changes: 1 addition & 1 deletion fs/crypto/fscrypt_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ struct fscrypt_mode {
const char *cipher_str;
int keysize;
int ivsize;
bool logged_impl_name;
int logged_impl_name;
};

static inline bool
Expand Down
6 changes: 2 additions & 4 deletions fs/crypto/keysetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,13 @@ struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode,
mode->cipher_str, PTR_ERR(tfm));
return tfm;
}
if (unlikely(!mode->logged_impl_name)) {
if (!xchg(&mode->logged_impl_name, 1)) {
/*
* fscrypt performance can vary greatly depending on which
* crypto algorithm implementation is used. Help people debug
* performance problems by logging the ->cra_driver_name the
* first time a mode is used. Note that multiple threads can
* race here, but it doesn't really matter.
* first time a mode is used.
*/
mode->logged_impl_name = true;
pr_info("fscrypt: %s using implementation \"%s\"\n",
mode->friendly_name,
crypto_skcipher_alg(tfm)->base.cra_driver_name);
Expand Down

0 comments on commit ff73c2c

Please sign in to comment.