From patchwork Wed Oct 9 23:43:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11182389 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F285376 for ; Wed, 9 Oct 2019 23:44:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D2D4C218DE for ; Wed, 9 Oct 2019 23:44:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570664656; bh=ttUVmrIzuf0w22zjSvNcVAQBL4zgFKEq3DaArSdKV2Y=; h=From:To:Cc:Subject:Date:List-ID:From; b=VPG7tXny3HCmc1UVusNti1ouSVtWju15Jy25dUCjkNTyoB3i5xKkx/Gv/bGxo0EUz sSqcfdyPXEHNtXFW+w3v5l12biErEDZa+vn7678I+2989YVyB2m0QrGyQoET7M9f6U 3AwVYgaIl5CbIxRfhVzXGD+LX8FEWXrtKvpKLHSk= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732353AbfJIXoO (ORCPT ); Wed, 9 Oct 2019 19:44:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:42190 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730815AbfJIXoO (ORCPT ); Wed, 9 Oct 2019 19:44:14 -0400 Received: from ebiggers-linuxstation.mtv.corp.google.com (unknown [104.132.1.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 653C120659; Wed, 9 Oct 2019 23:44:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570664653; bh=ttUVmrIzuf0w22zjSvNcVAQBL4zgFKEq3DaArSdKV2Y=; h=From:To:Cc:Subject:Date:From; b=v/OYP3CyOlLnK2h3sAFmFUGU3iqw5O5dngP0EVdfJjPUZ/ArystkmFd08w34mNqSz pjiomgybOGYsjelIiBb3dYzL6k9NlbeQIHJ7JfQdKraMPjd1qngnEmM3MMI0nz26Mc 4YdJTHcNq5ANvLpldLMsgV9LJAN6GWGIfFbZnkLc= From: Eric Biggers To: linux-fscrypt@vger.kernel.org Cc: "Theodore Y . Ts'o" , Jaegeuk Kim , linux-kernel@vger.kernel.org Subject: [PATCH] fscrypt: avoid data race on fscrypt_mode::logged_impl_name Date: Wed, 9 Oct 2019 16:43:37 -0700 Message-Id: <20191009234337.225469-1-ebiggers@kernel.org> X-Mailer: git-send-email 2.23.0.581.g78d2f28ef7-goog MIME-Version: 1.0 Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org From: Eric Biggers 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. Signed-off-by: Eric Biggers --- fs/crypto/keysetup.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c index 8eb5a0e762ec6..df3e1c8653884 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, true)) { /* * 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);