All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "周伟江 (Weijiang Zhou)" <weijiang.zhou@unisoc.com>,
	"Eric Biggers" <ebiggers@kernel.org>,
	"Mikulas Patocka" <mpatocka@redhat.com>
Cc: "dm-devel@lists.linux.dev" <dm-devel@lists.linux.dev>,
	"samitolvanen@google.com" <samitolvanen@google.com>,
	"agk@redhat.com" <agk@redhat.com>,
	"snitzer@kernel.org" <snitzer@kernel.org>,
	"bmarzins@redhat.com" <bmarzins@redhat.com>,
	"金红宇 (Hongyu Jin)" <hongyu.jin@unisoc.com>,
	"吴祥禄 (Xianglu Wu)" <Xianglu.Wu@unisoc.com>,
	"吴周镜 (Justin Wu)" <Justin.Wu@unisoc.com>,
	"耿慈熙 (Cixi Geng/10039)" <Cixi.Geng1@unisoc.com>
Subject: Re: [RFC PATCH v3 2/3] dm-verity: prevent FEC from publishing uninitialized hash blocks
Date: Fri, 04 Dec 2026 05:36:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-249153@kernel.org> (raw)
In-Reply-To: <d7dbb5632f5a50e0b2f7c4cd70fa0857@zeshmbx08.spreadtrum.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] dm-verity: pinned bad buffer across verity_fec_decode()
  blocks replacement
- [Medium] dm-verity: FEC cache publish stalls on B_READING and evicts
  verified buffers
- [Low] dm-bufio: dm_bufio_new_filled() doc falsely promises
  unconditional release
- [Low] dm-verity: duplicated FEC cache publication logic in
  verity_verify_level()
--

commit f5fba3aa170f055431dd1c64efc15af12f0b569d
Author: 周伟江 (Weijiang Zhou) <weijiang.zhou@unisoc.com>

dm-verity: prevent FEC from publishing uninitialized hash blocks

This commit changes verity_verify_level() to decode corrupted or
unreadable metadata hash blocks into the per-I/O private FEC output
buffer and publish the corrected block into dm-bufio via
dm_bufio_new_filled(), rather than decoding into a cached buffer in
place.

> diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
> index 1906e487b20a5..12b5ff1b99ca8 100644
> --- a/drivers/md/dm-verity-target.c
> +++ b/drivers/md/dm-verity-target.c
> @@ -293,19 +293,42 @@ static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,

[ ... ]

> +		u8 *output = verity_fec_output(io);
> +
> +		dm_bufio_forget(v->bufio, hash_block);
> +		data = dm_bufio_new_filled(v->bufio, hash_block,
> +					   output, &buf);
> +		if (IS_ERR(data)) {
> +			data = output;
> +		} else if (memcmp(data, output,
> +				  1 << v->hash_dev_block_bits)) {
> +			dm_bufio_release(buf);
> +			buf = NULL;
> +			data = output;
> +		} else {
>  			aux = dm_bufio_get_aux_data(buf);
>  			aux->hash_verified = 1;
> -			goto release_ok;
> -		} else {
> -			dm_bufio_release(buf);
> -			dm_bufio_forget(v->bufio, hash_block);
> -			return r;
>  		}
> +		goto release_ok;

[Severity: Medium]
Can dm_bufio_forget() followed by dm_bufio_new_filled() stall an
already-recovered worker on a concurrent failing read and leave the
cache empty?

When a read fails, new_read() removes the failed buffer on release.
While one worker runs verity_fec_decode(), another reader can start a
new failing read for the same block:

CPU0                                CPU1
dm_bufio_read_with_ioprio() -> err
verity_fec_decode() ...
                                    dm_bufio_read_with_ioprio()
                                      inserts buf with B_READING
dm_bufio_forget()  <-- no-op (B_READING set)
dm_bufio_new_filled()
  wait_on_bit_io(B_READING)  <-- stalls on doomed read
  returns ERR_PTR(error)

