From patchwork Wed Nov 15 15:49:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13457015 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 1FA93C072A2 for ; Wed, 15 Nov 2023 15:50:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A73278001D; Wed, 15 Nov 2023 10:50:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9F9CC80018; Wed, 15 Nov 2023 10:50:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 874678001D; Wed, 15 Nov 2023 10:50:21 -0500 (EST) 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 71AE080018 for ; Wed, 15 Nov 2023 10:50:21 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 41C991A01C9 for ; Wed, 15 Nov 2023 15:50:21 +0000 (UTC) X-FDA: 81460625442.29.01F6A65 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf07.hostedemail.com (Postfix) with ESMTP id 8C46C4000C for ; Wed, 15 Nov 2023 15:50:19 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZmiC++lC; spf=pass (imf07.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=1700063419; a=rsa-sha256; cv=none; b=amfgqBvDqkTODRr6aSxav0LCjDkaydD9Hzl1fpJeeiMYOI5pKTzNV58HrMUMzYJCA9BAhA zXgidH8Tf93XXp0CGUsHZMvYU+Iv+xSyriZaDG5yuoXwcdvtoNe7F9X+t/j8TWMIPbvguC jYkepNyawX0B0Cd3O9VufRah1TvQKNA= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZmiC++lC; spf=pass (imf07.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=1700063419; 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=rwCPpplnarkjsvZJB79Uk8vm91RohI86seZogpCcfg4=; b=TFxve5Dd5IEFJU8CoGvE0XSLvWcZaWptbAgqC/GMxF68Y4jkG767awaK4TcIq29qk1RxWq QQ8WT7SrqQwVslHD9ME9d680AfNSFtENquQhKIa0J3Xchhxo/+HjGBE2/UEjCW1vtgZsaL Fgfs7eQIMeTJCX8/uiWX7CM8kqb+f0M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700063418; 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=rwCPpplnarkjsvZJB79Uk8vm91RohI86seZogpCcfg4=; b=ZmiC++lCaz6CVh/ziBqXNycgJ83WkMfpCmxZOmSedRXWaGvvl1CdkI8LkkmmLXcYoZSoNX frspTvZ7aJTfucYN7d8noWX01iOFnjwtTNoZehBinTg+XiPpK8ea4V1iXEJ0vIhLz4TPu5 +tmTDZTxt5iKTBrYeJMCqaDpimPJjVw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-166-paADNiDcMD-8KlevGlnejA-1; Wed, 15 Nov 2023 10:50:15 -0500 X-MC-Unique: paADNiDcMD-8KlevGlnejA-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 3CC4F811E92; Wed, 15 Nov 2023 15:50:14 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9D07F2166B27; Wed, 15 Nov 2023 15:50:11 +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 06/10] iov_iter: Add copy kunit tests for ITER_UBUF and ITER_IOVEC Date: Wed, 15 Nov 2023 15:49:42 +0000 Message-ID: <20231115154946.3933808-7-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-Server: rspam08 X-Rspamd-Queue-Id: 8C46C4000C X-Stat-Signature: he5opozf7d6zqw59ftcs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tZITpUyG+8ujPGoa7T4Z5/7lgO4sQ88HdIQnzW1hKH8Gi3frc63aQU0fwzNx/CMIxTnep91HnW6w2GyThhuneWSYiYPWtGzL21PCri37eAI4tPDpkb6CqDJ2CeurYqevJsrawRdfvVUL6CE4qy3y83fwfyPCNwr4FWJX21I6+f81N42dahjzsR1yToqHFGgJz/0tAWWpa1LR4GHPa0/ZpZIvzGSscYYLkea1ETWVr04Mr+ZjMjcrvDAEpSejNyOBtWF630eykxXO91dz/sKF7LWtiu204cNdT6JULUJdEUM+0M4gogVnPqljbc7p7PA7zZZLrrbYWC+kGxbgANuAcWNdwv3JGcgXGlsJZX2I1hZyAZNr7jA5Xtphu32MQ8HxU87Nm5q+u28iuMfUgKPC7ybP4/EiDBOzI1hQNoQYPv0MjQeO7vyZUHDtgCH9znoRHjD4TOUwWeCndx/DTuhe+ilNAbTtO2ng2fnwVid3ixs/vIu6F4UldszDKBjC96/XrqGuGXbXRZGDLffFNwRowPMn25Ed2SwHpKYhYwjP2c7Fc8KACkxNmLnuBcqsceRUGSA08idBHF1Vkcq2Fw/VsP+sftP9Cc4JbtMOA== 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 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 | 236 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) diff --git a/lib/kunit_iov_iter.c b/lib/kunit_iov_iter.c index 63e4dd1e7c1b..34f0d82674ee 100644 --- a/lib/kunit_iov_iter.c +++ b/lib/kunit_iov_iter.c @@ -117,6 +117,23 @@ static void *__init iov_kunit_create_buffer(struct kunit *test, return buffer; } +/* + * Fill a user buffer with a recognisable pattern. + */ +static void iov_kunit_fill_user_buf(struct kunit *test, + u8 __user *buffer, size_t bufsize) +{ + size_t i; + int err; + + for (i = 0; i < bufsize; i++) { + err = put_user(pattern(i), &buffer[i]); + KUNIT_EXPECT_EQ(test, err, 0); + if (test->status == KUNIT_FAILURE) + return; + } +} + /* * Build the reference pattern in the scratch buffer that we expect to see in * the iterator buffer (ie. the result of copy *to*). @@ -172,6 +189,25 @@ static void iov_kunit_check_pattern(struct kunit *test, const u8 *buffer, } } +/* + * Compare a user and a scratch buffer to see that they're the same. + */ +static void iov_kunit_check_user_pattern(struct kunit *test, const u8 __user *buffer, + const u8 *scratch, size_t bufsize) +{ + size_t i; + int err; + u8 c; + + for (i = 0; i < bufsize; i++) { + err = get_user(c, &buffer[i]); + KUNIT_EXPECT_EQ(test, err, 0); + KUNIT_EXPECT_EQ_MSG(test, c, scratch[i], "at i=%x", i); + if (c != scratch[i]) + return; + } +} + static const struct file_operations iov_kunit_user_file_fops = { .mmap = generic_file_mmap, }; @@ -294,6 +330,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, @@ -869,6 +1101,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),