Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1142557pxy; Fri, 23 Apr 2021 00:55:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyuTYAo+MWt+iS+9f/aLyVkC4ECtQpoUTiKykGRNJj94SNY2MC5mfNfC06e/tBz5dt6HoWc X-Received: by 2002:a17:906:cd17:: with SMTP id oz23mr2774811ejb.375.1619164541606; Fri, 23 Apr 2021 00:55:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619164541; cv=none; d=google.com; s=arc-20160816; b=PkWMDNgVsCrardR8qzRtNP9jiKeij1W4ERvfwSEckvIpnAf0u14UY1qOW5OOj7vGCj Txu7E/S3AnsgQYRvvcr9xzUqfzDG5CIuEGXtd2mII6vD/iEG/2v5Xb8YcCneiDFJE4Mj HGs/9ptZVWvJZ7WDYFEpCUvsfE6+J8gcOfPx3LRgKb9d0A36WbA9+RZ7msJOhNcCz1fh 65+iLx2vy0125PU7XNTlTfCVqLS8v5H9RPRjTBOpXyIypU0a6NsqMmU7QSHU24D1nq7v s2fMShZDx7VMYa6yoR/TbKbDnvxDOtrT0COskTeepgmP+WfzQdxnIVo5ehWHurPEAAEf tt1A== 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=o6FOYaB7lcjH4TMp8bQMsF18Zwxm+2AvI1zpKRLy4mo=; b=eP4nfP9T3+fULTrAjwvqNe1r4ptD3yUXb/AhrOi7Z63hE7vvlohPVQ1HD2OW6dWjOk kwRclsltBsALRoaOk0Ejpc7novTfRPpt4O+Eah3xnnmb5/F+PfbmRNRGbYDUnyBI0hQ+ 98phMzuWEsEjZRQ9ygMR73QFIJ6GdFIYU4JVB/zOn+KPD5ijjy7Nl4zgFLsNpw8VOqUj QBKh43e8c6X9ZZ5ftyXo+5/cgUirT1pw3VsA0JjYmIHw+teqi1sc/gtA5mGqBXcfWs6i et3P9tQRKqdxr97ZDqnSLahDCZXi2l5CLwiQOW/yB4vjdYsVeLyckAsLdX0DWG6DCth2 yJdQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 21si4508885ejv.432.2021.04.23.00.55.17; Fri, 23 Apr 2021 00:55:41 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241270AbhDWHyE (ORCPT + 99 others); Fri, 23 Apr 2021 03:54:04 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:17030 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241227AbhDWHyC (ORCPT ); Fri, 23 Apr 2021 03:54:02 -0400 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4FRRKP1P3zzPtWR; Fri, 23 Apr 2021 15:50:17 +0800 (CST) Received: from vm-Yoda-Ubuntu1804.huawei.com (10.67.174.59) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.498.0; Fri, 23 Apr 2021 15:53:09 +0800 From: Xu Yihang To: , , , , , CC: Subject: [PATCH -next] x86/apic: Force logial APIC ID in range from 0 to 8 Date: Fri, 23 Apr 2021 15:53:24 +0800 Message-ID: <20210423075324.133463-1-xuyihang@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.67.174.59] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org According to Intel 64 and IA-32 Architectures Software Developer’s Manuals Vol. 3A 10.6.2.2, Logical APIC ID locates on bit 24 to 31, can only support up to 8 local APIC under flat mode. Also C99 says left shift exceeding maximum value representable in the result type is undefined behavior. But under x86 architecture, it seems doesn't do any actual damage. There is KASAN warning on a 80 cores machine after booting #64 CPU. ``` [ 1.681097] UBSAN: Undefined behaviour in arch/x86/kernel/apic/apic_flat_64.c:51:11 [ 1.688739] shift exponent 64 is too large for 64-bit type 'long unsigned int' ``` Signed-off-by: Xu Yihang --- arch/x86/include/asm/apicdef.h | 1 + arch/x86/kernel/apic/apic_flat_64.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/apicdef.h b/arch/x86/include/asm/apicdef.h index 5716f22f81ac..150a5e831368 100644 --- a/arch/x86/include/asm/apicdef.h +++ b/arch/x86/include/asm/apicdef.h @@ -42,6 +42,7 @@ #define APIC_RRR 0xC0 #define APIC_LDR 0xD0 #define APIC_LDR_MASK (0xFFu << 24) +#define APIC_LOGICAL_ID_MAX 8 #define GET_APIC_LOGICAL_ID(x) (((x) >> 24) & 0xFFu) #define SET_APIC_LOGICAL_ID(x) (((x) << 24)) #define APIC_ALL_CPUS 0xFFu diff --git a/arch/x86/kernel/apic/apic_flat_64.c b/arch/x86/kernel/apic/apic_flat_64.c index 8f72b4351c9f..7bf91cadee21 100644 --- a/arch/x86/kernel/apic/apic_flat_64.c +++ b/arch/x86/kernel/apic/apic_flat_64.c @@ -41,7 +41,7 @@ void flat_init_apic_ldr(void) unsigned long num, id; num = smp_processor_id(); - id = 1UL << num; + id = 1UL << (num % APIC_LOGICAL_ID_MAX); apic_write(APIC_DFR, APIC_DFR_FLAT); val = apic_read(APIC_LDR) & ~APIC_LDR_MASK; val |= SET_APIC_LOGICAL_ID(id); -- 2.17.1