From patchwork Thu Sep 14 15:26:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13385626 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 304A4EEAA4E for ; Thu, 14 Sep 2023 15:26:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9597D6B02C3; Thu, 14 Sep 2023 11:26:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 90A306B02C4; Thu, 14 Sep 2023 11:26:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 783A46B02C5; Thu, 14 Sep 2023 11:26:34 -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 6411C6B02C3 for ; Thu, 14 Sep 2023 11:26:34 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3220A80545 for ; Thu, 14 Sep 2023 15:26:34 +0000 (UTC) X-FDA: 81235579908.27.31421BA Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf22.hostedemail.com (Postfix) with ESMTP id 684B8C0009 for ; Thu, 14 Sep 2023 15:26:32 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=TBCVFZth; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3JyYDZQYKCK4gifSbPUccUZS.QcaZWbil-aaYjOQY.cfU@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3JyYDZQYKCK4gifSbPUccUZS.QcaZWbil-aaYjOQY.cfU@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694705192; a=rsa-sha256; cv=none; b=XLVvRY2tO1rOLUGLWrOggKlorlAs6OXvBfg8bPryK3cw5peLqPZwfHazojI3SFQBERnNU4 KII61VeEYAFJYzlvJvVTy7KzGS0IaENn9MUimEnXrd2rxF1cZ50VfLdDb6GCw8uUvSILwi wTCYqPecBAvDv+4sSrjRedeN86o6GG4= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=TBCVFZth; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3JyYDZQYKCK4gifSbPUccUZS.QcaZWbil-aaYjOQY.cfU@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3JyYDZQYKCK4gifSbPUccUZS.QcaZWbil-aaYjOQY.cfU@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694705192; 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=Mu1OGPWARKH9oRMfZEcpV5n3maR0zmAnXssCvh8qI9k=; b=vAlzPCkdnII2MtKd+USBrNLzX3V6dVME1cZHYO/VYnwLuR8wSDKaslB5olYzFglEukccFG OeE+0k8AwpGE8AkZ1yVBR9jwwm3XZphqEmVrW+mODjWYygt3xO/I0LEjhyg9S7gsTG2zyW ozTbkcULEiQtP3zGUS7nWXxosmmF5GM= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5925fb6087bso14678317b3.2 for ; Thu, 14 Sep 2023 08:26:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694705191; x=1695309991; 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=Mu1OGPWARKH9oRMfZEcpV5n3maR0zmAnXssCvh8qI9k=; b=TBCVFZth4e3YerU/bBVBoy+YksK6dAz8ciWNo1Iz2CussJzcIPAaSSy6e0erEeSxT/ KHACziaw1XYnAjWXCcBTw1/xDjAMqOkZu9sWiku3ZYuqoHqRsaAjRMiNiN6Unr5Ymxtx hbefGOosvNUnHBX47ACgwRWv71N2GgBZYW220k+Ji1FV5gtftr8MFnwvzy8wUQ/DNPkk 9v1UtnG+z/wXBKOwM7u/QreBbKy1++c6+e+G3KWYsqBpWNaz1WmH9y3EM6iCwprxIEVa sxROPw2rvqe3cm47KSSccTGtl4/3mBI68+cETW6E+T6hxA80I6bDpj2vyQncfOYx1yhQ d2LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694705191; x=1695309991; 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=Mu1OGPWARKH9oRMfZEcpV5n3maR0zmAnXssCvh8qI9k=; b=VTRPJGYuNAjDHyT6M67de2rhnUlgHU5k2iCIGjHzMjW/txOnzKdHYCgRO/qPd8rQR5 f1oi03yl5QOkdcakGj8blmio9S0pmCvnG8lJInGc3vMm6/ELLPu4Dd49BV+mO0Z9PplV fY8FRcB7ZcJoAKbYutLROxGXK7Yy10LyCTyNJ2mjaI2IfuMPOWrRXt1HB4MVzqHgWfpI l55KEdAAAmSrHZfKwP4GwnLkNIWQ7LsE8+2ctwzMJcqrEYVlpVYQpKjyCdzzjebvVum3 F6g7wv+3jTFyrpPMC1LsPIEDhxKOkhtR5w8u3aWvWhZ+xI6Bm5M7gWDY5DQhp5WvQcZG xPrg== X-Gm-Message-State: AOJu0Ywiirmt+aCMhTAS3y/buAlEJx/jKOFTqimK55jvnl6B/uMmdOwa Sf+3r2TIzA7bguUAmAvS/Nad4OSzEY0= X-Google-Smtp-Source: AGHT+IFBVegGxf80f6tK8JTRTL2eDdZPWBJrMSOMFpp88sQURTGOGeLIWNKbAvxE88xxjjHdcadvLmZLUmU= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:405c:ba64:810f:5fee]) (user=surenb job=sendgmr) by 2002:a81:c94b:0:b0:59b:ba8a:97d0 with SMTP id c11-20020a81c94b000000b0059bba8a97d0mr154662ywl.3.1694705191508; Thu, 14 Sep 2023 08:26:31 -0700 (PDT) Date: Thu, 14 Sep 2023 08:26:13 -0700 In-Reply-To: <20230914152620.2743033-1-surenb@google.com> Mime-Version: 1.0 References: <20230914152620.2743033-1-surenb@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914152620.2743033-4-surenb@google.com> Subject: [PATCH 3/3] selftests/mm: add UFFDIO_REMAP 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 684B8C0009 X-Stat-Signature: tqku77wa7h1h5m7bp6nwfa8393p7no6n X-HE-Tag: 1694705192-941150 X-HE-Meta: U2FsdGVkX195F6wZb0KUxS67xH7NtnygyEb7xc+IkvPEari+NAngij8I4PIm9E6MFnO3YL8mYRaU0RCZFYazUBJ701ThyZL8esVmsgaP3CkWPn1l6HtOAgMs2hcyAe2br4nINOxomo3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BlGTO30oaxIPikh8jS2RHSRIGrZSLq8jTnyCz/6JzAdNrHj6wFMqikuM66HgBT708RLNVVBjaE4s6mV8HsWuVMNVT7vzQkoGTGDYPMUK6shuqVwTcgHRime2Rm60ieaytSujYKIA9C47GoVJ1PxlZnNSjGbir653pc3JvjXeuUKtgjvtBcG5PplffF3JK31dJQt7jLQMSSDVMPYClwj7uD//q3wAIsb2oc83AGDBZpC9BjbKyxxpREJOoRjtHlFLnMXXVZWIfbxAyA+TKyBQhQPJqCaMkD/kjCm5m9FSuj5SGGT8hFx0zl/fFTidsqjd3vAuXb+FpVgIdaPbJp/Icow7NIeAX5D1FB3Rt3j7nGQhtjeMbNJ+AyQ4TP+Jlxmnv2DnBJW3O4LYhiYWhaklAQ1blfJZzWaEBThcg== 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_REMAP ioctl which uses uffd to remaps 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 | 34 ++++++++++- tools/testing/selftests/mm/uffd-common.h | 1 + tools/testing/selftests/mm/uffd-unit-tests.c | 62 ++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c index 02b89860e193..fb449abc46e2 100644 --- a/tools/testing/selftests/mm/uffd-common.c +++ b/tools/testing/selftests/mm/uffd-common.c @@ -484,8 +484,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_REMAP is supported for anon non-shared mappings. */ + if (uffd_test_ops == &anon_uffd_test_ops && !map_shared) { + if (remap_page(uffd, offset)) + args->missing_faults++; + } else { + if (copy_page(uffd, offset, args->apply_wp)) + args->missing_faults++; + } } } @@ -620,6 +626,30 @@ int copy_page(int ufd, unsigned long offset, bool wp) return __copy_page(ufd, offset, false, wp); } +int remap_page(int ufd, unsigned long offset) +{ + struct uffdio_remap uffdio_remap; + + if (offset >= nr_pages * page_size) + err("unexpected offset %lu\n", offset); + uffdio_remap.dst = (unsigned long) area_dst + offset; + uffdio_remap.src = (unsigned long) area_src + offset; + uffdio_remap.len = page_size; + uffdio_remap.mode = UFFDIO_REMAP_MODE_ALLOW_SRC_HOLES; + uffdio_remap.remap = 0; + if (ioctl(ufd, UFFDIO_REMAP, &uffdio_remap)) { + /* real retval in uffdio_remap.remap */ + if (uffdio_remap.remap != -EEXIST) + err("UFFDIO_REMAP error: %"PRId64, + (int64_t)uffdio_remap.remap); + wake_range(ufd, uffdio_remap.dst, page_size); + } else if (uffdio_remap.remap != page_size) { + err("UFFDIO_REMAP error: %"PRId64, (int64_t)uffdio_remap.remap); + } 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..2bbb15d1920c 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 remap_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..a33819639187 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_remap_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 remap 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 = "remap", + .uffd_fn = uffd_remap_test, + .mem_targets = MEM_ANON, + .uffd_feature_required = 0, + }, { .name = "wp-fork", .uffd_fn = uffd_wp_fork_test,