From: Linlin Zhang <linlin.zhang@oss.qualcomm.com>
To: Benjamin Marzinski <bmarzins@redhat.com>
Cc: linux-block@vger.kernel.org, ebiggers@kernel.org,
mpatocka@redhat.com, gmazyland@gmail.com,
linux-kernel@vger.kernel.org, adrianvovk@gmail.com,
dm-devel@lists.linux.dev, quic_mdalam@quicinc.com,
israelr@nvidia.com, hch@infradead.org, axboe@kernel.dk
Subject: Re: [PATCH v2 2/3] dm-inlinecrypt: add target for inline block device encryption
Date: Tue, 28 Apr 2026 18:43:08 +0800 [thread overview]
Message-ID: <f92f318b-5c99-4880-906c-136aa180d58c@oss.qualcomm.com> (raw)
In-Reply-To: <af84cfb7-b673-404a-a7f9-6efd9d23a08f@oss.qualcomm.com>
Correct the response to Benjamin's comments.
On 4/27/2026 8:20 PM, Linlin Zhang wrote:
>
>
> On 4/27/2026 9:19 AM, Benjamin Marzinski wrote:
>> On Fri, Apr 10, 2026 at 06:40:30AM -0700, Linlin Zhang wrote:
>>> From: Eric Biggers <ebiggers@google.com>
>>> +
>>> +static int inlinecrypt_map(struct dm_target *ti, struct bio *bio)
>>> +{
>>> + const struct inlinecrypt_ctx *ctx = ti->private;
>>> + sector_t sector_in_target;
>>> + u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE] = {};
>>> +
>>> + bio_set_dev(bio, ctx->dev->bdev);
>>> +
>>> + /*
>>> + * If the bio is a device-level request which doesn't target a specific
>>> + * sector, there's nothing more to do.
>>> + */
>>> + if (bio_sectors(bio) == 0)
>>> + return DM_MAPIO_REMAPPED;
>>> +
>>> + /*
>>> + * The bio should never have an encryption context already, since
>>> + * dm-inlinecrypt doesn't pass through any inline encryption
>>> + * capabilities to the layer above it.
>>> + */
>>> + if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
>>> + return DM_MAPIO_KILL;
>>> +
>>> + /* Map the bio's sector to the underlying device. (512-byte sectors) */
>>> + sector_in_target = dm_target_offset(ti, bio->bi_iter.bi_sector);
>>> + bio->bi_iter.bi_sector = ctx->start + sector_in_target;
>>> + /*
>>> + * If the bio doesn't have any data (e.g. if it's a DISCARD request),
>>> + * there's nothing more to do.
>>> + */
>>> + if (!bio_has_data(bio))
>>> + return DM_MAPIO_REMAPPED;
>>> +
>>> + /* Calculate the DUN and enforce data-unit (crypto sector) alignment. */
>>> + dun[0] = ctx->iv_offset + sector_in_target; /* 512-byte sectors */
>>> + if (dun[0] & ((ctx->sector_size >> SECTOR_SHIFT) - 1))
>>> + return DM_MAPIO_KILL;
>>
>> If ctx->iv_offset is not a multiple of ctx->sector_size, this will
>> always fail. ctx->iv_offset should probably get validated in
>> inlinecrypt_ctr()
>
> ACK
>
> Yes, this assumes iv_offset is aligned to sector_size when large crypto
> sectors are used. That’s a requirement of dm-inlinecrypt semantics, and
> adding an explicit check in inlinecrypt_ctr() would make this fail earlier
> and more clearly.
Sorry, the last response is wrong. No need to add check in inlinecrypt_ctr().
iv_offset is the starting offset for IVs that are generated as if the target were
preceded by iv_offset 512-byte sectors.
I think this concern is based on an implicit assumption that
sector_in_target is always data-unit (crypto sector) aligned. In this
target, however, sector_in_target is derived from dm_target_offset() and
is in 512-byte sectors, so it is not guaranteed to be a multiple of
(sector_size >> SECTOR_SHIFT).
The intended alignment requirement is on the *final* DUN, i.e. on
(iv_offset + sector_in_target) in 512-byte sector units, before it gets
shifted down to crypto-sector units. That's why the code checks alignment
on the sum (iv_offset + sector_in_target).
With this definition, iv_offset itself does not need to be aligned to the
data-unit size; any non-negative value is valid as long as the resulting
DUN for a given bio is data-unit aligned. For example, iv_offset = 1 can
still be valid when sector_in_target is 7 (4096-byte sector case), since
their sum is aligned. Validating iv_offset alone in inlinecrypt_ctr()
would therefore reject configurations that are otherwise correct per the
(sum-based) DUN definition.
So I believe the runtime check in ->map() is the right place to enforce
the data-unit alignment constraint, as it has the actual bio offset
(sector_in_target) needed to evaluate the constraint.
Let me know if you'd prefer we document this more explicitly in the map()
argument description; I'm fine adding a short note about the
iv_offset units and the sum-based alignment rule.
>
>>
>> -Ben
>>
>>> + dun[0] >>= ctx->sector_bits - SECTOR_SHIFT; /* crypto sectors */
>>> +
>>> + /*
>>> + * This check isn't necessary as we should have calculated max_dun
>>> + * correctly, but be safe.
>>> + */
>>> + if (WARN_ON_ONCE(dun[0] > ctx->max_dun))
>>> + return DM_MAPIO_KILL;
>>> +
>>> + bio_crypt_set_ctx(bio, &ctx->key, dun, GFP_NOIO);
>>> +
>>> + /*
>>> + * Since we've added an encryption context to the bio and
>>> + * blk-crypto-fallback may be needed to process it, it's necessary to
>>> + * use the fallback-aware bio submission code rather than
>>> + * unconditionally returning DM_MAPIO_REMAPPED.
>>> + *
>>> + * To get the correct accounting for a dm target in the case where
>>> + * __blk_crypto_submit_bio() doesn't take ownership of the bio (returns
>>> + * true), call __blk_crypto_submit_bio() directly and return
>>> + * DM_MAPIO_REMAPPED in that case, rather than relying on
>>> + * blk_crypto_submit_bio() which calls submit_bio() in that case.
>>> + */
>>> + if (__blk_crypto_submit_bio(bio))
>>> + return DM_MAPIO_REMAPPED;
>>> + return DM_MAPIO_SUBMITTED;
>>> +}
>>> +
<snip>
>>> +MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
>>> +MODULE_AUTHOR("Linlin Zhang <linlin.zhang@oss.qualcomm.com>");
>>> +MODULE_DESCRIPTION(DM_NAME " target for inline encryption");
>>> +MODULE_LICENSE("GPL");
>>> --
>>> 2.34.1
>>>
>>
>
next prev parent reply other threads:[~2026-04-28 10:43 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-10 13:40 [PATCH v2 0/3] dm-inlinecrypt: add target for inline block device encryption Linlin Zhang
2026-04-10 13:40 ` [PATCH v2 1/3] block: export blk-crypto symbols required by dm-inlinecrypt Linlin Zhang
2026-04-10 13:40 ` [PATCH v2 2/3] dm-inlinecrypt: add target for inline block device encryption Linlin Zhang
2026-04-27 1:19 ` Benjamin Marzinski
2026-04-27 12:20 ` Linlin Zhang
2026-04-28 10:43 ` Linlin Zhang [this message]
2026-04-28 16:20 ` Benjamin Marzinski
2026-04-29 12:16 ` Linlin Zhang
2026-04-27 5:23 ` Benjamin Marzinski
2026-04-27 23:21 ` Benjamin Marzinski
2026-04-28 9:20 ` Linlin Zhang
2026-04-28 16:36 ` Benjamin Marzinski
2026-04-29 12:34 ` Linlin Zhang
2026-04-29 15:25 ` Benjamin Marzinski
2026-04-29 15:58 ` Benjamin Marzinski
2026-04-30 9:21 ` Linlin Zhang
2026-04-10 13:40 ` [PATCH v2 3/3] dm: add documentation for dm-inlinecrypt target Linlin Zhang
2026-04-10 17:07 ` Milan Broz
2026-04-24 13:53 ` Linlin Zhang
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=f92f318b-5c99-4880-906c-136aa180d58c@oss.qualcomm.com \
--to=linlin.zhang@oss.qualcomm.com \
--cc=adrianvovk@gmail.com \
--cc=axboe@kernel.dk \
--cc=bmarzins@redhat.com \
--cc=dm-devel@lists.linux.dev \
--cc=ebiggers@kernel.org \
--cc=gmazyland@gmail.com \
--cc=hch@infradead.org \
--cc=israelr@nvidia.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mpatocka@redhat.com \
--cc=quic_mdalam@quicinc.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