{"id":811877,"url":"http://patchwork.ozlabs.org/api/patches/811877/?format=json","web_url":"http://patchwork.ozlabs.org/project/linuxppc-dev/patch/1504910713-7094-22-git-send-email-linuxram@us.ibm.com/","project":{"id":2,"url":"http://patchwork.ozlabs.org/api/projects/2/?format=json","name":"Linux PPC development","link_name":"linuxppc-dev","list_id":"linuxppc-dev.lists.ozlabs.org","list_email":"linuxppc-dev@lists.ozlabs.org","web_url":"https://github.com/linuxppc/wiki/wiki","scm_url":"https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git","webscm_url":"https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git/","list_archive_url":"https://lore.kernel.org/linuxppc-dev/","list_archive_url_format":"https://lore.kernel.org/linuxppc-dev/{}/","commit_url_format":"https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git/commit/?id={}"},"msgid":"<1504910713-7094-22-git-send-email-linuxram@us.ibm.com>","list_archive_url":"https://lore.kernel.org/linuxppc-dev/1504910713-7094-22-git-send-email-linuxram@us.ibm.com/","date":"2017-09-08T22:45:01","name":"[13/25] powerpc: implementation for arch_override_mprotect_pkey()","commit_ref":null,"pull_url":null,"state":"changes-requested","archived":false,"hash":"5643966d07f732451f47be04ce99c80e1d319e4d","submitter":{"id":2667,"url":"http://patchwork.ozlabs.org/api/people/2667/?format=json","name":"Ram Pai","email":"linuxram@us.ibm.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/linuxppc-dev/patch/1504910713-7094-22-git-send-email-linuxram@us.ibm.com/mbox/","series":[{"id":2303,"url":"http://patchwork.ozlabs.org/api/series/2303/?format=json","web_url":"http://patchwork.ozlabs.org/project/linuxppc-dev/list/?series=2303","date":"2017-09-08T22:44:40","name":"powerpc: Free up RPAGE_RSV bits","version":1,"mbox":"http://patchwork.ozlabs.org/series/2303/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/811877/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/811877/checks/","tags":{},"related":[],"headers":{"Return-Path":"<linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org>","X-Original-To":["patchwork-incoming@ozlabs.org","linuxppc-dev@lists.ozlabs.org"],"Delivered-To":["patchwork-incoming@ozlabs.org","linuxppc-dev@lists.ozlabs.org"],"Received":["from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3])\n\t(using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xptlp4SPXz9sBW\n\tfor <patchwork-incoming@ozlabs.org>;\n\tSat,  9 Sep 2017 09:27:50 +1000 (AEST)","from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3])\n\tby lists.ozlabs.org (Postfix) with ESMTP id 3xptlp3HN9zDrSP\n\tfor <patchwork-incoming@ozlabs.org>;\n\tSat,  9 Sep 2017 09:27:50 +1000 (AEST)","from mail-qt0-x242.google.com (mail-qt0-x242.google.com\n\t[IPv6:2607:f8b0:400d:c0d::242])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128\n\tbits)) (No client certificate requested)\n\tby lists.ozlabs.org (Postfix) with ESMTPS id 3xpsrp2yZhzDrdP\n\tfor <linuxppc-dev@lists.ozlabs.org>;\n\tSat,  9 Sep 2017 08:47:06 +1000 (AEST)","by mail-qt0-x242.google.com with SMTP id q8so2360196qtb.1\n\tfor <linuxppc-dev@lists.ozlabs.org>;\n\tFri, 08 Sep 2017 15:47:06 -0700 (PDT)","from localhost.localdomain (50-39-103-96.bvtn.or.frontiernet.net.\n\t[50.39.103.96]) by smtp.gmail.com with ESMTPSA id\n\tx124sm2033726qka.85.2017.09.08.15.47.02\n\t(version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\n\tFri, 08 Sep 2017 15:47:03 -0700 (PDT)"],"Authentication-Results":["ozlabs.org;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=gmail.com header.i=@gmail.com\n\theader.b=\"GC+qLuEA\"; dkim-atps=neutral","lists.ozlabs.org;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=gmail.com header.i=@gmail.com\n\theader.b=\"GC+qLuEA\"; dkim-atps=neutral","ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=gmail.com\n\t(client-ip=2607:f8b0:400d:c0d::242; helo=mail-qt0-x242.google.com;\n\tenvelope-from=ram.n.pai@gmail.com; receiver=<UNKNOWN>)","lists.ozlabs.org; dkim=pass (2048-bit key;\n\tunprotected) header.d=gmail.com header.i=@gmail.com\n\theader.b=\"GC+qLuEA\"; dkim-atps=neutral"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;\n\th=sender:from:to:cc:subject:date:message-id:in-reply-to:references;\n\tbh=ffhvGSAYYl0zuVaBMwISCt8UrGJHefMpOpo06URiQnU=;\n\tb=GC+qLuEAI5yrm6uV30ktbJNQM8W4umMV9o4J0bn1RZPUXrWRz1TO4eLxYGn3vN1lUs\n\t7WZJAJnBisKbD3OIVkkef36zaxeWSVuVzLU0BcoSIQZ2eENouwmm1y4L0GcRBEN8GeKS\n\tRrtIXuiu5LyIStyPaOz8nZRd/8fq7UHd4DgzAImrkCUrNt+oa/8eDPsTjvFAR6y00Z2C\n\tC6dytHKySuoYrBfd2zQMh0vFQ1hgbMOG7IsE/YeyldY90s4/aFj+Do1mBDf2dKZwCZjz\n\tTPouzsUmRiTPW6svmztEb34Od4VXYT7G+iAqkIBSDRiO9nzoMCdUr8KBbdFvb/IWSQLr\n\tP8tw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:sender:from:to:cc:subject:date:message-id\n\t:in-reply-to:references;\n\tbh=ffhvGSAYYl0zuVaBMwISCt8UrGJHefMpOpo06URiQnU=;\n\tb=lz8XFY0Ctqxoy0XRqrgu81+N9R6lS2UQ0BVsaTwdjta6LiaeMnoTDr1ZZHaA+r/2/h\n\t4l++PJ4Clr7/oZfZi8Qo+/2Xe6F68vcxNHvV7Ep3q0j4aDjvJxO8X3X+tlNqQsHmIaZS\n\tGxz5ahjX0TV9Hr0YK8s77i1JG03xW7abjY9mc+lnAkk3nwuZ0oee6UTQ9z8NH5iAtnUA\n\tarIy2uWGyV0n5hW3wXTQVA/Q/91RJylg7aHHT8v6svpN6XlyySAbODW4gSQ33HuFcE3g\n\tQBcjh9Remkf5vGqBoYv7c0Hoq0oOWs+hhG357w4c+pEERRsExwFWCYXffyLwQ34sBc5e\n\to0BA==","X-Gm-Message-State":"AHPjjUh0TiFLZ9Lf48ELW6YxzCpXQX+pcqT8wGJ8Z3ZMwjoyqCplvyW/\n\td2eGXNhFTRC2ntXT","X-Google-Smtp-Source":"AOwi7QC7BHQFgh9fWZApmQgMUoYjTrzw2RbBe3y7/dEUHWAhYqi0swN0DDtE5ZB/DSAnociOYussfw==","X-Received":"by 10.200.28.123 with SMTP id j56mr6044793qtk.299.1504910824521; \n\tFri, 08 Sep 2017 15:47:04 -0700 (PDT)","From":"Ram Pai <linuxram@us.ibm.com>","To":"mpe@ellerman.id.au,\n\tlinuxppc-dev@lists.ozlabs.org","Subject":"[PATCH 13/25] powerpc: implementation for\n\tarch_override_mprotect_pkey()","Date":"Fri,  8 Sep 2017 15:45:01 -0700","Message-Id":"<1504910713-7094-22-git-send-email-linuxram@us.ibm.com>","X-Mailer":"git-send-email 1.7.1","In-Reply-To":"<1504910713-7094-1-git-send-email-linuxram@us.ibm.com>","References":"<1504910713-7094-1-git-send-email-linuxram@us.ibm.com>","X-BeenThere":"linuxppc-dev@lists.ozlabs.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"Linux on PowerPC Developers Mail List\n\t<linuxppc-dev.lists.ozlabs.org>","List-Unsubscribe":"<https://lists.ozlabs.org/options/linuxppc-dev>,\n\t<mailto:linuxppc-dev-request@lists.ozlabs.org?subject=unsubscribe>","List-Archive":"<http://lists.ozlabs.org/pipermail/linuxppc-dev/>","List-Post":"<mailto:linuxppc-dev@lists.ozlabs.org>","List-Help":"<mailto:linuxppc-dev-request@lists.ozlabs.org?subject=help>","List-Subscribe":"<https://lists.ozlabs.org/listinfo/linuxppc-dev>,\n\t<mailto:linuxppc-dev-request@lists.ozlabs.org?subject=subscribe>","Cc":"ebiederm@xmission.com, linuxram@us.ibm.com, mhocko@kernel.org,\n\tpaulus@samba.org, aneesh.kumar@linux.vnet.ibm.com,\n\tbauerman@linux.vnet.ibm.com, khandual@linux.vnet.ibm.com","Errors-To":"linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org","Sender":"\"Linuxppc-dev\"\n\t<linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org>"},"content":"arch independent code calls arch_override_mprotect_pkey()\nto return a pkey that best matches the requested protection.\n\nThis patch provides the implementation.\n\nSigned-off-by: Ram Pai <linuxram@us.ibm.com>\n---\n arch/powerpc/include/asm/mmu_context.h |    5 +++\n arch/powerpc/include/asm/pkeys.h       |   17 ++++++++++-\n arch/powerpc/mm/pkeys.c                |   47 ++++++++++++++++++++++++++++++++\n 3 files changed, 67 insertions(+), 2 deletions(-)","diff":"diff --git a/arch/powerpc/include/asm/mmu_context.h b/arch/powerpc/include/asm/mmu_context.h\nindex c705a5d..8e5a87e 100644\n--- a/arch/powerpc/include/asm/mmu_context.h\n+++ b/arch/powerpc/include/asm/mmu_context.h\n@@ -145,6 +145,11 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,\n #ifndef CONFIG_PPC64_MEMORY_PROTECTION_KEYS\n #define pkey_initialize()\n #define pkey_mm_init(mm)\n+\n+static inline int vma_pkey(struct vm_area_struct *vma)\n+{\n+\treturn 0;\n+}\n #endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */\n \n #endif /* __KERNEL__ */\ndiff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h\nindex f13e913..d2fffef 100644\n--- a/arch/powerpc/include/asm/pkeys.h\n+++ b/arch/powerpc/include/asm/pkeys.h\n@@ -41,6 +41,16 @@ static inline u64 pkey_to_vmflag_bits(u16 pkey)\n \t\t((pkey & 0x10UL) ? VM_PKEY_BIT4 : 0x0UL));\n }\n \n+#define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | \\\n+\t\t\t\tVM_PKEY_BIT3 | VM_PKEY_BIT4)\n+\n+static inline int vma_pkey(struct vm_area_struct *vma)\n+{\n+\tif (!pkey_inited)\n+\t\treturn 0;\n+\treturn (vma->vm_flags & ARCH_VM_PKEY_FLAGS) >> VM_PKEY_SHIFT;\n+}\n+\n #define arch_max_pkey()  pkeys_total\n #define AMR_RD_BIT 0x1UL\n #define AMR_WR_BIT 0x2UL\n@@ -142,11 +152,14 @@ static inline int execute_only_pkey(struct mm_struct *mm)\n \treturn __execute_only_pkey(mm);\n }\n \n-\n+extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,\n+\t\tint prot, int pkey);\n static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,\n \t\tint prot, int pkey)\n {\n-\treturn 0;\n+\tif (!pkey_inited)\n+\t\treturn 0;\n+\treturn __arch_override_mprotect_pkey(vma, prot, pkey);\n }\n \n extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,\ndiff --git a/arch/powerpc/mm/pkeys.c b/arch/powerpc/mm/pkeys.c\nindex 8a24983..fb1a76a 100644\n--- a/arch/powerpc/mm/pkeys.c\n+++ b/arch/powerpc/mm/pkeys.c\n@@ -245,3 +245,50 @@ int __execute_only_pkey(struct mm_struct *mm)\n \t\tmm->context.execute_only_pkey = execute_only_pkey;\n \treturn execute_only_pkey;\n }\n+\n+static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)\n+{\n+\t/* Do this check first since the vm_flags should be hot */\n+\tif ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC)\n+\t\treturn false;\n+\n+\treturn (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);\n+}\n+\n+/*\n+ * This should only be called for *plain* mprotect calls.\n+ */\n+int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,\n+\t\tint pkey)\n+{\n+\t/*\n+\t * Is this an mprotect_pkey() call?  If so, never\n+\t * override the value that came from the user.\n+\t */\n+\tif (pkey != -1)\n+\t\treturn pkey;\n+\n+\t/*\n+\t * If the currently associated pkey is execute-only,\n+\t * but the requested protection requires read or write,\n+\t * move it back to the default pkey.\n+\t */\n+\tif (vma_is_pkey_exec_only(vma) &&\n+\t    (prot & (PROT_READ|PROT_WRITE)))\n+\t\treturn 0;\n+\n+\t/*\n+\t * the requested protection is execute-only. Hence\n+\t * lets use a execute-only pkey.\n+\t */\n+\tif (prot == PROT_EXEC) {\n+\t\tpkey = execute_only_pkey(vma->vm_mm);\n+\t\tif (pkey > 0)\n+\t\t\treturn pkey;\n+\t}\n+\n+\t/*\n+\t * nothing to override.\n+\t */\n+\treturn vma_pkey(vma);\n+}\n","prefixes":["13/25"]}