From patchwork Mon Apr 10 19:40:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sweet Tea Dorminy X-Patchwork-Id: 13206688 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77D99C77B61 for ; Mon, 10 Apr 2023 19:40:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229757AbjDJTkj (ORCPT ); Mon, 10 Apr 2023 15:40:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229743AbjDJTkj (ORCPT ); Mon, 10 Apr 2023 15:40:39 -0400 Received: from box.fidei.email (box.fidei.email [IPv6:2605:2700:0:2:a800:ff:feba:dc44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 615BA1717 for ; Mon, 10 Apr 2023 12:40:38 -0700 (PDT) Received: from authenticated-user (box.fidei.email [71.19.144.250]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by box.fidei.email (Postfix) with ESMTPSA id 7380E80510; Mon, 10 Apr 2023 15:40:37 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=dorminy.me; s=mail; t=1681155638; bh=T/GWz5x9N9tt4cSe2QC2OCcSFKjxdBbLNMYUMT75UCs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V2Qz/zoOs0dCWSz9HPgOgPLk20EBSyEUbREvTsSKo3zqAExSXGR8M4WdCGNUKsWBG nSheuzXJAA798on9OTKoWOEXnqP4NHprZzDic6D6Yci837gWeQvzOz/hXaRDWnRZ+M kw5GtKPYK2mkVqY/k3Ad4gEchOwqixrAZZWYjOXTFPlLaHrAreKVYuMglX5/uVDbU/ o4FYvBNcBbjzicADsNqPGV0d/DdFunJyLPjTUrtmO/5h9nmJn/Uxu99YPaCxQu7IMO qwh4h7ZoY7lNbij6V2Z6ShK6PwWc7o08FBDJhGEJPH/nQpFhrt6O4EAyfWTOzvWhgE ipPuw9RBR4Vvw== From: Sweet Tea Dorminy To: Eric Biggers , "Theodore Y. Ts'o" , Jaegeuk Kim , linux-fscrypt@vger.kernel.org, kernel-team@meta.com Cc: Sweet Tea Dorminy Subject: [PATCH v2 08/11] fscrypt: make ci->ci_direct_key a bool not a pointer Date: Mon, 10 Apr 2023 15:40:01 -0400 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org The ci_direct_key field is only used for v1 direct key policies, recording the direct key that needs to have its refcount reduced when the crypt_info is freed. However, now that crypt_info->ci_enc_key is a pointer to the authoritative prepared key -- embedded in the direct key, in this case, we no longer need to keep a full pointer to the direct key -- we can use container_of() to go from the prepared key to its surrounding direct key. Thus we can make ci_direct_key a bool instead of a pointer, saving a few bytes. Signed-off-by: Sweet Tea Dorminy --- fs/crypto/fscrypt_private.h | 7 +++---- fs/crypto/keysetup.c | 2 +- fs/crypto/keysetup_v1.c | 7 +++++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h index 5011737b60b3..b575fb58a506 100644 --- a/fs/crypto/fscrypt_private.h +++ b/fs/crypto/fscrypt_private.h @@ -234,10 +234,9 @@ struct fscrypt_info { struct list_head ci_master_key_link; /* - * If non-NULL, then encryption is done using the master key directly - * and ci_enc_key will equal ci_direct_key->dk_key. + * If true, then encryption is done using the master key directly. */ - struct fscrypt_direct_key *ci_direct_key; + bool ci_direct_key; /* * This inode's hash key for filenames. This is a 128-bit SipHash-2-4 @@ -641,7 +640,7 @@ static inline int fscrypt_require_key(struct inode *inode) /* keysetup_v1.c */ -void fscrypt_put_direct_key(struct fscrypt_direct_key *dk); +void fscrypt_put_direct_key(struct fscrypt_prepared_key *prep_key); int fscrypt_setup_v1_file_key(struct fscrypt_info *ci, const u8 *raw_master_key); diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c index 845a92203c87..d81001bf0a51 100644 --- a/fs/crypto/keysetup.c +++ b/fs/crypto/keysetup.c @@ -578,7 +578,7 @@ static void put_crypt_info(struct fscrypt_info *ci) return; if (ci->ci_direct_key) - fscrypt_put_direct_key(ci->ci_direct_key); + fscrypt_put_direct_key(ci->ci_enc_key); else if (ci->ci_owns_key) { fscrypt_destroy_prepared_key(ci->ci_inode->i_sb, ci->ci_enc_key); diff --git a/fs/crypto/keysetup_v1.c b/fs/crypto/keysetup_v1.c index e1d761e8067f..09de84c65368 100644 --- a/fs/crypto/keysetup_v1.c +++ b/fs/crypto/keysetup_v1.c @@ -160,8 +160,11 @@ static void free_direct_key(struct fscrypt_direct_key *dk) } } -void fscrypt_put_direct_key(struct fscrypt_direct_key *dk) +void fscrypt_put_direct_key(struct fscrypt_prepared_key *prep_key) { + struct fscrypt_direct_key *dk = + container_of(prep_key, struct fscrypt_direct_key, dk_key); + if (!refcount_dec_and_lock(&dk->dk_refcount, &fscrypt_direct_keys_lock)) return; hash_del(&dk->dk_node); @@ -258,7 +261,7 @@ static int setup_v1_file_key_direct(struct fscrypt_info *ci, dk = fscrypt_get_direct_key(ci, raw_master_key); if (IS_ERR(dk)) return PTR_ERR(dk); - ci->ci_direct_key = dk; + ci->ci_direct_key = true; ci->ci_enc_key = &dk->dk_key; return 0; }