From: Eric Biggers <ebiggers@kernel.org>
To: Satya Tangirala <satyat@google.com>
Cc: linux-block@vger.kernel.org, linux-scsi@vger.kernel.org,
linux-fscrypt@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net,
Parshuram Raju Thombare <pthombar@cadence.com>,
Ladvine D Almeida <ladvine.dalmeida@synopsys.com>,
Barani Muthukumaran <bmuthuku@qti.qualcomm.com>,
Kuohong Wang <kuohong.wang@mediatek.com>
Subject: Re: [RFC PATCH v2 5/8] scsi: ufs: UFS crypto API
Date: Thu, 13 Jun 2019 10:11:13 -0700 [thread overview]
Message-ID: <20190613171113.GB686@sol.localdomain> (raw)
In-Reply-To: <20190605232837.31545-6-satyat@google.com>
Hi Satya,
On Wed, Jun 05, 2019 at 04:28:34PM -0700, Satya Tangirala wrote:
> Introduce functions to manipulate UFS inline encryption hardware
> in line with the JEDEC UFSHCI v2.1 specification and to work with the
> block keyslot manager.
>
> Signed-off-by: Satya Tangirala <satyat@google.com>
> ---
> drivers/scsi/ufs/Kconfig | 10 +
> drivers/scsi/ufs/Makefile | 1 +
> drivers/scsi/ufs/ufshcd-crypto.c | 438 +++++++++++++++++++++++++++++++
> drivers/scsi/ufs/ufshcd-crypto.h | 69 +++++
> 4 files changed, 518 insertions(+)
> create mode 100644 drivers/scsi/ufs/ufshcd-crypto.c
> create mode 100644 drivers/scsi/ufs/ufshcd-crypto.h
>
There is a build error after this patch because it adds code that uses the
crypto fields in struct ufs_hba, but those aren't added until the next patch.
It needs to be possible to compile a working kernel after each patch.
Otherwise it breaks bisection.
So, perhaps add the fields in this patch instead.
> +++ b/drivers/scsi/ufs/ufshcd-crypto.c
> @@ -0,0 +1,438 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright 2019 Google LLC
> + */
> +
> +#include <crypto/algapi.h>
> +
> +#include "ufshcd.h"
> +#include "ufshcd-crypto.h"
> +
> +bool ufshcd_hba_is_crypto_supported(struct ufs_hba *hba)
> +{
> + return hba->crypto_capabilities.reg_val != 0;
> +}
> +
> +bool ufshcd_is_crypto_enabled(struct ufs_hba *hba)
> +{
> + return hba->caps & UFSHCD_CAP_CRYPTO;
> +}
> +
> +static bool ufshcd_cap_idx_valid(struct ufs_hba *hba, unsigned int cap_idx)
> +{
> + return cap_idx < hba->crypto_capabilities.num_crypto_cap;
> +}
> +
> +#define NUM_KEYSLOTS(hba) (hba->crypto_capabilities.config_count + 1)
> +
> +bool ufshcd_keyslot_valid(struct ufs_hba *hba, unsigned int slot)
> +{
> + /*
> + * The actual number of configurations supported is (CFGC+1), so slot
> + * numbers range from 0 to config_count inclusive.
> + */
> + return slot < NUM_KEYSLOTS(hba);
> +}
Since ufshcd_hba_is_crypto_supported(), ufshcd_is_crypto_enabled(), and
ufshcd_keyslot_valid() are one-liners, don't access any private structures, and
are used outside this file including on the command submission path, how about
making them inline functions in ufshcd-crypto.h?
> +
> +static int ufshcd_crypto_alg_find(void *hba_p,
> + enum blk_crypt_mode_num crypt_mode,
> + unsigned int data_unit_size)
> +{
Now that the concept of "crypto alg IDs" is gone, rename this to
ufshcd_crypto_cap_find() and rename the crypto_alg_id variables to cap_idx.
This would make it consistent with using cap_idx elsewhere in the code and avoid
confusion with ufs_crypto_cap_entry::algorithm_id.
> +
> +static int ufshcd_crypto_keyslot_program(void *hba_p, const u8 *key,
> + enum blk_crypt_mode_num crypt_mode,
> + unsigned int data_unit_size,
> + unsigned int slot)
> +{
> + struct ufs_hba *hba = hba_p;
> + int err = 0;
> + u8 data_unit_mask;
> + union ufs_crypto_cfg_entry cfg;
> + union ufs_crypto_cfg_entry *cfg_arr = hba->crypto_cfgs;
> + int crypto_alg_id;
> +
> + crypto_alg_id = ufshcd_crypto_alg_find(hba_p, crypt_mode,
> + data_unit_size);
> +
> + if (!ufshcd_is_crypto_enabled(hba) ||
> + !ufshcd_keyslot_valid(hba, slot) ||
> + !ufshcd_cap_idx_valid(hba, crypto_alg_id))
> + return -EINVAL;
> +
> + data_unit_mask = get_data_unit_size_mask(data_unit_size);
> +
> + if (!(data_unit_mask &
> + hba->crypto_cap_array[crypto_alg_id].sdus_mask))
> + return -EINVAL;
Nit: the 'if' expression with data_unit_mask fits on one line.
> +static int ufshcd_crypto_keyslot_find(void *hba_p,
> + const u8 *key,
> + enum blk_crypt_mode_num crypt_mode,
> + unsigned int data_unit_size)
> +{
> + struct ufs_hba *hba = hba_p;
> + int err = 0;
> + int slot;
> + u8 data_unit_mask;
> + union ufs_crypto_cfg_entry cfg;
> + union ufs_crypto_cfg_entry *cfg_arr = hba->crypto_cfgs;
> + int crypto_alg_id;
> +
> + crypto_alg_id = ufshcd_crypto_alg_find(hba_p, crypt_mode,
> + data_unit_size);
> +
> + if (!ufshcd_is_crypto_enabled(hba) ||
> + !ufshcd_cap_idx_valid(hba, crypto_alg_id))
> + return -EINVAL;
> +
> + data_unit_mask = get_data_unit_size_mask(data_unit_size);
> +
> + if (!(data_unit_mask &
> + hba->crypto_cap_array[crypto_alg_id].sdus_mask))
> + return -EINVAL;
Same here.
> + for (slot = 0; slot < NUM_KEYSLOTS(hba); slot++) {
> + if ((cfg_arr[slot].config_enable &
> + UFS_CRYPTO_CONFIGURATION_ENABLE) &&
> + data_unit_mask == cfg_arr[slot].data_unit_size &&
> + crypto_alg_id == cfg_arr[slot].crypto_cap_idx &&
> + crypto_memneq(&cfg.crypto_key, cfg_arr[slot].crypto_key,
> + UFS_CRYPTO_KEY_MAX_SIZE) == 0) {
> + memzero_explicit(&cfg, sizeof(cfg));
> + return slot;
> + }
> + }
Nit: as I've mentioned before, I think !crypto_memneq() is easier to read than
'crypto_memneq() == 0'.
> + hba->crypto_cap_array =
> + devm_kcalloc(hba->dev,
> + hba->crypto_capabilities.num_crypto_cap,
> + sizeof(hba->crypto_cap_array[0]),
> + GFP_KERNEL);
> + if (!hba->crypto_cap_array) {
> + err = -ENOMEM;
> + goto out;
> + }
> +
> + hba->crypto_cfgs =
> + devm_kcalloc(hba->dev,
> + hba->crypto_capabilities.config_count + 1,
> + sizeof(union ufs_crypto_cfg_entry),
> + GFP_KERNEL);
> + if (!hba->crypto_cfgs) {
> + err = -ENOMEM;
> + goto out_cfg_mem;
> + }
Nit: use 'sizeof(hba->crypto_cfgs[0])' rather than 'sizeof(union
ufs_crypto_cfg_entry)', for consistency with the other array allocation.
Thanks,
- Eric
next prev parent reply other threads:[~2019-06-13 17:11 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-05 23:28 [RFC PATCH v2 0/8] Inline Encryption Support Satya Tangirala
2019-06-05 23:28 ` [RFC PATCH v2 1/8] block: Keyslot Manager for Inline Encryption Satya Tangirala
2019-06-07 22:28 ` Eric Biggers
2019-06-12 18:26 ` Eric Biggers
2019-06-05 23:28 ` [RFC PATCH v2 2/8] block: Add encryption context to struct bio Satya Tangirala
2019-06-12 18:10 ` Eric Biggers
2019-06-05 23:28 ` [RFC PATCH v2 3/8] block: blk-crypto for Inline Encryption Satya Tangirala
2019-06-12 23:34 ` Eric Biggers
2019-06-05 23:28 ` [RFC PATCH v2 4/8] scsi: ufs: UFS driver v2.1 spec crypto additions Satya Tangirala
2019-06-05 23:28 ` [RFC PATCH v2 5/8] scsi: ufs: UFS crypto API Satya Tangirala
2019-06-13 17:11 ` Eric Biggers [this message]
2019-06-05 23:28 ` [RFC PATCH v2 6/8] scsi: ufs: Add inline encryption support to UFS Satya Tangirala
2019-06-13 17:22 ` Eric Biggers
2019-06-05 23:28 ` [RFC PATCH v2 7/8] fscrypt: wire up fscrypt to use blk-crypto Satya Tangirala
2019-06-13 18:55 ` Eric Biggers
2019-06-05 23:28 ` [RFC PATCH v2 8/8] f2fs: Wire up f2fs to use inline encryption via fscrypt Satya Tangirala
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190613171113.GB686@sol.localdomain \
--to=ebiggers@kernel.org \
--cc=bmuthuku@qti.qualcomm.com \
--cc=kuohong.wang@mediatek.com \
--cc=ladvine.dalmeida@synopsys.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-fscrypt@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=pthombar@cadence.com \
--cc=satyat@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).