forget_buffer() skips eviction when B_READING is set, so
dm_bufio_new_filled() -> new_read() blocks in TASK_UNINTERRUPTIBLE until
the doomed bad-sector read fails and then returns ERR_PTR(error).  The
IS_ERR(data) fallback skips retrying publication, leaving the cache
empty so later readers submit further failing reads and redundant FEC
decodes.

In addition, in both recovery branches, if one worker publishes and
releases a verified buffer (b->state == 0, hold_count == 0), a second
worker finishing FEC calls dm_bufio_forget() unconditionally and evicts
that already-verified buffer, reopening a cache-miss window for fresh
failing reads.

[ ... ]

> @@ -331,10 +354,38 @@ static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
>  			 */
>  			r = -EAGAIN;
>  			goto release_ret_r;
> -		} else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_METADATA,
> -					     want_digest, hash_block, data) == 0)
> -			aux->hash_verified = 1;
> -		else if (verity_handle_err(v,
> +		} else if (verity_fec_decode(v, io,
> +					     DM_VERITY_BLOCK_TYPE_METADATA,
> +					     want_digest, hash_block, NULL) == 0) {
> +			u8 *output = verity_fec_output(io);

[ ... ]

> +			dm_bufio_release(buf);

[Severity: Medium]
Does holding buf across verity_fec_decode() prevent the corrupted buffer
from being replaced under concurrent reads?

fec_read_bufs() synchronously reads up to v->fec->rs_k blocks from
storage before verity_fec_decode() returns, stretching the buffer hold
window to tens or hundreds of milliseconds.  Concurrent workers on
v->verify_wq hitting the same corrupted hash block also pin buf across
their own decodes, so when the first worker finishes, dm_bufio_release()
leaves hold_count >= 1, dm_bufio_forget() -> cache_remove() refuses to
evict it, and dm_bufio_new_filled() returns the same corrupted buffer.

As long as reads touching that hash block arrive faster than a single
FEC decode, hold_count never drops to zero, aux->hash_verified stays
unset, and every arriving read triggers another full FEC decode storm.
Releasing buf before calling verity_fec_decode() (saving the digest at
data + offset if needed for DM_VERITY_MODE_LOGGING) narrows the hold
window back to verity_hash().

> +			dm_bufio_forget(v->bufio, hash_block);
> +			data = dm_bufio_new_filled(v->bufio, hash_block,
> +						   output, &buf);
> +			if (IS_ERR(data)) {
> +				data = output;
> +			} else if (memcmp(data, output,
> +					  1 << v->hash_dev_block_bits)) {
> +				dm_bufio_release(buf);
> +				buf = NULL;
> +				data = output;
> +			} else {
> +				aux = dm_bufio_get_aux_data(buf);
> +				aux->hash_verified = 1;
> +			}
> +			goto release_ok;

[Severity: Low]
This isn't a bug, but the dm_bufio_forget() / dm_bufio_new_filled()
publication and fallback sequence is duplicated verbatim between the
I/O error and hash-mismatch branches, and the first branch declares
u8 *output mid-block after executable statements.  Extracting a small
helper would remove both the duplication and the mid-block declaration.

[ ... ]

> @@ -351,7 +402,8 @@ static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
>  	r = 0;
>  
>  release_ret_r:
> -	dm_bufio_release(buf);
> +	if (buf)
> +		dm_bufio_release(buf);
>  	return r;
>  }

[Severity: Low]
Does the comment on dm_bufio_new_filled() added in commit "dm-bufio:
support atomic publication of pre-filled buffers" match
dm_bufio_release()?

That comment states that *bp is set to NULL on failure "so that callers
can release unconditionally", but dm_bufio_release() immediately
dereferences b->c without a NULL check and will NULL pointer
dereference if a caller follows the comment literally.  Since callers
must guard cleanup with if (buf) as done here, the comment should reflect
that requirement.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/f55322e3a71277d927476ddb372064e0@zeshmbx08.spreadtrum.com?part=2

  reply	other threads:[~2026-12-04  5:36 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10 13:06 [RFC PATCH 0/2] dm-verity: don't let FEC decode into a dm_bufio page that is already in the cache 周伟江 (Weijiang Zhou)
2026-11-10 13:06 ` [RFC PATCH 1/2] dm-verity: don't decode FEC " 周伟江 (Weijiang Zhou)
2026-11-10 21:08   ` Mikulas Patocka
2026-11-11  3:14     ` 周伟江 (Weijiang Zhou)
2026-11-11 23:33       ` Mikulas Patocka
2026-11-12  0:38     ` Sami Tolvanen
2026-11-13  5:36     ` Eric Biggers
2026-11-19 13:22       ` 周伟江 (Weijiang Zhou)
2026-12-02 13:05         ` 周伟江 (Weijiang Zhou)
2026-12-02 19:04           ` Eric Biggers
2026-12-03  8:51             ` [RFC PATCH v2 0/4] dm-verity: fix FEC turning I/O errors into corruption reports 周伟江 (Weijiang Zhou)
2026-12-03  8:51               ` [RFC PATCH v2 1/4] dm-verity: don't restart or panic on errors during system shutdown 周伟江 (Weijiang Zhou)
2026-12-03  8:51               ` [RFC PATCH v2 2/4] dm-bufio: support atomic publication of pre-filled buffers 周伟江 (Weijiang Zhou)
2026-12-03  8:51               ` [RFC PATCH v2 3/4] dm-verity: prevent FEC from publishing uninitialized hash blocks 周伟江 (Weijiang Zhou)
2026-12-03  8:51               ` [RFC PATCH v2 4/4] dm-verity: avoid corruption policy during FEC erasure lookup 周伟江 (Weijiang Zhou)
2026-12-03 11:07               ` [RFC PATCH v2 0/4] dm-verity: fix FEC turning I/O errors into corruption reports 周伟江 (Weijiang Zhou)
2026-12-03 18:41                 ` Eric Biggers
2026-12-04  5:19                   ` [RFC PATCH v3 0/3] dm-verity: fix FEC recovery of hash blocks and spurious corruption policy 周伟江 (Weijiang Zhou)
2026-12-04  5:19                     ` [RFC PATCH v3 1/3] dm-bufio: support atomic publication of pre-filled buffers 周伟江 (Weijiang Zhou)
2026-12-04  5:34                       ` sashiko-bot
2026-12-04  5:19                     ` [RFC PATCH v3 2/3] dm-verity: prevent FEC from publishing uninitialized hash blocks 周伟江 (Weijiang Zhou)
2026-12-04  5:36                       ` sashiko-bot [this message]
2026-12-04  5:19                     ` [RFC PATCH v3 3/3] dm-verity: avoid corruption policy during FEC erasure lookup 周伟江 (Weijiang Zhou)
2026-12-04  5:33                       ` sashiko-bot
2026-11-10 13:06 ` [RFC PATCH 2/2] dm-verity: don't restart or panic on errors during system shutdown 周伟江 (Weijiang Zhou)
2026-11-10 21:13   ` Mikulas Patocka
2026-11-11  3:14     ` 周伟江 (Weijiang Zhou)
2026-11-11 22:48       ` Mikulas Patocka
2026-11-13  5:55   ` Eric Biggers
2026-11-13  7:40     ` 回复: " 周伟江 (Weijiang Zhou)

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=sashiko-outbox-249153@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Cixi.Geng1@unisoc.com \
    --cc=Justin.Wu@unisoc.com \
    --cc=Xianglu.Wu@unisoc.com \
    --cc=agk@redhat.com \
    --cc=bmarzins@redhat.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=ebiggers@kernel.org \
    --cc=hongyu.jin@unisoc.com \
    --cc=mpatocka@redhat.com \
    --cc=samitolvanen@google.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=snitzer@kernel.org \
    --cc=weijiang.zhou@unisoc.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.