Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBE40C77B6E for ; Fri, 14 Apr 2023 14:28:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7AEAC900004; Fri, 14 Apr 2023 10:28:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 75E246B0078; Fri, 14 Apr 2023 10:28:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 64F31900004; Fri, 14 Apr 2023 10:28:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 564096B0071 for ; Fri, 14 Apr 2023 10:28:11 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2F64680362 for ; Fri, 14 Apr 2023 14:28:11 +0000 (UTC) X-FDA: 80680226382.18.5985F04 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 0063D140009 for ; Fri, 14 Apr 2023 14:28:08 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Yhli3Qz2; spf=pass (imf09.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681482489; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3+7E0N5YimEKCEkRUQMjEBrkYWjsqKlWAyhl1179nqM=; b=IFDaslXgh34vrEDpNhLch37aY0HHEFRI3nHThhNBlmXHHtKkMpMYvB2hI324399YMHt1cw 8WqU75JD7jU40ZbdRAzaPLuM0d5fcWTmIopcpLvt50WAmpqeH8HvGhU3R2blhWGKeENaN3 TN9XHHP7Lcex3CU3QR0kbeP2NYD0AYs= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Yhli3Qz2; spf=pass (imf09.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681482489; a=rsa-sha256; cv=none; b=p1GpI+axlhY5mHT2dVd6NLiMbTRJeJz/xLxld+4qgg7hXFYE3w5XvUYEKS1mTxuN4Sezu6 hA4lfHN3ae48M5m3w3G26wvjm7/qIIwW0cNrPb/DgK1CaEi5Vmn4rvxQkyPYv3y9Aum9qj zwTv0OpLuBufkajAg3uTxlIaMKLXLGo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681482487; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3+7E0N5YimEKCEkRUQMjEBrkYWjsqKlWAyhl1179nqM=; b=Yhli3Qz22ujTYFsFuwZ4N83w4Jr7ubu3hS5ptFisdpUp9t6cYRhLj1npP2C2NKXEFVusTT secttSpGvlFUTT20B7Mptg1Y/dis04kEQSxOi5EvZGFhOlAYu1W4tBUekgz/ociXZX3Wzd zlqpJgeW8Z0S8CKhRdNQPHq129DnPgQ= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-614-v1RU7jLpMoKU69ANab76xQ-1; Fri, 14 Apr 2023 10:28:06 -0400 X-MC-Unique: v1RU7jLpMoKU69ANab76xQ-1 Received: by mail-wm1-f72.google.com with SMTP id m4-20020a05600c3b0400b003f0968719a7so5349386wms.2 for ; Fri, 14 Apr 2023 07:28:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681482483; x=1684074483; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3+7E0N5YimEKCEkRUQMjEBrkYWjsqKlWAyhl1179nqM=; b=Ld+mekS9LuRS/Y5cOcL6zQLS/z8l8RjIQ0vE77NKoB96/RhCDvDVzmZY85XxtHc96y FOdrcaQ4vhTbxP5N9k9TmldqJ5lp0Zdv7qtLxfjs3jDQjctTdgzwuwr/zOX6aZylzgjG Z/w+gwotz9j4dVq5GeP3NF5tmpLS8H6EaeDIiV1HHtVn+P2+pzDQxk7rvLQD1JeSjthK bpwLf6a+D6hQcap7dHhmWt+naY/BB460a9ielHHvTUqaby/Lso0ctDFR3FW1Sj7MrPR8 L5NyhSkVNR7TUwRpCeTP+Oq9rpY9P7fi15xgZ3+BYhO9RZ9GOoWjaRYOLV5uazd3KchQ 5mlw== X-Gm-Message-State: AAQBX9f0TRS/2L1Q3o7b3ThE9ILi/eIuNck3oBU1suMJt8eR+Iijhx2B 7Y0xtneeliP4jB86I9tnXnBP91JpuATAOdsZTHxbmQXILkby3HDfE4qDaRVXPqeJkq4RF2omoJo Vwjwi4SBHnVM= X-Received: by 2002:a7b:cb85:0:b0:3f0:9fef:1028 with SMTP id m5-20020a7bcb85000000b003f09fef1028mr4298089wmi.17.1681482483649; Fri, 14 Apr 2023 07:28:03 -0700 (PDT) X-Google-Smtp-Source: AKy350bXdpprtWQPV5vLzlkhmiMtNbIMroahWYPyjUn400ftnGsQ6ha2RCpyFzKpq1NLfQNPIfHK1g== X-Received: by 2002:a7b:cb85:0:b0:3f0:9fef:1028 with SMTP id m5-20020a7bcb85000000b003f09fef1028mr4298069wmi.17.1681482483270; Fri, 14 Apr 2023 07:28:03 -0700 (PDT) Received: from ?IPV6:2003:cb:c702:5700:cb5b:f73a:c650:1d9? (p200300cbc7025700cb5bf73ac65001d9.dip0.t-ipconnect.de. [2003:cb:c702:5700:cb5b:f73a:c650:1d9]) by smtp.gmail.com with ESMTPSA id y24-20020a7bcd98000000b003ee44b2effasm4412739wmj.12.2023.04.14.07.28.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 14 Apr 2023 07:28:02 -0700 (PDT) Message-ID: Date: Fri, 14 Apr 2023 16:28:01 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 To: Stefan Roesch , kernel-team@fb.com Cc: linux-mm@kvack.org, riel@surriel.com, mhocko@suse.com, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, akpm@linux-foundation.org, hannes@cmpxchg.org, willy@infradead.org, Bagas Sanjaya References: <20230413233115.1878303-1-shr@devkernel.io> <20230413233115.1878303-4-shr@devkernel.io> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v7 3/3] selftests/mm: add new selftests for KSM In-Reply-To: <20230413233115.1878303-4-shr@devkernel.io> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 0063D140009 X-Stat-Signature: n1xtebt6bfbn5wb6ixkc3p5zjhx3y5ap X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1681482488-597029 X-HE-Meta: U2FsdGVkX19cZzumwYmTYOALZIgSBgeYAExg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Ho61FZ2FTCHa0OIA+ZXmhwKaxdRB2qFfxm5QEEv+0tpx3fbi/oYQhEsXqYoT1Ii+CvXHhtss4Sk8L0kpUZviCxGY/VDHLdjxrq3uMSFK0uZ2notiaeGHZxKPRQtVEcqpWwnKOmHq4DNwifxP3Q0spR82AM1QT0cJHRAEYdU8i1nz4BcmASHrsxp0iKvGKdhHmMdfSRBLYZR2coBy/iOYMEmRDcEXCWpdQkr7Umj7iY8RsDEm5xC4jhkmoca7cV8oh7KjGT4E+Y7N6WqwJzZ1FkAHPZN980UfB1KGuoTGc4WTYw== 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: Thanks for moving the functional tests. Some more feedback forksm_functional_tests change. Writing tests in the ksft testing framework can be a bit "special". I'm seeing some weird test failures due to prctl(PR_GET_MEMORY_MERGE, 0) Apparently, these go away when using prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0) to explicitly force the other values to 0. Most probably, we should do that for PR_SET_MEMORY_MERGE as well (especially if we check for the arguments as well). [...] > @@ -15,8 +15,10 @@ > #include > #include > #include > +#include > #include > #include > +#include > #include > > #include "../kselftest.h" > @@ -326,9 +328,80 @@ static void test_unmerge_uffd_wp(void) > } > #endif > > +/* Verify that KSM can be enabled / queried with prctl. */ > +static void test_ksm_prctl(void) Maybe call this "test_prctl", because after all, these are all KSM tests. > +{ > + bool ret = false; > + int is_on; > + int is_off; > + > + ksft_print_msg("[RUN] %s\n", __func__); > + > + if (prctl(PR_SET_MEMORY_MERGE, 1)) { > + perror("prctl set"); > + goto out; > + } > + > + is_on = prctl(PR_GET_MEMORY_MERGE, 0); > + if (prctl(PR_SET_MEMORY_MERGE, 0)) { > + perror("prctl set"); > + goto out; > + } > + > + is_off = prctl(PR_GET_MEMORY_MERGE, 0); > + if (is_on && is_off) > + ret = true; > + > +out: > + ksft_test_result(ret, "prctl get / set\n"); The test fails if the kernel does not support PR_SET_MEMORY_MERGE. I'd modify this test to: (1) skip if the first PR_SET_MEMORY_MERGE=1 failed with EINVAL. (2) distinguish for PR_GET_MEMORY_MERGE whether it returned an error or whether it returned a wrong value. Feel free to keep that as is, whatever you prefer. (3) exit early for all failures, you get exactly one expected skip/pass/fail for the test and use specific test failure messages. (4) Pass "0" for all other arguments of prctl. Something like: static void test_prctl(void) { int ret; ksft_print_msg("[RUN] %s\n", __func__); ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0); if (ret < 0 && errno == EINVAL){ ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n"); return; } else if (ret) { ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n"); return; } ret = prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0); if (ret < 0) { ksft_test_result_fail("PR_GET_MEMORY_MERGE failed\n"); return; } else if (ret != 1) { ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 not effective\n"); return; } ret = prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0); if (ret){ ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 failed\n"); return; } ret = prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0); if (ret < 0) { ksft_test_result_fail("PR_GET_MEMORY_MERGE failed\n"); return; } else if (ret != 0) { ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 not effective\n"); return; } ksft_test_result_pass("Setting/clearing PR_SET_MEMORY_MERGE works\n"); } > +} > + > +/* Verify that prctl ksm flag is inherited. */ > +static void test_ksm_fork(void) Maybe call it "test_prctl_fork" > +{ > + int status; > + bool ret = false; > + pid_t child_pid; > + > + ksft_print_msg("[RUN] %s\n", __func__); > + > + if (prctl(PR_SET_MEMORY_MERGE, 1)) { > + ksft_test_result_fail("prctl failed\n"); > + goto out; > + } > + > + child_pid = fork(); > + if (child_pid == 0) { > + int is_on = > + > + if (!is_on) > + exit(-1); > + > + exit(0); > + } > + > + if (child_pid < 0) { > + ksft_test_result_fail("child pid < 0\n"); > + goto out;> + > + if (waitpid(child_pid, &status, 0) < 0 || WEXITSTATUS(status) != 0) { > + ksft_test_result_fail("wait pid < 0\n"); > + goto out; > + } > + > + if (prctl(PR_SET_MEMORY_MERGE, 0)) > + ksft_test_result_fail("prctl 2 failed\n"); > + else > + ret = true; > + > +out: > + ksft_test_result(ret, "ksm_flag is inherited\n"); > +} Again, test fails if kernel support is not around. I'd modify this test to: (1) skip if the first PR_SET_MEMORY_MERGE=1 failed with EINVAL just as in the other test. (2) Use a simple exit(prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)); in the child. (3) exit early for all failures, you get exactly one expected skip/pass/fail for the test and use specific test failure messages. (4) Split up the waitpid() check to test what failed. (5) Pass "0" for all other arguments of prctl. Something like: static void test_prctl_fork(void) { int ret, status; pid_t child_pid; ksft_print_msg("[RUN] %s\n", __func__); ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0); if (ret < 0 && errno == EINVAL){ ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n"); return; } else if (ret) { ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n"); return; } child_pid = fork(); if (!child_pid) { exit(prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)); } else if (child_pid < 0) { ksft_test_result_fail("fork() failed\n"); return; } if (waitpid(child_pid, &status, 0) < 0) { ksft_test_result_fail("waitpid() failed\n"); return; } else if (WEXITSTATUS(status) != 1) { ksft_test_result_fail("unexpected PR_GET_MEMORY_MERGE result in child\n"); return; } if (prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0)) { ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 failed\n"); return; } ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n"); } > + > int main(int argc, char **argv) > { > - unsigned int tests = 2; > + unsigned int tests = 6; Assuming you execute exactly one ksft_test_result_skip/fail/pass on every path of your two test, this would become "4". > int err; > > #ifdef __NR_userfaultfd > @@ -358,6 +431,8 @@ int main(int argc, char **argv) > #ifdef __NR_userfaultfd > test_unmerge_uffd_wp(); > #endif > + test_ksm_prctl(); > + test_ksm_fork(); > With above outlined changes (feel free to integrate what you consider valuable), on an older kernel I get: $ sudo ./ksm_functional_tests TAP version 13 1..5 # [RUN] test_unmerge ok 1 Pages were unmerged # [RUN] test_unmerge_discarded ok 2 Pages were unmerged # [RUN] test_unmerge_uffd_wp ok 3 Pages were unmerged # [RUN] test_prctl ok 4 # SKIP PR_SET_MEMORY_MERGE not supported # [RUN] test_prctl_fork ok 5 # SKIP PR_SET_MEMORY_MERGE not supported # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:2 error:0 On a kernel with your patch #1: # ./ksm_functional_tests TAP version 13 1..5 # [RUN] test_unmerge ok 1 Pages were unmerged # [RUN] test_unmerge_discarded ok 2 Pages were unmerged # [RUN] test_unmerge_uffd_wp ok 3 Pages were unmerged # [RUN] test_prctl ok 4 Setting/clearing PR_SET_MEMORY_MERGE works # [RUN] test_prctl_fork ok 5 PR_SET_MEMORY_MERGE value is inherited # Totals: pass:5 fail:0 xfail:0 xpass:0 skip:0 error:0 > err = ksft_get_fail_cnt(); > if (err) > diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c > index f9eb4d67e0dd..35b3828d44b4 100644 > --- a/tools/testing/selftests/mm/ksm_tests.c > +++ b/tools/testing/selftests/mm/ksm_tests.c > @@ -1,6 +1,8 @@ > // SPDX-License-Identifier: GPL-2.0 [...] Changes to ksm_tests mostly look good. Two comments: > - if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout)) > + if (ksm_merge_pages(merge_type, map_ptr, page_size * page_count, start_time, timeout)) > goto err_out; > > /* verify that the right number of pages are merged */ > if (assert_ksm_pages_count(page_count)) { > printf("OK\n"); > - munmap(map_ptr, page_size * page_count); > + if (merge_type == KSM_MERGE_MADVISE) > + munmap(map_ptr, page_size * page_count); > + else if (merge_type == KSM_MERGE_PRCTL) > + prctl(PR_SET_MEMORY_MERGE, 0); Are you sure that we don't want to unmap here? I'd assume we want to unmap in either way. [...] > + case 'd': > + debug = 1; > + break; > case 's': > size_MB = atoi(optarg); > if (size_MB <= 0) { > printf("Size must be greater than 0\n"); > return KSFT_FAIL; > } > + case 't': > + { > + int tmp = atoi(optarg); > + > + if (tmp < 0 || tmp > KSM_MERGE_LAST) { > + printf("Invalid merge type\n"); > + return KSFT_FAIL; > + } > + merge_type = atoi(optarg); You can simply reuse tmp merge_type = tmp; -- Thanks, David / dhildenb