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: 10529535 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 17C026020A for ; Tue, 17 Jul 2018 13:51:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F000329203 for ; Tue, 17 Jul 2018 13:51:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E450929221; Tue, 17 Jul 2018 13:51:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7CE0C29203 for ; Tue, 17 Jul 2018 13:51:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F39B96B027F; Tue, 17 Jul 2018 09:50:50 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E98EB6B0280; Tue, 17 Jul 2018 09:50:50 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3BCA6B0281; Tue, 17 Jul 2018 09:50:50 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk0-f200.google.com (mail-qk0-f200.google.com [209.85.220.200]) by kanga.kvack.org (Postfix) with ESMTP id A195D6B027F for ; Tue, 17 Jul 2018 09:50:50 -0400 (EDT) Received: by mail-qk0-f200.google.com with SMTP id d194-v6so813818qkb.12 for ; Tue, 17 Jul 2018 06:50:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:sender:from:to:cc:subject:date :message-id:in-reply-to:references; bh=yYIfqspIb2W9t1HxLutXL4gP5ZOQBs2SC6vIiqZfOXQ=; b=rlZrhLJf9vktZ/Ds8esx3c49NzkMmd2LrO+1WrVU0WphyM6mlQ2Xv5Au81exFo24F0 pN1r7XIWtg8iDgqalD7bGNee3vCYRvTP4mcZKM11J9gszmZxbNOOIfB5dj15Cl+uNBRI fj+7+p7wUOcKR3jVURamaQlzZhe+tiXEx946mehioNSWQL2PgAyll3vlPfVwwi7M31EY I72iibfZuy4DT+EFG+F5uI1ABYYDic7X2JevNEfBqi/NzX9T1uZdkV0McNR6ag0Y3SA8 5Xw3TmDw7TBxecPCOgGHJAHdvTHm/6NiU257JRSN73qhrilhDZOwkzTC4ELtR9hJEpGU SL+g== X-Gm-Message-State: AOUpUlFfbvYjV6DJYfWRAJYOPaUf4hvebIEj8ZP+/qWN09krZvCTtuoZ bCfrhRZa3xbtEFq5V/hMMw154DHLN3tn+CTC4LNmhXogF8LU4EzgSmG0uiZWQzLyltws7wD/pw9 BpepBrpkEUhPMgzHWa3GNlBkmDuT609LGQIVVkR/MLXcBvUY4JKdiftTt/YVdcaAezury9aOx56 Et96fQah8Kw/CmASdj+xrNhngkgvH5vEt+uPMJNYqDcUmRUZkTG4KbU5Kn5gwmG1VZuq8VekPOZ iC1d2yyTpi9RGPK/Fwmqr85cHGNNDwBUfLqn8k8eDdJ0TwzMN2C8WEJ1xjxLeva6QgV/cV190GI OI3jjCBjK3z8B2Xslo8anLOzf2eF77covASZYFrcHkt+mP08qLYeRa4ynjOJq0gbmOx2xPxBQQ= = X-Received: by 2002:ac8:43da:: with SMTP id w26-v6mr1585157qtn.137.1531835450450; Tue, 17 Jul 2018 06:50:50 -0700 (PDT) X-Received: by 2002:ac8:43da:: with SMTP id w26-v6mr1585107qtn.137.1531835449836; Tue, 17 Jul 2018 06:50:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531835449; cv=none; d=google.com; s=arc-20160816; b=jWFopmX9G+sTYX+sDkjXRiHZI3AC0k4b5c6dfrf9D49LIMeEE4nLaO8v/4S1W+K/m6 gsP3nYrq59I0hLhvZLpjQRfTQsbUnvSaqzlQ6ubNXN+aNIGpTg9ZxUHq/UP6wdvEBfkq iquxcR0JgInqDriblburdgnR8LU+BSe8XhctLVv5MH9kGEgOkw5NcICCjm1XKUWGvowc YljLudCJWGEemyTIdsZdnTFD+fYKXZ+V4oYjoKhIE7qa4vOuln1T+Irm4n2aMFb8y4ny sEQw1M+tlCf6tYcWrSFWYBl4DZWH/ON1a+k32PSVpLlMfwcb8hC72d5pX6nCGM/3Gh1a l5TQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature:arc-authentication-results; bh=yYIfqspIb2W9t1HxLutXL4gP5ZOQBs2SC6vIiqZfOXQ=; b=um9SfH1VS9+XfbpV2K6n3tdDrA6CPKqxe40T/3ZtzGkFLEFXIC2qEDKAYHSu4EtXcb URU8kTsRngmtJTXVtaqQULG6GvGaEAyWQ2PCl36jeG1yvVOiAHofA7DYDgRA5borX1vg gZu7joAJOtJhPa9xkl7tZ16bekObwrONArdsETHfqTkUIP71HIUs/zhayg9OmI8O/9L7 61mZa6uaKp1/EjoZtYV0UIJY+aITGjJ0HZIal+SuOzjpkqek9E/8x9cTgJdi6LfgWThc J5VpkoKzf0mj6X4dSLM8GNyfv85L2knB9dQgo4V8vT1sPp2C2LgMiofPTGUXWeHfwa3W o1fQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=VckNBPSA; spf=pass (google.com: domain of ram.n.pai@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=ram.n.pai@gmail.com; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id z54-v6sor498858qth.85.2018.07.17.06.50.49 for (Google Transport Security); Tue, 17 Jul 2018 06:50:49 -0700 (PDT) Received-SPF: pass (google.com: domain of ram.n.pai@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=VckNBPSA; spf=pass (google.com: domain of ram.n.pai@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=ram.n.pai@gmail.com; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com 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-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 Cc: mpe@ellerman.id.au, linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, x86@kernel.org, linux-arch@vger.kernel.org, mingo@redhat.com, dave.hansen@intel.com, mhocko@kernel.org, bauerman@linux.vnet.ibm.com, linuxram@us.ibm.com, fweimer@redhat.com, msuchanek@suse.de, aneesh.kumar@linux.vnet.ibm.com 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-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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)