From patchwork Tue Jul 17 13:49:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ram Pai X-Patchwork-Id: 945098 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41VNqY4CXTz9rxs for ; Wed, 18 Jul 2018 01:04:57 +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="VckNBPSA"; 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 41VNqY299qzF3K6 for ; Wed, 18 Jul 2018 01:04:57 +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="VckNBPSA"; 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:c0d::241; helo=mail-qt0-x241.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="VckNBPSA"; dkim-atps=neutral Received: from mail-qt0-x241.google.com (mail-qt0-x241.google.com [IPv6:2607:f8b0:400d:c0d::241]) (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 41VMB42pTKzF3Hp for ; Tue, 17 Jul 2018 23:50:51 +1000 (AEST) Received: by mail-qt0-x241.google.com with SMTP id a18-v6so888195qtj.4 for ; Tue, 17 Jul 2018 06:50:51 -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:in-reply-to:references; bh=yYIfqspIb2W9t1HxLutXL4gP5ZOQBs2SC6vIiqZfOXQ=; b=VckNBPSAF3KZPD0ZaAPo35ov7AMJKXclbfZR295MMkdwoYuOQHYa/aV648x3wFbk6F HdYpJ8frjP81nNEwXxVpH7tF7SQldSyMZBdp+aix/NK98R08zC7Xms3dRR3edVSwVivC eZIF/883jQ+ZE+F3mtbAdHDoEo91bwCxz5IIaG8rv/STv9C9IqV8e3GNjKkHnjsCIcUt gV/glNmwQDno2HCjR259CI7jyXAHFL44h27Q3WEWMG/Z7OGGzA9w76FIuheoOHTLxIyX 0c+W/PAD4Y+A2oxfWRamOYuaO1b3+J8v/RU15adXVMlkkMJ39PCiplzlhb533APVowx4 Ecsw== 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 :in-reply-to:references; bh=yYIfqspIb2W9t1HxLutXL4gP5ZOQBs2SC6vIiqZfOXQ=; b=fk6pzb3D0C8+gneI1kYR/S1hB7B385O+NSFj0Li/9A3I2Uh2PNEcn+0Q4OhfAv/uFD VwxKWtxPPsI1tzCZYDd6VQ+SUBFRd2F21XxMwDTbDmHPVIqUg9Ycm58grBZJ16f5Jpu1 FoEuPesUsCXLgBaZpNBiXhA0MeySgk9YqQY+v2x+3tFE6AB0OeW5wnHgEBP1GlXId0Mm c/XSKVOCGn49+gvn/X4UIA672Zv1msNP81aBQljmJ8JqFalWDAbXlNbOrE9UFwQVeFT+ 7ZLOCVJ/STTxoktT31d1a9PgBoyf9OCotuJYu8YjEWCsTwQMX+/EUqMbBqTLOpdNoVGf cj0A== X-Gm-Message-State: AOUpUlF24TpnPRuTUi605nGEZ8uF26AK3/4uHZ6F0KxKvM2URU/CRQ2J MHmnyJbiGcneIJQKdfnFZyE= X-Google-Smtp-Source: AAOMgpf6WEDkoiZvEXexs2VhR6saE8yJ4rpaeuq411YinbRS/WiVnlspjS0qPkBi1+7KWz6gKNMxUw== X-Received: by 2002:ac8:29f6:: with SMTP id 51-v6mr1609813qtt.200.1531835449617; Tue, 17 Jul 2018 06:50:49 -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 s83-v6sm640937qki.20.2018.07.17.06.50.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Jul 2018 06:50:49 -0700 (PDT) From: Ram Pai To: shuahkh@osg.samsung.com, linux-kselftest@vger.kernel.org Subject: [PATCH v14 22/22] selftests/vm: test correct behavior of pkey-0 Date: Tue, 17 Jul 2018 06:49:25 -0700 Message-Id: <1531835365-32387-23-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1531835365-32387-1-git-send-email-linuxram@us.ibm.com> References: <1531835365-32387-1-git-send-email-linuxram@us.ibm.com> X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, fweimer@redhat.com, dave.hansen@intel.com, x86@kernel.org, linuxram@us.ibm.com, mhocko@kernel.org, linux-mm@kvack.org, mingo@redhat.com, aneesh.kumar@linux.vnet.ibm.com, 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" Ensure pkey-0 is allocated on start. Ensure pkey-0 can be attached dynamically in various modes, without failures. Ensure pkey-0 can be freed and allocated. Signed-off-by: Ram Pai --- tools/testing/selftests/vm/protection_keys.c | 66 +++++++++++++++++++++++++- 1 files changed, 64 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vm/protection_keys.c b/tools/testing/selftests/vm/protection_keys.c index 569faf1..156b449 100644 --- a/tools/testing/selftests/vm/protection_keys.c +++ b/tools/testing/selftests/vm/protection_keys.c @@ -999,6 +999,67 @@ void close_test_fds(void) return *ptr; } +void test_pkey_alloc_free_attach_pkey0(int *ptr, u16 pkey) +{ + int i, err; + int max_nr_pkey_allocs; + int alloced_pkeys[NR_PKEYS]; + int nr_alloced = 0; + int newpkey; + long size; + + assert(pkey_last_malloc_record); + size = pkey_last_malloc_record->size; + /* + * This is a bit of a hack. But mprotect() requires + * huge-page-aligned sizes when operating on hugetlbfs. + * So, make sure that we use something that's a multiple + * of a huge page when we can. + */ + if (size >= HPAGE_SIZE) + size = HPAGE_SIZE; + + + /* allocate every possible key and make sure key-0 never got allocated */ + max_nr_pkey_allocs = NR_PKEYS; + for (i = 0; i < max_nr_pkey_allocs; i++) { + int new_pkey = alloc_pkey(); + assert(new_pkey != 0); + + if (new_pkey < 0) + break; + alloced_pkeys[nr_alloced++] = new_pkey; + } + /* free all the allocated keys */ + for (i = 0; i < nr_alloced; i++) { + int free_ret; + + if (!alloced_pkeys[i]) + continue; + free_ret = sys_pkey_free(alloced_pkeys[i]); + pkey_assert(!free_ret); + } + + /* attach key-0 in various modes */ + err = sys_mprotect_pkey(ptr, size, PROT_READ, 0); + pkey_assert(!err); + err = sys_mprotect_pkey(ptr, size, PROT_WRITE, 0); + pkey_assert(!err); + err = sys_mprotect_pkey(ptr, size, PROT_EXEC, 0); + pkey_assert(!err); + err = sys_mprotect_pkey(ptr, size, PROT_READ|PROT_WRITE, 0); + pkey_assert(!err); + err = sys_mprotect_pkey(ptr, size, PROT_READ|PROT_WRITE|PROT_EXEC, 0); + pkey_assert(!err); + + /* free key-0 */ + err = sys_pkey_free(0); + pkey_assert(!err); + + newpkey = sys_pkey_alloc(0, 0x0); + assert(newpkey == 0); +} + void test_read_of_write_disabled_region(int *ptr, u16 pkey) { int ptr_contents; @@ -1144,10 +1205,10 @@ void test_kernel_gup_write_to_write_disabled_region(int *ptr, u16 pkey) void test_pkey_syscalls_on_non_allocated_pkey(int *ptr, u16 pkey) { int err; - int i = get_start_key(); + int i; /* Note: 0 is the default pkey, so don't mess with it */ - for (; i < NR_PKEYS; i++) { + for (i=1; i < NR_PKEYS; i++) { if (pkey == i) continue; @@ -1455,6 +1516,7 @@ void test_mprotect_pkey_on_unsupported_cpu(int *ptr, u16 pkey) test_pkey_syscalls_on_non_allocated_pkey, test_pkey_syscalls_bad_args, test_pkey_alloc_exhaust, + test_pkey_alloc_free_attach_pkey0, }; void run_tests_once(void)