Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1509049rdh; Fri, 27 Oct 2023 17:39:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEsijX2XsXinJH2L/1Pq7iHozCtEteGKKjg0Jg2/a2WORwh3cY6dRclz7g4sV405BKQKeQ3 X-Received: by 2002:a05:6358:e488:b0:168:e98e:7c21 with SMTP id by8-20020a056358e48800b00168e98e7c21mr5646827rwb.24.1698453588774; Fri, 27 Oct 2023 17:39:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698453588; cv=none; d=google.com; s=arc-20160816; b=01btJdxEqGghyORu0QVTRMKcWXKnhRxsCSzJ1X3jFXMaldyhuGORYUjVbHSzc1q1s3 RKfGs5xVUgefNKUEjeFGmT6pIipsR51Mm6qUJAXfHPp2hTcg4KDXyG25yGXOMhCEr+k3 KTgLuPIwXSDcHQJLfwwMc0pzV1rqKeQvnrfSn3UXSPnncERjusCgT9MPep3REwVrtXRo ULCGp20pqnYMkFTYL2ZlJhnnaDlNRy0Wzu7jA6u4kWZLcdfcBe/ZenbJAaq0uDRaXgz7 g+sL0pdIUl/o6qEuGJjdwZE9Wk3fQY1rKPlF1rfWwMjB+aS8rFiaEn0HWaIXL4zsaoJi 634g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=DPstGiI3zb7PZzW8m1s/TtyE02RR3fMaWMy1rl5jbQE=; fh=vBLx0KH+vftfPuCKolEKY+MKbAkCpwC5WBbaBlSuTXE=; b=LqLC4mQgBZX9X7J927F+DKz27gDxwaa0GSfksgaRYMGf55HjumGC/DcCJTqIPj0om7 U9Wb72vbJ3gXPKE9up9HgWyrGkawUvvnP7a38iGfz/Gcz3OKmSCJCE7xHkJmSIiUaQIE UHgtsa4CGMYeCwvjOwNEtHlfP2CjoGH+hqbVZBholATo6LWzeuv9w7HOe9xpFibogsvr RtYcs83JjuEv0tpliRFcAUUwzctckuMBz3fvLGJ/GH+tllvJSs/u9Xjffj3GAOUBus6A Hu3hXa+jPXVhMZbY7fSUH4vEnFn9EAadBrb9z8NxUIfkPFJX8C97VfshtMo8nziEX703 nCqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=BeSptafS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id x185-20020a6386c2000000b005644a9be955si1771203pgd.179.2023.10.27.17.39.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 17:39:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=BeSptafS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id 941C28384F69; Fri, 27 Oct 2023 17:39:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346785AbjJ1Aiy (ORCPT + 99 others); Fri, 27 Oct 2023 20:38:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232920AbjJ1Aip (ORCPT ); Fri, 27 Oct 2023 20:38:45 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AABB8D44 for ; Fri, 27 Oct 2023 17:38:34 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5ae5b12227fso23579017b3.0 for ; Fri, 27 Oct 2023 17:38:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698453514; x=1699058314; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DPstGiI3zb7PZzW8m1s/TtyE02RR3fMaWMy1rl5jbQE=; b=BeSptafSteAJ1GLueLN5Xs0S9464cQ+bGDix6+RjP+34ekltXa3wkWpIdTTHJr6Oxi hQzW97G8IwhN0NY11Y7fCf3o9LT4dD7Iezc3c1emKdNj6lwrdY87tw/pS7mo8uxFimEp TmWkXce2ZJWeP4ZCaeAm3jljkHHEiaAqRdwFQz52vos3qsswhARr34RI3opDXCU15+KL q2PS8XEks8jIskgBxTYOxTKIF8+uZ6u4GkXUv6tMt24+/3mqL8wSzgyalnx8b4UIBPbO hYFSq88kZ1QITcexW1GNbwenyIP6sdIlk4OhYOr2HD2zpx26YmvwCx85VVStEYvKDI/9 V+bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698453514; x=1699058314; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DPstGiI3zb7PZzW8m1s/TtyE02RR3fMaWMy1rl5jbQE=; b=AbPLsT+MxEFxcc6yf9V3p34SIbiACXguipO6jXkPb5yhc0AacaHQDnvgHGfWiyyCNj DBCBvpWyq5YurqdC5K553OwRdOhH1y1zBahgmiPWRuLy2Ui011j/mgjrqFG/46qtUYb/ mzCAM4TkJsNqIT/3ytdiK6ATCZpeDIR5lHoXAWtDUy8zNRILUZjL2jNJLZi1EAjPRpLA DG6D9QVZm7h70lMPzEaF19gFE2CHInIuNMlPchg2E7cQWkcAnpTYWyM70QRGoAW3OGR+ W/j0mV0SlzIIFFcNFn/uoa/fyHp23A3Oby80qpW7jkxPtln4jtIifA/zP8XPbz62ifQh 55MA== X-Gm-Message-State: AOJu0YzT14TMX0g9kiVlrgkFymw0hKJ04acYbBKCjytBo4WxGAuZxsIz wBGCqCVwKqd8l1pPswCKCWoRT7mw9pU= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:cba3:7e7f:79e4:fa57]) (user=surenb job=sendgmr) by 2002:a81:6d17:0:b0:59b:c6bb:babb with SMTP id i23-20020a816d17000000b0059bc6bbbabbmr87372ywc.6.1698453513868; Fri, 27 Oct 2023 17:38:33 -0700 (PDT) Date: Fri, 27 Oct 2023 17:38:15 -0700 In-Reply-To: <20231028003819.652322-1-surenb@google.com> Mime-Version: 1.0 References: <20231028003819.652322-1-surenb@google.com> X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231028003819.652322-6-surenb@google.com> Subject: [PATCH v4 5/5] selftests/mm: add UFFDIO_MOVE ioctl test From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: viro@zeniv.linux.org.uk, brauner@kernel.org, shuah@kernel.org, aarcange@redhat.com, lokeshgidra@google.com, peterx@redhat.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, surenb@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" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email 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 (fry.vger.email [0.0.0.0]); Fri, 27 Oct 2023 17:39:45 -0700 (PDT) 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++; +} + +static int adjust_page_size(void) +{ + page_size = default_huge_page_size(); + 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