Received: by 10.213.65.68 with SMTP id h4csp657566imn; Fri, 16 Mar 2018 14:51:55 -0700 (PDT) X-Google-Smtp-Source: AG47ELtTuJS/vCH07z3oYcPz/hMmNZ1G2muVnlJWN+JK7w+jNZ0Y94vp7NnElq8v7gFRHRINHH8E X-Received: by 2002:a17:902:526:: with SMTP id 35-v6mr3762905plf.276.1521237115231; Fri, 16 Mar 2018 14:51:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521237115; cv=none; d=google.com; s=arc-20160816; b=FxTkhRAvh8XaD/HQGVfR7wiWo/soGyvqnt+1Ie6j4gS0bg5Q9Y6zm8nfQ5AvD0osXB iShCw8JENvW1zCsKuBgWh4iqhYZn0yqJo2NcQ0j+2t+ixC/XqptkpT6wK/EXy5+pGyU2 F6F9tF84OsdAt5MA5rLstilFxYUeJjRPl1gmfKYHpXUg3r9HN88uD7Sc/JfP2fPq7MHK j/aAYxE1+BYe2+u55NIjzzRq/fWfjBd8vuzEDgg5ypCrS7SlLAikq4YRhEOMEb8JCa/k WgoAsX8q1OYGOp4AujpzaliIH1Y9dncvgn9Ie8H6an3409Ij8OALt/ojFohkEiV7VE0l jqWg== 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=cjE3kjNkx4zJdojxvIgh6wtnqeCxb01ZzorCqjuC6Iw=; b=fJEQ/1PNRODRKhIZ1PtiR24vCxp7henICw+kUrVL1jx7kgyLqSheN4fu1g9EMjBjFz n7xp4phmnB0BQ+ob2cDz49SBLOn8BshxsCoriHr29PZDa/4JMEuvuzutcq3p+FaL+Dh6 ylvb8xPQtHfWTwlsljWSIEhmYRp9I4LpLymqfataaYbY2GLNManxNkcwXnwEyvV55W7E 8COqmf47ow9gdolFhm78jrYKGFYxTF/gr77nbGcIkBjNqah88+2kwjg+gZkcWXxy9bLa jrGQj8xR69cXAxLqmGXR/rYe5kUyzLOWjS3Lq5VDgGLCU7Y5ACgpCi8Djh7QBR/JC57p q0wA== 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 y72-v6si1961659plh.72.2018.03.16.14.51.41; Fri, 16 Mar 2018 14:51:55 -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 S1751586AbeCPVsd (ORCPT + 99 others); Fri, 16 Mar 2018 17:48:33 -0400 Received: from mga12.intel.com ([192.55.52.136]:52828 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751233AbeCPVsb (ORCPT ); Fri, 16 Mar 2018 17:48:31 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Mar 2018 14:48:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,317,1517904000"; d="scan'208";a="183503467" Received: from viggo.jf.intel.com (HELO localhost.localdomain) ([10.54.39.119]) by orsmga004.jf.intel.com with ESMTP; 16 Mar 2018 14:48:30 -0700 Subject: [PATCH 1/3] 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, 16 Mar 2018 14:46:56 -0700 References: <20180316214654.895E24EC@viggo.jf.intel.com> In-Reply-To: <20180316214654.895E24EC@viggo.jf.intel.com> Message-Id: <20180316214656.0E059008@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-16 14:46:39.023285476 -0700 +++ b/arch/x86/include/asm/mmu_context.h 2018-03-16 14:46:39.028285476 -0700 @@ -191,7 +191,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-16 14:46:39.025285476 -0700 +++ b/arch/x86/include/asm/pkeys.h 2018-03-16 14:46:39.028285476 -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; _