From patchwork Thu Sep 14 22:15:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13386117 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 5E9A2EEAA7B for ; Thu, 14 Sep 2023 22:16:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E70C16B0302; Thu, 14 Sep 2023 18:15:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E24056B0303; Thu, 14 Sep 2023 18:15:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C99386B0304; Thu, 14 Sep 2023 18:15:59 -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 B4EA26B0302 for ; Thu, 14 Sep 2023 18:15:59 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8BC6D80457 for ; Thu, 14 Sep 2023 22:15:59 +0000 (UTC) X-FDA: 81236611638.09.8B0EA81 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf03.hostedemail.com (Postfix) with ESMTP id D2D1E20020 for ; Thu, 14 Sep 2023 22:15:57 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MViHRLLx; spf=pass (imf03.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694729757; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qqxTwY9HqQXE+DORhqdyNAmY3fFbiduJ/gi0Qzq6Lps=; b=zWp5T6FbwFIsjO3/JPzQxxz2eIDe7okYnDBsrESO+wcUeqzT2uup3KJYBXvzjSoTHhuGq7 fgL822Gedcy5nm605TIZDDCKIm2jymxRv0eTO/1sCWTWeCpoSAOWTowuNiQwaug2mFiSjG l2YAj0Au7O23RudykUbXOuBCK5gkSOI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694729757; a=rsa-sha256; cv=none; b=cP03ZUTyNobUhLW1XctUOOjIj4d8JFaKfacDmqqL5E4M1GEx6+Q9VHUVlYrrfUXRZFbWbZ hGJiTRn9/zjOJrm7RkSSheKvMH2UZ4teUVVSsczWGDXtSNQkCUMNY1fL6IvdR9UXjwVa6V tsg/qqKRikv+VWP2fz7Uy0c7NFYngsA= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MViHRLLx; spf=pass (imf03.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694729757; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qqxTwY9HqQXE+DORhqdyNAmY3fFbiduJ/gi0Qzq6Lps=; b=MViHRLLxoNJxcstaHLsGHewyf+Uwp4/tIVxsyPhNNIEQNtScEfU39JIneJaN42FflHz6JZ dUuP7awixA2MePTNymT0hlyYwzqg+BueiC2UPJjZ4NnFiSsHNZvWbsG6flROi7PQBO3Q6V w4JbyYJIawmowCKC9V1PSNXCUuxFgz8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-47-NrFIS32FOnS-jSfNadb49g-1; Thu, 14 Sep 2023 18:15:52 -0400 X-MC-Unique: NrFIS32FOnS-jSfNadb49g-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C7CF8800045; Thu, 14 Sep 2023 22:15:51 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.216]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6D3EC40C6EA8; Thu, 14 Sep 2023 22:15:49 +0000 (UTC) From: David Howells To: Al Viro , Linus Torvalds Cc: David Howells , Jens Axboe , Christoph Hellwig , Christian Brauner , David Laight , Matthew Wilcox , Brendan Higgins , David Gow , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Andrew Morton , Christian Brauner , David Hildenbrand , John Hubbard Subject: [RFC PATCH 7/9] iov_iter: Add extract kunit tests for ITER_UBUF and ITER_IOVEC Date: Thu, 14 Sep 2023 23:15:24 +0100 Message-ID: <20230914221526.3153402-8-dhowells@redhat.com> In-Reply-To: <20230914221526.3153402-1-dhowells@redhat.com> References: <20230914221526.3153402-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Stat-Signature: ngi4p5brceb9ofhs5588o5xut9am87iy X-Rspamd-Server: rspam10 X-Rspamd-Queue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ON3b2VVZOgfKYBclN13fxubdCLmyrrDGbOZRJJoohMUq7ywFZNkTpE6/TTMW1HY+uVrIttosKGABQVuj7a+MmzKZSijnY6d1aTTLASd2IK6NKYrb4PrwMkCgS3Lq4mcqkeIcmQevhPN9i7I9MB9M82kB0bCDq7xZHhWVROwcKXTfN+FvUBpy6CV3yI9Ic6YXofRfvwnGP8Wc+Tctp8UBNOcY3brBuM4oQS9qm5RmmV6/K9/SO7hI6mbh4Rd5UZpEOMR42FJ8dY9sZsyQNBg7T2op/TL01UvMdKJIPLmjTixiMI0fZA3zJXHTJV4j+Y29tmJlF0WtKSq67rEbx28460TZJvVBtCyZh1fEtGYe1SUHgj1lTmfI84nN0Fo4FGbNR1zK1UwgafnMq9WXo3BMzKWv8VvgYhlVdT8ghUd6m7YSsL74M9Bf8KiX+uRh0zigrrsRROHTWEDMczCuQyH/m5aCKFIIQEU3bl+sTz6n6mUhm/h//Wc0GjmcxM95pUHYZezpVXFvywnKbDeZ3Xar3di1rk+Y5CkBqDoi5a8tzHiVh1jkPbY2+AQAiBIOEZOXwaOq194T9m+vGs9VwKQoF/0g== 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 extraction kunit tests for ITER_UBUF- and ITER_IOVEC-type iterators. This attaches a userspace VM with a mapped file in it temporarily to the test thread. [!] Note that this requires the kernel thread running the test to obtain and deploy an mm_struct so that a user-side buffer can be created with mmap - basically it has to emulated part of execve(). Doing so requires access to additional core symbols: mm_alloc(), vm_area_alloc(), insert_vm_struct() and arch_pick_mmap_layout(). See the iov_kunit_create_user_buf() function added in the patch. Signed-off-by: David Howells cc: Andrew Morton cc: Christoph Hellwig cc: Christian Brauner cc: Jens Axboe cc: Al Viro cc: Matthew Wilcox cc: David Hildenbrand cc: John Hubbard cc: Brendan Higgins cc: David Gow cc: linux-mm@kvack.org cc: linux-fsdevel@vger.kernel.org cc: linux-kselftest@vger.kernel.org cc: kunit-dev@googlegroups.com --- lib/kunit_iov_iter.c | 164 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) diff --git a/lib/kunit_iov_iter.c b/lib/kunit_iov_iter.c index 78f566ebd4a6..0b7961bbab62 100644 --- a/lib/kunit_iov_iter.c +++ b/lib/kunit_iov_iter.c @@ -842,6 +842,168 @@ static void __init iov_kunit_copy_from_xarray(struct kunit *test) KUNIT_SUCCEED(); } +/* + * Test the extraction of ITER_UBUF-type iterators. + */ +static void __init iov_kunit_extract_pages_ubuf(struct kunit *test) +{ + const struct iov_kunit_range *pr; + struct iov_iter iter; + struct page **bpages, *pagelist[8], **pages = pagelist; + ssize_t len; + size_t bufsize, size = 0, npages; + int i, from; + u8 __user *buffer; + + bufsize = 0x100000; + npages = bufsize / PAGE_SIZE; + + buffer = iov_kunit_create_user_buf(test, npages, &bpages); + + for (pr = kvec_test_ranges; pr->page >= 0; pr++) { + from = pr->from; + size = pr->to - from; + KUNIT_ASSERT_LE(test, pr->to, bufsize); + + iov_iter_ubuf(&iter, ITER_SOURCE, buffer + pr->from, size); + + do { + size_t offset0 = LONG_MAX; + + for (i = 0; i < ARRAY_SIZE(pagelist); i++) + pagelist[i] = (void *)POISON_POINTER_DELTA + 0x5a; + + len = iov_iter_extract_pages(&iter, &pages, 100 * 1024, + ARRAY_SIZE(pagelist), 0, &offset0); + KUNIT_EXPECT_GE(test, len, 0); + if (len < 0) + break; + KUNIT_EXPECT_LE(test, len, size); + KUNIT_EXPECT_EQ(test, iter.count, size - len); + if (len == 0) + break; + size -= len; + KUNIT_EXPECT_GE(test, (ssize_t)offset0, 0); + KUNIT_EXPECT_LT(test, offset0, PAGE_SIZE); + + /* We're only checking the page pointers */ + unpin_user_pages(pages, (offset0 + len) / PAGE_SIZE); + + for (i = 0; i < ARRAY_SIZE(pagelist); i++) { + struct page *p; + ssize_t part = min_t(ssize_t, len, PAGE_SIZE - offset0); + int ix; + + KUNIT_ASSERT_GE(test, part, 0); + ix = from / PAGE_SIZE; + KUNIT_ASSERT_LT(test, ix, npages); + p = bpages[ix]; + KUNIT_EXPECT_PTR_EQ(test, pagelist[i], p); + KUNIT_EXPECT_EQ(test, offset0, from % PAGE_SIZE); + from += part; + len -= part; + KUNIT_ASSERT_GE(test, len, 0); + if (len == 0) + break; + offset0 = 0; + } + + if (test->status == KUNIT_FAILURE) + goto stop; + } while (iov_iter_count(&iter) > 0); + + KUNIT_EXPECT_EQ(test, size, 0); + KUNIT_EXPECT_EQ(test, iter.count, 0); + KUNIT_EXPECT_EQ(test, iter.iov_offset, pr->to - pr->from); + } + +stop: + KUNIT_SUCCEED(); +} + +/* + * Test the extraction of ITER_IOVEC-type iterators. + */ +static void __init iov_kunit_extract_pages_iovec(struct kunit *test) +{ + const struct iov_kunit_range *pr; + struct iov_iter iter; + struct iovec iov[8]; + struct page **bpages, *pagelist[8], **pages = pagelist; + ssize_t len; + size_t bufsize, size = 0, npages; + int i, from; + u8 __user *buffer; + + bufsize = 0x100000; + npages = bufsize / PAGE_SIZE; + + buffer = iov_kunit_create_user_buf(test, npages, &bpages); + + iov_kunit_load_iovec(test, &iter, ITER_SOURCE, iov, ARRAY_SIZE(iov), + buffer, bufsize, kvec_test_ranges); + size = iter.count; + + pr = kvec_test_ranges; + from = pr->from; + do { + size_t offset0 = LONG_MAX; + + for (i = 0; i < ARRAY_SIZE(pagelist); i++) + pagelist[i] = (void *)POISON_POINTER_DELTA + 0x5a; + + len = iov_iter_extract_pages(&iter, &pages, 100 * 1024, + ARRAY_SIZE(pagelist), 0, &offset0); + KUNIT_EXPECT_GE(test, len, 0); + if (len < 0) + break; + KUNIT_EXPECT_LE(test, len, size); + KUNIT_EXPECT_EQ(test, iter.count, size - len); + if (len == 0) + break; + size -= len; + KUNIT_EXPECT_GE(test, (ssize_t)offset0, 0); + KUNIT_EXPECT_LT(test, offset0, PAGE_SIZE); + + /* We're only checking the page pointers */ + unpin_user_pages(pages, (offset0 + len) / PAGE_SIZE); + + for (i = 0; i < ARRAY_SIZE(pagelist); i++) { + struct page *p; + ssize_t part = min_t(ssize_t, len, PAGE_SIZE - offset0); + int ix; + + KUNIT_ASSERT_GE(test, part, 0); + while (from == pr->to) { + pr++; + from = pr->from; + if (pr->page < 0) + goto stop; + } + + ix = from / PAGE_SIZE; + KUNIT_ASSERT_LT(test, ix, npages); + p = bpages[ix]; + KUNIT_EXPECT_PTR_EQ(test, pagelist[i], p); + KUNIT_EXPECT_EQ(test, offset0, from % PAGE_SIZE); + from += part; + len -= part; + KUNIT_ASSERT_GE(test, len, 0); + if (len == 0) + break; + offset0 = 0; + } + + if (test->status == KUNIT_FAILURE) + break; + } while (iov_iter_count(&iter) > 0); + +stop: + KUNIT_EXPECT_EQ(test, size, 0); + KUNIT_EXPECT_EQ(test, iter.count, 0); + KUNIT_SUCCEED(); +} + /* * Test the extraction of ITER_KVEC-type iterators. */ @@ -1090,6 +1252,8 @@ static struct kunit_case __refdata iov_kunit_cases[] = { KUNIT_CASE(iov_kunit_copy_from_bvec), KUNIT_CASE(iov_kunit_copy_to_xarray), KUNIT_CASE(iov_kunit_copy_from_xarray), + KUNIT_CASE(iov_kunit_extract_pages_ubuf), + KUNIT_CASE(iov_kunit_extract_pages_iovec), KUNIT_CASE(iov_kunit_extract_pages_kvec), KUNIT_CASE(iov_kunit_extract_pages_bvec), KUNIT_CASE(iov_kunit_extract_pages_xarray),