Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3616655rwb; Sun, 20 Nov 2022 18:19:08 -0800 (PST) X-Google-Smtp-Source: AA0mqf4GEourpBiS6sjSWUvwdGeENBC3zys7h+19R5CSSFd9UQ9D6ZCdpFN1bnvLKk21Ec2cdu7F X-Received: by 2002:a05:6402:5483:b0:468:d5a9:cb4b with SMTP id fg3-20020a056402548300b00468d5a9cb4bmr989412edb.409.1668997148523; Sun, 20 Nov 2022 18:19:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668997148; cv=none; d=google.com; s=arc-20160816; b=Jp/39oJAdzaK+v1tCwLR3WRLvR8Kw/x8i+2O0dM1AwCpiyL66IJN87XeNc0xbKN/I5 ssm2+001cLSgYWFPLXAXwWnmHTtLqt0lghAcpjpBe+ZPX4Ig6yZH5xjqiOjOf+aAcbMG P9HcxLyyghy3S4WMArRxPG/1bLro7k0skr9PSKP5uK/8M5VPgZvnV4hUxcf/+qJs0kxS G3ZB3/T7d5nJ43C3h80xica85D9e50P7oaEoLprnjgkOAMcu8ZHWCZiEvECj4xLlNApf xrdOP83TbP9MxUwcqEqGOl0VkuCy7gm18IXIWStsOPIKZkw2Tk4pJQftEcP7xsTvA7+J DNlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=dZDiwO5tuvFVS0OW71wot323cG0v7zY4X7ij/WeDBkc=; b=tGdrmyHhPm3xeVHKONWQGTALTiAG3384S8TKV1B89i9zxPv0cU8IV9RcQhPTO6RwHt IyLmwuv2UMbvprG+W1hUI/anLEB2aYAimOjtZIQqT6VgHi0Q5v4fG+TdtTRWM2NOKfke 8fG6CmZZdirg29dIP8spVN8EGVPb1BP3XIE7ggH5+n409Oet7x8VtFqv3v1z+0vSCVNG Orm14j4zEBLiNnUuwatg6UZ5TZdmn7+ROLACMs+7TyhvfmdpaquSdEKmmZPwKbgOllYc 5hRK9MzUAeHv3jW7AqSoKAvQH7BklZheUpwL9rQuCR5pFs9JJnoXTsBLeqPIBW2qjVuc y8Zg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id wv13-20020a170907080d00b007815e9c5b80si9110958ejb.617.2022.11.20.18.18.46; Sun, 20 Nov 2022 18:19:08 -0800 (PST) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229911AbiKUBio (ORCPT + 90 others); Sun, 20 Nov 2022 20:38:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229660AbiKUBin (ORCPT ); Sun, 20 Nov 2022 20:38:43 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A5062CDC8; Sun, 20 Nov 2022 17:38:41 -0800 (PST) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NFqgp3vzCzqSYt; Mon, 21 Nov 2022 09:34:46 +0800 (CST) Received: from dggpemm500006.china.huawei.com (7.185.36.236) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 21 Nov 2022 09:38:39 +0800 Received: from [10.174.178.55] (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 21 Nov 2022 09:38:38 +0800 Subject: Re: [PATCH] fs: Clear a UBSAN shift-out-of-bounds warning To: Eric Biggers CC: Alexander Viro , , References: <20221110031024.204-1-thunder.leizhen@huawei.com> From: "Leizhen (ThunderTown)" Message-ID: <5dbf72b4-e596-8954-4d8d-1e92edf46b90@huawei.com> Date: Mon, 21 Nov 2022 09:38:38 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS 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 On 2022/11/20 3:39, Eric Biggers wrote: > On Thu, Nov 10, 2022 at 11:10:24AM +0800, Zhen Lei wrote: >> UBSAN: shift-out-of-bounds in fs/locks.c:2572:16 >> left shift of 1 by 63 places cannot be represented in type 'long long int' >> >> Switch the calculation method to ((quarter - 1) * 2 + 1) can help us >> eliminate this false positive. >> >> On the other hand, the old implementation has problems with char and >> short types, although not currently involved. >> printf("%d: %x\n", sizeof(char), INT_LIMIT(char)); >> printf("%d: %x\n", sizeof(short), INT_LIMIT(short)); >> 1: ffffff7f >> 2: ffff7fff >> >> Signed-off-by: Zhen Lei >> --- >> include/linux/fs.h | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/include/linux/fs.h b/include/linux/fs.h >> index e654435f16512c1..88d42e2daed9f6c 100644 >> --- a/include/linux/fs.h >> +++ b/include/linux/fs.h >> @@ -1131,7 +1131,7 @@ struct file_lock_context { >> >> /* The following constant reflects the upper bound of the file/locking space */ >> #ifndef OFFSET_MAX >> -#define INT_LIMIT(x) (~((x)1 << (sizeof(x)*8 - 1))) >> +#define INT_LIMIT(x) ((((x)1 << (sizeof(x) * 8 - 2)) - 1) * 2 + 1) >> #define OFFSET_MAX INT_LIMIT(loff_t) >> #define OFFT_OFFSET_MAX INT_LIMIT(off_t) >> #endif > > This problem has already been solved by type_max() in include/linux/overflow.h. > How about removing INT_LIMIT() and using type_max() instead? It's better to use type_max(). INT_LIMIT() is currently valid only for signed type. Besides, my method is actually the same as type_max(). Using type_max() does not result in code duplication. > > - Eric > . > -- Regards, Zhen Lei