Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp3403952imm; Tue, 17 Jul 2018 04:24:16 -0700 (PDT) X-Google-Smtp-Source: AAOMgpf0aZdPjP2c017K/JrIkJ42JxgwGGjU5W7GOVjfy4FO+iFJjwSU3/DttyObtsDvc2opwtQO X-Received: by 2002:a63:5fc1:: with SMTP id t184-v6mr1153501pgb.183.1531826656763; Tue, 17 Jul 2018 04:24:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531826656; cv=none; d=google.com; s=arc-20160816; b=m6v5GV+QALAkn0bJGH3LFgAqgW4xwihUmyEUMl9b6h0TP4zgsN7/cvVANk0Kh5o/yX ADAhb0jUHTLQNzBqPd6tRLqYYS8VRB+NJZcJze4Whs5Zums2BdKlwDQxCYrSHVJdKpjz 5L1oKpDRxJE58lg5+ZvSduvHVH697zQJuEqjdYqYYHNhX9JMU0nP4wtMZH4HIKx31oRl fxYu7PJQHIEPS36i+XZOE3f/OEUr1ohsF0HLfxRQ3YBEpFUkC94/u3I0v9l1AYFW5m4B xYps9QXnbTf/3/dd+9RgL1dPkyssnm/ta99h0xlK03iwjU9mbrunbMbMLcNyjvztnXh/ MkPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=4hSUWDZCUBTeeDsArIAUszGAWEjInBF+D7KzAtvRX+4=; b=kRo2ZjmNrJ/kXSD2YrHrH+ORoPC+w0jsppFWBeFlGAV+JwdwuR2MGr+Ly7hOyjFVjC Kp4at/Zo8yE0l6m7D9gpbtt3ANuhJGiSc/vxVgy4cRRgE1pU8pR5t9jCz6eddxjk/tna queEqcYXzMDgUPcs+a8LbsjOWu+0MNJPDkXZoqCWQljZvRsmzdQQaeAMYZiDv5wy6U2Q kkGivwEpnezmRB+G2DcJdHc/RqCoD+pzvw8JUwS605bAoZvREz00c71fnZOX+lb8QbWn 5CWZ0eDolA3G50LdfCQ3BrZCzJdOchpblA/Tjt4JDr/3hp6Y70b4JmuDD+utRA80WJGI iYeA== 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 k1-v6si744591pgh.65.2018.07.17.04.24.01; Tue, 17 Jul 2018 04:24:16 -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 S1731704AbeGQLzJ (ORCPT + 99 others); Tue, 17 Jul 2018 07:55:09 -0400 Received: from mga01.intel.com ([192.55.52.88]:16951 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731372AbeGQLx6 (ORCPT ); Tue, 17 Jul 2018 07:53:58 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Jul 2018 04:21:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,365,1526367600"; d="scan'208";a="67585445" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga003.jf.intel.com with ESMTP; 17 Jul 2018 04:21:45 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 3154E74D; Tue, 17 Jul 2018 14:21:49 +0300 (EEST) From: "Kirill A. Shutemov" To: Ingo Molnar , x86@kernel.org, Thomas Gleixner , "H. Peter Anvin" , Tom Lendacky Cc: Dave Hansen , Kai Huang , Jacob Pan , linux-kernel@vger.kernel.org, linux-mm@kvack.org, "Kirill A. Shutemov" Subject: [PATCHv5 12/19] x86/mm: Implement prep_encrypted_page() and arch_free_page() Date: Tue, 17 Jul 2018 14:20:22 +0300 Message-Id: <20180717112029.42378-13-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180717112029.42378-1-kirill.shutemov@linux.intel.com> References: <20180717112029.42378-1-kirill.shutemov@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Flush cache on allocating encrypted page and on returning the page to the free pool. prep_encrypted_page() also takes care about zeroing the page. We have to do this after KeyID is set for the page. Signed-off-by: Kirill A. Shutemov --- arch/x86/include/asm/mktme.h | 6 +++++ arch/x86/mm/mktme.c | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/arch/x86/include/asm/mktme.h b/arch/x86/include/asm/mktme.h index f0b7844e36a4..44409b8bbaca 100644 --- a/arch/x86/include/asm/mktme.h +++ b/arch/x86/include/asm/mktme.h @@ -19,6 +19,12 @@ int page_keyid(const struct page *page); #define vma_keyid vma_keyid int vma_keyid(struct vm_area_struct *vma); +#define prep_encrypted_page prep_encrypted_page +void prep_encrypted_page(struct page *page, int order, int keyid, bool zero); + +#define HAVE_ARCH_FREE_PAGE +void arch_free_page(struct page *page, int order); + #else #define mktme_keyid_mask ((phys_addr_t)0) #define mktme_nr_keyids 0 diff --git a/arch/x86/mm/mktme.c b/arch/x86/mm/mktme.c index a1f40ee61b25..1194496633ce 100644 --- a/arch/x86/mm/mktme.c +++ b/arch/x86/mm/mktme.c @@ -1,4 +1,5 @@ #include +#include #include phys_addr_t mktme_keyid_mask; @@ -49,3 +50,51 @@ int vma_keyid(struct vm_area_struct *vma) prot = pgprot_val(vma->vm_page_prot); return (prot & mktme_keyid_mask) >> mktme_keyid_shift; } + +void prep_encrypted_page(struct page *page, int order, int keyid, bool zero) +{ + int i; + + /* It's not encrypted page: nothing to do */ + if (!keyid) + return; + + /* + * 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. + * + * We flush cache before allocating encrypted page + */ + clflush_cache_range(page_address(page), PAGE_SIZE << 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; + + /* Clear the page after the KeyID is set. */ + if (zero) + clear_highpage(page); + + page++; + } +} + +void arch_free_page(struct page *page, int order) +{ + int i; + + /* It's not encrypted page: nothing to do */ + if (!page_keyid(page)) + return; + + clflush_cache_range(page_address(page), PAGE_SIZE << 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); + lookup_page_ext(page)->keyid = 0; + page++; + } +} -- 2.18.0