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 672CFC4332F for ; Mon, 30 Oct 2023 20:22:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E8BA96B0286; Mon, 30 Oct 2023 16:22:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E3AD26B0287; Mon, 30 Oct 2023 16:22:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D01CB6B0288; Mon, 30 Oct 2023 16:22:18 -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 C0E106B0286 for ; Mon, 30 Oct 2023 16:22:18 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 968FD160404 for ; Mon, 30 Oct 2023 20:22:18 +0000 (UTC) X-FDA: 81403249956.26.A94AF2D Received: from mail-yw1-f173.google.com (mail-yw1-f173.google.com [209.85.128.173]) by imf19.hostedemail.com (Postfix) with ESMTP id D2C5B1A0017 for ; Mon, 30 Oct 2023 20:22:16 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=C4TbJiw+; spf=pass (imf19.hostedemail.com: domain of surenb@google.com designates 209.85.128.173 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698697336; a=rsa-sha256; cv=none; b=TKw2aiSNuIu405DtamUt+gyyQqtFW2ztgLGzL7+/w8MFDluWQMkkogz0dIYMneIO5F+KbL Q+EbRCRAlJ9mTjDUup2IdZ0PBFNjQvRa8s3K6Lv6hffZIARBHg7txeOFbHchR+LXxnWzDu uwAQvdOxySTZrQ/Uk/KVaSoAd2bEdC4= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=C4TbJiw+; spf=pass (imf19.hostedemail.com: domain of surenb@google.com designates 209.85.128.173 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698697336; 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=0MZDC0C692oKqu67pAXxbmQTWTZ0RHUdPxw5a4EVzJM=; b=DuStdzx3h3r4e9beTw64hZWx7sE8JL1UWSKy33nuN5MFibfbNTi5qV8FL5uVd1Mbi4mJ05 fCWUiSRDji9K7xwNjdpiRCmK90zY5cTjUyUuqPK4q8XKl9ZS+1VXGvcG5tdXRQzrhQNN7Q zZ+s/WkCI/iqJUNEVwemWSj+lVTvssM= Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-5a84204e7aeso45401957b3.0 for ; Mon, 30 Oct 2023 13:22:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698697336; x=1699302136; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=0MZDC0C692oKqu67pAXxbmQTWTZ0RHUdPxw5a4EVzJM=; b=C4TbJiw+Bpxa5dMAx01xIbfD4pYiYYl3kP2lli4RqRPFzY+JIeaf+bBNCPegBRKosw KoGZiEsQntP+G97iiljzZvjGCzgNeZzUSd3ovZsB/PBerFwoSa1QhRyfe5B7o3BHVYB9 cgRHeZNXY5AQyUywIWyg2rtJU572IVc61bZLekRkwaVQ/wG8Jux2XX3r6dhGaAGRCHSg vL8/rHbN6yhKEAboOAgEWhvpa7AOaLOfXkuxDYhQVQhaAIpiZaNRheLnGuPyYRRmmkM3 5KXYHcL1VR8xXJZA2o8Sy7WR3nDXo3jiHXVna7l1MTL5FSYwLPfQlAPvEysFDsw81NAx WeJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698697336; x=1699302136; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0MZDC0C692oKqu67pAXxbmQTWTZ0RHUdPxw5a4EVzJM=; b=tOg7rEhLix8E5SGrZw/cCQJLYQaekchqBDDMp99sBUme9hZE/uvicyikR0Xj1NLw43 fXRoJy6Av28Bbq0ddRjR87XWEQpe1hdWsTkWESJCayITZPNSRa9LVM0zwGXfP23G6k4H cIhRGnbd9FEMZ48jHf/EYDyWaOj5oxdAKrbPHijdOXNN2fHpmjF5ke0byRR584SVvbJM lDm8yD0RjNkR9yTT+GBIWrEpFvbvovfOYSMjgqobithEZ1x2ctLa12MM/mkIxx171ATy bDEoPfKJmHoTSwuwp+0LCyE7aCPwsu7lZgvRWbMT4Z2J8PnXEHUeXdY4XNy+lHlltth3 uj4Q== X-Gm-Message-State: AOJu0YxuDHDdux3h5zOop03H0EmPdXA5BlbYYY03JKPB3NDMn2DHK3wq /zljxXIMGnT7RcJoc1MQMzuWrWMs0ps+jZIyNhlWxg== X-Google-Smtp-Source: AGHT+IErTdQZnjT34lXTzXN3MzHAIzHtfhHdTZFEWa84Ne1hGq4DSlIR0kYYUcl8B03xzuvbq5tzsxkfTAgdkep7sF0= X-Received: by 2002:a81:a987:0:b0:5a7:be1d:7ad3 with SMTP id g129-20020a81a987000000b005a7be1d7ad3mr11134229ywh.1.1698697335636; Mon, 30 Oct 2023 13:22:15 -0700 (PDT) MIME-Version: 1.0 References: <20231028003819.652322-1-surenb@google.com> <20231028003819.652322-6-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Mon, 30 Oct 2023 13:22:02 -0700 Message-ID: Subject: Re: [PATCH v4 5/5] selftests/mm: add UFFDIO_MOVE ioctl test To: Peter Xu 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: D2C5B1A0017 X-Stat-Signature: 3e6wzwr9uunqujhg4cz4j46zj6nzf5ka X-Rspam-User: X-HE-Tag: 1698697336-521194 X-HE-Meta: U2FsdGVkX18fypB+RM0uQaijFwfSlI489hk5K0JYNYam1yc9bpBa7gfN4DZQeCo6JWhxu1eJ/k90HG2s56a60BxlusmTWH5R4BwQB+Ca0dPDbqMaIFORkuU6jIo7BtZ/0J3pOG+Q5vUfec1spbKjRF3mEfBGfwkmWt0HHpKAF6l+fovci+hniV0VIdLl31EKhqpehf86QnERTKeNgC0eGhWltXh3kPBlaIAxn6MChYUERcJUdTQXEVRe5yio3egf9CldvMGq6G7SLaLwo/MFPoKvV7egINaLFiiU5K0NbN+ODoxRDlzdvRvuubWVvoNFtu/6kUtgR8UwtTqbYJnYBA0ve2bSBoaNf6sg0ctevPdK1NXXoQ7xOATDK3MdwgMgImTlSSc8jN2qs/7ph+RPQwLX6Aq99A9Fr4Bit0orAilO7FTjT6Kaxe1DLG4STsJYkeTlOZ+ani/G1i/gEz5lVoik/3JUL4HBapSwQHXKpGggk5LRtIzi87dQVSfaYvtniDZleSvZmFh7nT542R+aTfNyUMLD4QloSVG3bNCWouZgkxsmTbTSiWLFubcSIN3xomhow93IypRynbxifxUe22lzkFBPZSBkskc345bUEOruaX5CAvAOysS6EilfMIY+W2U3RJEM9dSAlZYZcVE1WlASuIEal1YJPGRxn7XdIpjl+mBWAweyLSNRlw04UzcUnlnEV225/NbzPXfeHwVvsU98yeJgBvTuckH3fWsuQwrMFTKzrZRg7X7bQdSSe3Dvdj71isE3IyKCB9xW5XZDSup4VrpuZMYhGZlCsYRPKDk2yvTe02nvUm9RyUGz45Qdxc40a2ysf2z7+o5yXmcGgztNrEmJAPC5Bb8zH6F7F+Z/yU+4LcgTpcKyQZ11hfnH3/91ulAB5n0lPMT6J+gEz28TAN4SKCd7KmB2I59+7guvIqnyktvZ27iWk+CIcUrnscBII5ops4dkv86s2ah Tdt0Quv6 dx52uJ937RprllKqyjkMd6tAM87CJvur0LMchMtfCVzLxMfnSrdwa4nnwAb6OAkzJdDkAZrEo1/+mMQleSbqtJAXJTnmkYyWs3g1ojXIQf3PzARhiQg5kBeM+1mA8J3CXw/q6y4TxuLsNT/5tzUzunnRotwfdlPIiXvkwWmGxzUEQ8uxO2o004feo4ma90JutxMZyQ76sxxrT72hMghm2LJae4wAe+uwpHSekhKGVi41VeHo+xVlLCQpeFV9abjIEpzdeBYTh7e8E2Y5XBuinlFVxG32FuruEJbkatYqrNKXM1/AQAg0LdQvmTaGR6/+EiqeXORIJip2izEk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000065, 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 Mon, Oct 30, 2023 at 1:14=E2=80=AFPM Peter Xu wrote: > > 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 an= d > > 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/s= elftests/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 >=3D nr_pages * page_size) > > + err("unexpected offset %lu\n", offset); > > + uffdio_move.dst =3D (unsigned long) area_dst + offset; > > + uffdio_move.src =3D (unsigned long) area_src + offset; > > + uffdio_move.len =3D page_size; > > + uffdio_move.mode =3D UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES; > > + uffdio_move.move =3D 0; > > + if (ioctl(ufd, UFFDIO_MOVE, &uffdio_move)) { > > + /* real retval in uffdio_move.move */ > > + if (uffdio_move.move !=3D -EEXIST) > > + err("UFFDIO_MOVE error: %"PRId64, > > + (int64_t)uffdio_move.move); > > + wake_range(ufd, uffdio_move.dst, page_size); > > + } else if (uffdio_move.move !=3D page_size) { > > + err("UFFDIO_MOVE error: %"PRId64, (int64_t)uffdio_move.mo= ve); > > + } 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/s= elftests/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 *ar= gs); > > 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/testi= ng/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 !=3D UFFD_EVENT_PAGEFAULT) > > + err("unexpected msg event %u", msg->event); > > + > > + if (msg->arg.pagefault.flags & > > + (UFFD_PAGEFAULT_FLAG_WP | UFFD_PAGEFAULT_FLAG_MINOR | UFFD_PA= GEFAULT_FLAG_WRITE)) > > + err("unexpected fault type %llu", msg->arg.pagefault.flag= s); > > + > > + offset =3D (char *)(unsigned long)msg->arg.pagefault.address - ar= ea_dst; > > + offset &=3D ~(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 =3D { 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 =3D 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 sourc= e > > + * page to destination. > > + */ > > + for (nr =3D 0; nr < nr_pages; nr++) { > > + /* Check area_src content */ > > + count =3D *area_count(area_src, nr); > > + if (count !=3D 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 =3D *area_count(area_dst, nr); > > + if (count !=3D 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 =3D *area_count(area_src, nr); > > + if (count !=3D 0) > > + err("nr %lu move failed %llu %llu\n", > > + nr, count, count_verify[nr]); > > + } > > + > > + if (write(pipefd[1], &c, sizeof(c)) !=3D sizeof(c)) > > + err("pipe write"); > > + if (pthread_join(uffd_mon, NULL)) > > + err("join() failed"); > > + > > + if (args.missing_faults !=3D nr_pages || args.minor_faults !=3D 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=3Dn */ > > + if (errno !=3D EINVAL) > > + return -errno; > > + } > > + return 0; > > +} > > + > > +struct uffd_test_case_ops uffd_move_test_case_ops =3D { > > + .post_alloc =3D prevent_hugepages, > > +}; > > + > > +#define ALIGN_UP(x, align_to) \ > > + (__typeof__(x))((((unsigned long)(x)) + ((align_to)-1)) & ~((alig= n_to)-1)) > > + > > +static char *orig_area_src, *orig_area_dst; > > +static int pmd_align_areas(void) > > +{ > > + orig_area_src =3D area_src; > > + orig_area_dst =3D area_dst; > > + area_src =3D ALIGN_UP(area_src, page_size); > > + area_dst =3D ALIGN_UP(area_dst, page_size); > > + nr_pages--; > > + > > + return 0; > > +} > > + > > +static void pmd_restore_areas(void) > > +{ > > + area_src =3D orig_area_src; > > + area_dst =3D 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 allocati= ng > either small/thp/default? I was considering that but was not sure which way would be more preferable - using these new callbacks or adding new page size requirements. I'll change to the latter. > > > > + > > +static int adjust_page_size(void) > > +{ > > + page_size =3D 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 chu= nk > to replace the thp test, which may contain a few thps? It also doesn't > need to be fault based. Sorry, I didn't get your suggestion. Could you please clarify? Which thp test are you referring to? Thanks, Suren. > > > + nr_pages =3D UFFD_TEST_MEM_SIZE / page_size; > > + > > + return 0; > > +} > > + > > +struct uffd_test_case_ops uffd_move_pmd_test_case_ops =3D { > > + .pre_alloc =3D adjust_page_size, > > + .post_alloc =3D pmd_align_areas, > > + .pre_release =3D pmd_restore_areas, > > +}; > > + > > /* > > * Test the returned uffdio_register.ioctls with different register mo= des. > > * Note that _UFFDIO_ZEROPAGE is tested separately in the zeropage tes= t. > > @@ -1141,6 +1268,20 @@ uffd_test_case_t uffd_tests[] =3D { > > .mem_targets =3D MEM_ALL, > > .uffd_feature_required =3D 0, > > }, > > + { > > + .name =3D "move", > > + .uffd_fn =3D uffd_move_test, > > + .mem_targets =3D MEM_ANON, > > + .uffd_feature_required =3D UFFD_FEATURE_MOVE, > > + .test_case_ops =3D &uffd_move_test_case_ops, > > + }, > > + { > > + .name =3D "move-pmd", > > + .uffd_fn =3D uffd_move_test, > > + .mem_targets =3D MEM_ANON, > > + .uffd_feature_required =3D UFFD_FEATURE_MOVE, > > + .test_case_ops =3D &uffd_move_pmd_test_case_ops, > > + }, > > { > > .name =3D "wp-fork", > > .uffd_fn =3D uffd_wp_fork_test, > > -- > > 2.42.0.820.g83a721a137-goog > > > > -- > Peter Xu >