From patchwork Thu Sep 14 22:15:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13386116 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 79E57EEAA7A for ; Thu, 14 Sep 2023 22:15:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC4C86B02FF; Thu, 14 Sep 2023 18:15:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A76E36B0300; Thu, 14 Sep 2023 18:15:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8CA226B0301; Thu, 14 Sep 2023 18:15:56 -0400 (EDT) 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 74CC06B02FF for ; Thu, 14 Sep 2023 18:15:56 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 52FC6A0673 for ; Thu, 14 Sep 2023 22:15:56 +0000 (UTC) X-FDA: 81236611512.14.1E054EF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 8774A180015 for ; Thu, 14 Sep 2023 22:15:54 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FwxgI61O; spf=pass (imf24.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694729754; a=rsa-sha256; cv=none; b=8h08W3QjtxDhjyfyt6IrJQTlzEqhwxDF/2Nemn0K9uI8AL7e4BMKmQB3Q0Spiutno/b+n9 NPnVAXzOVmVGeMRmzFuEXazJsh1O3EUm1YSKFnUK2377cEHCcjj33ayyLggp7JGXDv1sph DMnifgBNgNBLvPF7ukisTKZsaxGF9pM= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FwxgI61O; spf=pass (imf24.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.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=1694729754; 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=VtAwaM2B3p8HaWMuYOi3rMk0MQyqz+yXDmdiAqy5Wgk=; b=fZQ84qp4YTavrk7Vtq5ebeTrK1GsE8hnFiYotNHRdlc6Wy6XG6tcaiXwR/9ZSyHt5wRa7I QeDooku4F3uefrMdRx5QwppqBskp3hn/UaRs7DhbsLz5oJIwAxv1Cr0VSLiq5kfA54f6mq PZHAyV2kzEp3qPxQwBhV6agMNv1j2MA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694729753; 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=VtAwaM2B3p8HaWMuYOi3rMk0MQyqz+yXDmdiAqy5Wgk=; b=FwxgI61OpNqLH1RnevRMqeFzHrhEodcO37VpG1CcURwOR0bm1kR5lK3/SzH6RhLb8SDTJU ssHs3mFgoxPmMgAIua/LyxS6oP/uyewzxIoQsHJ1gtIDenY0vXSTOMidWsoBjrgSp5vKzA TuhGuBIQNMQrPoDrDTcpLQUakJsL4eY= 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-680-F6lxP1o9O8qZVNHtllb3dg-1; Thu, 14 Sep 2023 18:15:49 -0400 X-MC-Unique: F6lxP1o9O8qZVNHtllb3dg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 CDD9D800159; Thu, 14 Sep 2023 22:15:48 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.216]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5DF992156A27; Thu, 14 Sep 2023 22:15:46 +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 6/9] iov_iter: Add copy kunit tests for ITER_UBUF and ITER_IOVEC Date: Thu, 14 Sep 2023 23:15:23 +0100 Message-ID: <20230914221526.3153402-7-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.6 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 8774A180015 X-Stat-Signature: icy54ij6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HDrLHRzbluY8rmxPmCB4J+8wgbPg3SDxzt1+kNklaazKjwUVU5MwP4p/NjWXvLZGFXtaVdt8aXdNmNZznzVgGqn2jdvyL+4Z/W9c/SUSXG1Q3uvIWWMebFMPclQverkg9UDFLFki5ZlpktbNn+J3jjBjJozcd2V7dHX8kfzkh48f+x3OdpK9P2hMG/qqg/F8UDm33kWZRj6iBofZVQbbGAb2TUovJVlE1NGD7Lxe2dBtL9fck5lyZ07CUnfPrhA1Qm1EWLu9QWoRbfZHgevkS5aHX2OF2yz3fOlpaeT48K8eMW6u0soEF4zlptMbu1LIgeKu3XmGDu9DpSq1ZwF/TrtOS4qPyxuq+Ar9tei9++3Rxcib3XO+En+hQdB1n6S3i3Osabjp2zxeGZ6y5bWdiWYNGtTN1CNHobrKWhhD8NyZAc5AVZB87Z3314o/TReK6vQ/YNQiK+SjA9RQTaSvAjY1LUlJjUJynMHx64ns2x4uVNPTqB1xE+dCZuCVOz97KaV4V9Tr66BRr9FSQIA0KLbcDf7jLnzOjFrGkAWYqkbWJgQLe0bIzZ524vJzyWjI3utLwh/B0XZjLt54oMOXVB48w9V+HrBuCs2jQ== 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 copy 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. 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 | 200 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) diff --git a/lib/kunit_iov_iter.c b/lib/kunit_iov_iter.c index 3353bca9c40f..78f566ebd4a6 100644 --- a/lib/kunit_iov_iter.c +++ b/lib/kunit_iov_iter.c @@ -309,6 +309,202 @@ static u8 __user *__init iov_kunit_create_user_buf(struct kunit *test, return buffer; } +/* + * Test copying to an ITER_UBUF-type iterator. + */ +static void __init iov_kunit_copy_to_ubuf(struct kunit *test) +{ + const struct iov_kunit_range *pr; + struct iov_iter iter; + struct page **spages; + u8 __user *buffer; + u8 *scratch; + ssize_t uncleared; + size_t bufsize, npages, size, copied; + int i; + + bufsize = 0x100000; + npages = bufsize / PAGE_SIZE; + + scratch = iov_kunit_create_buffer(test, &spages, npages); + for (i = 0; i < bufsize; i++) + scratch[i] = pattern(i); + + buffer = iov_kunit_create_user_buf(test, npages, NULL); + uncleared = clear_user(buffer, bufsize); + KUNIT_EXPECT_EQ(test, uncleared, 0); + if (uncleared) + return; + + i = 0; + for (pr = kvec_test_ranges; pr->page >= 0; pr++) { + size = pr->to - pr->from; + KUNIT_ASSERT_LE(test, pr->to, bufsize); + + iov_iter_ubuf(&iter, ITER_DEST, buffer + pr->from, size); + copied = copy_to_iter(scratch + i, size, &iter); + + KUNIT_EXPECT_EQ(test, copied, size); + KUNIT_EXPECT_EQ(test, iter.count, 0); + KUNIT_EXPECT_EQ(test, iter.iov_offset, size); + if (test->status == KUNIT_FAILURE) + break; + i += size; + } + + iov_kunit_build_to_reference_pattern(test, scratch, bufsize, kvec_test_ranges); + iov_kunit_check_user_pattern(test, buffer, scratch, bufsize); + KUNIT_SUCCEED(); +} + +/* + * Test copying from an ITER_UBUF-type iterator. + */ +static void __init iov_kunit_copy_from_ubuf(struct kunit *test) +{ + const struct iov_kunit_range *pr; + struct iov_iter iter; + struct page **spages; + u8 __user *buffer; + u8 *scratch, *reference; + size_t bufsize, npages, size, copied; + int i; + + bufsize = 0x100000; + npages = bufsize / PAGE_SIZE; + + buffer = iov_kunit_create_user_buf(test, npages, NULL); + iov_kunit_fill_user_buf(test, buffer, bufsize); + + scratch = iov_kunit_create_buffer(test, &spages, npages); + memset(scratch, 0, bufsize); + + reference = iov_kunit_create_buffer(test, &spages, npages); + + i = 0; + for (pr = kvec_test_ranges; pr->page >= 0; pr++) { + size = pr->to - pr->from; + KUNIT_ASSERT_LE(test, pr->to, bufsize); + + iov_iter_ubuf(&iter, ITER_SOURCE, buffer + pr->from, size); + copied = copy_from_iter(scratch + i, size, &iter); + + KUNIT_EXPECT_EQ(test, copied, size); + KUNIT_EXPECT_EQ(test, iter.count, 0); + KUNIT_EXPECT_EQ(test, iter.iov_offset, size); + if (test->status == KUNIT_FAILURE) + break; + i += size; + } + + iov_kunit_build_from_reference_pattern(test, reference, bufsize, kvec_test_ranges); + iov_kunit_check_pattern(test, scratch, reference, bufsize); + KUNIT_SUCCEED(); +} + +static void __init iov_kunit_load_iovec(struct kunit *test, + struct iov_iter *iter, int dir, + struct iovec *iov, unsigned int iovmax, + u8 __user *buffer, size_t bufsize, + const struct iov_kunit_range *pr) +{ + size_t size = 0; + int i; + + for (i = 0; i < iovmax; i++, pr++) { + if (pr->page < 0) + break; + KUNIT_ASSERT_GE(test, pr->to, pr->from); + KUNIT_ASSERT_LE(test, pr->to, bufsize); + iov[i].iov_base = buffer + pr->from; + iov[i].iov_len = pr->to - pr->from; + size += pr->to - pr->from; + } + KUNIT_ASSERT_LE(test, size, bufsize); + + iov_iter_init(iter, dir, iov, i, size); +} + +/* + * Test copying to an ITER_IOVEC-type iterator. + */ +static void __init iov_kunit_copy_to_iovec(struct kunit *test) +{ + struct iov_iter iter; + struct page **spages; + struct iovec iov[8]; + u8 __user *buffer; + u8 *scratch; + ssize_t uncleared; + size_t bufsize, npages, size, copied; + int i; + + bufsize = 0x100000; + npages = bufsize / PAGE_SIZE; + + scratch = iov_kunit_create_buffer(test, &spages, npages); + for (i = 0; i < bufsize; i++) + scratch[i] = pattern(i); + + buffer = iov_kunit_create_user_buf(test, npages, NULL); + uncleared = clear_user(buffer, bufsize); + KUNIT_EXPECT_EQ(test, uncleared, 0); + if (uncleared) + return; + + iov_kunit_load_iovec(test, &iter, ITER_DEST, iov, ARRAY_SIZE(iov), + buffer, bufsize, kvec_test_ranges); + size = iter.count; + + copied = copy_to_iter(scratch, size, &iter); + + KUNIT_EXPECT_EQ(test, copied, size); + KUNIT_EXPECT_EQ(test, iter.count, 0); + KUNIT_EXPECT_EQ(test, iter.nr_segs, 0); + + iov_kunit_build_to_reference_pattern(test, scratch, bufsize, kvec_test_ranges); + iov_kunit_check_user_pattern(test, buffer, scratch, bufsize); + KUNIT_SUCCEED(); +} + +/* + * Test copying from an ITER_IOVEC-type iterator. + */ +static void __init iov_kunit_copy_from_iovec(struct kunit *test) +{ + struct iov_iter iter; + struct page **spages; + struct iovec iov[8]; + u8 __user *buffer; + u8 *scratch, *reference; + size_t bufsize, npages, size, copied; + + bufsize = 0x100000; + npages = bufsize / PAGE_SIZE; + + buffer = iov_kunit_create_user_buf(test, npages, NULL); + iov_kunit_fill_user_buf(test, buffer, bufsize); + + scratch = iov_kunit_create_buffer(test, &spages, npages); + memset(scratch, 0, bufsize); + + reference = iov_kunit_create_buffer(test, &spages, npages); + + iov_kunit_load_iovec(test, &iter, ITER_SOURCE, iov, ARRAY_SIZE(iov), + buffer, bufsize, kvec_test_ranges); + size = iter.count; + + copied = copy_from_iter(scratch, size, &iter); + + KUNIT_EXPECT_EQ(test, copied, size); + KUNIT_EXPECT_EQ(test, iter.count, 0); + KUNIT_EXPECT_EQ(test, iter.nr_segs, 0); + + iov_kunit_build_from_reference_pattern(test, reference, bufsize, kvec_test_ranges); + iov_kunit_check_pattern(test, reference, scratch, bufsize); + KUNIT_SUCCEED(); +} + static void __init iov_kunit_load_kvec(struct kunit *test, struct iov_iter *iter, int dir, struct kvec *kvec, unsigned int kvmax, @@ -884,6 +1080,10 @@ static void __init iov_kunit_extract_pages_xarray(struct kunit *test) } static struct kunit_case __refdata iov_kunit_cases[] = { + KUNIT_CASE(iov_kunit_copy_to_ubuf), + KUNIT_CASE(iov_kunit_copy_from_ubuf), + KUNIT_CASE(iov_kunit_copy_to_iovec), + KUNIT_CASE(iov_kunit_copy_from_iovec), KUNIT_CASE(iov_kunit_copy_to_kvec), KUNIT_CASE(iov_kunit_copy_from_kvec), KUNIT_CASE(iov_kunit_copy_to_bvec),