From patchwork Tue Dec 4 07:39:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alison Schofield X-Patchwork-Id: 10711235 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 511E013BF for ; Tue, 4 Dec 2018 07:37:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F7C52A46B for ; Tue, 4 Dec 2018 07:37:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 33BB82A544; Tue, 4 Dec 2018 07:37:51 +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 B362B2A46B for ; Tue, 4 Dec 2018 07:37:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9871D6B6D94; Tue, 4 Dec 2018 02:37:29 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8736A6B6D97; Tue, 4 Dec 2018 02:37:29 -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 58B586B6D98; Tue, 4 Dec 2018 02:37:29 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) by kanga.kvack.org (Postfix) with ESMTP id 0779C6B6D94 for ; Tue, 4 Dec 2018 02:37:29 -0500 (EST) Received: by mail-pg1-f199.google.com with SMTP id g188so8437136pgc.22 for ; Mon, 03 Dec 2018 23:37:28 -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=LbACiwBCx2a3uE3XT2IfbdeHHGQ/4kGMSrVxRuSHvMI=; b=Va2T1z1TlhdlyLlo0wG9xEqd/U3I7Q4RFXcX7VEZ17+iNtJiPBauEhLVxEs8Y4cmCs 09HS4G7Lbppp9HMD/zFXZpBdpmi7TOUJruwr38zTGYFI1bA/NI+ginlJCrcJyYgbIKo+ bI0ENWWiiImYDX5ubI7vvnGYoC0VeRu55Q2x80W5VdD53tM3Evlw2Yc23XDZ5YCpmKtv /NAkdcmaXnuj08RY9z1lKpAuBMl6IuJbfzTFDiNdBzKNt/KZ5wNST4T5ixKwO0j6INyB 1kEcAJG4uO4FH67ka4ha7muocH4dNn+QshGAFfhMBUJ203xgWth43EcCIZYoN04+/GtC G4mA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of alison.schofield@intel.com designates 134.134.136.31 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+aEWbGxqJ8InZeO6St0f4EZDBnkLD7jpV2S5+Jo4n2BF9ql4mQpKEc rwl0yr17VFB3ZC9tUh1vYX8YKwBKe60JNIdRmlZIc9uKCUKJQUqvkWC4pJBI7ZXoU6+9FgJAqAK aFHCfggkwpwOrCBPdtvgvC8c6RUk7PX4/xha8ci3vQG4O9HZLmgG1gqLsZfI51VXV+A== X-Received: by 2002:a17:902:7b88:: with SMTP id w8mr18840454pll.320.1543909048668; Mon, 03 Dec 2018 23:37:28 -0800 (PST) X-Google-Smtp-Source: AFSGD/UGmjl9Jv2bRfmqI+o+XEHMw3TtzTbBCPqhC69dissSwE6X9lJkbSUDb3DghaR7MPIf4hMJ X-Received: by 2002:a17:902:7b88:: with SMTP id w8mr18840414pll.320.1543909047314; Mon, 03 Dec 2018 23:37:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543909047; cv=none; d=google.com; s=arc-20160816; b=0bo+oLC79dkFlNGrgVuOakMBPNZHo7TKLAfdo/KUjXos4NGDXV97pgR8qxrdXo94gx aYnNxB60tXEFG1EQR+dhtpRYi2FLV7MFDcJcNKGh60nR2n61iSBahq8skd/2e/euGEbq bTV/sasQUUrPGCNiM68R2c+VQSBjeNBVgE56iIryO/DmfTvtS4Wk20Kxya5cpi/Py6Tv e48kpQimMfmN8y1yQj2ecuykv9PodsHqNsVAJpCR+QNeVM+XedL+Wg3dUMuMF3rZJuMX wc5u81WztMg/MeyltvQWzPexjJhZhDUhginF3l6H+rj63Ytfoj1JeHbQffzpk3Sa9Q/u pqEw== 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=LbACiwBCx2a3uE3XT2IfbdeHHGQ/4kGMSrVxRuSHvMI=; b=Ihpe5TzwWJUOcw8bZCtJR3WIFvtKoSDG390D6QwdkTsualIfAqWPLfFh2FSEZmiyu3 pIC2g9h22kIWMHgvQ6JuFZ5JWdSutY3Y9kACa9DSmnsdrO1dr6/eKVVZcedhJHyjGa/f c77QC90JDJe2ajx/GoouRpK+Yoz9RWaCI/iTM1XZQbJmKS8YqiZeDLUEWPRcYfCOCF3Z bWXFn9k9lxh2DLGVT/oPDqxKfiTSX+ar/TTCBMl8iIuByAdBlFQ5h2pOSzMhEFgYxQS4 gfbasnTzkYBLZbtrGnvrKwovYBCyQ6F7WSAuR3vQ4SrXu57dKmzu7PRr3nrIUj/28Khr SkLA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of alison.schofield@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=alison.schofield@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga06.intel.com (mga06.intel.com. [134.134.136.31]) by mx.google.com with ESMTPS id 3si17199771plx.33.2018.12.03.23.37.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Dec 2018 23:37:27 -0800 (PST) Received-SPF: pass (google.com: domain of alison.schofield@intel.com designates 134.134.136.31 as permitted sender) client-ip=134.134.136.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of alison.schofield@intel.com designates 134.134.136.31 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 orsmga001.jf.intel.com ([10.7.209.18]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Dec 2018 23:37:26 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,313,1539673200"; d="scan'208";a="115772840" Received: from alison-desk.jf.intel.com (HELO alison-desk) ([10.54.74.53]) by orsmga001.jf.intel.com with ESMTP; 03 Dec 2018 23:37:26 -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 11/13] keys/mktme: Program memory encryption keys on a system wide basis Date: Mon, 3 Dec 2018 23:39:58 -0800 Message-Id: <72dd5f38c1fdbc4c532f8caf2d2010f1ddfa8439.1543903910.git.alison.schofield@intel.com> 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 The kernel manages the MKTME (Multi-Key Total Memory Encryption) Keys as a system wide single pool of keys. The hardware, however, manages the keys on a per physical package basis. Each physical package maintains a Key Table that all CPU's in that package share. In order to maintain the consistent, system wide view that the kernel requires, program all physical packages during a key program request. Change-Id: I0ff46f37fde47a0305842baeb8ea600b6c568639 Signed-off-by: Alison Schofield --- security/keys/mktme_keys.c | 61 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/security/keys/mktme_keys.c b/security/keys/mktme_keys.c index e615eb58e600..7f113146acf2 100644 --- a/security/keys/mktme_keys.c +++ b/security/keys/mktme_keys.c @@ -21,6 +21,7 @@ #include "internal.h" struct kmem_cache *mktme_prog_cache; /* Hardware programming cache */ +cpumask_var_t mktme_leadcpus; /* one cpu per pkg to program keys */ static const char * const mktme_program_err[] = { "KeyID was successfully programmed", /* 0 */ @@ -59,6 +60,37 @@ static void mktme_destroy_key(struct key *key) key_put_encrypt_ref(mktme_map_keyid_from_key(key)); } +struct mktme_hw_program_info { + struct mktme_key_program *key_program; + unsigned long status; +}; + +/* Program a KeyID on a single package. */ +static void mktme_program_package(void *hw_program_info) +{ + struct mktme_hw_program_info *info = hw_program_info; + int ret; + + ret = mktme_key_program(info->key_program); + if (ret != MKTME_PROG_SUCCESS) + WRITE_ONCE(info->status, ret); +} + +/* Program a KeyID across the entire system. */ +static int mktme_program_system(struct mktme_key_program *key_program, + cpumask_var_t mktme_cpumask) +{ + struct mktme_hw_program_info info = { + .key_program = key_program, + .status = MKTME_PROG_SUCCESS, + }; + get_online_cpus(); + on_each_cpu_mask(mktme_cpumask, mktme_program_package, &info, 1); + put_online_cpus(); + + return info.status; +} + /* Copy the payload to the HW programming structure and program this KeyID */ static int mktme_program_keyid(int keyid, struct mktme_payload *payload) { @@ -84,7 +116,7 @@ static int mktme_program_keyid(int keyid, struct mktme_payload *payload) kprog->key_field_2[i] ^= kern_entropy[i]; } } - ret = mktme_key_program(kprog); + ret = mktme_program_system(kprog, mktme_leadcpus); kmem_cache_free(mktme_prog_cache, kprog); return ret; } @@ -299,6 +331,28 @@ struct key_type key_type_mktme = { .destroy = mktme_destroy_key, }; +static int mktme_build_leadcpus_mask(void) +{ + int online_cpu, mktme_cpu; + int online_pkgid, mktme_pkgid = -1; + + if (!zalloc_cpumask_var(&mktme_leadcpus, GFP_KERNEL)) + return -ENOMEM; + + for_each_online_cpu(online_cpu) { + online_pkgid = topology_physical_package_id(online_cpu); + + for_each_cpu(mktme_cpu, mktme_leadcpus) { + mktme_pkgid = topology_physical_package_id(mktme_cpu); + if (mktme_pkgid == online_pkgid) + break; + } + if (mktme_pkgid != online_pkgid) + cpumask_set_cpu(online_cpu, mktme_leadcpus); + } + return 0; +} + /* * Allocate the global mktme_map structure based on the available keyids. * Create a cache for the hardware structure. Initialize the encrypt_count @@ -323,10 +377,15 @@ static int __init init_mktme(void) if (mktme_alloc_encrypt_array() < 0) goto free_cache; + if (mktme_build_leadcpus_mask() < 0) + goto free_array; + ret = register_key_type(&key_type_mktme); if (!ret) return ret; /* SUCCESS */ + free_cpumask_var(mktme_leadcpus); +free_array: mktme_free_encrypt_array(); free_cache: kmem_cache_destroy(mktme_prog_cache);