Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp535292pxb; Fri, 28 Jan 2022 04:51:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJzq8o8Bcg4+bL8ia5cND4K+wM6Z2IB3Sjww7jjSCz+cczR/diix4AvxkFC5DNtlEGk0qF1E X-Received: by 2002:a17:906:9382:: with SMTP id l2mr7013508ejx.267.1643374261040; Fri, 28 Jan 2022 04:51:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643374261; cv=none; d=google.com; s=arc-20160816; b=LCwDjgalz+4uKDgVObBodVyqXkv0agO7aqTqH0DTu8QI9GSx3MVDc+6LcRrPr2Vwr5 n6Y0Ox4Sg7VQJR9MtM44fZmTa7CfnbMpCBIBAC2VWsNTmUySunsna/cbCTLVyOZKG0Lf SPdpDM9xwvjj6SpOGGSKEdqgM3rlxW6rClobo0idSxDXmnEqmOGFURpcYLG0oMFdxdZV WqCm7f3Pbcir0JGo9i8Tw05GeaGo55tQio1bGc9ySBMOGltyuqOszT7K/zuIPij5gsN3 k6RDehb9nojb/6WSinwWgj44Tnw+Ahg9HM60f2iiFyHElifTA5wt2mrgHwiZVlbyW3w7 sLqg== 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=h+qUqf2jjWRCkkcmrWjWjx8iulUxnYwxvaWAIMti7ms=; b=sJM6d5bMj8sTvmBBZ0MERZVf/Wix1IhPgp9SJuwL/2uu2oUTX1u9LbWd/h6W10eDfx do9FVIGda6DEQshkCgum6cXefezfoZnkF70IbMwVzPMJwUbBVGRc8gi+pmcaJwRyGsZ3 EgD+yirm1swjMV8NqIK0eOoe9daUqI1QeMNy55FHmE5RVoAN3MHi5tf4sNn9Nc/kA+ZR DUPjNb6gtVIOHgaKQcrJ1U1CMgIQ1nwALz0vyhhPgUHuD+LQAc7Z600VCTGZUklPTGQq t4wzUma/01IobLr3gqcD6YhMQ0iVR4kTczd2wgN+tSyF1FFYi0yrIWrWi23tb1uemPhP +lVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cyEO9MFq; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n23si2919581ejl.220.2022.01.28.04.50.36; Fri, 28 Jan 2022 04:51:01 -0800 (PST) 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; dkim=pass header.i=@intel.com header.s=Intel header.b=cyEO9MFq; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238886AbiA0Rz1 (ORCPT + 99 others); Thu, 27 Jan 2022 12:55:27 -0500 Received: from mga02.intel.com ([134.134.136.20]:19413 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244437AbiA0RzP (ORCPT ); Thu, 27 Jan 2022 12:55:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643306115; x=1674842115; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Y+ZrmtCf1q4qznLjZH0k13EJLAex2Lre9GU17jNVyaE=; b=cyEO9MFqmMrdeATvEzrXos5LNrqq9u+h5HfL1AFGQiFZcwnY66zhA+v+ DI5W8wExqioVvksjQ9wB4krzjnPM4hnCyDdcVMlWAi5muv40lAHQdYc4F VkHkCgVGI3DN5TL0xObpNilvaEdjpFlF7GralgKGPj6VEIDIXV1GrjhM9 vEIGTIhDXckBlzgusYi1TtD3srTxZ2ZMMW5QaIKIHunRP7Q6skeA2eCiY 7eMCPw1PgMEiJSQyV2zbu1FqYICnTtQhPZ0nszp+N5w61phfak3WcUzQs RNbzl7BCWR7tCGTQAU4yUoZPBpHKwY1iWSjTnFFk3fk8nY1yx0PBFoVaJ g==; X-IronPort-AV: E=McAfee;i="6200,9189,10239"; a="234302428" X-IronPort-AV: E=Sophos;i="5.88,321,1635231600"; d="scan'208";a="234302428" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jan 2022 09:55:10 -0800 X-IronPort-AV: E=Sophos;i="5.88,321,1635231600"; d="scan'208";a="674796107" Received: from iweiny-desk2.sc.intel.com (HELO localhost) ([10.3.52.147]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jan 2022 09:55:10 -0800 From: ira.weiny@intel.com To: Dave Hansen , "H. Peter Anvin" , Dan Williams Cc: Ira Weiny , Fenghua Yu , Rick Edgecombe , linux-kernel@vger.kernel.org Subject: [PATCH V8 18/44] x86/fault: Add a PKS test fault hook Date: Thu, 27 Jan 2022 09:54:39 -0800 Message-Id: <20220127175505.851391-19-ira.weiny@intel.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220127175505.851391-1-ira.weiny@intel.com> References: <20220127175505.851391-1-ira.weiny@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ira Weiny The PKS test code is going to purposely create faults when testing invalid access. It will need a way to flag those faults as invalid and keep the kernel running properly. Create a hook in the fault handler to call back into the test code such that the test code can track when a test it runs results in a fault. The hook returns if the fault was caused by the test code so the main handler can consider the fault handled. Also the hook is responsible to clear up the reason for the fault. Predicate the hook on CONFIG_PKS_TEST. Signed-off-by: Ira Weiny --- arch/x86/include/asm/pks.h | 14 ++++++++++++++ arch/x86/mm/fault.c | 30 ++++++++++++++++++++---------- lib/pks/pks_test.c | 12 ++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/pks.h b/arch/x86/include/asm/pks.h index d211bf36492c..ee9fff5b4b13 100644 --- a/arch/x86/include/asm/pks.h +++ b/arch/x86/include/asm/pks.h @@ -14,4 +14,18 @@ static inline void pks_write_current(void) { } #endif /* CONFIG_ARCH_ENABLE_SUPERVISOR_PKEYS */ + +#ifdef CONFIG_PKS_TEST + +bool pks_test_callback(void); + +#else /* !CONFIG_PKS_TEST */ + +static inline bool pks_test_callback(void) +{ + return false; +} + +#endif /* CONFIG_PKS_TEST */ + #endif /* _ASM_X86_PKS_H */ diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 6ed91b632eac..bef879943260 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -33,6 +33,7 @@ #include /* kvm_handle_async_pf */ #include /* fixup_vdso_exception() */ #include +#include #define CREATE_TRACE_POINTS #include @@ -1147,16 +1148,25 @@ static void do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, unsigned long address) { - /* - * X86_PF_PK (Protection key exceptions) may occur on kernel addresses - * when PKS (PKeys Supervisor) is enabled. - * - * However, if PKS is not enabled WARN if this exception is seen - * because there are no user pages in the kernel portion of the address - * space. - */ - WARN_ON_ONCE(!cpu_feature_enabled(X86_FEATURE_PKS) && - (hw_error_code & X86_PF_PK)); + if (hw_error_code & X86_PF_PK) { + /* + * X86_PF_PK (Protection key exceptions) may occur on kernel + * addresses when PKS (PKeys Supervisor) is enabled. + * + * However, if PKS is not enabled WARN if this exception is + * seen because there are no user pages in the kernel portion + * of the address space. + */ + WARN_ON_ONCE(!cpu_feature_enabled(X86_FEATURE_PKS)); + + /* + * If a protection key exception occurs it could be because a PKS test + * is running. If so, pks_test_callback() will clear the protection + * mechanism and return true to indicate the fault was handled. + */ + if (pks_test_callback()) + return; + } #ifdef CONFIG_X86_32 /* diff --git a/lib/pks/pks_test.c b/lib/pks/pks_test.c index 159576dda47c..d84ab6e7a09c 100644 --- a/lib/pks/pks_test.c +++ b/lib/pks/pks_test.c @@ -47,6 +47,18 @@ struct pks_test_ctx { char data[64]; }; +/* + * pks_test_callback() is called by the fault handler to indicate it saw a PKey + * fault. + * + * NOTE: The callback is responsible for clearing any condition which would + * cause the fault to re-trigger. + */ +bool pks_test_callback(void) +{ + return false; +} + static void *alloc_test_page(int pkey) { return __vmalloc_node_range(PKS_TEST_MEM_SIZE, 1, VMALLOC_START, VMALLOC_END, -- 2.31.1