Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2598433pxb; Thu, 3 Feb 2022 09:50:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJxVmIfU/qGRpNjsxnU6gK8tiYcmDU/w4SJrMT+H2n9nT+IzvIXk6gaOTjBD0yPIDLzFJNpS X-Received: by 2002:a17:90b:1242:: with SMTP id gx2mr15070461pjb.194.1643910640327; Thu, 03 Feb 2022 09:50:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643910640; cv=none; d=google.com; s=arc-20160816; b=SDiRydI77ZemI/wziESZHQH1c7PrsgraCYOjGJLi4xiifZuI71LvDW8MqEPU1O+Egq fThdBkgaMCtur/5fDy4Th+qSxt4i/dbB516nR8IXwMYo3kMgVXYWFWzLPgNxqJADubnI 2ERoswvGh/iWxZv7CgsKsf9dblGns2sWpHXUKj7RmHS5FCsO5VaHTTae6dgaTfwHY5co QfR+gkRlc5X+KRBv4/8fZZLo3z8p38q/oXXe4Yg0121EWBrZa/oaowniSOg8JlTw/A9+ NFYvtc/HSvQ5ov9PnpyIsufS00dJ7iY2iDaekbvEk+heUiyKPmSL4RGIdZJ8CwRXn6V1 oxbw== 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:dkim-signature; bh=2sUU8zMJD+dD1jsGxQp0QIU9zfg8qDGgwLyvYbe21bI=; b=MldBzk/DW3E3BWw5Ek/dw7b43f27yChAQw6Z1ahsdDa7B0ngv69tqH4GM3R0/+W2X5 QUGK91mYrCTi43NQO0wFW8UbXJ606JfHD64zqo6QajIsaNjwpYy0lfscxlSYRXUSKhdB ROdcj6apcW4MCb6en8P4QFBXwbJUpeOe/tQrzBEMhv2l0rUucT7oGKxeMBJi/3Q9EtZc O2Io2B4to23ua03YAMSWbWpXz2DF3GugDRi0wMc9adnwp7D1UkmCwwKbpEBep/eG3wbW ltU62Z0z1ZgQ2QFYropJIRo/DQoclaxhHuH905eDsqRISNlU9PvWGV7DTJqc/M4cpTyf ArnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=L5PJKh4Z; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 22si7487480pje.109.2022.02.03.09.50.28; Thu, 03 Feb 2022 09:50:40 -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=@intel.com header.s=Intel header.b=L5PJKh4Z; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346941AbiBBTlV (ORCPT + 99 others); Wed, 2 Feb 2022 14:41:21 -0500 Received: from mga06.intel.com ([134.134.136.31]:48444 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229889AbiBBTlS (ORCPT ); Wed, 2 Feb 2022 14:41:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643830878; x=1675366878; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=H7G+M9z2UW1EGl0N9AVQnxh/5j9NciiuSvnbaYbcXME=; b=L5PJKh4ZvwMOvfD1VGIT161kzHYpbqdzR7V1oC7sEP3McM136y/I9otK soNcKdht6eEBYXZX0sZaLAiDA8mkX2NNferZw/l/QzsTr88T7NpQ/2XXT jBMuwQnUoD6+J1ESjPHm9WQPTYfA6itGImHeP8IPQtrsUiwcgyBHwNXEm eamzQFOkFYZk7kYtzevOXL4ZOBVae4QI2kRkBo9VJJLvvkkWQuquJ/cPJ exsapFKddeDh0ypDVVd7e8a1aSpMmOjl16B5yvc8znYHH9CWPNuY0o1hV lktUuuCjvq1XJdmibAQmgjVvwNfNQVfDRO0SfnMifMPFpkNJmaW+zuDjl Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10246"; a="308734863" X-IronPort-AV: E=Sophos;i="5.88,337,1635231600"; d="scan'208";a="308734863" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Feb 2022 11:41:18 -0800 X-IronPort-AV: E=Sophos;i="5.88,337,1635231600"; d="scan'208";a="523623678" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Feb 2022 11:41:18 -0800 From: Reinette Chatre To: tony.luck@intel.com, dave.hansen@linux.intel.com, jarkko@kernel.org, tglx@linutronix.de, bp@alien8.de, luto@kernel.org, mingo@redhat.com, linux-sgx@vger.kernel.org, x86@kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH V3] x86/sgx: Add poison handling to reclaimer Date: Wed, 2 Feb 2022 11:41:12 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The SGX reclaimer code lacks page poison handling in its main free path. This can lead to avoidable machine checks if a poisoned page is freed and reallocated instead of being isolated. A troublesome scenario is: 1. Machine check (#MC) occurs (asynchronous, !MF_ACTION_REQUIRED) 2. arch_memory_failure() is eventually called 3. (SGX) page->poison set to 1 4. Page is reclaimed 5. Page added to normal free lists by sgx_reclaim_pages() ^ This is the bug (poison pages should be isolated on the sgx_poison_page_list instead) 6. Page is reallocated by some innocent enclave, a second (synchronous) in-kernel #MC is induced, probably during EADD instruction. ^ This is the fallout from the bug (6) is unfortunate and can be avoided by replacing the open coded enclave page freeing code in the reclaimer with sgx_free_epc_page() to obtain support for poison page handling that includes placing the poisoned page on the correct list. Fixes: d6d261bded8a ("x86/sgx: Add new sgx_epc_page flag bit to mark free pages") Fixes: 992801ae9243 ("x86/sgx: Initial poison handling for dirty and free pages") Reviewed-by: Jarkko Sakkinen Signed-off-by: Reinette Chatre --- Changes since V2: - V2: https://lore.kernel.org/linux-sgx/be5af586f667c7bcb8ef01286ce75675de5d100f.1642630582.git.reinette.chatre@intel.com/ - Add Reviewed-by from Jarkko. - Remove unused variables. Changes since V1: - V1: https://lore.kernel.org/lkml/ef74bd9548df61f77e802e7505affcfb5159c48c.1642545829.git.reinette.chatre@intel.com/ - Complete rewrite of commit message with significant guidance from Dave who provided the summary as well as troublesome scenario. arch/x86/kernel/cpu/sgx/main.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 4b41efc9e367..8e4bc6453d26 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -344,10 +344,8 @@ static void sgx_reclaim_pages(void) { struct sgx_epc_page *chunk[SGX_NR_TO_SCAN]; struct sgx_backing backing[SGX_NR_TO_SCAN]; - struct sgx_epc_section *section; struct sgx_encl_page *encl_page; struct sgx_epc_page *epc_page; - struct sgx_numa_node *node; pgoff_t page_index; int cnt = 0; int ret; @@ -418,13 +416,7 @@ static void sgx_reclaim_pages(void) kref_put(&encl_page->encl->refcount, sgx_encl_release); epc_page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED; - section = &sgx_epc_sections[epc_page->section]; - node = section->node; - - spin_lock(&node->lock); - list_add_tail(&epc_page->list, &node->free_page_list); - spin_unlock(&node->lock); - atomic_long_inc(&sgx_nr_free_pages); + sgx_free_epc_page(epc_page); } } -- 2.25.1