Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp352969pxb; Wed, 20 Jan 2021 08:30:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJxU/YsjbCXqj1DTV/x5aRqNSylhoXx8gsHK0PXeF4+z/mNFJMe2zImG/Rb3mpnyMACJBHe3 X-Received: by 2002:a50:9ee9:: with SMTP id a96mr7922461edf.343.1611160256734; Wed, 20 Jan 2021 08:30:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611160256; cv=none; d=google.com; s=arc-20160816; b=U5J5JeriYfIDLO7QOklwHXtliJUhbvAoiGwNqZuTyizaP5QmYRr78T8deAtu2Gl1/S J3lNxmvTpsCa49u7p+/EBhCDTYd8ajNErVfDp3F+MJHttC3bq1sKsXLU/ktM8qT0REUZ JGHgAYH2b4jIcI+dbGf5cD0UzOirD5YE7eh4HEKKcnrzv1hMPb58N8hqtFhv0yaVRpVs 5DMZuH3A3ekoBDYqSETG1Dt5iGVjHdgiJQSViQwEhAu1pilR8P0ViAg+8lCfpj4B+hSt iWS9RJXNYyj9ENgWeEyX93oEh6xNzTZ1TL8PeMz5ymhx9QOEoDqKMQZUZWhNI9BAWTGk UOkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=6t8lIgtuar9+s884zrcCGunoGFI/cGmR+eRx6cwTKyA=; b=LlcNaLyw71l63njDDg+j94UmkQnNF2qx2wWw5s3clzKQNaWAYG6osuxrjhZq0sXK4T YbxPeab9OTlo5l67e4evHYkq2l3LTFPjycKGn6g+H+yFpfAfCdC7Z1qYjFRoP5Awl8SF uoFLrVet7HMsvBQB/RV2gm5rOhzPQc13O99s27lnLNThLHBwMOJTYI84mKLzMyORW9xV kASx+xMsk4TEeh5oU8H92Lj0tcxLMGB/eFhthC/UXDNahUfBuTvpK9iPH7IwW6d6JaM+ +kJwSmZc4OS59ZPi7/jw1VwrpFFO4/Iqi+Lqn80mdCCIsWjw3uyU+VC5g4wATU8XRTZW FHVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="MPP/+wUK"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z22si838811ejo.53.2021.01.20.08.30.32; Wed, 20 Jan 2021 08:30:56 -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=@google.com header.s=20161025 header.b="MPP/+wUK"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404044AbhATQ25 (ORCPT + 99 others); Wed, 20 Jan 2021 11:28:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391470AbhATQ1v (ORCPT ); Wed, 20 Jan 2021 11:27:51 -0500 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0EC0C061757 for ; Wed, 20 Jan 2021 08:27:10 -0800 (PST) Received: by mail-pg1-x533.google.com with SMTP id p18so15499765pgm.11 for ; Wed, 20 Jan 2021 08:27:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=6t8lIgtuar9+s884zrcCGunoGFI/cGmR+eRx6cwTKyA=; b=MPP/+wUKoze/haoerTH7BHaM2dJr1jwyfgdp5zkk4hD8jlpk+fFbidiCfNyrlaorXM lzPae+2FklwoExVYZm2xucxpEmzh82XB8+m287adQp7CXeiHIyQCC75MxVG+bOUZPYad 88f9lAIcVAvkDkdH7ASQvPRCV0nlAVb9JvwsSuNdBnMVDWlpbRi4iPgv4i2kSJkQsCmT lxVuhe9NNDZacJflG22XQNio9h1nXbkJzEeiF1l5ccX/MJ6hKpAin8oqqqGQ8qx5fc3/ gDArMHrdDWjzpbNmJtybAghkjVJXHATb/iVIMkx70NSQncRWqdkAIbDIwdfn4CPtKZyS UC9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=6t8lIgtuar9+s884zrcCGunoGFI/cGmR+eRx6cwTKyA=; b=g59Xw2A8EP8I3WVUzaO9ZU1Xtuw8rwgO2+4hjIsk2eICU0IMMT28Tifqx8kEPkaI92 6wazdiJwWdgFX6Dv5lQUg7sg87rMboD5e2yZbExP0pNkps8kQ1Qxdxs5YBHN73oDuNtX Xc+CWaMfaiLlwz8uHcrfFtOLViZiJJBFsus7C+gDgV0V5jABa9PxcQxb2jCw2S0WrKE9 q+il3RxcfYI4LlvXzvkx19IHp13X6B0hYPDbFtEDRxUOdw5zoxe64YAjSiozWrhwOalj OA0k/J510vJnPIemu+G8b00lTOBitCdr3H0Ewkae6mgtEhDzHqt/J03+9i/KyIDcTZGk j2FA== X-Gm-Message-State: AOAM5330PK8zjbV4ILNrhb7gUFjjP2eRMHkhkg8TKHDF0Dlk04S7eohl NO987eXgViN9tSUpVyBxAFlBsg== X-Received: by 2002:a65:628a:: with SMTP id f10mr9991712pgv.380.1611160030001; Wed, 20 Jan 2021 08:27:10 -0800 (PST) Received: from google.com ([2620:15c:f:10:1ea0:b8ff:fe73:50f5]) by smtp.gmail.com with ESMTPSA id k141sm530954pfd.9.2021.01.20.08.27.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jan 2021 08:27:09 -0800 (PST) Date: Wed, 20 Jan 2021 08:27:02 -0800 From: Sean Christopherson To: Tianjia Zhang Cc: Jarkko Sakkinen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, Jia Zhang Subject: Re: [PATCH v2] x86/sgx: Fix free_cnt counting logic in epc section Message-ID: References: <20210120035320.19709-1-tianjia.zhang@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210120035320.19709-1-tianjia.zhang@linux.alibaba.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 20, 2021, Tianjia Zhang wrote: > Increase `section->free_cnt` in sgx_sanitize_section() is more > reasonable, which is called in ksgxd kernel thread, instead of > assigning it to epc section pages number at initialization. > Although this is unlikely to fail, these pages cannot be > allocated after initialization, and which need to be reset > by ksgxd. > > At the same time, taking section->lock could be moved inside > the !ret flow so that EREMOVE is done without holding the lock. > it's theoretically possible that ksgxd hasn't finished > sanitizing the EPC when userspace starts creating enclaves. Moving the lock should be in a separate patch, they are clearly two different functional changes. > Reported-by: Jia Zhang > Suggested-by: Sean Christopherson Moving lock was suggested by me, the original patch was not. > Reviewed-by: Sean Christopherson > Signed-off-by: Tianjia Zhang > --- > arch/x86/kernel/cpu/sgx/main.c | 13 +++++++------ > 1 file changed, 7 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c > index c519fc5f6948..34a72a147983 100644 > --- a/arch/x86/kernel/cpu/sgx/main.c > +++ b/arch/x86/kernel/cpu/sgx/main.c > @@ -41,16 +41,18 @@ static void sgx_sanitize_section(struct sgx_epc_section *section) > if (kthread_should_stop()) > return; > > - /* needed for access to ->page_list: */ > - spin_lock(§ion->lock); > - > page = list_first_entry(§ion->init_laundry_list, > struct sgx_epc_page, list); > > ret = __eremove(sgx_get_epc_virt_addr(page)); > - if (!ret) > + > + /* needed for access to ->page_list: */ > + spin_lock(§ion->lock); This can actually be even more precise, as the lock doesn't need to be taken if __eremove() fails. The lock protects section->page_list, not page->list. At that point, the comment about why the lock is needed can probably be dropped? > + > + if (!ret) { > list_move(&page->list, §ion->page_list); > - else > + section->free_cnt += 1; Belated feedback, this can use "++". > + } else Need curly braces here. E.g. when all is said and done, this code can be: if (!ret) { spin_lock(§ion->lock); list_move(&page->list, §ion->page_list); section->free_cnt++; spin_unlock(§ion->lock); } else { list_move_tail(&page->list, &dirty); } > list_move_tail(&page->list, &dirty); > > spin_unlock(§ion->lock); > @@ -646,7 +648,6 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size, > list_add_tail(§ion->pages[i].list, §ion->init_laundry_list); > } > > - section->free_cnt = nr_pages; > return true; > } > > -- > 2.19.1.3.ge56e4f7 >