From patchwork Tue Nov 1 06:09:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaosheng Cui X-Patchwork-Id: 13527 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2765408wru; Mon, 31 Oct 2022 23:15:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6mFipTfbNIOrfAE3zcZ5oj55SGiNbs6546p9RyQBKCC+B4zpBwvvmz2A57+x9U5cVsB40t X-Received: by 2002:a17:906:49d1:b0:78a:5703:9740 with SMTP id w17-20020a17090649d100b0078a57039740mr61256ejv.313.1667283301794; Mon, 31 Oct 2022 23:15:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667283301; cv=none; d=google.com; s=arc-20160816; b=Zmeye3o156RmnWci/fHnaJ17oAx1Weo57MWRCS5Z9n9q707vT5MQo4f4lQ2/OxeMBu Rk4+NlORrJAx9N+56SyP0a/wHWSptggmQjkL6BiQJgIz9Tv+cANoqDQS6vhZKi9ZYOPc +52nQYANeOHqkOgivWgfK376Fh+A1pGWSrxhblH8gV+iXVfKNvJum3jU2g6zPdPJwPTG asZuWCVoxnNZWLSxoiMNgp3sB1V34zN9nZGEj7js2PV6XqWm65XiM/ARtB/r9O/n8BmK yoxJ2VZwIfIsAqk41O6umGJyeyJftlfGnZZUiA/ohXo/zN+UrGhfpfK4DYpr9azaq8YI h+PQ== 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 :message-id:date:subject:cc:to:from; bh=CUjErKiN0NF9sF1iWRzW7S2mZQ0j06lGnHEtG5ZnApk=; b=mS90gtEDvQhyrZhk2MHMBz6p4qpTj4pIONJ/YzMXHZyLYweDgbg2SDAlzVQv2A5Nxc rQ/03JyUf1CyXW7i5ufaw7KuntZ+esQerYJMDUQtSlyMuab6LIhyGAoSqZu3vasE/8iS 0+mCT/i8e/DV5JRLcP2vhL5lXzEk6NbxSTkmL/Ans0/mh8wXS1VLGEaMyzvkSwOA8nHs PXsoKtgPmS7vSIYmUVh9I24GHusnz9QYkXHWnG1Nc2+e7M+5ZgpJilP9gh7eJCppCIDP daPuwle9WxZeqMZ8WTfo4vQmFHKS672YuXACAWim7mhWoIe9Pwbuh8w4tWP7HRIAh3lk fDQw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qa38-20020a17090786a600b0078c4a772ea7si12259726ejc.11.2022.10.31.23.14.36; Mon, 31 Oct 2022 23:15:01 -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; 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 S229885AbiKAGJx (ORCPT + 99 others); Tue, 1 Nov 2022 02:09:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229613AbiKAGJv (ORCPT ); Tue, 1 Nov 2022 02:09:51 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 500FCFD12 for ; Mon, 31 Oct 2022 23:09:49 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4N1fk11zF6zHvS1; Tue, 1 Nov 2022 14:09:29 +0800 (CST) Received: from cgs.huawei.com (10.244.148.83) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 1 Nov 2022 14:09:46 +0800 From: Gaosheng Cui To: , , , , , , , , , , , , , , , , , , , , , CC: Subject: [PATCH] x86/cpu: replacing the open-coded shift with BIT(x) Date: Tue, 1 Nov 2022 14:09:45 +0800 Message-ID: <20221101060945.722565-1-cuigaosheng1@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.244.148.83] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748273255088012537?= X-GMAIL-MSGID: =?utf-8?q?1748273255088012537?= Replace the open-coded shift with BIT(x) for x86_power to make the code a bit more self-documenting, and we will get a UBSAN issue in arch/x86/kernel/cpu/proc.c, fix it. The UBSAN warning calltrace like below: UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/proc.c:138:25 left shift of 1 by 31 places cannot be represented in type 'int' Call Trace: dump_stack_lvl+0x7d/0xa5 dump_stack+0x15/0x1b ubsan_epilogue+0xe/0x4e __ubsan_handle_shift_out_of_bounds+0x1e7/0x20c show_cpuinfo+0x5ff/0x6d0 seq_read_iter+0x116/0x5b0 proc_reg_read_iter+0x45/0xc0 vfs_read+0x2ee/0x3c0 ksys_read+0xe1/0x130 __x64_sys_read+0x23/0x30 do_syscall_64+0x58/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd Signed-off-by: Gaosheng Cui --- arch/x86/kernel/cpu/amd.c | 2 +- arch/x86/kernel/cpu/centaur.c | 2 +- arch/x86/kernel/cpu/hygon.c | 2 +- arch/x86/kernel/cpu/intel.c | 2 +- arch/x86/kernel/cpu/proc.c | 2 +- arch/x86/kernel/cpu/zhaoxin.c | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index 860b60273df3..75d82cad323a 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -613,7 +613,7 @@ static void early_init_amd(struct cpuinfo_x86 *c) * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate * with P/T states and does not stop in deep C-states */ - if (c->x86_power & (1 << 8)) { + if (c->x86_power & BIT(8)) { set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC); set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC); } diff --git a/arch/x86/kernel/cpu/centaur.c b/arch/x86/kernel/cpu/centaur.c index 345f7d905db6..9910bb1d90fd 100644 --- a/arch/x86/kernel/cpu/centaur.c +++ b/arch/x86/kernel/cpu/centaur.c @@ -105,7 +105,7 @@ static void early_init_centaur(struct cpuinfo_x86 *c) #ifdef CONFIG_X86_64 set_cpu_cap(c, X86_FEATURE_SYSENTER32); #endif - if (c->x86_power & (1 << 8)) { + if (c->x86_power & BIT(8)) { set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC); set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC); } diff --git a/arch/x86/kernel/cpu/hygon.c b/arch/x86/kernel/cpu/hygon.c index 21fd425088fe..dc473bfbf1b5 100644 --- a/arch/x86/kernel/cpu/hygon.c +++ b/arch/x86/kernel/cpu/hygon.c @@ -251,7 +251,7 @@ static void early_init_hygon(struct cpuinfo_x86 *c) * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate * with P/T states and does not stop in deep C-states */ - if (c->x86_power & (1 << 8)) { + if (c->x86_power & BIT(8)) { set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC); set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC); } diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index 121c1c38162a..bbe86a2f3a43 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -286,7 +286,7 @@ static void early_init_intel(struct cpuinfo_x86 *c) * It is also reliable across cores and sockets. (but not across * cabinets - we turn it off in that case explicitly.) */ - if (c->x86_power & (1 << 8)) { + if (c->x86_power & BIT(8)) { set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC); set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC); } diff --git a/arch/x86/kernel/cpu/proc.c b/arch/x86/kernel/cpu/proc.c index 099b6f0d96bd..efa1d39c4f25 100644 --- a/arch/x86/kernel/cpu/proc.c +++ b/arch/x86/kernel/cpu/proc.c @@ -135,7 +135,7 @@ static int show_cpuinfo(struct seq_file *m, void *v) seq_puts(m, "power management:"); for (i = 0; i < 32; i++) { - if (c->x86_power & (1 << i)) { + if (c->x86_power & BIT(i)) { if (i < ARRAY_SIZE(x86_power_flags) && x86_power_flags[i]) seq_printf(m, "%s%s", diff --git a/arch/x86/kernel/cpu/zhaoxin.c b/arch/x86/kernel/cpu/zhaoxin.c index 05fa4ef63490..34a8a460f8f4 100644 --- a/arch/x86/kernel/cpu/zhaoxin.c +++ b/arch/x86/kernel/cpu/zhaoxin.c @@ -61,7 +61,7 @@ static void early_init_zhaoxin(struct cpuinfo_x86 *c) #ifdef CONFIG_X86_64 set_cpu_cap(c, X86_FEATURE_SYSENTER32); #endif - if (c->x86_power & (1 << 8)) { + if (c->x86_power & BIT(8)) { set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC); set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC); }