From patchwork Mon Oct 9 06:42:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13412985 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 26557E95A8E for ; Mon, 9 Oct 2023 06:42:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 299466B01A3; Mon, 9 Oct 2023 02:42:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1FB116B01A5; Mon, 9 Oct 2023 02:42:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 026196B01A8; Mon, 9 Oct 2023 02:42:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E1C536B01A3 for ; Mon, 9 Oct 2023 02:42:44 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C1B83B42A5 for ; Mon, 9 Oct 2023 06:42:44 +0000 (UTC) X-FDA: 81324979848.16.EE187E7 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf14.hostedemail.com (Postfix) with ESMTP id 0B185100020 for ; Mon, 9 Oct 2023 06:42:42 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=NoRwV03W; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 34qAjZQYKCOEVXUHQEJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=34qAjZQYKCOEVXUHQEJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696833763; 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=50i/QdEI5D071LaRqB1M3KzWwtbx6nE7wmO2EXtVHvA=; b=Gc5Tj8T8tnOc5/iHoIJgJdZbJaj6j9edQjCt8AiG9EjMfrJohNr6ZxoVD3gR858PBQxRsR TRmvjrH+oUUiG5rL8fN8PIJr7OYogKwOVnbJC21WOTluuRV/0+I41cgVGc28SP0lKIcRMV H9bMhT4qCajLmpkOdwV9j06HEl0Wgzs= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=NoRwV03W; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 34qAjZQYKCOEVXUHQEJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=34qAjZQYKCOEVXUHQEJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696833763; a=rsa-sha256; cv=none; b=tiC9O7vBKk5fX96PQ6ukPST+WQ6gfq4GJsxBskwVph2NDd4RDqf78q56eBCXA8aES/JqiG 0Db1iItL7X0oRDAI2obaHguApq4EDLQA7RrrP8DzPRs9/+1Ofd9SCCKjuq3JjQez1MrTK1 bjqgQDjCFXqUEu0a87FAAmxTBSsaVtw= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d86dac81f8fso5890091276.1 for ; Sun, 08 Oct 2023 23:42:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696833762; x=1697438562; darn=kvack.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=50i/QdEI5D071LaRqB1M3KzWwtbx6nE7wmO2EXtVHvA=; b=NoRwV03W3cHOMXZuviXkBvhKlDTYM179n8aGUXdBuehN8Q5aRyHdQel60erNMlegEQ DU+PRCFY30SC6Zi5Vf4spZNljGB8qh7A5U4XjzIA/Bg+LvxSx0r0Uk7S0i96Xjc3dPrW OpJ2BjOHE0F256PYEPZD50WY6vHg9cSU4KR23BdTgZHDCHlNS1o+QZqnaXgO6R3dHccQ bIhRs00ZP5+dxlcMHN/QRM8tfW9I4IkjChUofWOyjPg23uk7R/5VsJfslhjvn5diAhZM 4rA420cW8urpTvmo1U6IaSSOnX/8EaLe3C9RaiiGMU8xY8yPSrwPgOtzNNp05tgkdnoo ADAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696833762; x=1697438562; 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=50i/QdEI5D071LaRqB1M3KzWwtbx6nE7wmO2EXtVHvA=; b=UVNMmZLI1t2FZujfoPcazR470B2WiuBSCk4AlEgKi0btlvB9Chhv5ufZ6razifYYGv SqpnaQyCJkxVH8RpS4iFQq08HvdWAwFuGRElmo8G83UbcWKy0WJCSNQd8uA/LkE/03/b zYC7HOVsNYjext66YTnN76TM7scVz1anl3fviuxaVf5jBzcJD2URxbmUyYe49W+wMmgv chdEK52vTFtCAUcf08oQeWdUpHXSm+xGiwNvUfkqGtXbe5V4+mM0QpjsYEBHOCxgpasj uG/VDr8v6fhqkVRUP37Qqr2or1KoJjaJuba3qHs1lB8TxtcEK3gFEatflhiTW2ZScZ4d ttCA== X-Gm-Message-State: AOJu0YwK7w52mG72AQCdvIUfhTI8ng6GpFWyg14+6oryvUOI0o+mLNLi k8B8ATYtV1vPs1QM96qwYJIdWsBWK0Y= X-Google-Smtp-Source: AGHT+IE3ENVpgSSY9jHiu1FOcPq/GRVi/eysZgrxroKYd6Fm4a1cG8w7FQlArfhqzdMAUqw7rMWUDxE9tZI= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3e83:601e:dc42:a455]) (user=surenb job=sendgmr) by 2002:a25:68ce:0:b0:d86:56bc:e289 with SMTP id d197-20020a2568ce000000b00d8656bce289mr225181ybc.4.1696833762182; Sun, 08 Oct 2023 23:42:42 -0700 (PDT) Date: Sun, 8 Oct 2023 23:42:28 -0700 In-Reply-To: <20231009064230.2952396-1-surenb@google.com> Mime-Version: 1.0 References: <20231009064230.2952396-1-surenb@google.com> X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Message-ID: <20231009064230.2952396-4-surenb@google.com> Subject: [PATCH v3 3/3] 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 X-Rspamd-Queue-Id: 0B185100020 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: pnq1ykhedsrtkrcfh6hcqg6w8pi15wth X-HE-Tag: 1696833762-388301 X-HE-Meta: U2FsdGVkX1+j7FwAB1HA33Uj0bDjRmhsJ2LyePsGtOP/IRK2Pl48u8Xjplej8x/8WMZDWMlyeTqdejPXB0eS8dX7wBXcnRFAx2NFU1aYGoFGPO0L+S0hJDINYRjBpHk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jELXP/PSn1CRdER/qA5t3hj82jKPh/8098HnDSiWNXSURU9UfztGe8BwCeCy6tX3n+kVZdwqnSFdFnU7+O+OGfd5Whb0np7RwMa0x3UYy/5v/EqFBD0uDi9avYh17RzFTgufSaXKbiJnKOc5IsTQI3MSWK/meqZbm6DeaZx2nI3FUhTvPNZDfJt6KfZX+iTq+CrbGY8T4UooBs+OoWxwF2rNwaRQyu9TT5jbxsg2GXk+XNlsefcuGp6jDgQGezxzTY32hUSepRCIP1N9zOmmTUvzJoef5+7jFDWmA4I9FF0fRIWz9lYasTW9+ggo79w2jW+zQe/EfpZ1+HQk/UjJF3N9aKIYUjmxRFMXnkzP2ciOfYr1EezjXULoBof 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: Add a test for new UFFDIO_MOVE ioctl which uses uffd to move source into destination buffer while checking the contents of both after remapping. After the operation the content of the destination buffer should match the original source buffer's content while the source buffer should be zeroed. Signed-off-by: Suren Baghdasaryan --- tools/testing/selftests/mm/uffd-common.c | 41 ++++++++++++- tools/testing/selftests/mm/uffd-common.h | 1 + tools/testing/selftests/mm/uffd-unit-tests.c | 62 ++++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c index 02b89860e193..ecc1244f1c2b 100644 --- a/tools/testing/selftests/mm/uffd-common.c +++ b/tools/testing/selftests/mm/uffd-common.c @@ -52,6 +52,13 @@ static int anon_allocate_area(void **alloc_area, bool is_src) *alloc_area = NULL; return -errno; } + + /* Prevent source pages from collapsing into THPs */ + if (madvise(*alloc_area, nr_pages * page_size, MADV_NOHUGEPAGE)) { + *alloc_area = NULL; + return -errno; + } + return 0; } @@ -484,8 +491,14 @@ void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_args *args) offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst; offset &= ~(page_size-1); - if (copy_page(uffd, offset, args->apply_wp)) - args->missing_faults++; + /* UFFD_MOVE is supported for anon non-shared mappings. */ + if (uffd_test_ops == &anon_uffd_test_ops && !map_shared) { + if (move_page(uffd, offset)) + args->missing_faults++; + } else { + if (copy_page(uffd, offset, args->apply_wp)) + args->missing_faults++; + } } } @@ -620,6 +633,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 7c4fa964c3b0..f4d79e169a3d 100644 --- a/tools/testing/selftests/mm/uffd-common.h +++ b/tools/testing/selftests/mm/uffd-common.h @@ -111,6 +111,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 2709a34a39c5..f0ded3b34367 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -824,6 +824,10 @@ static void uffd_events_test_common(bool wp) char c; 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) failed"); + fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK); if (uffd_register(uffd, area_dst, nr_pages * page_size, true, wp, false)) @@ -1062,6 +1066,58 @@ static void uffd_poison_test(uffd_test_args_t *targs) uffd_test_pass(); } +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 }; + + if (uffd_register(uffd, area_dst, nr_pages * page_size, + true, false, false)) + err("register failure"); + + 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 remapping 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 remap 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(); +} + /* * Test the returned uffdio_register.ioctls with different register modes. * Note that _UFFDIO_ZEROPAGE is tested separately in the zeropage test. @@ -1139,6 +1195,12 @@ 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, + }, { .name = "wp-fork", .uffd_fn = uffd_wp_fork_test,