Received: by 10.213.65.68 with SMTP id h4csp113010imn; Mon, 26 Mar 2018 16:33:45 -0700 (PDT) X-Google-Smtp-Source: AIpwx49rcXaS69IUWqiQMutIisCJNXh10bUEHwqD1zQRWdkqU+LAMzwW8YmDp40x5Ce4RlktSpvm X-Received: by 10.167.130.76 with SMTP id e12mr2196685pfn.192.1522107225140; Mon, 26 Mar 2018 16:33:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522107225; cv=none; d=google.com; s=arc-20160816; b=M5CFk97ie/AyI0WPNBV+AU7e88plLueuAS7hx1ugdW3S+nBocrVhMpWzcg1yFFjeDV 7bzpxeOIgfOH90DXGm4swHvjzJ7zwoFOsgmNkof0r+C+jTF7MsCnj/M7d0FezKAuZuJt LqhPeGRWl6EB33p/XOyNUtcnNGG/n7ecL9Rjye4eGkMJHNjanamkm30ShKWCusMEFuvX LwjkfS4X6L9dTM54zZa6nuuaf0H5rgc7sUklDgodb0fZQaknXvCw8i5YDs3gvOfT6MD0 f5FCef1UN8dtSgBpAK05VFYnFjODOKcvvxR/bLoXfDr4YJ4u3LEkh8vFvMVq0ux0DJnB y3WQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=eib+yeCAZW8OMDRr2Vcq9gzCRvQBy2k98Lx12J53IL8=; b=qps197hMaJhA6k0nyeXhJ21lHzhx8+LqD1dde5xWJtcXeqvKQK9LB2O3662Z9YjYYb sm11/5mGfLI4KFk14P6QwZj6r0TdT7COP1nbo+6xtpOQQfH3NI2ZvfSt30jUFjkdlq21 cMddXrZCgMFY5HisB4TAgTV7Th9+Z5oQ3dkd0gkpj1q7gePm6sscLzB/US2h9QIEtfaY bFmZ8G5Wjm4hZXHbNBNrCADvkFu0vPSS4KMJdGYuWMIDtFmhv6KY0aZ0A71Yfk+CXWGR Ns6DMR6Rqw8q5uSgKlHfYus4xG7bT+4WOUlAnyabuK4GPIJmfhtIopjJMl6+4zHwuKLp FBVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=asn2CpEr; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t14si10759425pgr.277.2018.03.26.16.33.30; Mon, 26 Mar 2018 16:33:45 -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=fail header.i=@gmail.com header.s=20161025 header.b=asn2CpEr; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752231AbeCZXcF (ORCPT + 99 others); Mon, 26 Mar 2018 19:32:05 -0400 Received: from mail-qt0-f194.google.com ([209.85.216.194]:36602 "EHLO mail-qt0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751739AbeCZXcD (ORCPT ); Mon, 26 Mar 2018 19:32:03 -0400 Received: by mail-qt0-f194.google.com with SMTP id f16so9017187qth.3 for ; Mon, 26 Mar 2018 16:32:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id; bh=eib+yeCAZW8OMDRr2Vcq9gzCRvQBy2k98Lx12J53IL8=; b=asn2CpErnSRous1fvnT/TXdIhjC6HkjerNBHiUx0oy0ux/AFO+9zkbTd9FV+cIZOmm K1VZ7DTE2I29hlYY4ptC79CcC0qgjlJE7/ThnS4X2jNFaOG5LZsaXnX4IOWge0/KnwMA Af2yn5Wl6nfQJoqCmuwvrsA+hYlHQK2+WaOaGa5Ya10KhavV39N6QS75j83LkgePuSIa oCkmfto3TLGplbizQ/jhDeaqbD9nSFUH4Av49Yqkke7csGrYpDptFLTk45wU+7qwFJ/c rw/ag27iCpPlVUeZkfGUZZ9t6qE0i9Fa87LHjXouhExulgGGFcwQZyTsf/4E/NhV5xZU 3cYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=eib+yeCAZW8OMDRr2Vcq9gzCRvQBy2k98Lx12J53IL8=; b=YJk95gTx6ovPyr6dYFLHJIaidEyDKjVuv9b7UI+ePnLDO9/SYvAPB70u73+881MdrQ gHz9Bh/lQBN+Zu4LthuBvJAJY4P+VPDYxstofnmfcPozA/c6ZAua5ajHNe5AiIobirEY RFjhUbcN9ymtMjO8IBRYW5YqfYlgJh0eR5JqVisYNt/PYuVtX+6i75c+GoEYZz+3RaCL H4538MX93fQlcYmHexT4xF9mEHW4Y/uuKwXs0cf0k9Ak8cEE5CDcOIGIvASrGgIIV22O fs6ggFMtSs99DZKL89jr6OL53IxZSbcqgg5lQxGcCnyIuHg6+APofCaspnODasz8dOec wHYw== X-Gm-Message-State: AElRT7EdjmibGvR3ORzlH+dOobUz2qqYlvF1zBdMjHb0lpqnAO0u50Qh a2qSQUKrodUobgqGFxfCMH0= X-Received: by 10.200.57.228 with SMTP id v91mr59116278qte.16.1522107123349; Mon, 26 Mar 2018 16:32:03 -0700 (PDT) Received: from localhost.localdomain (50-39-100-161.bvtn.or.frontiernet.net. [50.39.100.161]) by smtp.gmail.com with ESMTPSA id m31sm11168920qtd.95.2018.03.26.16.32.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Mar 2018 16:32:02 -0700 (PDT) From: Ram Pai To: mpe@ellerman.id.au, mingo@redhat.com, akpm@linux-foundation.org Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, dave.hansen@intel.com, benh@kernel.crashing.org, paulus@samba.org, aneesh.kumar@linux.vnet.ibm.com, bsingharora@gmail.com, hbabu@us.ibm.com, mhocko@kernel.org, bauerman@linux.vnet.ibm.com, linuxram@us.ibm.com, fweimer@redhat.com, msuchanek@suse.com, tglx@linutronix.de, shuah@kernel.org Subject: [PATCH] powerpc, pkey: make protection key 0 less special Date: Mon, 26 Mar 2018 16:31:41 -0700 Message-Id: <1522107101-16083-1-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Applications need the ability to associate an address-range with some key and latter revert to its initial default key. Pkey-0 comes close to providing this function but falls short, because the current implementation disallows applications to explicitly associate pkey-0 to the address range. Lets make pkey-0 less special and treat it almost like any other key. Thus it can be explicitly associated with any address range, and can be freed. This gives the application more flexibility and power. The ability to free pkey-0 must be used responsibily, since pkey-0 is associated with almost all address-range by default. Even with this change pkey-0 continues to be slightly more special from the following point of view. (a) it is implicitly allocated. (b) it is the default key assigned to any address-range. Tested on powerpc. cc: Thomas Gleixner cc: Dave Hansen cc: Michael Ellermen cc: Ingo Molnar cc: Andrew Morton Signed-off-by: Ram Pai --- arch/powerpc/include/asm/pkeys.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h index 0409c80..9c7d3bd 100644 --- a/arch/powerpc/include/asm/pkeys.h +++ b/arch/powerpc/include/asm/pkeys.h @@ -101,10 +101,18 @@ static inline u16 pte_to_pkey_bits(u64 pteflags) static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey) { - /* A reserved key is never considered as 'explicitly allocated' */ - return ((pkey < arch_max_pkey()) && - !__mm_pkey_is_reserved(pkey) && - __mm_pkey_is_allocated(mm, pkey)); + /* pkey 0 is allocated by default. */ + if (!pkey) + return true; + + if (pkey < 0 || pkey >= arch_max_pkey()) + return false; + + /* Reserved keys are never allocated. */ + if (__mm_pkey_is_reserved(pkey)) + return false; + + return __mm_pkey_is_allocated(mm, pkey); } extern void __arch_activate_pkey(int pkey); @@ -200,6 +208,14 @@ static inline int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, { if (static_branch_likely(&pkey_disabled)) return -EINVAL; + + /* + * userspace is discouraged from changing permissions of + * pkey-0. powerpc hardware does not support it anyway. + */ + if (!pkey) + return init_val ? -EINVAL : 0; + return __arch_set_user_pkey_access(tsk, pkey, init_val); } -- 1.8.3.1