From: Carlos Llamas cmllamas@google.com
[ Upstream commit 0b39ca457241aeca07a613002512573e8804f93a ]
Make __blk_crypto_bio_prep() propagate BLK_STS_INVAL when IO segments fail the data unit alignment check.
This was flagged by an LTP test that expects EINVAL when performing an O_DIRECT read with a misaligned buffer [1].
Cc: Eric Biggers ebiggers@kernel.org Cc: Christoph Hellwig hch@lst.de Link: https://lore.kernel.org/all/aP-c5gPjrpsn0vJA@google.com/ [1] Signed-off-by: Carlos Llamas cmllamas@google.com Reviewed-by: Eric Biggers ebiggers@kernel.org Signed-off-by: Jens Axboe axboe@kernel.dk Signed-off-by: Sasha Levin sashal@kernel.org ---
LLM Generated explanations, may be completely bogus:
## Backport Analysis Result
**YES** - This commit should be backported to stable kernel trees.
---
## Detailed Analysis
### 1. Semantic Code Analysis Using MCP Tools
#### Function Location and Impact Scope Using `mcp__semcode__find_function`, I located the affected function `__blk_crypto_bio_prep` at **block/blk-crypto.c:283-311**. This function prepares bios for inline encryption.
#### Call Chain Analysis - User-Space Exposure Using `mcp__semcode__find_callers` and `mcp__semcode__find_callchain`, I traced the complete call path:
``` User-space (O_DIRECT I/O) → submit_bio() → submit_bio_noacct() → __submit_bio() → blk_crypto_bio_prep() → __blk_crypto_bio_prep() ← Changed function ```
**Finding**: This function is **directly exposed to user-space** through the standard I/O submission path. Users performing O_DIRECT operations with encrypted block devices will trigger this code.
#### Error Code Semantic Analysis I examined **block/blk-core.c:163-166** to understand error code mappings:
```c [BLK_STS_INVAL] = { -EINVAL, "invalid" }, // For invalid arguments [BLK_STS_IOERR] = { -EIO, "I/O" }, // For I/O errors ```
**Finding**: - **BLK_STS_IOERR** → `-EIO` (I/O error - device/hardware issue) - **BLK_STS_INVAL** → `-EINVAL` (Invalid argument - user error)
The `bio_crypt_check_alignment()` function validates user-provided data alignment against encryption requirements. Alignment violations are **user errors**, not I/O errors.
#### Consistency Check I verified other validation errors in the block layer: - `blk_validate_atomic_write_op_size()` (block/blk-core.c:764-768): Returns `BLK_STS_INVAL` for size validation failures - DMA alignment check (block/blk-mq-dma.c:181): Returns `BLK_STS_INVAL` for alignment failures
**Finding**: Using `BLK_STS_INVAL` for alignment/validation errors is the **established pattern** in the block layer.
### 2. Bug Impact Assessment
#### What Does This Fix? The commit fixes **incorrect error code propagation** when I/O segments fail the data unit alignment check in encrypted block operations.
**Before**: Returns `-EIO` (suggesting hardware/device problem) **After**: Returns `-EINVAL` (correctly indicating user's misaligned buffer)
#### Real-World Impact 1. **LTP Test Failure**: The commit message explicitly states this was "flagged by an LTP test that expects EINVAL when performing an O_DIRECT read with a misaligned buffer" 2. **User Confusion**: Applications receiving `-EIO` might retry or report hardware errors, when the actual problem is a programming error (misaligned buffer) 3. **POSIX Compliance**: EINVAL is the semantically correct error for invalid user arguments
### 3. Change Characteristics
#### Scope Analysis - **Files changed**: 1 file (block/blk-crypto.c) - **Lines changed**: 1 line (single error code constant) - **Behavioral changes**: None - only error code reporting - **Dependencies**: None - uses existing BLK_STS_INVAL constant - **Architectural impact**: None
#### Risk Assessment - **Risk level**: **VERY LOW** - **Regression potential**: Minimal - only changes which errno is returned - **Side effects**: Applications expecting EIO might need adjustment, but EINVAL is the correct semantic value - **Testing**: Already validated by LTP tests
### 4. Backport Suitability Evaluation
#### Stable Tree Compliance ✅ **Bug fix**: YES - fixes wrong error code ✅ **Small and contained**: YES - 1 line change ✅ **User-facing impact**: YES - fixes observable behavior ✅ **No new features**: Correct ✅ **No architectural changes**: Correct ✅ **Minimal dependencies**: Only requires blk-crypto support (kernel 5.7+)
#### Backport Indicators - **Review status**: Reviewed-by: Eric Biggers (blk-crypto maintainer) - **Signed-off**: Jens Axboe (block layer maintainer) - **Test coverage**: LTP test validates the fix - **Already backported**: This commit has already been applied to the 6.17 stable tree as c4802041cc9f6
### 5. Conclusion
This is an **ideal backport candidate**:
1. **Fixes a real bug**: Wrong error code breaks POSIX semantics and LTP tests 2. **User-visible**: Directly affects applications using O_DIRECT on encrypted devices 3. **Minimal risk**: One-line change with no functional logic modifications 4. **Well-tested**: Validated by LTP test suite 5. **Maintainer-approved**: Reviewed by subsystem experts 6. **Consistent with kernel patterns**: Aligns with how other validation errors are reported
The fix should be backported to all stable kernel trees that include blk-crypto support (5.7+) to ensure correct error reporting for user- space applications.
block/blk-crypto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/block/blk-crypto.c b/block/blk-crypto.c index 4b1ad84d1b5ab..3e7bf1974cbd8 100644 --- a/block/blk-crypto.c +++ b/block/blk-crypto.c @@ -292,7 +292,7 @@ bool __blk_crypto_bio_prep(struct bio **bio_ptr) }
if (!bio_crypt_check_alignment(bio)) { - bio->bi_status = BLK_STS_IOERR; + bio->bi_status = BLK_STS_INVAL; goto fail; }