Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1352176rdb; Mon, 19 Feb 2024 12:26:30 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV89R+MWrKpR+jPxSrIh6/xQzg+LW6Yd5u6mAPmvomiFGx8diNmjBLPh+lZeuJshoc0eqzyDqcT+iVrwvGLhQ4CJk/4+CloEMy4cqh67w== X-Google-Smtp-Source: AGHT+IHCrv/kqeCbWjUDQMvW2V/QOLW7BabKe/DT0BIyK/B5ppsTyFkGyYQS1N5/WDihKrSXJJLg X-Received: by 2002:a17:906:280c:b0:a3c:2f68:54a9 with SMTP id r12-20020a170906280c00b00a3c2f6854a9mr12768996ejc.3.1708374390068; Mon, 19 Feb 2024 12:26:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708374390; cv=pass; d=google.com; s=arc-20160816; b=DGGpM3OCib90IWpq5wVIonBeBL59wxyv/1qBBrbA2anNV0fBQsn1Ol+/DPY0dO56t+ ffa/hHeliT24nS6C2FRqoc0RCEFWdQ8M7vn834TVyZLU0Jr4clZSD/YIjtei3pMgBp64 i9lTUy+sfYn6fjJgFxuIPuqIONaWbwX25KbRX2x5WaytY0KXboyk6468VsSVUMvAESvG 34DQe2Ak/xiy6GzzaflAizxMV25Yg17xzsmnm1J+8lZpG2TfQvI383DU7PV+i3SbPel0 CL0GBg3RgKkwML/CnSvpibixsV6XvRpcL0fNuPXmflxm78vwtyFLJR32UZSQ0aKmlP6t zUFw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:dkim-signature; bh=UyiOY9W+aPSFc9rCsHc16+pvwgBeVh7V+zPlioFFjTs=; fh=bTBX2mLek7OrEOgJJwQ9/XdiT1XrYyUER0SvhGnl2pc=; b=JFx1Rb/O+F8aS73g9NA0BlMSdJ0YUfSfZAHd1DjT1kMOhSS6pbUfnd2yd0YyasrfIq al5+Knuu1os0gBv7BqxkE3LnovDBw9bf+KyZytYywHu1G3MRI8auJiWXXTPm5XgMLQBe vwT/49jgiEzVz+foo1KHRxkxnnLu9SojNSy6XSmsapOBFoAtZw0AV8z/vysexMljyciM oJdF/lzHAtG0XHjQhRakPLuz7ueuizgM08cXpqfxir9lD1vX+meWMf4Sx7epRwq7w8us SSxV0QgVNHIK6m+qLhWrxNnguFuQDBASrtrcUw37weB1JOunfirF2cmS3VpgyF91gCtR bciw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MjTtUSkY; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-71954-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71954-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id ga36-20020a1709070c2400b00a3ec7482fa7si678967ejc.915.2024.02.19.12.26.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 12:26:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-71954-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MjTtUSkY; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-71954-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71954-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A74D71F2559F for ; Mon, 19 Feb 2024 20:20:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4DEBE535A4; Mon, 19 Feb 2024 20:20:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MjTtUSkY" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F1EC374DD; Mon, 19 Feb 2024 20:20:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708374018; cv=none; b=B1RHWF9x0nKBwE9cLNSWHRfvzyIscQPWLWtXnV+cdoWXG/y1sUZOMJrBwmU1i8VKn0P7sjUYUvjI0LwCepCbwESZ6hU7hqoHeVEyjjJSRca2tqtpRp3k8pBqVxsy5UMae2W3C/e2gdNHOSjGSq4hLfq8EJP8LtXjlDZUtoBDKSQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708374018; c=relaxed/simple; bh=gHOp1QBYiHr0I6TnykCbbaoLqfJQnuRcGvEdxF39TMQ=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=WJkWmlL0WZ3Q86dmGhzkZmLZOk3mia4reHSIQSjNeNPeKcBmkhv8Z787f6iSDaNWN4mSxsIkebczasfDYfD4P1HbZ+W444t5bWTuNGnmKxXKgf8XF2ClAWGUzfU2XhzaVE0gsQ/VX2TAwOOU8PBzjQQyQhdRGsMgrPTZICkQjrg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MjTtUSkY; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55613C433F1; Mon, 19 Feb 2024 20:20:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708374017; bh=gHOp1QBYiHr0I6TnykCbbaoLqfJQnuRcGvEdxF39TMQ=; h=Date:Cc:Subject:From:To:References:In-Reply-To:From; b=MjTtUSkYwMCnetFVEyU3rTIW7slfhEkNYlnC5PSLvNOkTBn7jVZm5yq4Ko2MmODZJ XmyXQTTl+GD42Oeqq+4x9Vc9QgphUq3WNQd7Rl/0PWY9StAS7vaaMGRZiPQPvO88A0 LLduiZBeZlB5rFTsylvnz5ZyithTDB/e2wHsfagzBOnJrXsks6lSyw8j+PIt7JQ0T+ CBSTnEjzyavFGMIQpfdsgF5h2CcYbsfWZOeoAx9uFjq6RaZOkYXVFGVtBBOaRMFS6V pqSKP32SRiickys93tIvJgohpV/wE0x1i/1Uhw4JsQptKbxJOV8tkqfPYe3MqRqzFy QwNC4OwnXLClA== Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 19 Feb 2024 20:20:11 +0000 Message-Id: Cc: , , , , , , , Subject: Re: [PATCH v9 10/15] x86/sgx: Add EPC reclamation in cgroup try_charge() From: "Jarkko Sakkinen" To: "Haitao Huang" , , , , , , , , , , , , , X-Mailer: aerc 0.15.2 References: <20240205210638.157741-1-haitao.huang@linux.intel.com> <20240205210638.157741-11-haitao.huang@linux.intel.com> In-Reply-To: On Mon Feb 19, 2024 at 3:12 PM UTC, Haitao Huang wrote: > On Tue, 13 Feb 2024 19:52:25 -0600, Jarkko Sakkinen = =20 > wrote: > > > On Tue Feb 13, 2024 at 1:15 AM EET, Haitao Huang wrote: > >> Hi Jarkko > >> > >> On Mon, 12 Feb 2024 13:55:46 -0600, Jarkko Sakkinen > >> wrote: > >> > >> > On Mon Feb 5, 2024 at 11:06 PM EET, Haitao Huang wrote: > >> >> From: Kristen Carlson Accardi > >> >> > >> >> When the EPC usage of a cgroup is near its limit, the cgroup needs = to > >> >> reclaim pages used in the same cgroup to make room for new =20 > >> allocations. > >> >> This is analogous to the behavior that the global reclaimer is =20 > >> triggered > >> >> when the global usage is close to total available EPC. > >> >> > >> >> Add a Boolean parameter for sgx_epc_cgroup_try_charge() to indicate > >> >> whether synchronous reclaim is allowed or not. And trigger the > >> >> synchronous/asynchronous reclamation flow accordingly. > >> >> > >> >> Note at this point, all reclaimable EPC pages are still tracked in = =20 > >> the > >> >> global LRU and per-cgroup LRUs are empty. So no per-cgroup =20 > >> reclamation > >> >> is activated yet. > >> >> > >> >> Co-developed-by: Sean Christopherson =20 > >> > >> >> Signed-off-by: Sean Christopherson > >> >> Signed-off-by: Kristen Carlson Accardi > >> >> Co-developed-by: Haitao Huang > >> >> Signed-off-by: Haitao Huang > >> >> --- > >> >> V7: > >> >> - Split this out from the big patch, #10 in V6. (Dave, Kai) > >> >> --- > >> >> arch/x86/kernel/cpu/sgx/epc_cgroup.c | 26 ++++++++++++++++++++++++= -- > >> >> arch/x86/kernel/cpu/sgx/epc_cgroup.h | 4 ++-- > >> >> arch/x86/kernel/cpu/sgx/main.c | 2 +- > >> >> 3 files changed, 27 insertions(+), 5 deletions(-) > >> >> > >> >> diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c > >> >> b/arch/x86/kernel/cpu/sgx/epc_cgroup.c > >> >> index d399fda2b55e..abf74fdb12b4 100644 > >> >> --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c > >> >> +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c > >> >> @@ -184,13 +184,35 @@ static void > >> >> sgx_epc_cgroup_reclaim_work_func(struct work_struct *work) > >> >> /** > >> >> * sgx_epc_cgroup_try_charge() - try to charge cgroup for a single= =20 > >> EPC > >> >> page > >> >> * @epc_cg: The EPC cgroup to be charged for the page. > >> >> + * @reclaim: Whether or not synchronous reclaim is allowed > >> >> * Return: > >> >> * * %0 - If successfully charged. > >> >> * * -errno - for failures. > >> >> */ > >> >> -int sgx_epc_cgroup_try_charge(struct sgx_epc_cgroup *epc_cg) > >> >> +int sgx_epc_cgroup_try_charge(struct sgx_epc_cgroup *epc_cg, bool > >> >> reclaim) > >> >> { > >> >> - return misc_cg_try_charge(MISC_CG_RES_SGX_EPC, epc_cg->cg, =20 > >> PAGE_SIZE); > >> >> + for (;;) { > >> >> + if (!misc_cg_try_charge(MISC_CG_RES_SGX_EPC, epc_cg->cg, > >> >> + PAGE_SIZE)) > >> >> + break; > >> >> + > >> >> + if (sgx_epc_cgroup_lru_empty(epc_cg->cg)) > >> >> + return -ENOMEM; > >> >> + + if (signal_pending(current)) > >> >> + return -ERESTARTSYS; > >> >> + > >> >> + if (!reclaim) { > >> >> + queue_work(sgx_epc_cg_wq, &epc_cg->reclaim_work); > >> >> + return -EBUSY; > >> >> + } > >> >> + > >> >> + if (!sgx_epc_cgroup_reclaim_pages(epc_cg->cg, false)) > >> >> + /* All pages were too young to reclaim, try again a little late= r =20 > >> */ > >> >> + schedule(); > >> > > >> > This will be total pain to backtrack after a while when something > >> > needs to be changed so there definitely should be inline comments > >> > addressing each branch condition. > >> > > >> > I'd rethink this as: > >> > > >> > 1. Create static __sgx_epc_cgroup_try_charge() for addressing single > >> > iteration with the new "reclaim" parameter. > >> > 2. Add a new sgx_epc_group_try_charge_reclaim() function. > >> > > >> > There's a bit of redundancy with sgx_epc_cgroup_try_charge() and > >> > sgx_epc_cgroup_try_charge_reclaim() because both have almost the > >> > same loop calling internal __sgx_epc_cgroup_try_charge() with > >> > different parameters. That is totally acceptable. > >> > > >> > Please also add my suggested-by. > >> > > >> > BR, Jarkko > >> > > >> > BR, Jarkko > >> > > >> For #2: > >> The only caller of this function, sgx_alloc_epc_page(), has the same > >> boolean which is passed into this this function. > > > > I know. This would be good opportunity to fix that up. Large patch > > sets should try to make the space for its feature best possible and > > thus also clean up the code base overally. > > > >> If we separate it into sgx_epc_cgroup_try_charge() and > >> sgx_epc_cgroup_try_charge_reclaim(), then the caller has to have the > >> if/else branches. So separation here seems not help? > > > > Of course it does. It makes the code in that location self-documenting > > and easier to remember what it does. > > > > BR, Jarkko > > > > Please let me know if this aligns with your suggestion. > > > static int ___sgx_epc_cgroup_try_charge(struct sgx_epc_cgroup *epc_cg) > { > if (!misc_cg_try_charge(MISC_CG_RES_SGX_EPC, epc_cg->cg, > PAGE_SIZE)) > return 0; > > if (sgx_epc_cgroup_lru_empty(epc_cg->cg)) > return -ENOMEM; > > if (signal_pending(current)) > return -ERESTARTSYS; > > return -EBUSY; > } > > /** > * sgx_epc_cgroup_try_charge() - try to charge cgroup for a single page > * @epc_cg: The EPC cgroup to be charged for the page. > * > * Try to reclaim pages in the background if the group reaches its limit= =20 > and > * there are reclaimable pages in the group. > * Return: > * * %0 - If successfully charged. > * * -errno - for failures. > */ > int sgx_epc_cgroup_try_charge(struct sgx_epc_cgroup *epc_cg) > { > int ret =3D ___sgx_epc_cgroup_try_charge(epc_cg); > > if (ret =3D=3D -EBUSY) > queue_work(sgx_epc_cg_wq, &epc_cg->reclaim_work); > > return ret; > } > > /** > * sgx_epc_cgroup_try_charge_reclaim() - try to charge cgroup for a sing= le =20 > page > * @epc_cg: The EPC cgroup to be charged for the page. > * > * Try to reclaim pages directly if the group reaches its limit and ther= e =20 > are > * reclaimable pages in the group. > * Return: > * * %0 - If successfully charged. > * * -errno - for failures. > */ > int sgx_epc_cgroup_try_charge_reclaim(struct sgx_epc_cgroup *epc_cg) > { > int ret; > > for (;;) { > ret =3D ___sgx_epc_cgroup_try_charge(epc_cg); > if (ret !=3D -EBUSY) > return ret; > > if (!sgx_epc_cgroup_reclaim_pages(epc_cg->cg, current->m= m)) > /* All pages were too young to reclaim, try agai= n =20 > a little later */ > schedule(); > } > > return 0; > } > > It is a little more involved to remove the boolean for =20 > sgx_alloc_epc_page() and its callers like sgx_encl_grow(), =20 > sgx_alloc_va_page(). I'll send a separate patch for comments. With quick look, it is towards right direction for sure. BR, Jarkko