From patchwork Tue Dec 4 07:39:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alison Schofield X-Patchwork-Id: 10711225 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4C55D16B1 for ; Tue, 4 Dec 2018 07:37:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3B4BB2A46B for ; Tue, 4 Dec 2018 07:37:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2F27F2A544; Tue, 4 Dec 2018 07:37:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AB5E12A46B for ; Tue, 4 Dec 2018 07:37:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6ECE86B6D90; Tue, 4 Dec 2018 02:37:27 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 402B26B6D8F; Tue, 4 Dec 2018 02:37:27 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDDC86B6D96; Tue, 4 Dec 2018 02:37:26 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by kanga.kvack.org (Postfix) with ESMTP id 89A866B6D91 for ; Tue, 4 Dec 2018 02:37:26 -0500 (EST) Received: by mail-pf1-f198.google.com with SMTP id i3so13366863pfj.4 for ; Mon, 03 Dec 2018 23:37:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:in-reply-to :references; bh=dH5QDgU+bIHqzGW/gS1e5wfUCnLH2npmt22lIsLPKdk=; b=TUun7snmc7ZXDVzeEDl+cqTqKiLvRT9yRtPukB4juLjQMFrL5fhkWw5UgRK0rTX/Ca dlGXHv9KSI9uKz9NJ9lOMLwg25nP23BbC1gOxF4w5MxBjF/FZHv+LrcQiujsDO7ED2XV weD0fNgD1o6hk05WdCDNt4+R6LFXpQIZZEXe0zxQy3YsllVa30c4/ZY5b92uZqNYdyBH Z2ZReol5SwfNVnTe2IMx8FyADdNxExekK76YWgnvR9NZc2TMo+UrnpTtkAbVrD1dkrnm aDn+mgOmpanhAiw/g3WCZQ49js5zTvyguSQqUy9T51Dv7FAKGlXT6SanQyakJuO5nXJE Ydrw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of alison.schofield@intel.com designates 192.55.52.88 as permitted sender) smtp.mailfrom=alison.schofield@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: AA+aEWZ61NeKG89CunecocnutRP84IJ1r8rMwJv/YT0kL3vcLXiZN5qk 8jP+vXWgW40/GPUVOT+KKEhQ+loBZGUZFgco09tTZ4EQ8c57sbpmwBUMccDeasmTB96nvRVJCwG QX7WjKkMvibsqk64HNbtcqcr+C3PYCDQvm4oCYWrhHoZ3VZ9QBZxGCdlQ+YimcBgiGQ== X-Received: by 2002:a17:902:f091:: with SMTP id go17mr19489981plb.235.1543909046213; Mon, 03 Dec 2018 23:37:26 -0800 (PST) X-Google-Smtp-Source: AFSGD/VyAgaE9b9oF/21SQ2jGBBU4nj9+pgqIbHKDJ5/eK/W1AfNUc8IaL6dl3yEmFygPw9oiZFn X-Received: by 2002:a17:902:f091:: with SMTP id go17mr19489954plb.235.1543909045181; Mon, 03 Dec 2018 23:37:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543909045; cv=none; d=google.com; s=arc-20160816; b=hxTlXzqG/i3T6RQmVjPXFGAdi5nDJ2uEyujSuNnYogEmWrkoJpD5iJG94f/KRcOwNk rK2HsU3785JSveJbJRBwc2xqRe69W6vi/wLOLnySGv/jeUUor0uFW8rtTTUw3Gvk1cBW 5tJ8jo3iC+DrlXyA5P9BJxvwqxdSQDhgV6Vpec8WwFIoVPQdtcYTp3oQUcgsgmtDbiqc RIAtEcA0owFIREWarLB5jRIFpfV9/RQ/h7WSPdEhROiYRP1LWI7aStx/QD0DWkTZzIxN +3N8tfK5/cuhiMqZHfnfT+JN+jssUBunZ9II7OyJzCSpmx0skyXEQSs3vwzrB3SeeQEu j6eA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:references:in-reply-to:message-id:date :subject:cc:to:from; bh=dH5QDgU+bIHqzGW/gS1e5wfUCnLH2npmt22lIsLPKdk=; b=zO3D95CQGxuZ642089+XqiYNN8Uzx1YrsXN0y0QXLvsg9sHRIcoJrRSNGeIVOOjjgn r4XpBvHT5m+i17eDgveQUmquyFMG8z94fvr61W7bStPLnQnfnVuTawlLrR8XjFaj0HY3 yKYcZNd16ziidJNjN50yPFRx52ufxPhuwOzMckpgHxGwBR1eNAQeQlwFTCOAf/GmoYlu /UH8hcsJg/iMo0kA9kT4ei+z3dSAs7KnYunASizjjesQxv6Z+Z2vDE1sKf30o7bIqEfO MVoFgayT9EapIOH1B3IDgCKh37uR6nTjEZQwi0Lp+zUZ4d/pfFV64oRfjkI1FsRaSmF9 s0Dw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of alison.schofield@intel.com designates 192.55.52.88 as permitted sender) smtp.mailfrom=alison.schofield@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga01.intel.com (mga01.intel.com. [192.55.52.88]) by mx.google.com with ESMTPS id 28si21308808pfm.50.2018.12.03.23.37.24 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Dec 2018 23:37:25 -0800 (PST) Received-SPF: pass (google.com: domain of alison.schofield@intel.com designates 192.55.52.88 as permitted sender) client-ip=192.55.52.88; Authentication-Results: mx.google.com; spf=pass (google.com: domain of alison.schofield@intel.com designates 192.55.52.88 as permitted sender) smtp.mailfrom=alison.schofield@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Dec 2018 23:37:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,313,1539673200"; d="scan'208";a="106817038" Received: from alison-desk.jf.intel.com (HELO alison-desk) ([10.54.74.53]) by fmsmga008.fm.intel.com with ESMTP; 03 Dec 2018 23:37:23 -0800 From: Alison Schofield To: dhowells@redhat.com, tglx@linutronix.de Cc: jmorris@namei.org, mingo@redhat.com, hpa@zytor.com, bp@alien8.de, luto@kernel.org, peterz@infradead.org, kirill.shutemov@linux.intel.com, dave.hansen@intel.com, kai.huang@intel.com, jun.nakajima@intel.com, dan.j.williams@intel.com, jarkko.sakkinen@intel.com, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-mm@kvack.org, x86@kernel.org Subject: [RFC v2 07/13] x86/mm: Add helpers for reference counting encrypted VMAs Date: Mon, 3 Dec 2018 23:39:54 -0800 Message-Id: X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP In order to safely manage the usage of memory encryption keys, VMAs using each KeyID need to be counted. This count allows the MKTME (Multi-Key Total Memory Encryption) Key Service to know when the KeyID resource is actually in use, or when it is idle and may be considered for reuse. Define a global refcount_t array and provide helper functions to manipulate the counts. Signed-off-by: Alison Schofield Signed-off-by: Kirill A. Shutemov --- arch/x86/include/asm/mktme.h | 9 +++++++ arch/x86/mm/mktme.c | 58 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 2 ++ 3 files changed, 69 insertions(+) diff --git a/arch/x86/include/asm/mktme.h b/arch/x86/include/asm/mktme.h index de3e529f3ab0..22d52635562c 100644 --- a/arch/x86/include/asm/mktme.h +++ b/arch/x86/include/asm/mktme.h @@ -28,6 +28,15 @@ extern int mktme_map_get_free_keyid(void); extern void mprotect_set_encrypt(struct vm_area_struct *vma, int newkeyid, unsigned long start, unsigned long end); +/* Manage the MTKME encrypt_count references */ +extern int mktme_alloc_encrypt_array(void); +extern void mktme_free_encrypt_array(void); +extern int mktme_read_encrypt_ref(int keyid); +extern void vma_get_encrypt_ref(struct vm_area_struct *vma); +extern void vma_put_encrypt_ref(struct vm_area_struct *vma); +extern void key_get_encrypt_ref(int keyid); +extern void key_put_encrypt_ref(int keyid); + 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 e3fdf7b48173..facf08f9cb74 100644 --- a/arch/x86/mm/mktme.c +++ b/arch/x86/mm/mktme.c @@ -157,6 +157,64 @@ void mprotect_set_encrypt(struct vm_area_struct *vma, int newkeyid, unlink_anon_vmas(vma); } +/* + * Helper functions manage the encrypt_count[] array that counts + * references on each MKTME hardware keyid. The gets & puts are + * used in core mm code that allocates and free's VMA's. The alloc, + * free, and read functions are used by the MKTME key service to + * manage key allocation and programming. + */ +refcount_t *encrypt_count; + +int mktme_alloc_encrypt_array(void) +{ + encrypt_count = kvcalloc(mktme_nr_keyids, sizeof(refcount_t), + GFP_KERNEL); + if (!encrypt_count) + return -ENOMEM; + return 0; +} + +void mktme_free_encrypt_array(void) +{ + kvfree(encrypt_count); +} + +int mktme_read_encrypt_ref(int keyid) +{ + return refcount_read(&encrypt_count[keyid]); +} + +void vma_get_encrypt_ref(struct vm_area_struct *vma) +{ + if (vma_keyid(vma)) + refcount_inc(&encrypt_count[vma_keyid(vma)]); +} + +void vma_put_encrypt_ref(struct vm_area_struct *vma) +{ + if (vma_keyid(vma)) + if (refcount_dec_and_test(&encrypt_count[vma_keyid(vma)])) { + mktme_map_lock(); + mktme_map_free_keyid(vma_keyid(vma)); + mktme_map_unlock(); + } +} + +void key_get_encrypt_ref(int keyid) +{ + refcount_inc(&encrypt_count[keyid]); +} + +void key_put_encrypt_ref(int keyid) +{ + if (refcount_dec_and_test(&encrypt_count[keyid])) { + mktme_map_lock(); + mktme_map_free_keyid(keyid); + mktme_map_unlock(); + } +} + /* 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 09182d78e7b7..453d675dd116 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2812,6 +2812,8 @@ static inline void mprotect_set_encrypt(struct vm_area_struct *vma, int newkeyid, unsigned long start, unsigned long end) {} +static inline void vma_get_encrypt_ref(struct vm_area_struct *vma) {} +static inline void vma_put_encrypt_ref(struct vm_area_struct *vma) {} #endif /* CONFIG_X86_INTEL_MKTME */ #endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */