From patchwork Thu Jun 14 00:45:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ram Pai X-Patchwork-Id: 10463101 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 3EE85603EE for ; Thu, 14 Jun 2018 00:47:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2F01128988 for ; Thu, 14 Jun 2018 00:47:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 234BA28BA8; Thu, 14 Jun 2018 00:47: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 7DA1A28988 for ; Thu, 14 Jun 2018 00:47:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 256D76B027A; Wed, 13 Jun 2018 20:47:14 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 2068B6B027C; Wed, 13 Jun 2018 20:47:14 -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 0A8186B027D; Wed, 13 Jun 2018 20:47:14 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt0-f197.google.com (mail-qt0-f197.google.com [209.85.216.197]) by kanga.kvack.org (Postfix) with ESMTP id D1E216B027A for ; Wed, 13 Jun 2018 20:47:13 -0400 (EDT) Received: by mail-qt0-f197.google.com with SMTP id l10-v6so3350515qth.14 for ; Wed, 13 Jun 2018 17:47:13 -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=HI9LoqRiOZWCEjLEpHJR6OkKcCsClS86IFXge6gdTAI=; b=q0wlN0HdZMBrQl0PY1y0WFEdoHBmM2b8h/apQnvCKhmr11yRpUTlxXWgIxNzGFs6Lh +EVTzQeYt+MEZIi1dnnb+o3SzTRMsXa8hVRtuTD0NjqGUf7jtBHMxZw8XtjfOSDJHcLT +j9xJzYoiSNXX0c/NTedjYHkO8JC3tfekkp6tgF85vKMegHrYD9KqVPqm074ccKMknUR iceDc108wCHUFOYoPIvwS+8UgY1tFJlVPo2obShUz5fzvU6beBpsg0iQnc0qcu9xo/ga eOv2ksi493jD7kG6X8GA3EFIn3fcdDptv1RuXmurHWURHiweCH5OA9/1I2JJ9dtek6pq K5GA== X-Gm-Message-State: APt69E0hkzMaqqnI0j4cSS1WboTVJoea4qDiqGjLhk/+R0YNQkHH9ZxG itPs7ziVB0OCUEs3sOiz7ebwkK+zUVNexFs5g6Pfhs01Loi4kLhI4O+sAw+RuA9C+J30fJmEB9V z69oyyaOy/fFLzs/HXiUUGaGZ3SJyh51f0ZSUNjv9y8p1lsRMScDOLSrWpQQ8jSpc4MeueZBiMW owxfpJcjk4qf69WZwDoMlf+1DtkYrQfwkr3tMJCcU2HBxqWBgVtIa0Qxj0HrLgkA7oUvTnPcLik X8L10kBBv6PhsxmO2W3BfJGgIKduMKj9y+MgVZ+7o5QxcBmbdreV6FrT1M6uHsCnqXQ8eMKoWO9 j9t8BpL20THVhQRJSIXP3gxMp/hCUeYuYXMSOlRQr5mp6wWRfVoC0hrBuVFhHFWO7yaPjrNbdw= = X-Received: by 2002:a0c:b084:: with SMTP id o4-v6mr357048qvc.133.1528937233591; Wed, 13 Jun 2018 17:47:13 -0700 (PDT) X-Received: by 2002:a0c:b084:: with SMTP id o4-v6mr357018qvc.133.1528937232756; Wed, 13 Jun 2018 17:47:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528937232; cv=none; d=google.com; s=arc-20160816; b=IPcrtYA/2imBuRMX92BqD01hstx+epH2G8deAK6UGB0ah39DPLLhpoCi+kM6gFBGPC Xl6DdEILavFO/ACtXxExP6NJ4Mrrx7JJ6EAvxw3h4qd5B+dcRVU1RMKSGBHUSXbzmpV7 LsoeqGnxzKQbWlbHWv86RgB3oWj2cypP7nVz6JD7Iw3pJeAnH2DkeYSZKa3h841VrRV5 ENyEJkHKfZK4S+29CUiQzbOQ8ThBfOvnTbORsLgESGmlHgRfIlWWTocvH3HdjAxR8982 4Nnw8stFwUlocE449ByFTlumbfvTWyjIw2S0yxmyqHdKov0HplPhNwgBH7Tl/W9VpCu2 IuYw== 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=HI9LoqRiOZWCEjLEpHJR6OkKcCsClS86IFXge6gdTAI=; b=TLP4/j8qkAxJqWJC8UDeIP+czkHufDL7AmBYgYiOsgiDiRmZ411rTOyXtC8yuTzcVQ F74pc6CZr2DwRSBZHnu6RHHuMUejPYzcYwkNljPHi5SgHi4f0X+X7T3+eoanfnaRLMr/ tJ2Ila2rTMO29VFIbUnLrRJTjPZl347XwumGUmt+Ydg4zBFM6DnqsyUsFouzNxsoRXK7 dcQeBWyYLwP6Lseqz6lnZBOFCqHQiOCdBulHS7GYh4/tpPkgIKIXYs7cCjRGXy0h6jVy MTTg2HMJEucRcbzb+MGrkqiYYbdEeOoYuhsT1/UM4Z4Ak8nL+FJiGAHZKHuXPRxlVlyL iIDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qCngRLv0; 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 t14-v6sor2312017qtg.80.2018.06.13.17.47.12 for (Google Transport Security); Wed, 13 Jun 2018 17:47:12 -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=qCngRLv0; 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=HI9LoqRiOZWCEjLEpHJR6OkKcCsClS86IFXge6gdTAI=; b=qCngRLv0enwWEbI2Ts5N2RW3yuNOAsv1pFtlxKu61r1SwHl1agMO2SsUV5G1hbqS/I YV54fesrJyPuBEUvBnoLcfBGVsnhYpH2UDu8/aT+pvN+QvgvL+yCkYEP+HCMqp60PqSl w6vvn5ktWZg+R5ooYwwp5RF4N6D5WAvVbeZ9rJAH0X2OJ+eJagGR+kpCYUH1qyi9WDrG fd5ichPEP/ZebStEnhud8tD3HM1YmnZMVC8vkqCCuhxOAa0XdDmZd1RNLdxkb0rfbbSt k7xZSfpdnjN8UkYtyqveGhbQ8o6zZJxHpcxfakYwC5wVj2QGl6O03rAbVb4ZKBk13Wdj runQ== X-Google-Smtp-Source: ADUXVKJYspQchSOeP8CQnGfmugqYaM1zim9NWUuOXo4T+GdYvLaGA0FdMt6I4uEVsDDxIk4eD8DevQ== X-Received: by 2002:ac8:3338:: with SMTP id t53-v6mr368573qta.312.1528937232452; Wed, 13 Jun 2018 17:47:12 -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.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Jun 2018 17:47:12 -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 14/24] selftests/vm: generic cleanup Date: Wed, 13 Jun 2018 17:45:05 -0700 Message-Id: <1528937115-10132-15-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> 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 cleanup the code to satisfy coding styles. cc: Dave Hansen cc: Florian Weimer Signed-off-by: Ram Pai --- tools/testing/selftests/vm/protection_keys.c | 64 +++++++++++++++++-------- 1 files changed, 43 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/vm/protection_keys.c b/tools/testing/selftests/vm/protection_keys.c index adcae4a..f43a319 100644 --- a/tools/testing/selftests/vm/protection_keys.c +++ b/tools/testing/selftests/vm/protection_keys.c @@ -4,7 +4,7 @@ * * There are examples in here of: * * how to set protection keys on memory - * * how to set/clear bits in pkey registers (the rights register) + * * how to set/clear bits in Protection Key registers (the rights register) * * how to handle SEGV_PKUERR signals and extract pkey-relevant * information from the siginfo * @@ -13,13 +13,18 @@ * prefault pages in at malloc, or not * protect MPX bounds tables with protection keys? * make sure VMA splitting/merging is working correctly - * OOMs can destroy mm->mmap (see exit_mmap()), so make sure it is immune to pkeys - * look for pkey "leaks" where it is still set on a VMA but "freed" back to the kernel - * do a plain mprotect() to a mprotect_pkey() area and make sure the pkey sticks + * OOMs can destroy mm->mmap (see exit_mmap()), + * so make sure it is immune to pkeys + * look for pkey "leaks" where it is still set on a VMA + * but "freed" back to the kernel + * do a plain mprotect() to a mprotect_pkey() area and make + * sure the pkey sticks * * Compile like this: - * gcc -o protection_keys -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm - * gcc -m32 -o protection_keys_32 -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm + * gcc -o protection_keys -O2 -g -std=gnu99 + * -pthread -Wall protection_keys.c -lrt -ldl -lm + * gcc -m32 -o protection_keys_32 -O2 -g -std=gnu99 + * -pthread -Wall protection_keys.c -lrt -ldl -lm */ #define _GNU_SOURCE #include @@ -263,10 +268,12 @@ void signal_handler(int signum, siginfo_t *si, void *vucontext) __read_pkey_reg()); dprintf1("pkey from siginfo: %jx\n", siginfo_pkey); *(u64 *)pkey_reg_ptr = 0x00000000; - dprintf1("WARNING: set PRKU=0 to allow faulting instruction to continue\n"); + dprintf1("WARNING: set PKEY_REG=0 to allow faulting instruction " + "to continue\n"); pkey_faults++; dprintf1("<<<<==================================================\n"); dprint_in_signal = 0; + return; } int wait_all_children(void) @@ -384,7 +391,7 @@ void pkey_disable_set(int pkey, int flags) { unsigned long syscall_flags = 0; int ret; - int pkey_rights; + u32 pkey_rights; pkey_reg_t orig_pkey_reg = read_pkey_reg(); dprintf1("START->%s(%d, 0x%x)\n", __func__, @@ -487,9 +494,10 @@ int sys_mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot, return sret; } -int sys_pkey_alloc(unsigned long flags, unsigned long init_val) +int sys_pkey_alloc(unsigned long flags, u64 init_val) { int ret = syscall(SYS_pkey_alloc, flags, init_val); + dprintf1("%s(flags=%lx, init_val=%lx) syscall ret: %d errno: %d\n", __func__, flags, init_val, ret, errno); return ret; @@ -513,7 +521,7 @@ void pkey_set_shadow(u32 key, u64 init_val) int alloc_pkey(void) { int ret; - unsigned long init_val = 0x0; + u64 init_val = 0x0; dprintf1("%s()::%d, pkey_reg: "PKEY_REG_FMT" shadow: "PKEY_REG_FMT"\n", __func__, __LINE__, __read_pkey_reg(), shadow_pkey_reg); @@ -672,7 +680,9 @@ void record_pkey_malloc(void *ptr, long size, int prot) /* every record is full */ size_t old_nr_records = nr_pkey_malloc_records; size_t new_nr_records = (nr_pkey_malloc_records * 2 + 1); - size_t new_size = new_nr_records * sizeof(struct pkey_malloc_record); + size_t new_size = new_nr_records * + sizeof(struct pkey_malloc_record); + dprintf2("new_nr_records: %zd\n", new_nr_records); dprintf2("new_size: %zd\n", new_size); pkey_malloc_records = realloc(pkey_malloc_records, new_size); @@ -698,9 +708,11 @@ void free_pkey_malloc(void *ptr) { long i; int ret; + dprintf3("%s(%p)\n", __func__, ptr); for (i = 0; i < nr_pkey_malloc_records; i++) { struct pkey_malloc_record *rec = &pkey_malloc_records[i]; + dprintf4("looking for ptr %p at record[%ld/%p]: {%p, %ld}\n", ptr, i, rec, rec->ptr, rec->size); if ((ptr < rec->ptr) || @@ -781,11 +793,13 @@ void setup_hugetlbfs(void) char buf[] = "123"; if (geteuid() != 0) { - fprintf(stderr, "WARNING: not run as root, can not do hugetlb test\n"); + fprintf(stderr, + "WARNING: not run as root, can not do hugetlb test\n"); return; } - cat_into_file(__stringify(GET_NR_HUGE_PAGES), "/proc/sys/vm/nr_hugepages"); + cat_into_file(__stringify(GET_NR_HUGE_PAGES), + "/proc/sys/vm/nr_hugepages"); /* * Now go make sure that we got the pages and that they @@ -806,7 +820,8 @@ void setup_hugetlbfs(void) } if (atoi(buf) != GET_NR_HUGE_PAGES) { - fprintf(stderr, "could not confirm 2M pages, got: '%s' expected %d\n", + fprintf(stderr, "could not confirm 2M pages, got:" + " '%s' expected %d\n", buf, GET_NR_HUGE_PAGES); return; } @@ -948,6 +963,7 @@ void __save_test_fd(int fd) int get_test_read_fd(void) { int test_fd = open("/etc/passwd", O_RDONLY); + __save_test_fd(test_fd); return test_fd; } @@ -989,7 +1005,8 @@ void test_read_of_access_disabled_region(int *ptr, u16 pkey) { int ptr_contents; - dprintf1("disabling access to PKEY[%02d], doing read @ %p\n", pkey, ptr); + dprintf1("disabling access to PKEY[%02d], doing read @ %p\n", + pkey, ptr); read_pkey_reg(); pkey_access_deny(pkey); ptr_contents = read_ptr(ptr); @@ -1111,13 +1128,14 @@ void test_pkey_syscalls_bad_args(int *ptr, u16 pkey) /* Assumes that all pkeys other than 'pkey' are unallocated */ void test_pkey_alloc_exhaust(int *ptr, u16 pkey) { - int err; + int err = 0; int allocated_pkeys[NR_PKEYS] = {0}; int nr_allocated_pkeys = 0; int i; for (i = 0; i < NR_PKEYS*2; i++) { int new_pkey; + dprintf1("%s() alloc loop: %d\n", __func__, i); new_pkey = alloc_pkey(); dprintf4("%s()::%d, err: %d pkey_reg: 0x"PKEY_REG_FMT @@ -1125,9 +1143,11 @@ void test_pkey_alloc_exhaust(int *ptr, u16 pkey) __func__, __LINE__, err, __read_pkey_reg(), shadow_pkey_reg); read_pkey_reg(); /* for shadow checking */ - dprintf2("%s() errno: %d ENOSPC: %d\n", __func__, errno, ENOSPC); + dprintf2("%s() errno: %d ENOSPC: %d\n", + __func__, errno, ENOSPC); if ((new_pkey == -1) && (errno == ENOSPC)) { - dprintf2("%s() failed to allocate pkey after %d tries\n", + dprintf2("%s() failed to allocate pkey " + "after %d tries\n", __func__, nr_allocated_pkeys); break; } @@ -1419,7 +1439,8 @@ void run_tests_once(void) tracing_off(); close_test_fds(); - printf("test %2d PASSED (iteration %d)\n", test_nr, iteration_nr); + printf("test %2d PASSED (iteration %d)\n", + test_nr, iteration_nr); dprintf1("======================\n\n"); } iteration_nr++; @@ -1431,7 +1452,7 @@ int main(void) setup_handlers(); - printf("has pku: %d\n", cpu_has_pku()); + printf("has pkey: %d\n", cpu_has_pku()); if (!cpu_has_pku()) { int size = PAGE_SIZE; @@ -1439,7 +1460,8 @@ int main(void) printf("running PKEY tests for unsupported CPU/OS\n"); - ptr = mmap(NULL, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + ptr = mmap(NULL, size, PROT_NONE, + MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); assert(ptr != (void *)-1); test_mprotect_pkey_on_unsupported_cpu(ptr, 1); exit(0);