Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6256399iob; Tue, 10 May 2022 14:07:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz14o5qwtrBl6L5pyThe2vCWwpnU6+mRhO1QByZB9irC+t0Qrlb31U6OOHkAg2GhOGGIMej X-Received: by 2002:a17:907:3f26:b0:6f4:dc59:3cfe with SMTP id hq38-20020a1709073f2600b006f4dc593cfemr21642163ejc.528.1652216869983; Tue, 10 May 2022 14:07:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652216869; cv=none; d=google.com; s=arc-20160816; b=YenWOGLR3S0gXOioJmvy2AGyPtFxkfQksajoBhdqhXATBBAT8RmVKKrXIU5RMx9z9w MTRlBflt4o8hDhXU7/wVbiq+ur0hz5cCxr1HiSbo6BvyVIdHZPnxxf2DPtp1ssVlzJAA E87LBOZwzb5dJ/ddCKSiqDVW63GLgm8pcUlHpZVrh7NI9hyETcu8JPxsPO0RBc6kcGxn VOvFDOw2oGjvxHtiyrtE0eParwWXUwVlY+fNKscxe5VsihJW0OFm5tCH8YEapFLcOqBM DjNl+GW3p5WlHkl/KuVx06hqJXo5eT2ysOZMu/88WsrnOwYj7AVibKsmjRkEW4oOE3vo EAFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=X7HPPummtVT1Cpry90v/glBuyXh4uT47kyH0y1UUgaY=; b=RHF0VTXFmf3qBA+5cQbfMp+TwjAGuzNzfrQaut4j+7LKocgsbZvbknIpSQdV0bn4H1 OaVAKQvHqWljwSPJOJZ84KEYOnNdcxfCXYkKDuVgzc+bratBPKibzlwB+TNj3vUhXW6J Gpk8dHz4dOuzfmZgPl2coetes9mUQZf3Rxk0grebEdU4FGCNx5byb8z/H03Se+YNS/9l CbwaXp2D8bcgQJIZFy92kYJXJqPQw7Huz2wqiixWMSG38MubqPidrnY/mFsiXe4JZpgb 4oDHmjTH6jrl9eIjTJGKYMC20SXPJETWGSXNtasK5yvyC3mkDikMbVKP07tng1AlnIT0 Kn8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=google header.b=Uc7pkau1; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c19-20020aa7d613000000b00428727e4425si211358edr.408.2022.05.10.14.07.26; Tue, 10 May 2022 14:07:49 -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=@linuxfoundation.org header.s=google header.b=Uc7pkau1; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346112AbiEJPrd (ORCPT + 99 others); Tue, 10 May 2022 11:47:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346107AbiEJPra (ORCPT ); Tue, 10 May 2022 11:47:30 -0400 Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C355269EE5 for ; Tue, 10 May 2022 08:43:32 -0700 (PDT) Received: by mail-io1-xd2e.google.com with SMTP id r27so18955931iot.1 for ; Tue, 10 May 2022 08:43:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=X7HPPummtVT1Cpry90v/glBuyXh4uT47kyH0y1UUgaY=; b=Uc7pkau17EpYJQhbTAIrqfH1vsSsm9BU5xXIoSfOd+odUD5uLfxvMbeckrt1do7sgf hgtf9g0Stj+DxnPYF1j0fTWtnBjXms9b8D2wBzRo68PJUWd8lWEBlfMpcf7z0JKSpTQT sJlr9MvJ+uK8n/qePydSHov2/4p3MDPX8KFfs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=X7HPPummtVT1Cpry90v/glBuyXh4uT47kyH0y1UUgaY=; b=kIJfN3OKxwzQxw8Fyi8cxDdxhvgBD5zFqhZ6577PFH89o1Dn6HoJUlEOpUkKXjM7ts Zd7BkOFqRNcOapS7bDd3ATN4cMeWonqVeI/ti0HbSW7El+RtCN07tJhvsFF5+66wDxEp h/lmW9FSFpYFn4c7nukiw97jeRAn4yWKFdyXa6nff1L9+c9IEijAcvy7MnUWIlLu6BPy awsK5GjZMgQ7DDDke02Qw49chFZqmVKp4BZrqt5SYHUessdIt96B+y6kHLbT+pmsub2p hrWqEB7/zJa612OBJmIj/u5fGU7x4xG7+IBna9t3TMQfg+E3d7jO4/YtFkS2XDPhZxdb Cqpg== X-Gm-Message-State: AOAM532CzDEAiEXQJ6FLGDHjQIcA7HNSEcDz/BjVbMNROZA7CJbRi0Dl JwxWumk0ErPuMu67DNZiB1Ru630g4dxeCw== X-Received: by 2002:a05:6638:37a8:b0:32b:5d5c:7f9d with SMTP id w40-20020a05663837a800b0032b5d5c7f9dmr10371462jal.3.1652197409849; Tue, 10 May 2022 08:43:29 -0700 (PDT) Received: from [192.168.1.128] ([38.15.45.1]) by smtp.gmail.com with ESMTPSA id q19-20020a02c8d3000000b0032b3a781744sm4453024jao.8.2022.05.10.08.43.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 May 2022 08:43:29 -0700 (PDT) Subject: Re: [PATCH 1/3] selftests: vm: add process_mrelease tests To: Suren Baghdasaryan , 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, Shuah Khan References: <20220510030014.3842475-1-surenb@google.com> From: Shuah Khan Message-ID: <04858a5d-98c8-69be-025f-214e4b10d502@linuxfoundation.org> Date: Tue, 10 May 2022 09:43:28 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <20220510030014.3842475-1-surenb@google.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=ham 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 On 5/9/22 9:00 PM, Suren Baghdasaryan wrote: > 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. > +1 on Mike's comments on improving the change log. List what is getting tested as opposed to describing the test code. > 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 Please update .gitignore with the new executable. > > 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; This isn't an error - this would be skip because this syscall isn't supported. > + return -1; > +#endif Key off of syscall return instead of these ifdefs - same comment on all of the ifdefs > +} > + I am not seeing any reason for breaking this code up have a separate routine for pidfd_open(). > +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> +} > + Same comments on ifdefs and skips here as well. > +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"); A bit more descriptive message what the test would do will be helpful. Also consider if this should be a skip or fail for the test. > + 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); > + What happens if reparenting doesn't happen? Will this loop for ever? This test could hang? > + 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"); Incorrect spelling "wring/wrong" > + 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"); Close the pipe? > + 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); > + } > + The code is very hard to read. Add comments to indicate parent and child paths clearly so reviewers can follow the logic and be able to do effective review. > + /* 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); So is this an expected fail or a test fail? > + } > + /* Test reapling while process is still alive */ > + if (!process_mrelease(pidfd, 0) || > + errno != EINVAL) { > + perror("process_mrelease on a live process"); So is this an expected fail or a test fail? > + exit(1); > + } > + negative_tests_done = 1; > + } Now the above negative_tests_done block could be in a separate function --- All the others aren't really needed. It will be good for abstraction and readability. > + > + if (kill(pid, SIGKILL)) { > + perror("kill"); Include test results in the change log - so we can see the test report. > + 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 > + */ Add more info. on why allocating more memory helps. > + 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 "-------------------" > In general, the code flow is hard to read to make sure resources are released e.g: pipefd in all the error paths. The code is broken up into smaller chunks where it isn't needed in some cases and left as a large block when it could benefit from abstraction e.g: negative test block. Please make changes and send v2. thanks, -- Shuah