From patchwork Thu Jun 14 00:45:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ram Pai X-Patchwork-Id: 10463097 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 95042600D0 for ; Thu, 14 Jun 2018 00:47:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87D7928988 for ; Thu, 14 Jun 2018 00:47:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7BFB728BA8; Thu, 14 Jun 2018 00:47:36 +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=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1684B28988 for ; Thu, 14 Jun 2018 00:47:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935720AbeFNArf (ORCPT ); Wed, 13 Jun 2018 20:47:35 -0400 Received: from mail-qt0-f193.google.com ([209.85.216.193]:42030 "EHLO mail-qt0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S936071AbeFNArd (ORCPT ); Wed, 13 Jun 2018 20:47:33 -0400 Received: by mail-qt0-f193.google.com with SMTP id y31-v6so4266875qty.9; Wed, 13 Jun 2018 17:47:32 -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=qUUbPlETo6D6vmHu5K+2mwBGEckBptKPXfmzRqNtbBU=; b=O/aw1krjOmVijT+4SS7tjz4vdX5Z2PtbAeuhtwS6PEg6nzFb0/3z0MFzjhs2x7zVdH 4CH0v5AoZIXIVUIKGdKSQS3Jfo9FnpA/q/lxrWkHFENprvzCD97Iy15VUiNuxutnfL00 +m9/kenISd9RNhqm8oMnQrauWDT/shSUh1hplu1ytuLqamwjn45OPcAvQDI9mw6RU3on /7fB0tsaCoBya6m9/J1xyV32y59RQbMcfm89xnG8C3NdW5U8z88pMBjCApOa7MujqZ4w hkF+qPnP8NYjuC1+J6gm7yYuQ6vbCH4YwJHSR4v3DDJDGdnht6PxKsT7YZsxLZ5bnAH9 Rn+w== 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=qUUbPlETo6D6vmHu5K+2mwBGEckBptKPXfmzRqNtbBU=; b=bsBuMK6612Kq9xtPZQp6JawCksJBBzSXVWe/Q869/KTtysuCbUEz1+lbkP6XOEBHpk J0v9hlC0wn3+oL+rQGWiXKBkB+IBWu60jPqt8fYjBFGNsCvof+s2GX9ohehv3/h7rtap O1xO1ETGqZCtxc8EdDXPivFHR2RTqM05IHwQehMJ4VisuA6nauQHB1ea2UbihHfPv2Z6 ijw33KPbZiFjmkd+Y+4WCzcTAC/DeR1OtP9geHLKRjXuvaS8YlLjQb30WHWywZUtDJ6Q rN5Z6osqFn3Mm+82qJrnnVFIcXT+IbLxtklml3Fs627DOICD+bSfngH0OpE9rxS5v37f Jh/A== X-Gm-Message-State: APt69E17xHHv6R91EqisrHzRYqGKCRVoOMqVanuVJrqoHpssXMTP2MLR vqUde5G0f5M0X0B5gTGurd4= X-Google-Smtp-Source: ADUXVKJlBgf41UmASKU6WzEdpRIM1oBbUx3Vcd2vYJtZZCi0AIKrabDrmKD+CbvRLhkQ8TP0SMNjPQ== X-Received: by 2002:a0c:d1f7:: with SMTP id k52-v6mr353572qvh.199.1528937252278; Wed, 13 Jun 2018 17:47:32 -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 u31-v6sm3225898qtc.28.2018.06.13.17.47.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Jun 2018 17:47:31 -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 v13 24/24] selftests/vm: test correct behavior of pkey-0 Date: Wed, 13 Jun 2018 17:45:15 -0700 Message-Id: <1528937115-10132-25-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1528937115-10132-1-git-send-email-linuxram@us.ibm.com> References: <1528937115-10132-1-git-send-email-linuxram@us.ibm.com> Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org 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 Acked-by: Dave Hansen --- 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 cbd87f8..f37b031 100644 --- a/tools/testing/selftests/vm/protection_keys.c +++ b/tools/testing/selftests/vm/protection_keys.c @@ -1003,6 +1003,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; @@ -1153,10 +1214,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; @@ -1465,6 +1526,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)