Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1276071ybi; Fri, 14 Jun 2019 11:38:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqxe6cOpPCZ8BIph3bmO3hb+4zsSDlV6XMnoHUs8CQvsJrjBZPzr5NOyvLXAwg6PHMOztUpl X-Received: by 2002:a17:90a:898e:: with SMTP id v14mr12577799pjn.119.1560537519927; Fri, 14 Jun 2019 11:38:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560537519; cv=none; d=google.com; s=arc-20160816; b=tAYKNnCCLuMkM3kThWphM17/DMVdBzq+y9GZjrFNdlvCFokDrIVF3hktyKGMNRqNDP skiJ38jkL5TgJD7Z/kVUwbv8VpMZ8cjDiJCUhNpSz23v2LS/F/5M6cYyQpBVsCdWtIrn VmozH5esL/TxGC/ypoMP8YUWohXI2QRMdpJMcg7Be6xu/Zd5o5QyNIAgAdRLsV7HHXCh 6aoLLqYeXZDpz812hm+KFGa9ZQbquBSHT+pOOK45uIc3GhrsJtVEThXmbM5lmxKn6vyf s6lBDMxkcuCDnOU1Gw9dxOt1eATkdnGIkRbPqc0Ud7l/T6Wkh88NwxbWOskhKC0+RMJH K6Nw== 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=NM9O7OiIIOAneHmYP7fBDG0QJlVHFQg9SbvEgFkcLPY=; b=nsGRI2l9eAP+1DQkefxm6G/ycLWAqomxyoU81fXd+XCDgm4C+639aI7NbrM4BzDskH VaooQzXZgCrEvYPV6wC9Q3OihrZXDGJ6dx37JXhhvAqjMNJCPR1w6R5PHxMHHHCidVgu f8eYsYua1EdYGj/pJx0nQSrGVy3GRwAcvRruiqkKJH2p3O6H49xTJQL1tIe6KyJguX/x GEQ50VE22OdphD84KUzwtUQx0p2WWMMh0au6aUp9BY/Wf72aECLO1H6RXMPXYge7CPhq fx+mB8HbZox30gasq6ZvsAft68kGSfbKdv9VdcVdM/TdhRZfX+Fyw7yRaUYMkEp5dZLn Nv4w== 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 e123si3227982pfa.252.2019.06.14.11.38.24; Fri, 14 Jun 2019 11:38:39 -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 S1726389AbfFNSgm (ORCPT + 99 others); Fri, 14 Jun 2019 14:36:42 -0400 Received: from mga03.intel.com ([134.134.136.65]:52676 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726340AbfFNSgm (ORCPT ); Fri, 14 Jun 2019 14:36:42 -0400 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 Jun 2019 11:36:41 -0700 Received: from alison-desk.jf.intel.com ([10.54.74.53]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 Jun 2019 11:36:40 -0700 Date: Fri, 14 Jun 2019 11:39:47 -0700 From: Alison Schofield To: Peter Zijlstra Cc: "Kirill A. Shutemov" , Andrew Morton , x86@kernel.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Borislav Petkov , Andy Lutomirski , David Howells , Kees Cook , Dave Hansen , Kai Huang , Jacob Pan , linux-mm@kvack.org, kvm@vger.kernel.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH, RFC 46/62] x86/mm: Keep reference counts on encrypted VMAs for MKTME Message-ID: <20190614183947.GA7252@alison-desk.jf.intel.com> References: <20190508144422.13171-1-kirill.shutemov@linux.intel.com> <20190508144422.13171-47-kirill.shutemov@linux.intel.com> <20190614115424.GG3436@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190614115424.GG3436@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 14, 2019 at 01:54:24PM +0200, Peter Zijlstra wrote: > On Wed, May 08, 2019 at 05:44:06PM +0300, Kirill A. Shutemov wrote: > > From: Alison Schofield > > > > The MKTME (Multi-Key Total Memory Encryption) Key Service needs > > a reference count on encrypted VMAs. This reference count is used > > to determine when a hardware encryption KeyID is no longer in use > > and can be freed and reassigned to another Userspace Key. > > > > The MKTME Key service does the percpu_ref_init and _kill, so > > these gets/puts on encrypted VMA's can be considered the > > intermediaries in the lifetime of the key. > > > > Increment/decrement the reference count during encrypt_mprotect() > > system call for initial or updated encryption on a VMA. > > > > Piggy back on the vm_area_dup/free() helpers. If the VMAs being > > duplicated, or freed are encrypted, adjust the reference count. > > That all talks about VMAs, but... > > > @@ -102,6 +115,22 @@ void __prep_encrypted_page(struct page *page, int order, int keyid, bool zero) > > > > page++; > > } > > + > > + /* > > + * Make sure the KeyID cannot be freed until the last page that > > + * uses the KeyID is gone. > > + * > > + * This is required because the page may live longer than VMA it > > + * is mapped into (i.e. in get_user_pages() case) and having > > + * refcounting per-VMA is not enough. > > + * > > + * Taking a reference per-4K helps in case if the page will be > > + * split after the allocation. free_encrypted_page() will balance > > + * out the refcount even if the page was split and freed as bunch > > + * of 4K pages. > > + */ > > + > > + percpu_ref_get_many(&encrypt_count[keyid], 1 << order); > > } snip > > counts pages, what gives? Yeah. Comments are confusing. We implemented the refcounting w VMA's in mind, and then added the page counting. I'll update the comments and dig around some more based on your overall concerns about the refcounting you mentioned in the cover letter.