From patchwork Tue Feb 23 20:21:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Broz X-Patchwork-Id: 12100875 X-Patchwork-Delegate: snitzer@redhat.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD9E4C433DB for ; Tue, 23 Feb 2021 20:22:32 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1962464E6B for ; Tue, 23 Feb 2021 20:22:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1962464E6B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=tempfail smtp.mailfrom=dm-devel-bounces@redhat.com Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-72-oMcy9V-sNR2goEtI_RtAXw-1; Tue, 23 Feb 2021 15:22:28 -0500 X-MC-Unique: oMcy9V-sNR2goEtI_RtAXw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 14FC4804036; Tue, 23 Feb 2021 20:22:23 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4A84610016DB; Tue, 23 Feb 2021 20:22:22 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4AE5118095CB; Tue, 23 Feb 2021 20:22:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 11NKMIPq029842 for ; Tue, 23 Feb 2021 15:22:18 -0500 Received: by smtp.corp.redhat.com (Postfix) id ACEEF202E940; Tue, 23 Feb 2021 20:22:18 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A7AED2026D48 for ; Tue, 23 Feb 2021 20:22:16 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E31628CBAB7 for ; Tue, 23 Feb 2021 20:22:15 +0000 (UTC) Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-470-JWSe2iSyNMeMBQ9lSk-qVA-1; Tue, 23 Feb 2021 15:22:11 -0500 X-MC-Unique: JWSe2iSyNMeMBQ9lSk-qVA-1 Received: by mail-wm1-f47.google.com with SMTP id x16so3646278wmk.3; Tue, 23 Feb 2021 12:22:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BkPOxcgjrqWtZmRCdmfyLFJG8McoAwV+9oi6TjuwMnk=; b=HGEp9+dpzyn3pfD3irZjhDkh7HxtrKCFKd0B/S7M1X0Oh5aQwB1HI3bv2uqFRcd5mt HoXuQVe9L4R2y9+QfStbmQUgxYWJtDDP71yaY6dg9oktSnFPpkkCOhE5/AInJeKrHnQe FwLzrt25tURFhouqGaY+XkVQJcrp6CtRuxyqWuZ76enszIb2NfljLkd2CfW3c/jwImCD JC+2BPZmKW/uOpRJ+R8Zzwsx8X0Yl4Vcs0ADslmDm7dsusxd4oYKyCOgTBX6uYA+Lvh2 A8Sd8dHFlTKk87fjiQ2YvRgiy+u/ZT9vodQES6dgZQcijfqOaKYQ8ci5RZEh9ObO+WA+ oqtQ== X-Gm-Message-State: AOAM532t1qDvVNhDF5k1MTj+M2l+EY1GjgtLpaj2AEz3XH4X/a2U4wIi HOERWfrpL9mddIe0sd1l//NPIfyKx7A= X-Google-Smtp-Source: ABdhPJw1gZf5OWCT9QjMn2XBExF7dqGlffcNklaIuAYRmwYLCtHi8+TmvmO7NtM2yVt/Y0W6TGl47g== X-Received: by 2002:a05:600c:4f86:: with SMTP id n6mr479726wmq.22.1614111729857; Tue, 23 Feb 2021 12:22:09 -0800 (PST) Received: from merlot.mazyland.net (39.35.broadband4.iol.cz. [85.71.35.39]) by smtp.googlemail.com with ESMTPSA id w4sm3718072wmc.13.2021.02.23.12.22.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Feb 2021 12:22:09 -0800 (PST) From: Milan Broz To: dm-devel@redhat.com Date: Tue, 23 Feb 2021 21:21:20 +0100 Message-Id: <20210223202121.898736-1-gmazyland@gmail.com> In-Reply-To: <20210222211528.848441-1-gmazyland@gmail.com> References: <20210222211528.848441-1-gmazyland@gmail.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: dm-devel@redhat.com Cc: mpatocka@redhat.com, Milan Broz , stable@vger.kernel.org Subject: [dm-devel] [PATCH 1/2] dm-bufio: subtract the number of initial sectors in dm_bufio_get_device_size X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dm-devel-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com From: Mikulas Patocka dm_bufio_get_device_size returns returns the device size in blocks. Before returning the value, we must subtract the nubmer of starting sectors. The number of starting sectos may not be divisible by block size. Note that currently, no target is using dm_bufio_set_sector_offset and dm_bufio_get_device_size simultaneously, so this patch has no effect. However, an upcoming patch that fixes dm-verity-fec needs this change. Signed-off-by: Mikulas Patocka Reviewed-by: Milan Broz Cc: stable@vger.kernel.org --- drivers/md/dm-bufio.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/md/dm-bufio.c b/drivers/md/dm-bufio.c index fce4cbf9529d..50f3e673729c 100644 --- a/drivers/md/dm-bufio.c +++ b/drivers/md/dm-bufio.c @@ -1526,6 +1526,10 @@ EXPORT_SYMBOL_GPL(dm_bufio_get_block_size); sector_t dm_bufio_get_device_size(struct dm_bufio_client *c) { sector_t s = i_size_read(c->bdev->bd_inode) >> SECTOR_SHIFT; + if (s >= c->start) + s -= c->start; + else + s = 0; if (likely(c->sectors_per_block_bits >= 0)) s >>= c->sectors_per_block_bits; else From patchwork Tue Feb 23 20:21:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Milan Broz X-Patchwork-Id: 12100877 X-Patchwork-Delegate: snitzer@redhat.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9712DC433E0 for ; Tue, 23 Feb 2021 20:23:09 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1711B64E4B for ; Tue, 23 Feb 2021 20:23:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1711B64E4B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=tempfail smtp.mailfrom=dm-devel-bounces@redhat.com Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-76-BGTbM4NgNAqwg8V10gc97g-1; Tue, 23 Feb 2021 15:23:06 -0500 X-MC-Unique: BGTbM4NgNAqwg8V10gc97g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5E0C1107ACC7; Tue, 23 Feb 2021 20:23:01 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3D56C6FEF0; Tue, 23 Feb 2021 20:23:01 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B0F1B4EA32; Tue, 23 Feb 2021 20:23:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 11NKMx1u029900 for ; Tue, 23 Feb 2021 15:22:59 -0500 Received: by smtp.corp.redhat.com (Postfix) id 418271005B62; Tue, 23 Feb 2021 20:22:59 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0C9BF1000DAA for ; Tue, 23 Feb 2021 20:22:56 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F3FDD100AFF1 for ; Tue, 23 Feb 2021 20:22:55 +0000 (UTC) Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-319-fyDJjEAfMUKBXhAx4VyUxA-1; Tue, 23 Feb 2021 15:22:21 -0500 X-MC-Unique: fyDJjEAfMUKBXhAx4VyUxA-1 Received: by mail-wr1-f49.google.com with SMTP id 7so23893215wrz.0; Tue, 23 Feb 2021 12:22:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5fh/D1A+JiA68ji7CME2QlrIQrJhB9RCBAudcPZNzck=; b=qmW8hwLlWkk0YvB1nsTtbuGH388fm9iFvNLWeySk8Wg19x/Bb+zZd0FQS/z/bdbP10 UCSHvqRYQMfJFnTiZcegVlwxMkaCpp+6WYPnfggOU3ZcZz+LH47+lQsLPyiAWA7M14pD QjXXWbfD+y9Ua/Ae6lf+jfqxX0MAAX2twfY36Sya9garaOQ7AAcKwdvJ1mSUHtcyY3H9 8+Sd8cYNBC02PrWHeKCAacbbwrkuEmEKNMAGgt9x1f1LCFJggu8zW5l2RC+KibZGR4ZK KJz7/00NrgBs9s8Y3rQjPT2tuRVSWhF5AJ5EEWcC1o2Fc5JgnOMtxB0ZxIPe2U9aRiTI A+Fg== X-Gm-Message-State: AOAM5327LK3g3/KtAxEP34hux8PagUfAt52z7nO8j8uj6Na3PUeFhA9k 4wHBLbicAKl4bes56cIjNcv92ccQKiE= X-Google-Smtp-Source: ABdhPJx/+Ep89Sibo1iSUcyjlYpnVeP5r9e3humFdcQNG+3kFULnpLmOL4DjXQXHJopW3TUVHL4tkQ== X-Received: by 2002:a5d:4044:: with SMTP id w4mr20003557wrp.121.1614111740229; Tue, 23 Feb 2021 12:22:20 -0800 (PST) Received: from merlot.mazyland.net (39.35.broadband4.iol.cz. [85.71.35.39]) by smtp.googlemail.com with ESMTPSA id w4sm3718072wmc.13.2021.02.23.12.22.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Feb 2021 12:22:19 -0800 (PST) From: Milan Broz To: dm-devel@redhat.com Date: Tue, 23 Feb 2021 21:21:21 +0100 Message-Id: <20210223202121.898736-2-gmazyland@gmail.com> In-Reply-To: <20210223202121.898736-1-gmazyland@gmail.com> References: <20210222211528.848441-1-gmazyland@gmail.com> <20210223202121.898736-1-gmazyland@gmail.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: dm-devel@redhat.com Cc: stable@vger.kernel.org, =?utf-8?b?SsOpcsO0bWUgQ2FycmV0ZXJv?= , mpatocka@redhat.com, Milan Broz , Sami Tolvanen Subject: [dm-devel] [PATCH 2/2] dm-verity: Fix FEC for RS roots non-aligned to block size X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dm-devel-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 patch 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 reproducible using veritysetup, here for example 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 Without it, FEC code usually ends on unrecoverable failure in RS decoder: device-mapper: verity-fec: 7:1: FEC 0: failed to correct: -74 ... With the patch, errors are properly repaired. device-mapper: verity-fec: 7:1: FEC 0: corrected 8 errors ... This problem is present in all kernels since the FEC code introduction (kernel 4.5). AFAIK the problem is not visible in Android ecosystem because it always use default RS roots=2. Signed-off-by: Milan Broz Tested-by: Jérôme Carretero Reviewed-by: Sami Tolvanen Cc: stable@vger.kernel.org --- drivers/md/dm-verity-fec.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/drivers/md/dm-verity-fec.c b/drivers/md/dm-verity-fec.c index fb41b4f23c48..66401ca1f624 100644 --- a/drivers/md/dm-verity-fec.c +++ b/drivers/md/dm-verity-fec.c @@ -61,18 +61,18 @@ static int fec_decode_rs8(struct dm_verity *v, struct dm_verity_fec_io *fio, 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), + (unsigned long long)block, PTR_ERR(res)); *buf = NULL; } @@ -155,7 +155,7 @@ static int fec_decode_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio, /* 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 +674,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 +744,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; }