Received: by 10.213.65.68 with SMTP id h4csp597769imn; Fri, 23 Mar 2018 11:14:18 -0700 (PDT) X-Google-Smtp-Source: AG47ELtlx6qA5+7qODgf4fC+1TbncXy06OtTo+vtVlMilPcTY/EeXZ8wuJdKn12BwHSsyEPB8AFt X-Received: by 10.99.182.73 with SMTP id v9mr21367504pgt.158.1521828857921; Fri, 23 Mar 2018 11:14:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521828857; cv=none; d=google.com; s=arc-20160816; b=S/P/xKXgJ59vvmfG+yHWJIhRSCAUozo30lnLv28NJ5DpdhcLnPHTH/TzcZuxTp1MPN OXC2s+avyivOj2Kryn+OBQ/YmtlYWHQjEsOPYGuuxM0M3sC/nQ63rN6cjdxPfsrj/s1S WwsPRUGcbsHzsX4blCqkcurdKQll7x8iQ3k1UgRrXzPZNfNYoOQ1ofVMZafCUXFnXFod mYeK47vov8M+wUjnNAvHNStS8XhqFccEJ/seb1nLSCNdhNo2n4m1eHpe7Sf3hffwuAfx WTSUbDWCLpa2bCvuqHNv8zRYcTBGcyODOfBGDs/IOs1wqPJCFc5Iz/9tJrQXaTTrmANP Ktxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:in-reply-to:references:date :from:cc:to:subject:arc-authentication-results; bh=0gnabBZJnApa1ki+FVjP9Hd2BJ+Pyk93d4upnh8nqFE=; b=kL1afvL+UA5PA4+N5XdL4Mzyth9FZ1bYbbmeuQbLtdisgMGcb52cJQ5hI7C3/Nw0g9 xXYZIpU2DMgYqdy6V/1rLUGXSfxpwUVS3j5rlI5PdOOWiQz5nwfLdUPIlw+wwJjrXCCF 6T41HwyfPWhiWvtHV7+suFHtbJRtaImU+VwnF4g3MFyJN//ruZJ9r8eZTTsmEAzeT/bp 0GiXv9qVOUk8Y6Nf0rVT1eId/E2qCtpN01VlbZdyAAlSZypegIrPw2g/feukdmtE0/42 nuYMbcW3IoMdwFaG/5BjkBPlAs4sDEf8ovDcjgItTFGM/4mxLKc/g0zL1HP42TekjEuY 2nuA== 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 g4si6346948pgq.322.2018.03.23.11.14.03; Fri, 23 Mar 2018 11:14:17 -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 S1752319AbeCWSLQ (ORCPT + 99 others); Fri, 23 Mar 2018 14:11:16 -0400 Received: from mga11.intel.com ([192.55.52.93]:19427 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751595AbeCWSLK (ORCPT ); Fri, 23 Mar 2018 14:11:10 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Mar 2018 11:11:09 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,351,1517904000"; d="scan'208";a="30730330" Received: from viggo.jf.intel.com (HELO localhost.localdomain) ([10.54.39.119]) by fmsmga002.fm.intel.com with ESMTP; 23 Mar 2018 11:11:06 -0700 Subject: [PATCH 1/9] x86, pkeys: do not special case protection key 0 To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, Dave Hansen , linuxram@us.ibm.com, tglx@linutronix.de, dave.hansen@intel.com, mpe@ellerman.id.au, mingo@kernel.org, akpm@linux-foundation.org, shuah@kernel.org From: Dave Hansen Date: Fri, 23 Mar 2018 11:09:05 -0700 References: <20180323180903.33B17168@viggo.jf.intel.com> In-Reply-To: <20180323180903.33B17168@viggo.jf.intel.com> Message-Id: <20180323180905.B40984E6@viggo.jf.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dave Hansen mm_pkey_is_allocated() treats pkey 0 as unallocated. That is inconsistent with the manpages, and also inconsistent with mm->context.pkey_allocation_map. Stop special casing it and only disallow values that are actually bad (< 0). The end-user visible effect of this is that you can now use mprotect_pkey() to set pkey=0. This is a bit nicer than what Ram proposed because it is simpler and removes special-casing for pkey 0. On the other hand, it does allow applciations to pkey_free() pkey-0, but that's just a silly thing to do, so we are not going to protect against it. Signed-off-by: Dave Hansen Cc: Ram Pai Cc: Thomas Gleixner Cc: Dave Hansen Cc: Michael Ellermen Cc: Ingo Molnar Cc: Andrew Morton p Cc: Shuah Khan --- b/arch/x86/include/asm/mmu_context.h | 2 +- b/arch/x86/include/asm/pkeys.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff -puN arch/x86/include/asm/mmu_context.h~x86-pkey-0-default-allocated arch/x86/include/asm/mmu_context.h --- a/arch/x86/include/asm/mmu_context.h~x86-pkey-0-default-allocated 2018-03-21 15:47:48.182198927 -0700 +++ b/arch/x86/include/asm/mmu_context.h 2018-03-21 15:47:48.187198927 -0700 @@ -192,7 +192,7 @@ static inline int init_new_context(struc #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS if (cpu_feature_enabled(X86_FEATURE_OSPKE)) { - /* pkey 0 is the default and always allocated */ + /* pkey 0 is the default and allocated implicitly */ mm->context.pkey_allocation_map = 0x1; /* -1 means unallocated or invalid */ mm->context.execute_only_pkey = -1; diff -puN arch/x86/include/asm/pkeys.h~x86-pkey-0-default-allocated arch/x86/include/asm/pkeys.h --- a/arch/x86/include/asm/pkeys.h~x86-pkey-0-default-allocated 2018-03-21 15:47:48.184198927 -0700 +++ b/arch/x86/include/asm/pkeys.h 2018-03-21 15:47:48.188198927 -0700 @@ -49,10 +49,10 @@ bool mm_pkey_is_allocated(struct mm_stru { /* * "Allocated" pkeys are those that have been returned - * from pkey_alloc(). pkey 0 is special, and never - * returned from pkey_alloc(). + * from pkey_alloc() or pkey 0 which is allocated + * implicitly when the mm is created. */ - if (pkey <= 0) + if (pkey < 0) return false; if (pkey >= arch_max_pkey()) return false; _