From mboxrd@z Thu Jan 1 00:00:00 1970 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 C2FB4C4332F for ; Mon, 30 Oct 2023 20:14:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 053E36B0278; Mon, 30 Oct 2023 16:14:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 003726B027A; Mon, 30 Oct 2023 16:14:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E0D656B027D; Mon, 30 Oct 2023 16:14:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D201F6B0278 for ; Mon, 30 Oct 2023 16:14:39 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9A024120A86 for ; Mon, 30 Oct 2023 20:14:39 +0000 (UTC) X-FDA: 81403230678.09.2E6E61D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 5BD30140015 for ; Mon, 30 Oct 2023 20:14:37 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Z0l0Mzrq; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698696877; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=asnx9CEzoFGwj1+7ZCvfWv++NHlGBEeGv3uTjA7pOPs=; b=npB1bwtDFM6HQjT1N4B3WEV7gAbNxo5b5X9ys7WpXclXn95ONygCw7nxtdY9GrKa5tBBNz fnG9b+zcpCRZnCy5V6zbO4Z3QC0khDukf+A3vXRYKrJEklzRB1mVVhgmHhz6lnUCLVYArY 1tQJymE3HYlHoNV36ijCMRP0/iHwphc= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Z0l0Mzrq; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698696877; a=rsa-sha256; cv=none; b=M1ThguDzV1mphf9x3TN7oOq+QpEl15XtvZR759LC/s4TFibhkUJ4fq9b91uCpdTWbS79x9 /TzZy0DPibdAQ1CQOwzcNhqtjn2L/QVMJlLhnWAAZLt5+gE+5yYRf8zY8taFanTl8Ai4At TauWHL7YYUUaG7HOWlS0k56MBSCKuhw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698696876; 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: in-reply-to:in-reply-to:references:references; bh=asnx9CEzoFGwj1+7ZCvfWv++NHlGBEeGv3uTjA7pOPs=; b=Z0l0MzrqdWyjXhoVpURMvgKcG9NXok1IsCDt6N5sC3jxjXplZLBh4ZKcQBIfBp8J31Rkie s9wdbB9kN6KLfAk0CG8lTGk/I5f+ACnIf/3KeaoasYBAJYDKNI2Hg3YeytZBRzeFE72pZK Fb/s8CDN3uOS9B5W20HGCQJt/pvCtck= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-487-aIBCdL3PMSmqsnxOtDNC9A-1; Mon, 30 Oct 2023 16:14:35 -0400 X-MC-Unique: aIBCdL3PMSmqsnxOtDNC9A-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-778b5c628f4so107997485a.0 for ; Mon, 30 Oct 2023 13:14:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698696875; x=1699301675; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=asnx9CEzoFGwj1+7ZCvfWv++NHlGBEeGv3uTjA7pOPs=; b=H83BbwLWNd6B8HGgRvs9+fONupqCyWx+0+BdKcMJLNlg83oNOJPvRn/LHsNspwM5Nj SP5y97FNUOAnMbrOM9tcZaw5XTT2+4RxgBCsbMJfmezN39p/DtlSNecEKDomeXFBUGGK gqXANNg974WPe9kbgZmgNA2Nx+QCRSpjAM6zR+5C8fKy0uspgEUUolMXkIpyXwRU2Xya dI/gJhBdPNCPee0dY7IGNqEeuM7x4/VQDcY4QJzL1AXPW2hMLUjIrToi7+kA7sDCmmRe idV1OiNIxP0OUfEbveNlny+FMerWBeOOroMWDEANnSYgOde97klxhTGNq2yxv7eyg7QF sXVA== X-Gm-Message-State: AOJu0YwbSzMM+bWkMnq6zGPsHoNIp1l6odFtsB8NmbV9tV5jDsUQx3k7 Zu0oQMSqMyj+psmNA9LKqhRE2tBHE94s24ErwNB1N44ZnzSI9DTHaFzJ4HybQMnDbQPZ+OuII1y xdZvdFztVvCg= X-Received: by 2002:a05:620a:2711:b0:778:9b9f:ccbb with SMTP id b17-20020a05620a271100b007789b9fccbbmr10786820qkp.0.1698696874715; Mon, 30 Oct 2023 13:14:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFm+l4O5xPyFHZN599pI4y/UvLTXRLRuSpjLW8jsASedoJRQ/T+4FgyVXsYFWI6R7KKPLaWdg== X-Received: by 2002:a05:620a:2711:b0:778:9b9f:ccbb with SMTP id b17-20020a05620a271100b007789b9fccbbmr10786779qkp.0.1698696874331; Mon, 30 Oct 2023 13:14:34 -0700 (PDT) Received: from x1n (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id z22-20020ae9c116000000b007677347e20asm3601178qki.129.2023.10.30.13.14.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 13:14:34 -0700 (PDT) Date: Mon, 30 Oct 2023 16:14:30 -0400 From: Peter Xu To: Suren Baghdasaryan Cc: akpm@linux-foundation.org, viro@zeniv.linux.org.uk, brauner@kernel.org, shuah@kernel.org, aarcange@redhat.com, lokeshgidra@google.com, david@redhat.com, hughd@google.com, mhocko@suse.com, axelrasmussen@google.com, rppt@kernel.org, willy@infradead.org, Liam.Howlett@oracle.com, jannh@google.com, zhangpeng362@huawei.com, bgeffon@google.com, kaleshsingh@google.com, ngeoffray@google.com, jdduke@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v4 5/5] selftests/mm: add UFFDIO_MOVE ioctl test Message-ID: References: <20231028003819.652322-1-surenb@google.com> <20231028003819.652322-6-surenb@google.com> MIME-Version: 1.0 In-Reply-To: <20231028003819.652322-6-surenb@google.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 5BD30140015 X-Stat-Signature: 6m5zysx1a75p4jwxcq7658gcukxjxb18 X-HE-Tag: 1698696877-209080 X-HE-Meta: U2FsdGVkX192hcGXGn+Zj1unJZJmlafBn5QB29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rNK4Sfq7CgHP+NyNIEowFHZWUQwBVnU/KO/3V2HWzPULyupcJX7e3DxbRErvssKBzrzrR8fzAFZgu57VPYtZluY3EZnwS1kTAY8b9iLfQ7Sf6RindeAankRThQUd8skR+4zkrCIeLc34uGWKeGQkUu6jbOKdyEws1xU1Dww/pyzmPbhzEmtTTJ1uBlJc+Xs6t4NC9UVrHHzwpoCpNjMZTY9brFyRqTsT3UYYActnIMsvAX3K3FaJW6dQDEEnISsH0d5GoNfyQ8yxFwn79UZBIEnjFG8DmKL3pOirTuewOXXjSPhMN2co9/LS7O7ckbOlEctwBsQ== 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: List-Subscribe: List-Unsubscribe: On Fri, Oct 27, 2023 at 05:38:15PM -0700, Suren Baghdasaryan wrote: > Add tests for new UFFDIO_MOVE ioctl which uses uffd to move source > into destination buffer while checking the contents of both after > the move. After the operation the content of the destination buffer > should match the original source buffer's content while the source > buffer should be zeroed. Separate tests are designed for PMD aligned and > unaligned cases because they utilize different code paths in the kernel. > > Signed-off-by: Suren Baghdasaryan > --- > tools/testing/selftests/mm/uffd-common.c | 24 ++++ > tools/testing/selftests/mm/uffd-common.h | 1 + > tools/testing/selftests/mm/uffd-unit-tests.c | 141 +++++++++++++++++++ > 3 files changed, 166 insertions(+) > > diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c > index 69e6653ad255..98957fd788d8 100644 > --- a/tools/testing/selftests/mm/uffd-common.c > +++ b/tools/testing/selftests/mm/uffd-common.c > @@ -643,6 +643,30 @@ int copy_page(int ufd, unsigned long offset, bool wp) > return __copy_page(ufd, offset, false, wp); > } > > +int move_page(int ufd, unsigned long offset) > +{ > + struct uffdio_move uffdio_move; > + > + if (offset >= nr_pages * page_size) > + err("unexpected offset %lu\n", offset); > + uffdio_move.dst = (unsigned long) area_dst + offset; > + uffdio_move.src = (unsigned long) area_src + offset; > + uffdio_move.len = page_size; > + uffdio_move.mode = UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES; > + uffdio_move.move = 0; > + if (ioctl(ufd, UFFDIO_MOVE, &uffdio_move)) { > + /* real retval in uffdio_move.move */ > + if (uffdio_move.move != -EEXIST) > + err("UFFDIO_MOVE error: %"PRId64, > + (int64_t)uffdio_move.move); > + wake_range(ufd, uffdio_move.dst, page_size); > + } else if (uffdio_move.move != page_size) { > + err("UFFDIO_MOVE error: %"PRId64, (int64_t)uffdio_move.move); > + } else > + return 1; > + return 0; > +} > + > int uffd_open_dev(unsigned int flags) > { > int fd, uffd; > diff --git a/tools/testing/selftests/mm/uffd-common.h b/tools/testing/selftests/mm/uffd-common.h > index 19930fd6682b..c9526b2cb6b3 100644 > --- a/tools/testing/selftests/mm/uffd-common.h > +++ b/tools/testing/selftests/mm/uffd-common.h > @@ -121,6 +121,7 @@ void wp_range(int ufd, __u64 start, __u64 len, bool wp); > void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_args *args); > int __copy_page(int ufd, unsigned long offset, bool retry, bool wp); > int copy_page(int ufd, unsigned long offset, bool wp); > +int move_page(int ufd, unsigned long offset); > void *uffd_poll_thread(void *arg); > > int uffd_open_dev(unsigned int flags); > diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c > index debc423bdbf4..89e9529ce941 100644 > --- a/tools/testing/selftests/mm/uffd-unit-tests.c > +++ b/tools/testing/selftests/mm/uffd-unit-tests.c > @@ -1064,6 +1064,133 @@ static void uffd_poison_test(uffd_test_args_t *targs) > uffd_test_pass(); > } > > +static void uffd_move_handle_fault( > + struct uffd_msg *msg, struct uffd_args *args) > +{ > + unsigned long offset; > + > + if (msg->event != UFFD_EVENT_PAGEFAULT) > + err("unexpected msg event %u", msg->event); > + > + if (msg->arg.pagefault.flags & > + (UFFD_PAGEFAULT_FLAG_WP | UFFD_PAGEFAULT_FLAG_MINOR | UFFD_PAGEFAULT_FLAG_WRITE)) > + err("unexpected fault type %llu", msg->arg.pagefault.flags); > + > + offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst; > + offset &= ~(page_size-1); > + > + if (move_page(uffd, offset)) > + args->missing_faults++; > +} > + > +static void uffd_move_test(uffd_test_args_t *targs) > +{ > + unsigned long nr; > + pthread_t uffd_mon; > + char c; > + unsigned long long count; > + struct uffd_args args = { 0 }; > + > + /* Prevent source pages from being mapped more than once */ > + if (madvise(area_src, nr_pages * page_size, MADV_DONTFORK)) > + err("madvise(MADV_DONTFORK) failure"); > + > + if (uffd_register(uffd, area_dst, nr_pages * page_size, > + true, false, false)) > + err("register failure"); > + > + args.handle_fault = uffd_move_handle_fault; > + if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args)) > + err("uffd_poll_thread create"); > + > + /* > + * Read each of the pages back using the UFFD-registered mapping. We > + * expect that the first time we touch a page, it will result in a missing > + * fault. uffd_poll_thread will resolve the fault by moving source > + * page to destination. > + */ > + for (nr = 0; nr < nr_pages; nr++) { > + /* Check area_src content */ > + count = *area_count(area_src, nr); > + if (count != count_verify[nr]) > + err("nr %lu source memory invalid %llu %llu\n", > + nr, count, count_verify[nr]); > + > + /* Faulting into area_dst should move the page */ > + count = *area_count(area_dst, nr); > + if (count != count_verify[nr]) > + err("nr %lu memory corruption %llu %llu\n", > + nr, count, count_verify[nr]); > + > + /* Re-check area_src content which should be empty */ > + count = *area_count(area_src, nr); > + if (count != 0) > + err("nr %lu move failed %llu %llu\n", > + nr, count, count_verify[nr]); > + } > + > + if (write(pipefd[1], &c, sizeof(c)) != sizeof(c)) > + err("pipe write"); > + if (pthread_join(uffd_mon, NULL)) > + err("join() failed"); > + > + if (args.missing_faults != nr_pages || args.minor_faults != 0) > + uffd_test_fail("stats check error"); > + else > + uffd_test_pass(); > +} > + > +static int prevent_hugepages(void) > +{ > + /* This should be done before source area is populated */ > + if (madvise(area_src, nr_pages * page_size, MADV_NOHUGEPAGE)) { > + /* Ignore if CONFIG_TRANSPARENT_HUGEPAGE=n */ > + if (errno != EINVAL) > + return -errno; > + } > + return 0; > +} > + > +struct uffd_test_case_ops uffd_move_test_case_ops = { > + .post_alloc = prevent_hugepages, > +}; > + > +#define ALIGN_UP(x, align_to) \ > + (__typeof__(x))((((unsigned long)(x)) + ((align_to)-1)) & ~((align_to)-1)) > + > +static char *orig_area_src, *orig_area_dst; > +static int pmd_align_areas(void) > +{ > + orig_area_src = area_src; > + orig_area_dst = area_dst; > + area_src = ALIGN_UP(area_src, page_size); > + area_dst = ALIGN_UP(area_dst, page_size); > + nr_pages--; > + > + return 0; > +} > + > +static void pmd_restore_areas(void) > +{ > + area_src = orig_area_src; > + area_dst = orig_area_dst; > + nr_pages++; > +} Please stop using more global variables.. uffd tests are even less maintainable. Maybe you can consider add a flag for uffd_test_ctx_init()? For allocating either small/thp/default? > + > +static int adjust_page_size(void) > +{ > + page_size = default_huge_page_size(); This is hacky too, currently page_size is the real page_size backing the memory. To make thp test simple, maybe just add one more test to MOVE a large chunk to replace the thp test, which may contain a few thps? It also doesn't need to be fault based. > + nr_pages = UFFD_TEST_MEM_SIZE / page_size; > + > + return 0; > +} > + > +struct uffd_test_case_ops uffd_move_pmd_test_case_ops = { > + .pre_alloc = adjust_page_size, > + .post_alloc = pmd_align_areas, > + .pre_release = pmd_restore_areas, > +}; > + > /* > * Test the returned uffdio_register.ioctls with different register modes. > * Note that _UFFDIO_ZEROPAGE is tested separately in the zeropage test. > @@ -1141,6 +1268,20 @@ uffd_test_case_t uffd_tests[] = { > .mem_targets = MEM_ALL, > .uffd_feature_required = 0, > }, > + { > + .name = "move", > + .uffd_fn = uffd_move_test, > + .mem_targets = MEM_ANON, > + .uffd_feature_required = UFFD_FEATURE_MOVE, > + .test_case_ops = &uffd_move_test_case_ops, > + }, > + { > + .name = "move-pmd", > + .uffd_fn = uffd_move_test, > + .mem_targets = MEM_ANON, > + .uffd_feature_required = UFFD_FEATURE_MOVE, > + .test_case_ops = &uffd_move_pmd_test_case_ops, > + }, > { > .name = "wp-fork", > .uffd_fn = uffd_wp_fork_test, > -- > 2.42.0.820.g83a721a137-goog > -- Peter Xu