From patchwork Wed Jun 20 05:34:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ram Pai X-Patchwork-Id: 931963 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 419YVY5MC4z9s4v for ; Wed, 20 Jun 2018 15:36:53 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=us.ibm.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="hNKMnScv"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 419YVY3vBlzF13x for ; Wed, 20 Jun 2018 15:36:53 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=us.ibm.com Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="hNKMnScv"; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gmail.com (client-ip=2607:f8b0:400d:c09::242; helo=mail-qk0-x242.google.com; envelope-from=ram.n.pai@gmail.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=us.ibm.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="hNKMnScv"; dkim-atps=neutral Received: from mail-qk0-x242.google.com (mail-qk0-x242.google.com [IPv6:2607:f8b0:400d:c09::242]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 419YSL1nr9zF0vb for ; Wed, 20 Jun 2018 15:34:58 +1000 (AEST) Received: by mail-qk0-x242.google.com with SMTP id q70-v6so1236537qke.1 for ; Tue, 19 Jun 2018 22:34:57 -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=wLQkHrJ6aRJEJG5TUTysLmQyAaWE0kDq6eesBRpqfe0=; b=hNKMnScvBrqec5TGp/kKZvDFMY9A+VpAykAXuhTIMY48b9ryxCoM5U6OgYGUrv6HAg X/oh1LhmsgFf4fykRpOUMLDC07ZmWt2BXgJRQibgt+HPDjoumc+Vwss+Tf2lTHLAaGeZ gQGmK/UzP5pmKJvw0M2g5BQnd8XWuvuUnPNONvencanEvJWSgXAD/hqAh6yam1J5wHf9 bvkRCLBCyo8mNoPpClFvu+c6DBV0XG9kEd6MrpXg09H0IprvxwWUle/KRjXI4UEec80L bnqqC28Zz934Br4hFAcXkyR0tTZmXC0DgRD7f5OpNN6hrpPj8/NSeH8MbkE2wQiCr7Sm 7r7A== 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=wLQkHrJ6aRJEJG5TUTysLmQyAaWE0kDq6eesBRpqfe0=; b=W24c0/Svi9FXmh/8xw8GsZdvyFSLxf51+DmZuePs/jUtEw4b+wiEjHCI2XipJgSFqC spvGkOG0+SujX8aUNK9K1rIJLo0eOiqlvw+BabnwNyxvrYdBU1Cwl+HGtPpgynk0YY7M vln1n1LSPLZ5UwiWLAhRxmHq7WAIkVLR5hzLvyHvh3MxrxLOCTsQk55jVAZMBx6lk9vq TRDACzWnv6njnSaHlKAdk36+TRztwybNx9aROtCooeNgMeap33nCmUnEKsnQ4ArvT/+C Pqui4mqJWNv9iZkNsFoDPTzVs5iKlsNTE1QePTtuG4xKz+xE24bCj3W87qBI/jlvOqeZ 8OEQ== X-Gm-Message-State: APt69E2pvsUCpX8hDytYnPeI6MfQfGFd9lXRf3lmS7fDgmsIOhmAhwDi G9pricnt/4eJNn7m22FWlZc= X-Google-Smtp-Source: ADUXVKLOHEk6yxhiADQuTgs9U5pWIvEi2gTXAx1Wnet6uLzZiXcVmBg2VvPGgovCPRXinWYZS0nlQQ== X-Received: by 2002:a37:307:: with SMTP id 7-v6mr16753878qkd.317.1529472893226; Tue, 19 Jun 2018 22:34:53 -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 z135-v6sm1024581qkz.82.2018.06.19.22.34.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Jun 2018 22:34:52 -0700 (PDT) From: Ram Pai To: mpe@ellerman.id.au Subject: [PATCH] powerpc/pkeys: preallocate execute_only key only if the key is available. Date: Tue, 19 Jun 2018 22:34:22 -0700 Message-Id: <1529472862-26506-1-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fweimer@redhat.com, Ulrich.Weigand@de.ibm.com, linuxram@us.ibm.com, mhocko@kernel.org, bauerman@linux.vnet.ibm.com, msuchanek@suse.de, linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" Key 2 is preallocated and reserved for execute-only key. In rare cases if key-2 is unavailable, mprotect(PROT_EXEC) will behave incorrectly. NOTE: mprotect(PROT_EXEC) uses execute-only key. Ensure key 2 is available for preallocation before reserving it for execute_only purpose. CC: Michael Ellerman CC: Thiago Jung Bauermann Signed-off-by: Ram Pai --- arch/powerpc/mm/pkeys.c | 14 +++++++++----- 1 files changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/powerpc/mm/pkeys.c b/arch/powerpc/mm/pkeys.c index cec990c..2013ef0 100644 --- a/arch/powerpc/mm/pkeys.c +++ b/arch/powerpc/mm/pkeys.c @@ -19,6 +19,7 @@ u64 pkey_amr_mask; /* Bits in AMR not to be touched */ u64 pkey_iamr_mask; /* Bits in AMR not to be touched */ u64 pkey_uamor_mask; /* Bits in UMOR not to be touched */ +u32 execute_only_key = 2; #define AMR_BITS_PER_PKEY 2 #define AMR_RD_BIT 0x1UL @@ -26,7 +27,6 @@ #define IAMR_EX_BIT 0x1UL #define PKEY_REG_BITS (sizeof(u64)*8) #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY)) -#define EXECUTE_ONLY_KEY 2 static void scan_pkey_feature(void) { @@ -122,8 +122,12 @@ int pkey_initialize(void) #else os_reserved = 0; #endif + + if ((pkeys_total - os_reserved) <= execute_only_key) + execute_only_key = -1; + /* Bits are in LE format. */ - reserved_allocation_mask = (0x1 << 1) | (0x1 << EXECUTE_ONLY_KEY); + reserved_allocation_mask = (0x1 << 1) | (0x1 << execute_only_key); initial_allocation_mask = reserved_allocation_mask | (0x1 << PKEY_0); /* register mask is in BE format */ @@ -132,11 +136,11 @@ int pkey_initialize(void) pkey_iamr_mask = ~0x0ul; pkey_iamr_mask &= ~(0x3ul << pkeyshift(PKEY_0)); - pkey_iamr_mask &= ~(0x3ul << pkeyshift(EXECUTE_ONLY_KEY)); + pkey_iamr_mask &= ~(0x3ul << pkeyshift(execute_only_key)); pkey_uamor_mask = ~0x0ul; pkey_uamor_mask &= ~(0x3ul << pkeyshift(PKEY_0)); - pkey_uamor_mask &= ~(0x3ul << pkeyshift(EXECUTE_ONLY_KEY)); + pkey_uamor_mask &= ~(0x3ul << pkeyshift(execute_only_key)); for (i = (pkeys_total - os_reserved); i < pkeys_total; i++) pkey_uamor_mask &= ~(0x3ul << pkeyshift(i)); @@ -151,7 +155,7 @@ void pkey_mm_init(struct mm_struct *mm) if (static_branch_likely(&pkey_disabled)) return; mm_pkey_allocation_map(mm) = initial_allocation_mask; - mm->context.execute_only_pkey = EXECUTE_ONLY_KEY; + mm->context.execute_only_pkey = execute_only_key; } static inline u64 read_amr(void)