Received: by 10.213.65.68 with SMTP id h4csp637306imn; Wed, 28 Mar 2018 09:57:24 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+WWGO9TqVHs80sjMme1o5sT1HjOOSPxTTUuPO6vMegu0AgPDoSGS/4Mr6uO7d28ETUhlOY X-Received: by 2002:a17:902:6b8b:: with SMTP id p11-v6mr4589618plk.213.1522256244054; Wed, 28 Mar 2018 09:57:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522256244; cv=none; d=google.com; s=arc-20160816; b=mVnHfLLjEvnZd3EcLQwXLCxeXkpTeG0mmQej/a8274TdWfms0Zh1eGNGYc5VowpXRE Z+dR/fYFEM6zGBT3sbxDfN0UbtplTiFw2xW3MSUsyQRjFYRkNMHlw0WIqpcVH1he/81a X/3JodXeLFPTW51c2q8piXvxFYEaushgnftzwm8nJyK4Za7kebDGS6Wwt+VyOVNzeh4y JT5BBB5+7LCzOeQtfVHh5kHPoKq9mgfTUujzOL8Sxo6zq6i+5lxFsoObA1tSBOlcbHbf aD6Geo7DLUWZ8CDGqxnecVHm67fTx80RP5wUp3lUBv0zaAS7S2iVtlq3B7mAozHQLDet LSAg== 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=0DJJeYoI9z8wxD5ntpzurzkpMO+OPZVo7cVX/+H1NIU=; b=gyYTaaKP94TUaxKymZSrOb7R42nzEVQ83rhxa94ZTZ8dzzGiLeLU1oun87T6N3izPC n9AnCo2R32lPLsNbjiksd7fS3JkRKEemEAd6rISp8w6t4dzcryX6rzPj49t65EznYOxS A7+zCRrqrqm+JB+jq6/XQn3706mOl/AZ4Krnxjmyh/xIgxjUgz+CKgmHFMQXtBQgQf22 sqsa1n8+cmEomboX5IaoMNDI8FvR7+BMxxixxf1fugsou5d+5EmRsYECoYLLbnWigDX9 0B201VDU7JSpa4/DpXR7WgC7DTbLZPlirfL9WDhESEltqhvBAVPNj7zx8U/1N0LTns/p RujQ== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c24-v6si3731649plo.273.2018.03.28.09.57.09; Wed, 28 Mar 2018 09:57:24 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752935AbeC1Q4J (ORCPT + 99 others); Wed, 28 Mar 2018 12:56:09 -0400 Received: from mga11.intel.com ([192.55.52.93]:50175 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752829AbeC1Qzz (ORCPT ); Wed, 28 Mar 2018 12:55:55 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Mar 2018 09:55:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,372,1517904000"; d="scan'208";a="28816307" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga007.jf.intel.com with ESMTP; 28 Mar 2018 09:55:48 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id B548874E; Wed, 28 Mar 2018 19:55:43 +0300 (EEST) From: "Kirill A. Shutemov" To: Ingo Molnar , x86@kernel.org, Thomas Gleixner , "H. Peter Anvin" , Tom Lendacky Cc: Dave Hansen , Kai Huang , linux-kernel@vger.kernel.org, linux-mm@kvack.org, "Kirill A. Shutemov" Subject: [PATCHv2 13/14] x86/mm: Implement prep_encrypted_page() Date: Wed, 28 Mar 2018 19:55:39 +0300 Message-Id: <20180328165540.648-14-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180328165540.648-1-kirill.shutemov@linux.intel.com> References: <20180328165540.648-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 encrypt ion keys. We are responsible for cache management. We flush cache before changing KeyID of the page. KeyID is preserved for freed pages to avoid excessive cache flushing. Signed-off-by: Kirill A. Shutemov --- arch/x86/include/asm/mktme.h | 3 +++ arch/x86/mm/mktme.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/arch/x86/include/asm/mktme.h b/arch/x86/include/asm/mktme.h index 5f440d57aa47..5b22ef0f0ae3 100644 --- a/arch/x86/include/asm/mktme.h +++ b/arch/x86/include/asm/mktme.h @@ -11,6 +11,9 @@ extern phys_addr_t mktme_keyid_mask; extern int mktme_nr_keyids; extern int mktme_keyid_shift; +#define prep_encrypted_page prep_encrypted_page +void prep_encrypted_page(struct page *page, int order, int keyid); + #define vma_is_encrypted vma_is_encrypted bool vma_is_encrypted(struct vm_area_struct *vma); diff --git a/arch/x86/mm/mktme.c b/arch/x86/mm/mktme.c index 3da25212a372..cebec794bae8 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; @@ -21,6 +22,34 @@ int vma_keyid(struct vm_area_struct *vma) return (prot & mktme_keyid_mask) >> mktme_keyid_shift; } +void prep_encrypted_page(struct page *page, int order, int new_keyid) +{ + int i; + void *v; + + /* + * The hardware/CPU does not enforce coherency between mappings of the + * same physical page with different KeyIDs or encrypt ion keys. + * We are responsible for cache management. + * + * We flush cache before changing KeyID of the page. KeyID is preserved + * for freed pages to avoid exessive cache flushing. + */ + + for (i = 0; i < (1 << order); i++) { + int old_keyid = page_keyid(page); + + if (old_keyid == new_keyid) + continue; + + v = kmap_atomic(page + i); + clflush_cache_range(v, PAGE_SIZE); + kunmap_atomic(v); + + lookup_page_ext(page)->keyid = new_keyid; + } +} + static bool need_page_mktme(void) { /* Make sure keyid doesn't collide with extended page flags */ -- 2.16.2