Received: by 2002:a05:7412:85a1:b0:e2:908c:2ebd with SMTP id n33csp47859rdh; Mon, 30 Oct 2023 13:22:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHNSpkUS9YBpDLmnNG1bR2JpQgnWqQi+4dMCfqcm4w2AI7hytWeQ7SK+8SmB5OjPgBaDyeh X-Received: by 2002:a05:6a20:7f87:b0:15b:c800:48af with SMTP id d7-20020a056a207f8700b0015bc80048afmr10760934pzj.23.1698697359696; Mon, 30 Oct 2023 13:22:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698697359; cv=none; d=google.com; s=arc-20160816; b=IMwbkHqq2qUWiwSXEYOYVkvZCFT8iVlLACeFjrn4ZvvailATo0hGi8sh4FhFf565w2 Vh3r4M5+jm2OSBLluPHwR15aRzeUToR22CH5d9N9RY/RlYwYgXzSe1izN1MQfWbk9kfd SuaecSxS0R9+syQWaq5Pwln2sDe0R7OvBSzwqBl5GmmALTmAWB/6e8oJ5GCzp+lvuYEZ IvK2qxLwmmbsfeFxElcRsgL9McE7kxiK61GyBfSExVwVc06Z5LlzSf0+BCFihkPy/vCZ iZ6TKqxq6o+/kI7WfmyUNO9idZ5b5j/bOOOy0p61xXNDN9gwVffq+wMEahMLqQh5j0vG CO8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=0MZDC0C692oKqu67pAXxbmQTWTZ0RHUdPxw5a4EVzJM=; fh=4OSpxeiBwsuIzSsgUf+PjxsJgx9x8GY3HkIgnmcCqx8=; b=QHdFK7BavDFYQzXMYy7AWU9IncsnCVxZg8UDvqJ/J19bp60v/DEgx5fby3YE9l/wn4 5bVu/ZTQLfzVNRe0sv2wPwUWdOgWkSj4jeQG+glwchW2L24Loojfnmz43dR5z4UD+SSt Dtf9bwgMw7TzJlkKE7ek82dEytueEFOkIJqugZlTUYi9iv4DoOpUF1/jhkZfLutIwskS +ctJY5rB3O8qEuhsKb3blqCF84OhwQPKtBQGp41TVvTpaIkhebP61Z2JgCTUKQw1BY33 pJqaf+vIXTKndOF+8ny17yTgPKtvh06dUyqD2gSzLAojCFDVEM1ZMv0tuKWs9uXRpALx iAxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=0VlrHSGh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id b67-20020a633446000000b005b8f7bbeb92si5296672pga.153.2023.10.30.13.22.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 13:22:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=0VlrHSGh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id F04618061B41; Mon, 30 Oct 2023 13:22:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229837AbjJ3UWW (ORCPT + 99 others); Mon, 30 Oct 2023 16:22:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229485AbjJ3UWV (ORCPT ); Mon, 30 Oct 2023 16:22:21 -0400 Received: from mail-yw1-x112b.google.com (mail-yw1-x112b.google.com [IPv6:2607:f8b0:4864:20::112b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1709310B for ; Mon, 30 Oct 2023 13:22:17 -0700 (PDT) Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-5a90d6ab962so45244947b3.2 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=vger.kernel.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=0VlrHSGhwJIJkMtZFxHFwGGbWJFDorb6gAu4FeP8UzhWC6LI3o3rn275cluUJjzhrU WU0uYGc8cMV9zuP17dEcv90eosCcRFFhpzgLIQNjipUuX7p8zpuI6Rbi9mcIA4HBXx38 SREPRoWVKEATsdVRrYFFUUdon24wn5RL0+FkJ/42fPscZ2Lb3ky7NHPQ2Cgmq/GvL1ZG sHKp59MQeO33YokwkhO+x1QvzgyQHMDrNIf4NbZZzAPYqjmjYpHPsLE8mhHRDCEgj/j/ xUJRVKTpXCxgCqpFzOo/QtW2l5RgJV3OpuqarEi9e8frRwznpiX3NFKQAmIjwA5i996n Nb1g== 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=eMZTshEBrTgTSUjNK5/Xsm88NzfnAJMBzjmnWfNe3dp2vQz7/nfhXdHJ05cxMH8p5V 6FDDYXhQWfh8uV7aJcC17sT8w7rsEHof6jFGNF0aZMPHR+z6JJ/07ZHsxlYHS0PuJUqh OTiKQ1riC8ca+Rd+gWQXFMy0bDNsjgSBhtfrV4+TMaTjDYUGTshBuG7am5dIMBdowvDV 5QHo/nx1VlQINH45JOgvhieazAU7pJoY+t/wZ82HXBqnGrel6zLeTSzC5sd3DS9h4Wre rREjypP9m0dbCbkxDsE+Kypy6owGBvxo+hDxNR9AVMwtvOgI53c+kNp5HFbAlPHQ971o hLsg== X-Gm-Message-State: AOJu0YwWqNEOAORhHZJ5w/AThAebCDPr0g/SWp5evjY/2SAij9J6suGe lmf5ewTchJOUlnO8Sjt/eN81RGwEM+LEHcWlHsKgDzpT/s2zgsztb0I= 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-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 30 Oct 2023 13:22:24 -0700 (PDT) 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 >