From patchwork Wed Nov 15 15:49:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13457016 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 4E81EC2BB3F for ; Wed, 15 Nov 2023 15:50:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D09A28001E; Wed, 15 Nov 2023 10:50:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CB8D680018; Wed, 15 Nov 2023 10:50:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B324E8001E; Wed, 15 Nov 2023 10:50:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9DBB080018 for ; Wed, 15 Nov 2023 10:50:27 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6B2771CAC74 for ; Wed, 15 Nov 2023 15:50:27 +0000 (UTC) X-FDA: 81460625694.29.BB58ABC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf20.hostedemail.com (Postfix) with ESMTP id B842D1C0006 for ; Wed, 15 Nov 2023 15:50:25 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AK+gtyqp; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf20.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700063425; 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=zNOJj+YS6mMGjsLK5S7tjKltIfgKSXCOHuT8F0vbcKQ=; b=i8SNLCJnWve3XAXTcZ0gDSB/gcBLs1ow1A10a8Q+CT7/aYmVDc1dW4ucOahDjYfFqsWy/L GJuT3QolAT/EExgCQdK6ZicBJCoXMMUgILTzIPSXXYj7vlBgMt2myToCzqeg7DE/tLwOav r0+A6VzConf8+hvnonRHp9I0IVG3IP4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AK+gtyqp; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf20.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700063425; a=rsa-sha256; cv=none; b=5hAOyPc5urrG4yCtYJO9uwLwjF48c2ZncVioBSGu7ex4ARCGyjuEY56sRwF1gPCFmXoKco +HFGROh2wCD/Be0R/Aov9vAxWJHtQEv1uUB33qfztGKpJem/hsJDo1ZZzU11dWK2bUNUWS +Z80T8btbeGdvSx1Tw/BncNoX9/9wgg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700063425; 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=zNOJj+YS6mMGjsLK5S7tjKltIfgKSXCOHuT8F0vbcKQ=; b=AK+gtyqp/CM+y44wSJQOBdEXebFWvF3cHOmLgElYS2O5hASfCjqfACnDxc0b4D3fpXXNjf rHIJhbA++Ii7v4f0vfTyTzz/AlkcCLdqK35NQl5uZrDe3qNsPF5VrnsG76PSaDk6BLW+8Z h2zGEYNNj9plYsU6SkWOniOsIsWCZ6Y= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-aUOqZDu2NJavfuyT2sjYzQ-1; Wed, 15 Nov 2023 10:50:19 -0500 X-MC-Unique: aUOqZDu2NJavfuyT2sjYzQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AB4C33C000B4; Wed, 15 Nov 2023 15:50:17 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id 040352166B27; Wed, 15 Nov 2023 15:50:14 +0000 (UTC) From: David Howells To: Christian Brauner Cc: David Howells , Jens Axboe , Al Viro , Linus Torvalds , Christoph Hellwig , 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: [PATCH v3 07/10] iov_iter: Add extract kunit tests for ITER_UBUF and ITER_IOVEC Date: Wed, 15 Nov 2023 15:49:43 +0000 Message-ID: <20231115154946.3933808-8-dhowells@redhat.com> In-Reply-To: <20231115154946.3933808-1-dhowells@redhat.com> References: <20231115154946.3933808-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Rspamd-Queue-Id: B842D1C0006 X-Rspam-User: X-Rspamd-Server: rspam05 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dlm+lx8duifv5IJ6gTg7Bn0dAZ26wl4FHrBPKWa2Gdel/55Feb0bvqME3Y8cnjCSIQYCgweWk5CbUjXYlUytJNV0e6gEtLL2YQwu0SyGZpRtpOvt5ciarcI1QR8e93f8f4beqGiE/imPbf0VP7moOR8q1ps2uC8FSID2Pit9s+b2BzvcLflSI6Q3nHCpOJjcYtoMxk0puFcUAmkbgKC86YW+y7QS3yvaDxsuNdcXNGmpr2pOshHEfjhOlgSTg6EmW40dNxinmmP5LxPy3pOgfvGUyOIRv1fc/J+RscinBNY+rFhk00OOwucW9Ja1CT1lgKYfyuCIkwQDIHQ0EjY7Ndzpe+t7Yebb5/dRA7qUbyKY/Ypdfcfk8kWcbgfa+spolxWRDzjvWxDz4rV5jjpi1qb0ri25tV6rZJaZAohsyX9xR1wZdvH8elbK+kdVnVmNPyGZcJ0bGll/IiO4kT7f/O6L75ZOHMrwQXrX8hZHijndtcb10jbbTh8q2GG9Um3Dl3mUVbHlj+Z3TF4UvaNtZI1O1pzJQAxqmpSkebbOMpP13NksF60BZwHq7w5D7afUBN/s8pHkmbcJuGYUqsnT3Ut7IfrFOpde6qdyi6Q== 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: List-Subscribe: List-Unsubscribe: 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 34f0d82674ee..fdf598e49c0b 100644 --- a/lib/kunit_iov_iter.c +++ b/lib/kunit_iov_iter.c @@ -863,6 +863,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. */ @@ -1111,6 +1273,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),