Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2764018imm; Tue, 4 Sep 2018 09:37:21 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbGnTK1QZyAmPv37pn41vU/sT7LggNncz3V994yv9vab3LiU+UiPTgbJ8AgLbHvU7LBiDhd X-Received: by 2002:a17:902:47c2:: with SMTP id d2-v6mr8217401plh.317.1536079041507; Tue, 04 Sep 2018 09:37:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536079041; cv=none; d=google.com; s=arc-20160816; b=gBZJHA9HiAXS6nNNndUZlJ0Sc9ay8oAqYGqvVWX+zSuJy2H6DL/cwM4kHLLPnxLfC+ 8L4vv9mGs6G5+8qgGDx98aOB/yhf19cF+DHw3GKbuZu68GjBHx0TmhxtUlM/jM8D0DWF F0PYy9dxDbCO37P6SpsuRUI9Dzi1yCm7eUQA5McLeEGcAs9dKSOuUQS5/6Fh26vWyrg6 x57MgUtB1jmLFXASUXlLb4X4wAT32SQD4l9fGa+jTktdhKNVxTrGJF/ONZ6gnomYOl8r iAaJtH6EW6y9wZ9Y2x1LsCxE7uIgQnUpe8kF260t+osKBe0K2RBZWxfYxOyYXGXCg7al 8uSg== 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:arc-authentication-results; bh=0EmEH62SwBbH2cJWXvaEbzXKlaHRpbJUQLzlZz8acaM=; b=oO4BlzhzbuVAjgbl6JmzVkF+xTGYFcbePzJllCZc1dtfyVAbEwnRXjnTEz95/R1EOu FNBr3uKwYMmcpnV7YxVyE6z6bH6CHtx1mRisqUpTmpuJQbuocrV4pbKm0DYrQhkstEYj l/sZtoY8llbZVqyJo9b1l1sY43ZPpDSau8gzrQe0WeYUybm5f7fBz4ttTNXthsvxM8wq k8QJQUIGW8PiTgp9ME0RssvjBBWFdR9B1aeG3ZA3QGzYw+vTEE4IWr4T32QZPeLoZedW z2mRzh0gsnQez1Y8jPDaH52Gb9iH7dhPxmAcJ8njQ5GtOyjxpLOxWjZgw4djrvslyUIn KPXA== 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 e30-v6si21727711pga.272.2018.09.04.09.37.06; Tue, 04 Sep 2018 09:37:21 -0700 (PDT) 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 S1727272AbeIDVBw (ORCPT + 99 others); Tue, 4 Sep 2018 17:01:52 -0400 Received: from mga02.intel.com ([134.134.136.20]:64460 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726200AbeIDVBw (ORCPT ); Tue, 4 Sep 2018 17:01:52 -0400 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Sep 2018 09:35:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,329,1531810800"; d="scan'208";a="260624934" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.20]) by fmsmga006.fm.intel.com with ESMTP; 04 Sep 2018 09:35:46 -0700 Date: Tue, 4 Sep 2018 09:35:46 -0700 From: Sean Christopherson To: Jarkko Sakkinen Cc: "Huang, Kai" , "platform-driver-x86@vger.kernel.org" , "x86@kernel.org" , "nhorman@redhat.com" , "linux-kernel@vger.kernel.org" , "tglx@linutronix.de" , "suresh.b.siddha@intel.com" , "Ayoun, Serge" , "hpa@zytor.com" , "npmccallum@redhat.com" , "mingo@redhat.com" , "linux-sgx@vger.kernel.org" , "Hansen, Dave" Subject: Re: [PATCH v13 10/13] x86/sgx: Add sgx_einit() for initializing enclaves Message-ID: <20180904163546.GA5421@linux.intel.com> References: <20180827185507.17087-11-jarkko.sakkinen@linux.intel.com> <1535406078.3416.9.camel@intel.com> <20180828070129.GA5301@linux.intel.com> <105F7BF4D0229846AF094488D65A09893541037C@PGSMSX112.gar.corp.intel.com> <20180831121645.GA18075@linux.intel.com> <20180831181509.GB21555@linux.intel.com> <20180903191926.GC13497@linux.intel.com> <105F7BF4D0229846AF094488D65A09893541970F@PGSMSX112.gar.corp.intel.com> <20180904145451.GA5233@linux.intel.com> <20180904153021.GB8344@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180904153021.GB8344@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 Tue, Sep 04, 2018 at 06:30:21PM +0300, Jarkko Sakkinen wrote: > On Tue, Sep 04, 2018 at 07:54:51AM -0700, Sean Christopherson wrote: > > I don't see any value in trying to rule out specific causes of > > INVALID_TOKEN, but we should only retry EINIT if ret==INVALID_TOKEN > > and RDMSR(HASH0) != sgx_lepubkeyhash[0]. Only the first MSR needs to > > be checked for validity as they're a package deal, i.e. they'll all be > > valid or all be reset. There shouldn't be a limit on retry attempts, > > e.g. the MSRs could theoretically be reset between WRMSR and EINIT. > > Why is doing rdmsrs necessary? With the INVALID_TOKEN error we know we > are out-of-sync i.e. have been sleeping and then one just needs to do > wrmsrs. As Kai mentioned, INVALID_TOKEN is returned for other reasons, e.g. a production enclave trying to use a debug token or reserved bits set in the token. And in the KVM case, the hash and token are provided by the guest, so it's entirely possible the enclave/token is not signed with the key specified in the hash. RDMSR is relatively inexpensive compared to the overall cost of EINIT. Though of course EINIT failure isn't exactly a fast path, so I'm ok if you want to opt for simplicity and retry on INVALID_TOKEN without checking the MSRs, just make sure to add a comment indicating we're intentionally not checking the MSRs. > I think one retry should be enough given that VMM traps EINIT. One retry > is needed to take care of the guest itself (or host if we are running on > bare metal) having been in a sleep state. Assuming we do RDMSR(hash0), that should be sufficient to prevent infinite retry and it protects against the MSRs being lost between WRMSR and EINIT during retry. That being said, I'm ok retrying only once, especially if you want to omit the RDMSR. Disabling preemption should prevent the kernel from suspending between WRMSR and EINIT, I'm just being paranoid.