($INBOX_DIR/description missing)
 help / color / Atom feed
* [PATCH v2] fscrypt: avoid data race on fscrypt_mode::logged_impl_name
@ 2019-10-21 20:49 Eric Biggers
  2019-11-06 21:02 ` Eric Biggers
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Biggers @ 2019-10-21 20:49 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: Theodore Y . Ts'o, Jaegeuk Kim, linux-kernel

From: Eric Biggers <ebiggers@google.com>

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>
---
 fs/crypto/fscrypt_private.h | 2 +-
 fs/crypto/keysetup.c        | 6 ++----
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
index dacf8fcbac3be..d9a3e8614049f 100644
--- a/fs/crypto/fscrypt_private.h
+++ b/fs/crypto/fscrypt_private.h
@@ -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
diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
index b03b33643e4b2..28bc2da9be3c7 100644
--- a/fs/crypto/keysetup.c
+++ b/fs/crypto/keysetup.c
@@ -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);
-- 
2.23.0.866.gb869b98d4c-goog

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] fscrypt: avoid data race on fscrypt_mode::logged_impl_name
  2019-10-21 20:49 [PATCH v2] fscrypt: avoid data race on fscrypt_mode::logged_impl_name Eric Biggers
@ 2019-11-06 21:02 ` Eric Biggers
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Biggers @ 2019-11-06 21:02 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: Theodore Y . Ts'o, Jaegeuk Kim, linux-kernel

On Mon, Oct 21, 2019 at 01:49:03PM -0700, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> 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>

Applied to fscrypt.git#master for 5.5.

- Eric

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-21 20:49 [PATCH v2] fscrypt: avoid data race on fscrypt_mode::logged_impl_name Eric Biggers
2019-11-06 21:02 ` Eric Biggers

($INBOX_DIR/description missing)

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-fscrypt/0 linux-fscrypt/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-fscrypt linux-fscrypt/ https://lore.kernel.org/linux-fscrypt \
		linux-fscrypt@vger.kernel.org
	public-inbox-index linux-fscrypt

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-fscrypt


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git