Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1611687pxb; Fri, 27 Aug 2021 12:58:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy6EVyOynIaGnDvwOlMEsLUVouGJRyQczOGcwuJFl+/SDsbRK4C9IvNNdPLYj/iLJe2JLWC X-Received: by 2002:a05:6402:1d2d:: with SMTP id dh13mr6989642edb.281.1630094307081; Fri, 27 Aug 2021 12:58:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630094307; cv=none; d=google.com; s=arc-20160816; b=cogcdpHuzJlZa+pavogtLa27YBk5WtLhO7B3sCgGbnY88zyIe78GhkcDz9S23yNwSo 0f4peD3X9MHZ2xApk6ZteO/zHXdAzK7ObcE7Qwtpj0TxshYnDKysD7Wj/dAP+8GxOWit 4FknZiIUonvY58ir1pNPweC0zmr5+6sdKYynaJHsH9dnQr5N425+C3tmJLTb8/XXjhL1 bQSx08KN2JXjxGxQtIG0R3UXgHxmPUUkP4PBY3Mr26hfeKlBBdtFLkdG+bKNNrj1+Hot Bkga0bV3kJoOhiuGpzMaH8Zmqz9z/aa+vAYRcWWy9Yys/Ze2RB8Av/HpdGruCLMcfEh7 QPBA== 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; bh=uw6OlgALoOZjqQvyeMM+Uh9q0hV1T/oy3iBqtWIrewM=; b=wmsFuGJl2W9xrD/lL4YgXKSbLRGCAsCOr7WJS6x1tLwbLR/mNhVxLBLubP0Ip8KMNY +/7jiuSEbkZLhjO8Mzl3iP4zv4/fKQUXButP2YlWUrLmdscSqK2RgBRuML/fi+mvl7eP 3wBkti4mSMyOR4UV63GShBok3IdkAsSmjm2qcLSRdPWsj/6gMhbvnOfwCgbiCUd7vC8e fxWCma37dj6AZdXNGb3bUq50DowPLzlazDIjaChOAUvJiXulUCv2D8RWaKfHA1n3B3wq ivU8q0b89nYj2GSd60o/AyFiS1rO+YleOi5Rbv/Jru2jEWor8G5TFy77xv0GGrK8TZVr hk6Q== 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qf39si6857791ejc.644.2021.08.27.12.58.03; Fri, 27 Aug 2021 12:58:27 -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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231395AbhH0T4v (ORCPT + 99 others); Fri, 27 Aug 2021 15:56:51 -0400 Received: from mga09.intel.com ([134.134.136.24]:26659 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231287AbhH0T4q (ORCPT ); Fri, 27 Aug 2021 15:56:46 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10089"; a="218024430" X-IronPort-AV: E=Sophos;i="5.84,357,1620716400"; d="scan'208";a="218024430" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Aug 2021 12:55:56 -0700 X-IronPort-AV: E=Sophos;i="5.84,357,1620716400"; d="scan'208";a="528422081" Received: from agluck-desk2.sc.intel.com ([10.3.52.146]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Aug 2021 12:55:56 -0700 From: Tony Luck To: Sean Christopherson , Jarkko Sakkinen , Dave Hansen Cc: Cathy Zhang , x86@kernel.org, linux-kernel@vger.kernel.org, Tony Luck Subject: [PATCH v4 4/6] x86/sgx: Add SGX infrastructure to recover from poison Date: Fri, 27 Aug 2021 12:55:41 -0700 Message-Id: <20210827195543.1667168-5-tony.luck@intel.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210827195543.1667168-1-tony.luck@intel.com> References: <20210728204653.1509010-1-tony.luck@intel.com> <20210827195543.1667168-1-tony.luck@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Provide a recovery function sgx_memory_failure(). If the poison was consumed synchronously then send a SIGBUS. Note that the virtual address of the access is not included with the SIGBUS as is the case for poison outside of SGX enclaves. This doesn't matter as addresses of code/data inside an enclave is of little to no use to code executing outside the (now dead) enclave. Poison found in a free page results in the page being moved from the free list to the poison page list. Signed-off-by: Tony Luck --- arch/x86/kernel/cpu/sgx/main.c | 77 ++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index c08df4e35ff0..d9fe08f68d13 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -682,6 +682,83 @@ bool sgx_is_epc_page(u64 paddr) } EXPORT_SYMBOL_GPL(sgx_is_epc_page); +static struct sgx_epc_page *sgx_paddr_to_page(u64 paddr) +{ + struct sgx_epc_section *section; + + section = xa_load(&epc_page_ranges, paddr); + if (!section) + return NULL; + + return §ion->pages[PFN_DOWN(paddr - section->phys_addr)]; +} + +/* + * Called in process context to handle a hardware reported + * error in an SGX EPC page. + * If the MF_ACTION_REQUIRED bit is set in flags, then the + * context is the task that consumed the poison data. Otherwise + * this is called from a kernel thread unrelated to the page. + */ +int sgx_memory_failure(unsigned long pfn, int flags) +{ + struct sgx_epc_page *page = sgx_paddr_to_page(pfn << PAGE_SHIFT); + struct sgx_epc_section *section; + struct sgx_numa_node *node; + + /* + * mm/memory-failure.c calls this routine for all errors + * where there isn't a "struct page" for the address. But that + * includes other address ranges besides SGX. + */ + if (!page) + return -ENXIO; + + /* + * If poison was consumed synchronously. Send a SIGBUS to + * the task. Hardware has already exited the SGX enclave and + * will not allow re-entry to an enclave that has a memory + * error. The signal may help the task understand why the + * enclave is broken. + */ + if (flags & MF_ACTION_REQUIRED) + force_sig(SIGBUS); + + section = &sgx_epc_sections[page->section]; + node = section->node; + + spin_lock(&node->lock); + + /* Already poisoned? Nothing more to do */ + if (page->poison) + goto out; + + page->poison = 1; + + /* + * If there is no owner, then the page is on a free list. + * Move it to the poison page list. + */ + if (!page->private) { + list_del(&page->list); + list_add(&page->list, &sgx_poison_page_list); + goto out; + } + + /* + * TBD: Add additional plumbing to enable pre-emptive + * action for asynchronous poison notification. Until + * then just hope that the poison: + * a) is not accessed - sgx_free_epc_page() will deal with it + * when the user gives it back + * b) results in a recoverable machine check rather than + * a fatal one + */ +out: + spin_unlock(&node->lock); + return 0; +} + /** * A section metric is concatenated in a way that @low bits 12-31 define the * bits 12-31 of the metric and @high bits 0-19 define the bits 32-51 of the -- 2.29.2