Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1854158pxb; Mon, 8 Mar 2021 07:57:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJyhSTP977PIUfneMIw//74JIUHqv3eIuwIEZlFTgp5nIFxr+ysK2EczoFg6zlOBQyxmWv4A X-Received: by 2002:a05:6402:5211:: with SMTP id s17mr23074561edd.327.1615219069459; Mon, 08 Mar 2021 07:57:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615219069; cv=none; d=google.com; s=arc-20160816; b=DIDtou+1VhFpBWKJ+rpEDeMBW/w511EiZCgexpUi2iszeEtiBjJNYAPasK/azE3XzI FYeLY4my1kr6MhXECQXuLNVhV9bKOCBMs3IfhSpU2e2qye968PE68QmUysfIgyAMGwBj +zxAX7A4KeA+S/ASoDJOtGxk1/wxeALfjoOPDFe9HvYmYfJL9UJc+D+Ol264Uiug2Sy9 4lF7r0K6i4c4P1ixha9FfKP3NZGzL27UrGBUn77cYCwRrdB1yH2twADF0ZRceZqQVSxQ wYvfnqCmoiR9YF2cHAepXc3nZEs0BIIip72wmsqtTldFoT7KXt0lg6K/jDp72Gjeoa4H 5+Hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=PbsgHwcNOd0sY9z3RXMztO+VPtbjNmN5TpXYwhuIc5M=; b=ksJJ8pPgN2JOzXcx38Y2bqdA+fCsavXwJoASs/2JwIoY7LXhL1U24iv+ft87kHZrPg A4534nptIfs0oEcPJcz2fv84pVhlogsLI1v26DIdgRlsiNYcJ2KLd3vomR5ZSZwl8hKS 2Mfm9fzgqmeG+BcfkrK/+jU5MwpSX2G0UFso5S0PO3J3gJsWocAFrdu6aTCFYrbxcoFH /S6MeYrW9TYfThbdOjXXpOahNf9laa5CukisZRsqBf3+JQ5L79N9B5IIDrvH4/z1KaR9 0a4dTD7LQNuEwj2h80C4gySF7XfxI0nGUc/yuaxflrODCJ1ARRbr3R4V+VWwf9SQCybf zVMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RTbE+zvO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w14si7650675ede.222.2021.03.08.07.57.26; Mon, 08 Mar 2021 07:57:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RTbE+zvO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230320AbhCHMbf (ORCPT + 99 others); Mon, 8 Mar 2021 07:31:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:40422 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbhCHMbC (ORCPT ); Mon, 8 Mar 2021 07:31:02 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 52D0764EBC; Mon, 8 Mar 2021 12:31:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615206662; bh=+RgNA1ZBYhKK+ZrC08MdTJPnKPajaLov2A12pLs/go0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RTbE+zvOesop1z4NyG3C4VWYhcZEP/vtBjn2KAj1pE69HSdGbeXT9l+527Bq8Rkp6 uZWFmsspPp1a276kl9kXNU4I8tPfO9tPY/O1wGMRh9y0FMi79T39BE0ZR+apl5QH8K 5o29A1Alky2WQkDNFfYXvOqkUJat4IAP9ULo39rs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Milan Broz , =?UTF-8?q?J=C3=A9r=C3=B4me=20Carretero?= , Sami Tolvanen , Mike Snitzer Subject: [PATCH 5.4 11/22] dm verity: fix FEC for RS roots unaligned to block size Date: Mon, 8 Mar 2021 13:30:28 +0100 Message-Id: <20210308122714.944261556@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210308122714.391917404@linuxfoundation.org> References: <20210308122714.391917404@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Milan Broz commit df7b59ba9245c4a3115ebaa905e3e5719a3810da upstream. Optional Forward Error Correction (FEC) code in dm-verity uses Reed-Solomon code and should support roots from 2 to 24. The error correction parity bytes (of roots lengths per RS block) are stored on a separate device in sequence without any padding. Currently, to access FEC device, the dm-verity-fec code uses dm-bufio client with block size set to verity data block (usually 4096 or 512 bytes). Because this block size is not divisible by some (most!) of the roots supported lengths, data repair cannot work for partially stored parity bytes. This fix changes FEC device dm-bufio block size to "roots << SECTOR_SHIFT" where we can be sure that the full parity data is always available. (There cannot be partial FEC blocks because parity must cover whole sectors.) Because the optional FEC starting offset could be unaligned to this new block size, we have to use dm_bufio_set_sector_offset() to configure it. The problem is easily reproduced using veritysetup, e.g. for roots=13: # create verity device with RS FEC dd if=/dev/urandom of=data.img bs=4096 count=8 status=none veritysetup format data.img hash.img --fec-device=fec.img --fec-roots=13 | awk '/^Root hash/{ print $3 }' >roothash # create an erasure that should be always repairable with this roots setting dd if=/dev/zero of=data.img conv=notrunc bs=1 count=8 seek=4088 status=none # try to read it through dm-verity veritysetup open data.img test hash.img --fec-device=fec.img --fec-roots=13 $(cat roothash) dd if=/dev/mapper/test of=/dev/null bs=4096 status=noxfer # wait for possible recursive recovery in kernel udevadm settle veritysetup close test With this fix, errors are properly repaired. device-mapper: verity-fec: 7:1: FEC 0: corrected 8 errors ... Without it, FEC code usually ends on unrecoverable failure in RS decoder: device-mapper: verity-fec: 7:1: FEC 0: failed to correct: -74 ... This problem is present in all kernels since the FEC code's introduction (kernel 4.5). It is thought that this problem is not visible in Android ecosystem because it always uses a default RS roots=2. Depends-on: a14e5ec66a7a ("dm bufio: subtract the number of initial sectors in dm_bufio_get_device_size") Signed-off-by: Milan Broz Tested-by: Jérôme Carretero Reviewed-by: Sami Tolvanen Cc: stable@vger.kernel.org # 4.5+ Signed-off-by: Mike Snitzer Signed-off-by: Greg Kroah-Hartman --- drivers/md/dm-verity-fec.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) --- a/drivers/md/dm-verity-fec.c +++ b/drivers/md/dm-verity-fec.c @@ -61,19 +61,18 @@ static int fec_decode_rs8(struct dm_veri static u8 *fec_read_parity(struct dm_verity *v, u64 rsb, int index, unsigned *offset, struct dm_buffer **buf) { - u64 position, block; + u64 position, block, rem; u8 *res; position = (index + rsb) * v->fec->roots; - block = position >> v->data_dev_block_bits; - *offset = (unsigned)(position - (block << v->data_dev_block_bits)); + block = div64_u64_rem(position, v->fec->roots << SECTOR_SHIFT, &rem); + *offset = (unsigned)rem; - res = dm_bufio_read(v->fec->bufio, v->fec->start + block, buf); + res = dm_bufio_read(v->fec->bufio, block, buf); if (IS_ERR(res)) { DMERR("%s: FEC %llu: parity read failed (block %llu): %ld", v->data_dev->name, (unsigned long long)rsb, - (unsigned long long)(v->fec->start + block), - PTR_ERR(res)); + (unsigned long long)block, PTR_ERR(res)); *buf = NULL; } @@ -155,7 +154,7 @@ static int fec_decode_bufs(struct dm_ver /* read the next block when we run out of parity bytes */ offset += v->fec->roots; - if (offset >= 1 << v->data_dev_block_bits) { + if (offset >= v->fec->roots << SECTOR_SHIFT) { dm_bufio_release(buf); par = fec_read_parity(v, rsb, block_offset, &offset, &buf); @@ -674,7 +673,7 @@ int verity_fec_ctr(struct dm_verity *v) { struct dm_verity_fec *f = v->fec; struct dm_target *ti = v->ti; - u64 hash_blocks; + u64 hash_blocks, fec_blocks; int ret; if (!verity_fec_is_enabled(v)) { @@ -744,15 +743,17 @@ int verity_fec_ctr(struct dm_verity *v) } f->bufio = dm_bufio_client_create(f->dev->bdev, - 1 << v->data_dev_block_bits, + f->roots << SECTOR_SHIFT, 1, 0, NULL, NULL); if (IS_ERR(f->bufio)) { ti->error = "Cannot initialize FEC bufio client"; return PTR_ERR(f->bufio); } - if (dm_bufio_get_device_size(f->bufio) < - ((f->start + f->rounds * f->roots) >> v->data_dev_block_bits)) { + dm_bufio_set_sector_offset(f->bufio, f->start << (v->data_dev_block_bits - SECTOR_SHIFT)); + + fec_blocks = div64_u64(f->rounds * f->roots, v->fec->roots << SECTOR_SHIFT); + if (dm_bufio_get_device_size(f->bufio) < fec_blocks) { ti->error = "FEC device is too small"; return -E2BIG; }