Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3151022imu; Mon, 17 Dec 2018 14:20:43 -0800 (PST) X-Google-Smtp-Source: AFSGD/VHvJoGl8gR7E/zZX34z0BiAbRSmqpj9LTBt8Yj1mygqx8+Emys0lWyNN5vcQOIOypku5Qw X-Received: by 2002:a63:6006:: with SMTP id u6mr13642816pgb.176.1545085243089; Mon, 17 Dec 2018 14:20:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545085243; cv=none; d=google.com; s=arc-20160816; b=NGcqyhW37c7QNWjm8CIStKVmFRisA+O3jN45SbUVTGCMkrgaEH3jdwwicWySIj3RRc i4pGu6EdXkLu8aZTkqvA7Z9n7iMTRvXXl04S6IruFCKE8iTswmzXuu3LiTQuIxVAmyHH VGYQGtCRlaolNbl9pAANK89UCLt/WixNxAuVgHXb+y6gtq/FhhkOFkVUH5XCBoxlXGkS p8WN3wXQnO1n2m4X3w4l60hrqTPbCqS8NwgO3IW3ARcgp+TtHMIbqWIRN1VJ6EflQ6fa T6SRD6yAgd2F2zhcK/px8RokM+c9JDI8GlK7iR5iyE4xW28iHAos1ZEsp3QhXcUPZtEO kQVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=yCROxeAtqY9oRYtTDPVQrTDStOgGuWs9GNgIW83bX+U=; b=ab0/kNTBujBd8Aqc1R9iwQ8EYbGt8RByulcyMzPOX61fUsV6erb3x1W2XxNDc7jdOT WYtjlRCvzClaVOn+bKEvMnqtNXGTIyupuhpP/dwrK3OE/cjoiW/c0rN3z0DI6jKeSOmd 3ZE4ntjz+okuNRzTwBFFC3pd1Scjo+4J6p7EB9m22jBsRr+8wLK2OxBTD+tr59/9Wics 41lR/ZI2ZBryMv7b1Hji2/9DLGHOXuwfBAr1/3v6oEDyRjyQk8bTN9osgbijmeGCC3AM 0/ObeDoD9Ngq1iPDZxLr5oIA219ZR9yL/uMZLbERx9VDdRjABS6ArT0Ghc45izIQM1hu 8SBA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o14si11417805pgi.134.2018.12.17.14.20.28; Mon, 17 Dec 2018 14:20:43 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728108AbeLQSJ6 (ORCPT + 99 others); Mon, 17 Dec 2018 13:09:58 -0500 Received: from mga12.intel.com ([192.55.52.136]:39979 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726574AbeLQSJ6 (ORCPT ); Mon, 17 Dec 2018 13:09:58 -0500 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Dec 2018 10:09:57 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,366,1539673200"; d="scan'208";a="102212968" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.154]) by orsmga008.jf.intel.com with ESMTP; 17 Dec 2018 10:09:57 -0800 Date: Mon, 17 Dec 2018 10:09:57 -0800 From: Sean Christopherson To: Jarkko Sakkinen Cc: "Dr. Greg" , Andy Lutomirski , Andy Lutomirski , X86 ML , Platform Driver , linux-sgx@vger.kernel.org, Dave Hansen , nhorman@redhat.com, npmccallum@redhat.com, "Ayoun, Serge" , shay.katz-zamir@intel.com, haitao.huang@linux.intel.com, Andy Shevchenko , Thomas Gleixner , "Svahn, Kai" , mark.shanahan@intel.com, Suresh Siddha , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Darren Hart , Andy Shevchenko , LKML , jethro@fortanix.com Subject: Re: [PATCH v17 18/23] platform/x86: Intel SGX driver Message-ID: <20181217180957.GC12491@linux.intel.com> References: <20181128192228.GC9023@linux.intel.com> <20181210104908.GA23132@wind.enjellic.com> <20181212180036.GC6333@linux.intel.com> <20181214235917.GA14049@wind.enjellic.com> <20181215000627.GA5799@linux.intel.com> <20181217132859.GA31936@linux.intel.com> <20181217133928.GA32706@linux.intel.com> <20181217140811.GA4601@linux.intel.com> <20181217173106.GB12491@linux.intel.com> <20181217174935.GA12617@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181217174935.GA12617@linux.intel.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 17, 2018 at 07:49:35PM +0200, Jarkko Sakkinen wrote: > On Mon, Dec 17, 2018 at 09:31:06AM -0800, Sean Christopherson wrote: > > This doesn't work as-is. sgx_encl_release() needs to use sgx_free_page() > > and not __sgx_free_page() so that we get a WARN() if the page can't be > > freed. sgx_invalidate() needs to use __sgx_free_page() as freeing a page > > can fail due to running concurrently with reclaim. I'll play around with > > the code a bit, there's probably a fairly clean way to share code between > > the two flows. > > Hmm... but why issue a warning in that case? It should be legit > behaviour. No, EREMOVE should never fail if the enclave is being released, i.e. all references to the enclave are gone. And failure during sgx_encl_release() means we leaked an EPC page, which warrants a WARN. The only legitimate reason __sgx_free_page() can fail in sgx_invalidate() is because a page might be in the process of being reclaimed. We could theoretically WARN on EREMOVE failure in that case, but it'd make the code a little fragile and it's not "fatal" in the sense that we get a second chance to free the page during sgx_encl_release(). And unless I missed something, using sgx_invalidate() means were' leaking all sgx_encl_page structs as well as the radix tree entries. > > sgx_encl_release_worker() calls do_unmap() without checking the validity > > of the page tables[1]. As is, the code doesn't even guarantee mm_struct > > itself is valid. > > > > The easiest fix I can think of is to add a SGX_ENCL_MM_RELEASED flag > > that is set along with SGX_ENCL_DEAD in sgx_mmu_notifier_release(), and > > only call do_unmap() if SGX_ENCL_MM_RELEASED is false. Note that this > > means we cant unregister the mmu_notifier until after do_unmap(), but > > that's true no matter what since we're relying on the mmu_notifier to > > hold a reference to mm_struct. Patch attached. > > OK, the fix change makes sense but I'm thinking that would it be a > better idea just to set mm NULL and check that instead? That makes sense.