Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp956065ybi; Fri, 14 Jun 2019 06:15:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqxfj4bw1vL2dbDPxti66y51nOEq7iytyHaNDKA5VaZDah2Dm/TqzK+PUoyJZSHg2b3e6cLA X-Received: by 2002:aa7:818b:: with SMTP id g11mr15020542pfi.122.1560518148958; Fri, 14 Jun 2019 06:15:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560518148; cv=none; d=google.com; s=arc-20160816; b=XSh0Q4TAW335FdBirefvwdmkDqxDz8tmhBNaFrcHA0w7Su/aY8TWYVMWc1xNpayB/P w3jXiv79mTa4D3rxJ9jYTbXeM0aDZ/y1P+n15IKfyjTtkP+O27wfIPZsMDF3PN+EDVK0 n8D2u3HLYjZ+aL2ASJODN3xErbxFmjWcqIXXeS6ZQQHSRYwnX7L18YIusZaOYj9zK3J6 ZCZltQDG6ItYQ8LUmq5S/Fsg0ieIHg4ZutYGlewg/fdXltLnXtBgCz5a3ta9wyRF/Tyn 2yeahAEwNZU9JUulSRd0MM5lg+uK7FBxQ3+Fk3XjsuoHFln14g7jNOh12ykisYrA+Gra OQ4w== 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=lo8jzIeEl3qvMScl2nYeZEzcklK1JpdjNABkHh1fbHg=; b=uk1TWendX4ga+DK5eHxUZ7bY+IeKHpuUJRu22cxnxWZhwy5G5EStYAjaLIZpQACtRH GrBsc2W5mYB+u6Ti6YpRQG7SRXpBJs/YR0dAG1Z/FJtmQuE6lskEebUJum/yIV7rezRT 5B1HPUX7yQ5lZo73A06Dok7e6t0ByXQtlsiEuWHaYzxpsAZmBzMhB3mcKjljl/rL/Sk3 Lgow/1Q6JKWe0uEm6sotRLEJZ93sawEGgaJ4m0fDjEsr6bxW0wj9G8Mlqi5Od+o0Vkm8 oDXOshdTbuLJZD122qBqXmCJ+vqZ/MxVVU/dNnPfsxo4hV+MdLgxWDmaBx/jWzleIbS/ Au0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=eFeAcZtg; 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 29si2612570pgy.132.2019.06.14.06.15.32; Fri, 14 Jun 2019 06:15:48 -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=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=eFeAcZtg; 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 S1728170AbfFNNO5 (ORCPT + 99 others); Fri, 14 Jun 2019 09:14:57 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:41378 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728121AbfFNNO4 (ORCPT ); Fri, 14 Jun 2019 09:14:56 -0400 Received: by mail-ed1-f67.google.com with SMTP id p15so3437534eds.8 for ; Fri, 14 Jun 2019 06:14:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=lo8jzIeEl3qvMScl2nYeZEzcklK1JpdjNABkHh1fbHg=; b=eFeAcZtgO0/eRC+taEsG3o/626r+gJzLYZLu6JCyggVrs1QqMuSldztZimYV/jdkJJ KpqfmnAYmMswNNumhYoKPsqVbi6oXCsorOhOJsdBSnOChpopGRX1/ui0nf+Nlcf6FXPr gGpw+PJGJIdyx5tMB8ohHf7Qp8YL0Pn4pTIC3KYego9Ujsgpyiq4SkfHbxvn1Rplq1Pp IoST/0AGJDllFkhSDArOhMHcOdLQczefbj/Em08KjBQgqt7nPa6It4nRH8wS9yRppE3g SyL/Y8mhbNTD3QF8XNH0x1JlkrNyAot22I9vIuPdXcIIAhttxk+quH+74xWONAEiEFsY kixQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=lo8jzIeEl3qvMScl2nYeZEzcklK1JpdjNABkHh1fbHg=; b=asYUK+eMAZJo7SRD2+aeLAg6CCRuvrYu6NZr08fVvCTqXM1aD+a5wpSrQggXjfTq4/ O333emWncafII73K5SVzD26aanybhYsMA/6FLkbQJHD2LwQJBp7KVPOtK+WCwkmE81vn gkioFlbHHMqoOJ8kthggrkr9BSS3h6O8NjaKw2XGtZL974jmOVYSc/T4R/Ko1OLQL7qD 35l8UQVjjqBHuLPXHmwD/lCWnTW6NfdmPjwee2tZQBN1hkiONdEIy1KfSrgIpwtKktKf Kvt3hcQUR5ZICmVvxJfF/DZEly4PCI6oKhKxij5mlA6rNUMCD9aw1G7h329QSeX7CJmt KV0A== X-Gm-Message-State: APjAAAUxC1BrsvRFLFaCHdDpBhjDins6rHhszPBODuQ4LXe8HghXqb/w 9IQfIImpicuForDMDXfbXbNSLg== X-Received: by 2002:aa7:c619:: with SMTP id h25mr39051647edq.295.1560518094495; Fri, 14 Jun 2019 06:14:54 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id t3sm593997ejk.56.2019.06.14.06.14.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Jun 2019 06:14:53 -0700 (PDT) Received: by box.localdomain (Postfix, from userid 1000) id BB20210086F; Fri, 14 Jun 2019 16:14:53 +0300 (+03) Date: Fri, 14 Jun 2019 16:14:53 +0300 From: "Kirill A. Shutemov" 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 , Alison Schofield , linux-mm@kvack.org, kvm@vger.kernel.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH, RFC 13/62] x86/mm: Add hooks to allocate and free encrypted pages Message-ID: <20190614131453.ludfm4ufzqwa326k@box> References: <20190508144422.13171-1-kirill.shutemov@linux.intel.com> <20190508144422.13171-14-kirill.shutemov@linux.intel.com> <20190614093409.GX3436@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190614093409.GX3436@hirez.programming.kicks-ass.net> User-Agent: NeoMutt/20180716 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 11:34:09AM +0200, Peter Zijlstra wrote: > On Wed, May 08, 2019 at 05:43:33PM +0300, Kirill A. Shutemov wrote: > > > +/* Prepare page to be used for encryption. Called from page allocator. */ > > +void __prep_encrypted_page(struct page *page, int order, int keyid, bool zero) > > +{ > > + int i; > > + > > + /* > > + * The hardware/CPU does not enforce coherency between mappings > > + * of the same physical page with different KeyIDs or > > + * encryption keys. We are responsible for cache management. > > + */ > > On alloc we should flush the unencrypted (key=0) range, while on free > (below) we should flush the encrypted (key!=0) range. > > But I seem to have missed where page_address() does the right thing > here. As you've seen by now, it will be addressed later in the patchset. I'll update the changelog to indicate that page_address() handles KeyIDs correctly. > > + clflush_cache_range(page_address(page), PAGE_SIZE * (1UL << order)); > > + > > + for (i = 0; i < (1 << order); i++) { > > + /* All pages coming out of the allocator should have KeyID 0 */ > > + WARN_ON_ONCE(lookup_page_ext(page)->keyid); > > + lookup_page_ext(page)->keyid = keyid; > > + > > So presumably page_address() is affected by this keyid, and the below > clear_highpage() then accesses the 'right' location? Yes. clear_highpage() -> kmap_atomic() -> page_address(). > > + /* Clear the page after the KeyID is set. */ > > + if (zero) > > + clear_highpage(page); > > + > > + page++; > > + } > > +} > > + > > +/* > > + * Handles freeing of encrypted page. > > + * Called from page allocator on freeing encrypted page. > > + */ > > +void free_encrypted_page(struct page *page, int order) > > +{ > > + int i; > > + > > + /* > > + * The hardware/CPU does not enforce coherency between mappings > > + * of the same physical page with different KeyIDs or > > + * encryption keys. We are responsible for cache management. > > + */ > > I still don't like that comment much; yes the hardware doesn't do it, > and yes we have to do it, but it doesn't explain the actual scheme > employed to do so. Fair enough. I'll do better. > > + clflush_cache_range(page_address(page), PAGE_SIZE * (1UL << order)); > > + > > + for (i = 0; i < (1 << order); i++) { > > + /* Check if the page has reasonable KeyID */ > > + WARN_ON_ONCE(lookup_page_ext(page)->keyid > mktme_nr_keyids); > > It should also check keyid > 0, so maybe: > > (unsigned)(keyid - 1) > keyids-1 > > instead? Makes sense. -- Kirill A. Shutemov