Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6110724ybi; Wed, 31 Jul 2019 08:30:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqyN9C+DDC5k0rmlfF36Jo7+gPC7T+e4LJSCRkhSok0wVKKx9FftRhZmaHxHvN8TmeHlGnWe X-Received: by 2002:a62:2a4d:: with SMTP id q74mr48083397pfq.86.1564587047398; Wed, 31 Jul 2019 08:30:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564587047; cv=none; d=google.com; s=arc-20160816; b=zR0mOC8W8j2PxFnmLdzsqLfT+lgseQTe8DRT/kYW+jtdw7em45z2561+zWHJ96ZzdV qSbgKIN3HzjaMnQ0mOsl1ogaL5MlUW2CCtJ+veq6WPktJxx9pGPgF0GgzwieRw5UJy2J wuCplRsSF4APO6J/muV0sZNfgbvunpu5oBxHqJ3NxoX/QBkEakK9uIXFCNjYIxssRNGi t9Xp6yXZ8x86Lz2QcVneV7EmRjM36g9rh6wZb4C/Ua4Tsi2UzLJUG0QqpXOFdyZ6Y3xB 4NLQ6LGq8hG5fhbqe3zmN36SJhp2ANcfCiL4zLwe8UYyVRJX61c7+zJ7GE1QIDGrJpWI XMqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZkftAsGdSxojE0fJOOOF/MZdp7LK73Cocd1bOG6H+X8=; b=NawYHQDsh4sxxjaY8H/56KPM0D6k7zoQPRpTB+dqerVtX5hMK0cJkkwG/i88s1Wj+I YcHTv0S8hB6ZGpsOwlYpvg2vGM0MaVd6yS9RqwjJDdxc0wygpeALWA1VQbqH2pE8jRIN bsZWuUtAeSALAwB3VcymuOSse73TIOGrUFzh5h4m8Z/isU5HQ481o0KocaDZ5JPPZHJf Ad6U5hpD8hYNPF2JnJviHJW2mF7XNnkC+KTczIr8fjFbwAo2sDhJRTY0A4Vwmcmk6Bvp 94QUnFeHiHpdrW3P8Jfqo13AGzjLmiF2AlVlFWYmDmIHOqYL+ColFOtg/I0i3C0WlmQE HZMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=yNcUiWdQ; 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 t62si31082259pgd.175.2019.07.31.08.30.32; Wed, 31 Jul 2019 08:30:47 -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=yNcUiWdQ; 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 S1727089AbfGaPV4 (ORCPT + 99 others); Wed, 31 Jul 2019 11:21:56 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:35522 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726526AbfGaPVz (ORCPT ); Wed, 31 Jul 2019 11:21:55 -0400 Received: by mail-ed1-f67.google.com with SMTP id w20so66035915edd.2 for ; Wed, 31 Jul 2019 08:21:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZkftAsGdSxojE0fJOOOF/MZdp7LK73Cocd1bOG6H+X8=; b=yNcUiWdQFKls9CJ+s1kmqNYTq4HAt/+z4PQnyTb2R/O/QG4HejvHYbZEnDpZkx4u6m rYryPmrZZw5X0yVl9cqTObJ/vAbi3N2kcWUJTn/nS5m7u0j1NeO76bPx86u5y6/FFXzG GwvkGwtp3dVrAmLGNQwceerYEf8lbhe3A3hpQ9jH6pM/+5oR2hB1poc9BJZ/qpFk3lFg AVoV+a8D/wx7gKubBM5x0ly/CpnY4m7riIc5gECoUZ1ttMWB61O3sn/gxcaUYdZYR3rm riJBLh8bckwgmhvRhWjFVk43K9z3429hxlIIgWZDxIvHcCVj0zmgN7kgBdEg+aubIbZF aYoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZkftAsGdSxojE0fJOOOF/MZdp7LK73Cocd1bOG6H+X8=; b=K7hBRWwavWIs3HybuIHsCsbuq9c+VR7xGY0NvGKKSQnSv5/PYekxqvvwRC1hDQMOCH EGtHfOdF0qhlo5lJpp+2SUA8kshX+0clWRtSs1nXFCTJA/feTRzJUtXUJDSPatYGlqOV nFRjuSgebPRemNuSiCt8pF99Fp70dSq2raXg9DXfbWmzGikzMCQ9Yu8P/q/RdB9UqIxc nzwJxDrvTw9gmX0NMO8JYLViV5Kb7A5lhEnYjKBa6aQ8nM+/K+l91iOMnJwXpyvE3dfM +8WyAbv4AnRV2+CB1nBbP4C+n61PVW8HQSizGG0IEvoDxoNPNk5fBbnuBInWVMurSeWw 9Y7g== X-Gm-Message-State: APjAAAXpJ9xfpiWENk2T7y9AVTVKh3ZbuP8/SZUAIveOCNmJVeyDE7tO bVZwZS720/iSxDaA6aRwvMg= X-Received: by 2002:a17:906:94ce:: with SMTP id d14mr97075606ejy.251.1564586031480; Wed, 31 Jul 2019 08:13:51 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id j37sm17791942ede.23.2019.07.31.08.13.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 08:13:50 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id 251F0104601; Wed, 31 Jul 2019 18:08:17 +0300 (+03) To: Andrew Morton , x86@kernel.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , David Howells Cc: 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, "Kirill A . Shutemov" Subject: [PATCHv2 43/59] x86/mm: Set KeyIDs in encrypted VMAs for MKTME Date: Wed, 31 Jul 2019 18:07:57 +0300 Message-Id: <20190731150813.26289-44-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> References: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alison Schofield MKTME architecture requires the KeyID to be placed in PTE bits 51:46. To create an encrypted VMA, place the KeyID in the upper bits of vm_page_prot that matches the position of those PTE bits. When the VMA is assigned a KeyID it is always considered a KeyID change. The VMA is either going from not encrypted to encrypted, or from encrypted with any KeyID to encrypted with any other KeyID. To make the change safely, remove the user pages held by the VMA and unlink the VMA's anonymous chain. Signed-off-by: Alison Schofield Signed-off-by: Kirill A. Shutemov --- arch/x86/include/asm/mktme.h | 4 ++++ arch/x86/mm/mktme.c | 26 ++++++++++++++++++++++++++ include/linux/mm.h | 6 ++++++ 3 files changed, 36 insertions(+) diff --git a/arch/x86/include/asm/mktme.h b/arch/x86/include/asm/mktme.h index d26ada6b65f7..e8f7f80bb013 100644 --- a/arch/x86/include/asm/mktme.h +++ b/arch/x86/include/asm/mktme.h @@ -16,6 +16,10 @@ extern int __mktme_nr_keyids; extern int mktme_nr_keyids(void); extern unsigned int mktme_algs; +/* Set the encryption keyid bits in a VMA */ +extern void mprotect_set_encrypt(struct vm_area_struct *vma, int newkeyid, + unsigned long start, unsigned long end); + DECLARE_STATIC_KEY_FALSE(mktme_enabled_key); static inline bool mktme_enabled(void) { diff --git a/arch/x86/mm/mktme.c b/arch/x86/mm/mktme.c index ed13967bb543..05bbf5058ade 100644 --- a/arch/x86/mm/mktme.c +++ b/arch/x86/mm/mktme.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -71,6 +72,31 @@ int __vma_keyid(struct vm_area_struct *vma) return (prot & mktme_keyid_mask()) >> mktme_keyid_shift(); } +/* Set the encryption keyid bits in a VMA */ +void mprotect_set_encrypt(struct vm_area_struct *vma, int newkeyid, + unsigned long start, unsigned long end) +{ + int oldkeyid = vma_keyid(vma); + pgprotval_t newprot; + + /* Unmap pages with old KeyID if there's any. */ + zap_page_range(vma, start, end - start); + + if (oldkeyid == newkeyid) + return; + + newprot = pgprot_val(vma->vm_page_prot); + newprot &= ~mktme_keyid_mask(); + newprot |= (unsigned long)newkeyid << mktme_keyid_shift(); + vma->vm_page_prot = __pgprot(newprot); + + /* + * The VMA doesn't have any inherited pages. + * Start anon VMA tree from scratch. + */ + unlink_anon_vmas(vma); +} + /* Prepare page to be used for encryption. Called from page allocator. */ void __prep_encrypted_page(struct page *page, int order, int keyid, bool zero) { diff --git a/include/linux/mm.h b/include/linux/mm.h index 3f9640f388ac..98a6d2bd66a6 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2905,5 +2905,11 @@ void __init setup_nr_node_ids(void); static inline void setup_nr_node_ids(void) {} #endif +#ifndef CONFIG_X86_INTEL_MKTME +static inline void mprotect_set_encrypt(struct vm_area_struct *vma, + int newkeyid, + unsigned long start, + unsigned long end) {} +#endif /* CONFIG_X86_INTEL_MKTME */ #endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */ -- 2.21.0