Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp3076396pxu; Mon, 19 Oct 2020 03:22:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwZpill2tFU1qhmNC7HYWQlHVtF0CGrl/YEyebZlCuFrUSK46DtcbRAWe0CCUek3+ZfucpR X-Received: by 2002:a17:906:fa84:: with SMTP id lt4mr10161308ejb.61.1603102956792; Mon, 19 Oct 2020 03:22:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603102956; cv=none; d=google.com; s=arc-20160816; b=q6jdYrY1Dm07v/2s4YFRdgAJUG2YNJti7Xh/W2s30roT3L24DENB2JXNEVrFHX5dPY x2XLPpLxLvs5bO0DA0WOMwsiaaI+9+aO6hQmkLFT3lBPJIE1i0S21/WmRdvRh5eSSmFT cyl1QhcjCMIw85EAZi/1CEZu9Gxuu5wESQh1GuS2fR2ISmrNSUlb8FtEy40Yh8y1e0DV 7kTaE6DU98lyqlCKa1KHLrTuROe87l0B393LTZrAVybhTUMfKAkYLardybBXEGT8dM62 GoHH7K2NmYDw75IExV58i5oTMweICGqlCe9N/kDPQ5ARKfZTFZoVQNs1EayiOxPQP3SH kaSw== 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=c8lrLZpm59IDR/lKmKRp/yZzD3iTxUwoZHm4RCbbtf4=; b=mzjt64F3EfW7h9uY9QssAXNdtNDG4PDZt6KiGTw/qaox6sLQHz97dcze94l+oHw97l gIyMNX8LfhTbQMFkCwT2Teu6c5sdqWOOLd500OHvlLW8oqk26a5vV8Nb5EWSaEk3IjLq 9hCa4OiGpADL9uKA9AqPXNkEzaGqLxkawBZulOuNAZoJ+j4CwCWhZ4YXgWyZoF2brXuW SM12sJWRDIoB4FsMfkPuHlaZ6Dq3dnYbX6kpFxZGZRrAe3ZUvIXZxkw3XO35kX2BvZNq zdSPmtvAGVW0qoOr0hwDzHPmmRFL559Q4d7BByE7q3SX6tZ2Fq4H+5lpX/QG7linW6LZ W/CA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="zga4ZuU/"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q26si7379550edr.553.2020.10.19.03.22.14; Mon, 19 Oct 2020 03:22:36 -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; dkim=pass header.i=@kernel.org header.s=default header.b="zga4ZuU/"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727276AbgJSHDq (ORCPT + 99 others); Mon, 19 Oct 2020 03:03:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:60772 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727269AbgJSHDq (ORCPT ); Mon, 19 Oct 2020 03:03:46 -0400 Received: from localhost (83-245-197-237.elisa-laajakaista.fi [83.245.197.237]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 009BF22203; Mon, 19 Oct 2020 07:03:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603091025; bh=ybPoRLwk9gRRObCHrDxH1kw1JCNmskeqI4YCYwMypEA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=zga4ZuU/cKcUHl6owOPPlnthLPPS+62m6z6xAdPtPFnS1IziCk2q7yXRJyHgWwglo Jtj2gdMIxvJDtCYLHMxC5v2iVN7MRzWgggdKixdKgTGU+HurVLAAhWaZZml4k1vpIg nfXoTyRbcai2sAPKOmeFEUl4CBZRw/8ImQ2KB3OY= Date: Mon, 19 Oct 2020 10:03:43 +0300 From: Jarkko Sakkinen To: Jarkko Sakkinen Cc: Dave Hansen , x86@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, Jethro Beekman , Haitao Huang , Chunyang Hui , Jordan Hand , Nathaniel McCallum , Seth Moore , Darren Kenny , Sean Christopherson , Suresh Siddha , akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, asapek@google.com, bp@alien8.de, cedric.xing@intel.com, chenalexchen@google.com, conradparker@google.com, cyhanish@google.com, haitao.huang@intel.com, kai.huang@intel.com, kai.svahn@intel.com, kmoy@google.com, ludloff@google.com, luto@kernel.org, nhorman@redhat.com, puiterwijk@redhat.com, rientjes@google.com, tglx@linutronix.de, yaozhangx@google.com, mikko.ylinen@intel.com Subject: Re: [PATCH v39 13/24] x86/sgx: Add SGX_IOC_ENCLAVE_ADD_PAGES Message-ID: <20201019070343.GA1141388@kapsi.fi> References: <20201003045059.665934-1-jarkko.sakkinen@linux.intel.com> <20201003045059.665934-14-jarkko.sakkinen@linux.intel.com> <1739984e-0010-2031-1561-809a0b6380bb@intel.com> <20201018050311.GK68722@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201018050311.GK68722@linux.intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Oct 18, 2020 at 08:03:11AM +0300, Jarkko Sakkinen wrote: > > > + mmap_read_lock(current->mm); > > > + mutex_lock(&encl->lock); > > > + > > > + /* > > > + * Insert prior to EADD in case of OOM. > > > > I wouldn't say OOM. Maybe: > > > > xa_insert() and EADD can both fail. But xa_insert() is easier > > to unwind so do it first. > > > > > EADD modifies MRENCLAVE, i.e. > > > > What is MRENCLAVE? > > The measurement stored in SECS. I'm wondering with xarray, is it > possible to preallocate entry without inserting anything? > > Then we could get rid of this unwind and also would not need to > take encl->lock in sgx_encl_may_map(). I'm still a bit confused with the unfamiliar Xarray API but I think I got it: 1. xa_insert() with a NULL entry reserves index and more importantly does the memory allocation. 2. xa_cmpxchg() with the enclave page, if EADD and EEXTEND's succceed. 3. xa_release() otherwise. This way sgx_encl_may_map() will never see a stale enclave page when it does the permission check, even if encl->lock is not taken. I mean right now I have to take both xas lock and enclave lock, which is terrible but this will take care of it. I will rewrite the comment to something more reasonable, once I've done this code change. The reason for doing insert first is that, if we get -ENOMEM after successful EADD and EEXTEND's we have a legit microarchitectural state but you cannot rollback a hash (MRENCLAVE), so game is anyway over because your data structures are not in sync. If -ENOMEM comes before, everything is still in sync and we don't have invalidate the enclave. /Jarkko