Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp873231ybi; Fri, 14 Jun 2019 04:55:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqzdUBsX/6T27NyfDssMEe7bTh4FgFqQkLzo5bQ0z0I9EXw6ZrqmjgUPhQozhF9EsK1GI63I X-Received: by 2002:a62:e815:: with SMTP id c21mr57257017pfi.244.1560513358275; Fri, 14 Jun 2019 04:55:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560513358; cv=none; d=google.com; s=arc-20160816; b=XW/XyE0pJT4jM2Qyy9Zy2mVqxFc/HDULLZNrGsYNAPVV3zwQM6KbF08yWZz/MZZlYC SVquYBBJGj/Zt977gkR+b0pLYGPRIjEdFGvy+bB3WCBVf39D808hFUMBxDedwSNsb1Gp 785DrAGvoujxmt2taz5j4Vg0vFvmzuGlmRb+MC9aw5vy28SPQXi+5U82RHLdq/jQIqH+ Uf/YNsLEWEQFfch9MOWmW+wwY9qq9z7C2XGKu/NhQ5FB9qGxpMlFBIn/dRzgnhJD/tvQ 50RUu8ThdE2lIfAdlmTpPI7t4lr1w9Ymg7IWteyBAHv+2Aj5DHAYQZyNIwPKL2xGV+iU aBEg== 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:dkim-signature; bh=V9MxUDuNmfO4r0z06SPalcFOxnjAwgY7mVSx/TAv4CA=; b=Uzj2DUYwsuHVkGStvA5xPkXJIX4yrItlHiROSYWg950hGj80PjCWBZtK7fqeXtmC8C gEnDThTazNgkKHrVP1U3ShrvxzsToFwWbMWy80Xn0sz/pFyR+uP9npJNodvzAo15Mex8 JIOrQ+xgBUn4yAKp874yZFFJhLSsExOV31dDItH9nARyA7vV3K0YKz9VxYuU5P4hKrtS ZTy/Gp+cSHkfO6Xnpz8cLrJamo2V+4OPA6Oup7dIMfovE2yvyFQYseDHCsUWzYLH5Jqi O0+yE6fIrtS2yfoJ3MtScsQCKFu+LVTwo5WSSwlu278SlaKkn7eiPXIPgIdGFeh4uuzY TumA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=M7O9zXWb; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 65si2242698pff.66.2019.06.14.04.55.43; Fri, 14 Jun 2019 04:55:58 -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; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=M7O9zXWb; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727678AbfFNLyb (ORCPT + 99 others); Fri, 14 Jun 2019 07:54:31 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:33540 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727164AbfFNLyb (ORCPT ); Fri, 14 Jun 2019 07:54:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=V9MxUDuNmfO4r0z06SPalcFOxnjAwgY7mVSx/TAv4CA=; b=M7O9zXWbjnUSmnDAOEmTK7Md1 Z232umC/Ih4w0N8OJkMIQxcnrGvrzB8H/aYEJh2NILrV6grz+ZS4iMVMPIljrdAZzeSQl32EoqVmc j9dPtuTqOLDXMls53IKjGHjUpO6mg61K24O95pcgqQDd1mWbASyhdbtqNRtmFf4tWCGOhyPlyJCiY ZDgjGaiymge3jtRgtsGSf4cgMUsVeTBqlSeI08iRRUyXxzHYKrr1kkOTU+kESF5T+cSeUNPSLeSBF cFezL2X72sFAc99UeeE4ZVIdO12z7QcyfpUqx61BiAewhS9S5JkG8drNIEvQRD6wZWwOGyebGriLr T3LxQ3aCw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.92 #3 (Red Hat Linux)) id 1hbkmn-0002Jj-Qb; Fri, 14 Jun 2019 11:54:25 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 4E56A20A26CE7; Fri, 14 Jun 2019 13:54:24 +0200 (CEST) Date: Fri, 14 Jun 2019 13:54:24 +0200 From: Peter Zijlstra To: "Kirill A. Shutemov" Cc: 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 , Alison Schofield , 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: <20190614115424.GG3436@hirez.programming.kicks-ass.net> References: <20190508144422.13171-1-kirill.shutemov@linux.intel.com> <20190508144422.13171-47-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190508144422.13171-47-kirill.shutemov@linux.intel.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); > } > > /* > @@ -110,7 +139,9 @@ void __prep_encrypted_page(struct page *page, int order, int keyid, bool zero) > */ > void free_encrypted_page(struct page *page, int order) > { > - int i; > + int i, keyid; > + > + keyid = page_keyid(page); > > /* > * The hardware/CPU does not enforce coherency between mappings > @@ -125,6 +156,8 @@ void free_encrypted_page(struct page *page, int order) > lookup_page_ext(page)->keyid = 0; > page++; > } > + > + percpu_ref_put_many(&encrypt_count[keyid], 1 << order); > } counts pages, what gives?