From patchwork Wed Sep 13 04:06:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haitao Huang X-Patchwork-Id: 138743 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp991706vqx; Wed, 13 Sep 2023 03:45:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZjQQ7QcHMiBIUe3x/mO0ivkg3CZSo/z+b+wq13dtWiQRSmzFMnEsxUatHBoueGVBgJfVg X-Received: by 2002:a05:6a20:3955:b0:13e:b58a:e3e8 with SMTP id r21-20020a056a20395500b0013eb58ae3e8mr2207460pzg.50.1694601903054; Wed, 13 Sep 2023 03:45:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694601903; cv=none; d=google.com; s=arc-20160816; b=RZ/HqhhograT8lX0DsH7fn5BSWuatDMADkZv5pQmD/HdkQho2hGbK53NmXbh8ZfRvN OphLEn1vLdVO//W48t3I3qNHWx2pyFHyda/twaq7UPnOxlsIbFQ/JUdZ77jtpH4hf+Cm pQzuZ8wu3okIFsNiOQR0bVSo5T993vnXTehZ5+4RjJQGuxNoQAatFI4BiFLiu+590TRc 64koxtNNBt054RYjH+OsLdtgsizRyn/XXK6Y3o0m5SESRJFqWFqTQKaeiZuM4LDm5Ife swntmAgM3SEmXEYJBisWKap5uHpmyaCAjHmL6SpigOYWwAfJp7BJDJyS1dzYlU8OVkC7 nioQ== 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=E1fnC8mzkeHkJ/eIEDsfcrEPrxi4OTuTlNyZvO4/V/Q=; fh=j8PE345l5Ydlo3KwK7JeWnjqRgjiq4AteUoOZeOwa0I=; b=aL6X70R74ODPdCOXJgHwLHBEk6Xe3aaXmqwjvDrnzRDZ8VFkN4ttJGWBCy2I8cVXdf S1B3+ZIvGUgZ9Jsevi3BB6hW15DNiah71x716Z91YrqO9xZr7MLY+9LQXgfpxubfyHoX LJEkz6TfVGyC1rwEwrQCEI2N9UXaeI1vtqONe5LwihgsSiCdvsHyMLlx4+PbNDQ1/E4Z dRdsrIIl1HgrGHX+S+E1LcFGb3uiYIzHiKyes7Rq1nr8L4pmE5nAeOMDg3S1ksIlup6V NPBvxwr6VdcPHC2q5pvd/cPvJ/mCIKONHWNwf5YuJGnhNmctpu6J6gblNFemjO3MaQNL 3MWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="V6Z/A85g"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id u13-20020a170903124d00b001b86671b3f1si10339175plh.190.2023.09.13.03.45.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 03:45:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="V6Z/A85g"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 2088F8246943; Tue, 12 Sep 2023 21:09:30 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238445AbjIMEJ2 (ORCPT + 36 others); Wed, 13 Sep 2023 00:09:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238291AbjIMEI6 (ORCPT ); Wed, 13 Sep 2023 00:08:58 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76ADC173F; Tue, 12 Sep 2023 21:08:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1694578133; x=1726114133; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vepZbnyqDblpA7sGXTqcBcK8nK4OglstUykMn9D3KzU=; b=V6Z/A85geapDrS8Vv8upjlkTxTdF0di2X5kjpeRtZQ3n5cg8uzZDmabX MmN00F3Oa8bNs+/AbG7+dEyy7JPGoRgRAolgiWmxzn+N0SCjE8Dy8do2d 1cy5tTLO8fNAsBWSqc9t/6rEmbVloAM5XtEYJ+CEq3mhmwNQ/wf9aHZvD wdp8TJMfolzbOLwGzz4rVPmB/Lafm3btqlGXrx/5ZiIC/kuzdsJ7GZ05J ZEgGHf8nULPMK30S58ucZJ0nppio8flaMUfK2efxJPSdOQ30JCvZWJvQz FqB6E1QIvztttIAnCBh+3ijmDMxYlKWWNOYeKAjcGqDDjl2yShxHhdqC/ w==; X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="357990425" X-IronPort-AV: E=Sophos;i="6.02,142,1688454000"; d="scan'208";a="357990425" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Sep 2023 21:06:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="747155959" X-IronPort-AV: E=Sophos;i="6.02,142,1688454000"; d="scan'208";a="747155959" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by fmsmga007.fm.intel.com with ESMTP; 12 Sep 2023 21:06:47 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com Subject: [PATCH v4 11/18] x86/sgx: store unreclaimable pages in LRU lists Date: Tue, 12 Sep 2023 21:06:28 -0700 Message-Id: <20230913040635.28815-12-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230913040635.28815-1-haitao.huang@linux.intel.com> References: <20230913040635.28815-1-haitao.huang@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Sep 2023 21:09:30 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776918884855569085 X-GMAIL-MSGID: 1776918884855569085 From: Kristen Carlson Accardi When an OOM event occurs, all pages associated with an enclave will need to be freed, including pages that are not currently tracked by the cgroup LRU lists. Add a new "unreclaimable" list to the sgx_epc_lru_lists struct and update the "sgx_record/drop_epc_pages()" functions for adding/removing VA and SECS pages to/from this "unreclaimable" list. Signed-off-by: Kristen Carlson Accardi Signed-off-by: Haitao Huang --- V4: - Updates for patch reordering. - Revised commit messages. - Revised comments for the list. V3: - Removed tracking virtual EPC pages in unreclaimable list as host kernel does not reclaim them. The EPC cgroups implemented later only blocks allocating for a guest if the limit is reached by returning -ENOMEM from sgx_alloc_epc_page() called by virt_epc, and does nothing else. Therefore, no need to track those in LRU lists. --- arch/x86/kernel/cpu/sgx/encl.c | 2 ++ arch/x86/kernel/cpu/sgx/ioctl.c | 1 + arch/x86/kernel/cpu/sgx/main.c | 3 +++ arch/x86/kernel/cpu/sgx/sgx.h | 8 +++++++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index 91f83a5e543d..bf0ac3677ca8 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -748,6 +748,7 @@ void sgx_encl_release(struct kref *ref) xa_destroy(&encl->page_array); if (!encl->secs_child_cnt && encl->secs.epc_page) { + sgx_drop_epc_page(encl->secs.epc_page); sgx_encl_free_epc_page(encl->secs.epc_page); encl->secs.epc_page = NULL; } @@ -756,6 +757,7 @@ void sgx_encl_release(struct kref *ref) va_page = list_first_entry(&encl->va_pages, struct sgx_va_page, list); list_del(&va_page->list); + sgx_drop_epc_page(va_page->epc_page); sgx_encl_free_epc_page(va_page->epc_page); kfree(va_page); } diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c index 95ec20a6992f..8c23bb524674 100644 --- a/arch/x86/kernel/cpu/sgx/ioctl.c +++ b/arch/x86/kernel/cpu/sgx/ioctl.c @@ -48,6 +48,7 @@ void sgx_encl_shrink(struct sgx_encl *encl, struct sgx_va_page *va_page) encl->page_cnt--; if (va_page) { + sgx_drop_epc_page(va_page->epc_page); sgx_encl_free_epc_page(va_page->epc_page); list_del(&va_page->list); kfree(va_page); diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index ed813288af44..f3a3ed894616 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -268,6 +268,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page, goto out; sgx_encl_ewb(encl->secs.epc_page, &secs_backing); + sgx_drop_epc_page(encl->secs.epc_page); sgx_encl_free_epc_page(encl->secs.epc_page); encl->secs.epc_page = NULL; @@ -510,6 +511,8 @@ void sgx_record_epc_page(struct sgx_epc_page *page, unsigned long flags) page->flags |= flags; if (sgx_epc_page_reclaimable(flags)) list_add_tail(&page->list, &sgx_global_lru.reclaimable); + else + list_add_tail(&page->list, &sgx_global_lru.unreclaimable); spin_unlock(&sgx_global_lru.lock); } diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index e06b4aadb6a1..e210af77f0cf 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -150,17 +150,23 @@ static inline void *sgx_get_epc_virt_addr(struct sgx_epc_page *page) } /* - * Tracks EPC pages reclaimable by the reclaimer (ksgxd). + * Contains EPC pages tracked by the reclaimer (ksgxd). */ struct sgx_epc_lru_lists { spinlock_t lock; struct list_head reclaimable; + /* + * Tracks SECS, VA pages,etc., pages only freeable after all its + * dependent reclaimables are freed. + */ + struct list_head unreclaimable; }; static inline void sgx_lru_init(struct sgx_epc_lru_lists *lrus) { spin_lock_init(&lrus->lock); INIT_LIST_HEAD(&lrus->reclaimable); + INIT_LIST_HEAD(&lrus->unreclaimable); } struct sgx_epc_page *__sgx_alloc_epc_page(void);