From patchwork Thu Feb 9 07:22:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juergen Gross X-Patchwork-Id: 54778 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp186784wrn; Wed, 8 Feb 2023 23:24:35 -0800 (PST) X-Google-Smtp-Source: AK7set8wrkqFi3DyDVNxm2+IKuFX9jdZ3TbbnBPFMQHhwHMy0X9VccBlv7GmB961pEgXF406H1/N X-Received: by 2002:a17:906:1e8a:b0:870:6554:92ac with SMTP id e10-20020a1709061e8a00b00870655492acmr10027522ejj.18.1675927475448; Wed, 08 Feb 2023 23:24:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675927475; cv=none; d=google.com; s=arc-20160816; b=itV6XzfKLd9MckT53itKFJKbkcZi0nlsQu4ZtZnuI5xwV15rJt3Dyi0AqjESJE4D41 kng6jBj4YW1/BwWtv0RtV99cRaG9ec2a5t/cJfb8n0a/V9g9QMccRMKInB5McPk3oMdd QZ5CItsyB8UK7W1lWAPfU6T5YQD1Tb44tQ7gkIix3JOv7WCWKasn7btvA4FQ2EuiBxLv qjf9mhKaLULOFvveZNDPRqcOQik4nBJzS+OvWqQDU1pRMHVNaMMrOgl3k8HHGFf7WzJa hbqqsj+2zkHjnG93DGDHNAHgQK85R/GjDZj3Asu5Y2JVO0q1JVpDAUq9mt6YItETpOvi SZFw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=z/TdnBDA6vtZIeltc4dNIeTCjXcEoUw2SXAii0hNOAs=; b=GEG3xBQtpJcGJEfJmI+NXzkfFd6XwSpifPCBWEFeVz6EjSis2io7AnzPe0BuMZQH1j ddL+GspYdq39+GKhBGnQHnLFI89zlO2QCs3Gyc7jIzsVPbyr78FzSopthFjADFUJNPiv 2qmer7kJFOIYtcSIGqRoR55vr6lN8M6qPS333mwWubDLNixst+oHDxYFjncB6d9onM6f 6pFc5f6cN5r3++EulPs1kU0XEYQGrxTacj76dc4TtGtrfdBOD+kCbUCK70EljtY2IUIf BkSRTxmwx6Xg0bNjbuxEJt1n7Wm8kyYN4o9Cfh0vTTnmXdKHq1qVrMXWgu+gOhVgoPOu l1FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b="rU6L7/gA"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c25-20020a17090620d900b008aba508d1d2si1820039ejc.741.2023.02.08.23.24.12; Wed, 08 Feb 2023 23:24:35 -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; dkim=pass header.i=@suse.com header.s=susede1 header.b="rU6L7/gA"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229970AbjBIHX0 (ORCPT + 99 others); Thu, 9 Feb 2023 02:23:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229868AbjBIHXM (ORCPT ); Thu, 9 Feb 2023 02:23:12 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D35342DF6 for ; Wed, 8 Feb 2023 23:22:59 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C4ECF5C222; Thu, 9 Feb 2023 07:22:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1675927377; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=z/TdnBDA6vtZIeltc4dNIeTCjXcEoUw2SXAii0hNOAs=; b=rU6L7/gAsZQbnSs8QIonGyYpZQf6gX6EDnm1EQJPpZsDdwU2Vk/eF1pD4eIU7yjHIQbBp3 ywLutBDNczAAroxue8LtKXBGjwEyucyXXa/ajr7nNHKEYpoTn+EiQKdT0f2AmHHi7S3eHY TU2dpIs4JBoXxMC7lMAgUMHReF8NnFg= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 7C97D1339E; Thu, 9 Feb 2023 07:22:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 7S87HVGf5GMTeQAAMHmgww (envelope-from ); Thu, 09 Feb 2023 07:22:57 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: lists@nerdbynature.de, mikelley@microsoft.com, torvalds@linux-foundation.org, Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH v2 6/8] x86/mtrr: don't let mtrr_type_lookup() return MTRR_TYPE_INVALID Date: Thu, 9 Feb 2023 08:22:18 +0100 Message-Id: <20230209072220.6836-7-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230209072220.6836-1-jgross@suse.com> References: <20230209072220.6836-1-jgross@suse.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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?1757337328252509893?= X-GMAIL-MSGID: =?utf-8?q?1757337328252509893?= mtrr_type_lookup() should always return a valid memory type. In case there is no information available, it should return the default UC. At the same time the mtrr_type_lookup() stub for the !CONFIG_MTRR case should set uniform to 1, as if the memory range would be covered by no MTRR at all. In the CONFIG_MTRR case make sure uniform is always set to either 0 or 1. Without mtrr_state_set set it to 0, as it isn't known yet whether the covered range is uniform or not (in fact there is currently no caller interested in the uniform value before mtrr_state_set is being set). With MTRRs disabled uniform can be set to 1. Suggested-by: Linus Torvalds Signed-off-by: Juergen Gross --- V2: - always set uniform - set uniform to 1 in case of disabled MTRRs (Linus Torvalds) --- arch/x86/include/asm/mtrr.h | 7 +++++-- arch/x86/kernel/cpu/mtrr/generic.c | 12 ++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/mtrr.h b/arch/x86/include/asm/mtrr.h index 0b8f51d683dc..f362c33e3d74 100644 --- a/arch/x86/include/asm/mtrr.h +++ b/arch/x86/include/asm/mtrr.h @@ -53,9 +53,12 @@ void mtrr_generic_set_state(void); static inline u8 mtrr_type_lookup(u64 addr, u64 end, u8 *uniform) { /* - * Return no-MTRRs: + * Return the default MTRR type, without any known other types in + * that range. */ - return MTRR_TYPE_INVALID; + *uniform = 1; + + return MTRR_TYPE_UNCACHABLE; } #define mtrr_save_fixed_ranges(arg) do {} while (0) #define mtrr_save_state() do {} while (0) diff --git a/arch/x86/kernel/cpu/mtrr/generic.c b/arch/x86/kernel/cpu/mtrr/generic.c index 788bc16888a5..afb59ff2cc00 100644 --- a/arch/x86/kernel/cpu/mtrr/generic.c +++ b/arch/x86/kernel/cpu/mtrr/generic.c @@ -299,11 +299,15 @@ u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform) /* Make end inclusive instead of exclusive */ end--; - if (!mtrr_state_set) - return MTRR_TYPE_INVALID; + if (!mtrr_state_set) { + *uniform = 0; /* Uniformity is unknown. */ + return MTRR_TYPE_UNCACHABLE; + } - if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED)) - return MTRR_TYPE_INVALID; + if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED)) { + *uniform = 1; + return MTRR_TYPE_UNCACHABLE; + } /* * Look up the fixed ranges first, which take priority over