Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp4868094rwn; Sun, 11 Sep 2022 23:51:57 -0700 (PDT) X-Google-Smtp-Source: AA6agR4HkAfbXAs6776a0nSKYo2Ashm8lcmNhgtw6fBPBqDh7T2YS983pPkIsxrUj1eVBwb5sPVb X-Received: by 2002:a17:907:8a04:b0:77c:398c:90fa with SMTP id sc4-20020a1709078a0400b0077c398c90famr4630600ejc.595.1662965517264; Sun, 11 Sep 2022 23:51:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662965517; cv=none; d=google.com; s=arc-20160816; b=h0L2fpTK7mz3MlKdsQT5BnlR/QGaBGkh7D4ADnLSOkv1DGXNUyol9w5vIBT0rq66uM CbCg6YSNFik+UGD2dci+YsBaVw/DEjTiQfKEMAZe5WM+W4KSh9X+hHOZE7qH6cDfatIs VladErbgs2jSfHC5KZguJzIBQmS/yqqY0n0/xNpHAyy83zOYgHZyficaW0tAD8M3TtnO XUY56gCAhZ9h8yfvprVx1QIxmB+EXmJm2bLqLdAWgcjeof91URGc3DKB0RFSquWH9DzO bD/9tFqoaGOGvSucWpRVirYqyYcJrJdUk1KWuwYkaa+bQ7VQqg/V66qUsGrKF6tdeQwf 24dA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=TLRcC5IeLSQPuJjdu28jfpJ3HTwzjIn6aZaZhEyqqb8=; b=NQJv4Zf1F1wzCd3t4/ZuBoo1wNStHNlNYc9JW4ncEC7gjSB7qrFQNrqXsTneoT42eL pmMjYKW03HabVcVQfijFA8oHOb1CQPH6snH2GI0khNnAT+pFJ5Er1Cp7b48nUHmRcPc8 XOVT9X5TFog5/IUZGS5/WWinHEfheV3mZnE7gU9uAdZMGG727ETF+i8hNnLew3TGjPZC YvuGWJioX0eK19TJJ1Dxvf9haw5fUAnsA3ugRMj7Reye73XP+uThM0UKdOzlT4Ah8nsT IdIzIcPPn+7BG0MIuHYh87Quv1qzzYsNUXSEOpKD6taWiCOlag7/ZEPSA92j/P6EYn/7 cAlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=RndJJ2rD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bi4-20020a170906a24400b00773db9444dbsi5739110ejb.652.2022.09.11.23.51.31; Sun, 11 Sep 2022 23:51:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=RndJJ2rD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229738AbiILGkG (ORCPT + 99 others); Mon, 12 Sep 2022 02:40:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229566AbiILGkC (ORCPT ); Mon, 12 Sep 2022 02:40:02 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED5C5101EE; Sun, 11 Sep 2022 23:40:00 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id 29so11210512edv.2; Sun, 11 Sep 2022 23:40:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date; bh=TLRcC5IeLSQPuJjdu28jfpJ3HTwzjIn6aZaZhEyqqb8=; b=RndJJ2rDg5R1zqzv716+YXL8VScLD/w+CjQXUcOaIWgngLo3cgIIg+nzNWVHsTGqPA K2bgU8De2y5cpU/kiV9+5S2HtZlYDffixm8WJGLagHZvZ40GiyD1VFI2YnXBAVrdj3XH vUApN1xtg37pA7G+gpnZJdDBK/j0AyES1k8Grx3HPNitxHqUOBMt0lbn2weuAhtNhE3I YcDdRTBKVXjaS+Vzr4QLMRxMGbcgWufjOxzp38VTltykThp/MtEP24Fwumn6bAL544Mi Awd3CgiGCl9DMeR0b9kBQaO5jHMv/KETO0rCGqcqMNBZVCQLAwbF+cSAeMKRVj+0wdG2 uMaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=TLRcC5IeLSQPuJjdu28jfpJ3HTwzjIn6aZaZhEyqqb8=; b=0XL4MntvjLtscJ1TvsP2rFvZVqmG0HlsvbW7rSzxaA1HzT4+GSDyqXztR5rgKgr6eO jMK79IBm2AdDpQC7ZE92TtCkgmu49Q1+RaRpsPNWC43bsDNSdMc9um68jptqJ0wH+YV5 iH1zeErrbz02WPx2qarVho71fJVV4DnfejOJSxetKWMa+4XbFys3OYvkNspVQlIkHV3+ ykTbZVRfINxsSF+N9p88l00X0qKGSFm8K9+uivle8kcZirVq1irlh++n0LUP1nAZtqPB tbSo9GPdwl30vqhKJri9yQgY9Vp/WCfdqc0y5S3sQqrq9MHwWXimFpkkVEK+WFUtspF+ 45CQ== X-Gm-Message-State: ACgBeo3HXpIzl9haTE4FtLr5mqJ09i7p7j6+B51sHqRXn2KtFa73ECIb naCpXmdxuiBUwk7g6jKqOqY42gKzDbpiNfIXQPg= X-Received: by 2002:a05:6402:1d55:b0:451:756e:439d with SMTP id dz21-20020a0564021d5500b00451756e439dmr5670556edb.226.1662964799232; Sun, 11 Sep 2022 23:39:59 -0700 (PDT) MIME-Version: 1.0 References: <20220909030756.3916297-1-zhangshida@kylinos.cn> <20220911231251.GA3600936@dread.disaster.area> In-Reply-To: <20220911231251.GA3600936@dread.disaster.area> From: Stephen Zhang Date: Mon, 12 Sep 2022 14:39:23 +0800 Message-ID: Subject: Re: [PATCH] xfs: remove the redundant check in xfs_bmap_first_unused To: Dave Chinner Cc: djwong@kernel.org, dchinner@redhat.com, chandan.babu@oracle.com, zhangshida@kylinos.cn, linux-kernel@vger.kernel.org, linux-xfs@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Dave Chinner =E4=BA=8E2022=E5=B9=B49=E6=9C=8812=E6=97= =A5=E5=91=A8=E4=B8=80 07:12=E5=86=99=E9=81=93=EF=BC=9A > Given that all the types and comparisons involved are 64 bit > unsigned: > > typedef uint64_t xfs_fileoff_t; /* block number in a file */ > > #define XFS_FILEOFF_MAX(a,b) max_t(xfs_fileoff_t, (a), (b)) > > xfs_fileoff_t br_startoff; > > xfs_fileoff_t lastaddr =3D 0; > xfs_fileoff_t lowest, max; > > We end up with the following calculations (in FSBs, not bytes): > > lowest + len =3D 0x800000ULL + 1 > =3D 0x800001ULL > > got.br_startoff - max =3D 0ULL - 0x800000 > =3D 0xffffffffff800000ULL > > and so the existing check is: > > if (0 >=3D 0x800001ULL && 0xffffffffff800000 >=3D 1) > > which evaluates as false because the extent that was found is not > beyond the initial offset (first_unused) that we need to start > searching at. > > With your modification, this would now evaluate as: > > if (0xffffffffff800000 >=3D 1) > > Because of the underflow, this would then evaluate as true and we'd > return 0 as the first unused offset. This is incorrect as we do not > have a hole at offset 0, nor is it within the correct directory > offset segment, nor is it within the search bounds we have > specified. > > If these were all signed types, then your proposed code might be > correct. But they are unsigned and hence we have to ensure that we > handle overflow/underflow appropriately. > > Which leads me to ask: did you test this change before you send > it to the list? > I am so sorry about the mistake, and thanks for your elaboration about this problem. it indeed teaches me a lesson about the necessity of test even for the simplest change. By the way, theoretically, in order to solve this, I wonder if we could change the code in the following way: =3D=3D=3D=3D xfs_bmap_first_unused( /* * See if the hole before this extent will work. */ - if (got.br_startoff >=3D lowest + len && - got.br_startoff - max >=3D len) + if (got.br_startoff >=3D max + len) break; =3D=3D=3D=3D Thanks, Stephen.