Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp5552576iob; Mon, 9 May 2022 20:26:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzoCMlhE1+H5mp2hVBQaILEC5/vzI7Mj+c4fsTgfxsuJDBI5UbXIVSdTX1JQ6sdpgjzJAQn X-Received: by 2002:a17:90b:1d82:b0:1dc:ba02:52a0 with SMTP id pf2-20020a17090b1d8200b001dcba0252a0mr20790474pjb.246.1652153218909; Mon, 09 May 2022 20:26:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652153218; cv=none; d=google.com; s=arc-20160816; b=mOq87hCYMyCWkEACHFtNkzcrKYSz6We/Aoccq68kAmaOAomt84zFroz9EdJwbBEzu8 I3goB5ExfsMflsIvi0LHacse6yLhGPc0PyQ+4O7bV0SbOLSMcuyjQPSHd4gQQEumhCyc miH0OFrE0sJAXMLJkvHBkZdg62n7wOLlTkU06pr3Vz/8tSwHabm/JV4wMidzpgKB+Dzg +LY8aPEoCm67Zw/dNWIIuuS7k5li3189B4TsPMvJvFa0/hmYVPvx/B7fn39aIXZfqtgW 6HT2/p1lFDhXvHuJ6ETDavXJfnHWVEdF0cHPxdsRFdh9HEKe1EcgSiVA8ISoC59zxes9 v8/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=DVOA1HWoo1xAq42eK94cnyQwLywIDvR1bD4VuJZi5js=; b=z/5kKN48J//RHHKEAIApO2A0savWL1I7hvJ5hUvCqnOsrd3WVXp6G8hZUugrU0pnw0 YIeF5f1LVlBuwt7r+e8Y4R21uLqyTqyj6fMgBla/l8bXNEkpKAW9HRDiaPrcMd3LBSGh p8PiMq8gcI5/KpYFSIzS08UvK5aGRfBAmyTyifjTGowPKLxpWu6iJmHi64Ca5BwJnqJQ I3rjT+bBHRnk8KywMbGj4BwBrBnj1rSH0YjZDNeb6oF6L4HGOQQOc1Sbcr/Szx3dnt6p G7cUmCbxTq3vqRkHbzpMm2qQsNj0gljJDfo1f7Py8MLkdd4frb46k5XLAaJ35r7IN50M qS6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=aeumVFAe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o15-20020a170902d4cf00b001542a6e4c9bsi2009438plg.485.2022.05.09.20.26.45; Mon, 09 May 2022 20:26:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=aeumVFAe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235042AbiEJDET (ORCPT + 99 others); Mon, 9 May 2022 23:04:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232406AbiEJDER (ORCPT ); Mon, 9 May 2022 23:04:17 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFB3D1C743A for ; Mon, 9 May 2022 20:00:19 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id ij27-20020a170902ab5b00b0015d41282214so9224679plb.9 for ; Mon, 09 May 2022 20:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=DVOA1HWoo1xAq42eK94cnyQwLywIDvR1bD4VuJZi5js=; b=aeumVFAeovTeBq/VEg4tfDKCF6fMQ1t6TdeJRMNC9XG/pCoX4IKrBBUuBKC7essrdM xaQntmkvQauUlJzB30tqRxOBxi5H6dLwNHLJGngdDd6DsCIxqECmPSdFb1ogrYsIGfoM qHtLUDtdeVuJL6hj9Evd2QIKlCfYt3+CBsHD/ydwPp31U+6TbXJ3W3Q1P9h20lh+b+Pd dMvRmB1AbU9PG5NHV6k+LSt8C00ytOZHl4x8wgy6/v0LdP0KDfvzwuEZt+0wmZbyzVAB aYvyzXQeLhb1TRQB3ar28rLR4YqbGKxpNY29XGu8AVBd7T5g/fideFxVJMYo6VgrtBCl /yWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=DVOA1HWoo1xAq42eK94cnyQwLywIDvR1bD4VuJZi5js=; b=6gFLJ1dCrbJm5qPnojSoTcCVEij8PRLVEabzakKcjxrDaEu5VMy4fFF0ndNZPBYl+h z9R/yDpNClcwxfDmOyePas8deC4Irl+H3Wv9jbiDWCDPJ4ch6xfaVZSbYzB+WP8Z+v+M kMMdhFQJsPwjZCMebsQZairtvaPpPPdIuO1o9woJzxn+5GoKJEHHzw9k7wn0DC9x/oPK LRgQPzXnIDPDBAXkFUAJMBVni9xEdBEyYyLVEy/XpdjYyPV9oWrgTnfofWhUswirZKog zx+PLdiUZlpb1PA2WE+bp6MDR8H4oYuYQVr5H9vPCIXjzHL6autHztGFzUIpyucjQgtn PtSA== X-Gm-Message-State: AOAM531heIlPxlM/a5Amu557VIL0QH0VXbGZ8ZWZzDtr6yBGh8b2SiTC ODG0TG3v8O6mcwGRKzmBdRCSYEyqK58= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:c861:f04:adab:761b]) (user=surenb job=sendgmr) by 2002:a17:90a:930b:b0:1d5:684b:8e13 with SMTP id p11-20020a17090a930b00b001d5684b8e13mr21049188pjo.153.1652151619049; Mon, 09 May 2022 20:00:19 -0700 (PDT) Date: Mon, 9 May 2022 20:00:12 -0700 Message-Id: <20220510030014.3842475-1-surenb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog Subject: [PATCH 1/3] selftests: vm: add process_mrelease tests From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: mhocko@suse.com, rientjes@google.com, willy@infradead.org, hannes@cmpxchg.org, guro@fb.com, minchan@kernel.org, kirill@shutemov.name, aarcange@redhat.com, brauner@kernel.org, hch@infradead.org, oleg@redhat.com, david@redhat.com, jannh@google.com, shakeelb@google.com, peterx@redhat.com, jhubbard@nvidia.com, shuah@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, kernel-team@android.com, surenb@google.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Introduce process_mrelease syscall sanity tests. They include tests of invalid pidfd and flags inputs, attempting to call process_mrelease with a live process and a valid usage of process_mrelease. Because process_mrelease has to be used against a process with a pending SIGKILL, it's possible that the process exits before process_mrelease gets called. In such cases we retry the test with a victim that allocates twice more memory up to 1GB. This would require the victim process to spend more time during exit and process_mrelease has a better chance of catching the process before it exits. Signed-off-by: Suren Baghdasaryan --- tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/mrelease_test.c | 176 +++++++++++++++++++++ tools/testing/selftests/vm/run_vmtests.sh | 16 ++ 3 files changed, 193 insertions(+) create mode 100644 tools/testing/selftests/vm/mrelease_test.c diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index 04a49e876a46..733fccbff0ef 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -43,6 +43,7 @@ TEST_GEN_FILES += map_populate TEST_GEN_FILES += memfd_secret TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += mlock2-tests +TEST_GEN_FILES += mrelease_test TEST_GEN_FILES += mremap_dontunmap TEST_GEN_FILES += mremap_test TEST_GEN_FILES += on-fault-limit diff --git a/tools/testing/selftests/vm/mrelease_test.c b/tools/testing/selftests/vm/mrelease_test.c new file mode 100644 index 000000000000..a61061bf8433 --- /dev/null +++ b/tools/testing/selftests/vm/mrelease_test.c @@ -0,0 +1,176 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2022 Google LLC + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include "util.h" + +static inline int pidfd_open(pid_t pid, unsigned int flags) +{ +#ifdef __NR_pidfd_open + return syscall(__NR_pidfd_open, pid, flags); +#else + errno = ENOSYS; + return -1; +#endif +} + +static inline int process_mrelease(int pidfd, unsigned int flags) +{ +#ifdef __NR_process_mrelease + return syscall(__NR_process_mrelease, pidfd, flags); +#else + errno = ENOSYS; + return -1; +#endif +} + +static void write_fault_pages(char *addr, unsigned long nr_pages) +{ + unsigned long i; + + for (i = 0; i < nr_pages; i++) + *((unsigned long *)(addr + (i * PAGE_SIZE))) = i; +} + +static int alloc_noexit(unsigned long nr_pages, int pipefd) +{ + int ppid = getppid(); + void *buf; + + buf = mmap(NULL, nr_pages * PAGE_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON, 0, 0); + if (buf == MAP_FAILED) { + perror("mmap"); + return 1; + } + + write_fault_pages((char *)buf, nr_pages); + + /* Signal the parent that the child is ready */ + if (write(pipefd, "", 1) < 0) { + perror("write"); + return 1; + } + + /* Wait to be killed (when reparenting happens) */ + while (getppid() == ppid) + sleep(1); + + munmap(buf, nr_pages * PAGE_SIZE); + + return 0; +} + + +#define MB(x) (x << 20) +#define MAX_SIZE_MB 1024 + +int main(void) +{ + int res; + int pipefd[2], pidfd; + pid_t pid; + char byte; + size_t size; + int negative_tests_done = 0; + + /* Test a wrong pidfd */ + if (!process_mrelease(-1, 0) || errno != EBADF) { + perror("process_mrelease with wring pidfd"); + exit(1); + } + + /* + * Start the test with 1MB allocation and double every time + * process_mrelease fails + */ + for (size = 1; size <= MAX_SIZE_MB; size *= 2) { + /* + * Pipe for the child to signal when it's done allocating + * memory + */ + if (pipe(pipefd)) { + perror("pipe"); + exit(1); + } + pid = fork(); + if (pid < 0) { + perror("fork"); + exit(1); + } + + if (pid == 0) { + close(pipefd[0]); + res = alloc_noexit(MB(size) / PAGE_SIZE, pipefd[1]); + close(pipefd[1]); + exit(res); + } + + close(pipefd[1]); + /* Block until the child is ready */ + res = read(pipefd[0], &byte, 1); + close(pipefd[0]); + if (res < 0) { + perror("read"); + exit(1); + } + + pidfd = pidfd_open(pid, 0); + if (pidfd < 0) { + perror("pidfd_open"); + exit(1); + } + + /* Run negative tests which require a valid child only once */ + if (!negative_tests_done) { + /* Test invalid flags */ + if (!process_mrelease(pidfd, (unsigned int)-1) || + errno != EINVAL) { + perror("process_mrelease with wrong flags"); + exit(1); + } + /* Test reapling while process is still alive */ + if (!process_mrelease(pidfd, 0) || + errno != EINVAL) { + perror("process_mrelease on a live process"); + exit(1); + } + negative_tests_done = 1; + } + + if (kill(pid, SIGKILL)) { + perror("kill"); + exit(1); + } + + if (!process_mrelease(pidfd, 0)) { + /* Terminate the test once process_mrelease succeeds */ + return 0; + } + + /* + * Ignore the failure if the child exited before mrelease got + * called, increase allocation size and retry the test + */ + if (errno != ESRCH) { + perror("process_mrelease"); + exit(1); + } + + if (waitpid(pid, NULL, 0) < 0) { + perror("waitpid"); + exit(1); + } + close(pidfd); + } + + printf("All process_mrelease attempts failed!\n"); + exit(1); +} diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh index 352ba00cf26b..1986162fea39 100755 --- a/tools/testing/selftests/vm/run_vmtests.sh +++ b/tools/testing/selftests/vm/run_vmtests.sh @@ -287,6 +287,22 @@ else echo "[PASS]" fi +echo "---------------------" +echo "running mrelease_test" +echo "---------------------" +./mrelease_test +ret_val=$? + +if [ $ret_val -eq 0 ]; then + echo "[PASS]" +elif [ $ret_val -eq $ksft_skip ]; then + echo "[SKIP]" + exitcode=$ksft_skip +else + echo "[FAIL]" + exitcode=1 +fi + echo "-------------------" echo "running mremap_test" echo "-------------------" -- 2.36.0.512.ge40c2bad7a-goog