From patchwork Mon Jun 3 00:33:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 13683158 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 6835DC25B74 for ; Mon, 3 Jun 2024 00:33:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BA15F6B00D4; Sun, 2 Jun 2024 20:33:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B4EF96B00D5; Sun, 2 Jun 2024 20:33:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9EFA76B00D7; Sun, 2 Jun 2024 20:33:26 -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 7ADC66B00D4 for ; Sun, 2 Jun 2024 20:33:26 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 24B6340555 for ; Mon, 3 Jun 2024 00:33:26 +0000 (UTC) X-FDA: 82187703612.15.CA3FEAD Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) by imf09.hostedemail.com (Postfix) with ESMTP id 0B0B8140015 for ; Mon, 3 Jun 2024 00:33:23 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="h9/irGBP"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf09.hostedemail.com: domain of kent.overstreet@linux.dev designates 91.218.175.178 as permitted sender) smtp.mailfrom=kent.overstreet@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717374804; 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=qtDVS9A12/FhM6gLI1GwHXngLSArqAMBUGSjtT60l7Y=; b=iDXQP2axbKCoLgY9S1MrBX+kx0KTxHaMowT3zN2kzvBtGJAMRCI5JpUKE4TO+3HaBKMEiw GOMPYdsS8ydUh5hpFz26ZsoiCze4Rbl9appPVps5V9BLDWNOWF7bof0QbsbPSp6hl0ElGw 1FaYW6czMOl6wGwffBFDfrCKuCrfk6k= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="h9/irGBP"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf09.hostedemail.com: domain of kent.overstreet@linux.dev designates 91.218.175.178 as permitted sender) smtp.mailfrom=kent.overstreet@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717374804; a=rsa-sha256; cv=none; b=IgUMpHj+Yy8FdwXgHEHYpBTmn3u/bFPdSTZIWZGdQvucLVT9GlqmoSUkQZRacYfStYQv4s tJJFRA6bt2j4Re1tM+E4IWgRUa3/NRJToQ5EQxd6cMHy8VaA7sh6T+J+Ssej+V2gDU/7rK QbaBTGlUr/ZLlacZmKJsw0WVhtnjlbY= X-Envelope-To: linux-fsdevel@vger.kernel.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1717374801; 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=qtDVS9A12/FhM6gLI1GwHXngLSArqAMBUGSjtT60l7Y=; b=h9/irGBPZpS9RHEtUgiTJNJaSvRPgN6nJTrpu0j6bdb35L0d1exKDknSJ8k4wJbNc2SO/S d4jzM7YVLiQa1bS20ok3j4tjM0kEsbo2oWdezuBzLqXltHouvdgqN82Pjvqo6orf1MOT+1 37IexeJNG/zfZ4kQykqf74E8mRdY+bw= X-Envelope-To: linux-kernel@vger.kernel.org X-Envelope-To: axboe@kernel.dk X-Envelope-To: kent.overstreet@linux.dev X-Envelope-To: brauner@kernel.org X-Envelope-To: viro@zeniv.linux.org.uk X-Envelope-To: bernd.schubert@fastmail.fm X-Envelope-To: linux-mm@kvack.org X-Envelope-To: josef@toxicpanda.com X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Kent Overstreet To: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Jens Axboe , Kent Overstreet , brauner@kernel.org, viro@zeniv.linux.org.uk, Bernd Schubert , linux-mm@kvack.org, Josef Bacik Subject: [PATCH 4/5] ringbuffer: Test device Date: Sun, 2 Jun 2024 20:33:01 -0400 Message-ID: <20240603003306.2030491-5-kent.overstreet@linux.dev> In-Reply-To: <20240603003306.2030491-1-kent.overstreet@linux.dev> References: <20240603003306.2030491-1-kent.overstreet@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 0B0B8140015 X-Stat-Signature: rrd1cnhd5q83i9ejaajapw4pbs6txqi7 X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1717374803-745844 X-HE-Meta: U2FsdGVkX1+OMxCfmOV61AgouhvyVUgl8puklI00vjyhC1VyFmgZ07p8hJO8EJljs1MnzerpggwwiW9vBGfwHz9KiVTZPQDApwI8WDc1KS+jfOxpuaznffJl6SZnjBfWLxr+M2SEh3gCBHEOLgsl3Kg1Jxk0PFK+AZzt6xhvDo3sa4C4Ka17Y3qYUOmXYcKx8VEgorlgj0UL0GLE83ffSMDo/9y5kGbw1d+DLN86SLP4nTOjhwafOGc6fTkk24W2Hxou5QL9CoBe8lCuVOby/LUpRR0NkOHT9JvvAK2FDmvF48v599MeLSySP2wUxCM66xwwwtbZ56obR3bLKGPyE6BCIg8Jx392KH95FUH+R2KS6xDzuso99jrxrPL+UzJaoWPNRtha3TCN/Joqo0x2SaR2e9GnAVxh5vHcG94wKlb5ni6DwX7clzWHlmc0RVQyYLCSertA/8yhPSVhnEzsv4e/lZhgC6aMA3Hll+Ja6T4brg627Qb9QKgdAGQCFwMyHq7KmosxFnmo/u60hl8bNI4cyBz8aGyKcQr7XDXJJiFYnxyRvXrCTnfxzw1a4UcPsJDhry0YCPiZ8OYgUqG98G+xib4ZT6UVbAYSyuc32IgacJviKlY7Ea9Uf0eIqohQKhtth4z3EO3Gey53W5+sCt3BH9FA/1DrhmM1vZXwET7ZUbby1CSfZfUNg2j05B2Wg4rs4v8c/hCptinK38IB77U4aeK7YDX+4s5ptlmbxwnZZXoDlsMd+cC7o7KOqy2F2jqfmet65C4BNyJWBAHtQuvIpPyQqbkK5k2vRJHlI80QZFE1+WXY8hG+hZoa6lYeEbez0DEsiqJ2lLWbKULSV4EqT8iA2K0ToTXUsfij26VfE6EcMilwfq1actQ1J7gyC+EbHrKAHYvVisJVad/FOWz9Or7b48/ilxQ+wWh1O+aQMxrMiQPKfK607f6SuTSXJ5jQUUlO6P1IYTi+Wp0 nPrhoPkG v2btBj/sen6H7f495epjqNr1WeveeJKHtGc3S62chPJ7VVmQ49FdR5W8ci9z8RhCUaMdV4wwnOJOPJOX/gPvVN/gGog== 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: This adds /dev/ringbuffer-test, which supports reading and writing a sequence of integers, to test performance and correctness. Signed-off-by: Kent Overstreet --- fs/Makefile | 1 + fs/ringbuffer_test.c | 209 +++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 5 ++ 3 files changed, 215 insertions(+) create mode 100644 fs/ringbuffer_test.c diff --git a/fs/Makefile b/fs/Makefile index 48e54ac01fb1..91061f281f0a 100644 --- a/fs/Makefile +++ b/fs/Makefile @@ -29,6 +29,7 @@ obj-$(CONFIG_EVENTFD) += eventfd.o obj-$(CONFIG_USERFAULTFD) += userfaultfd.o obj-$(CONFIG_AIO) += aio.o obj-$(CONFIG_RINGBUFFER) += ringbuffer.o +obj-$(CONFIG_RINGBUFFER_TEST) += ringbuffer_test.o obj-$(CONFIG_FS_DAX) += dax.o obj-$(CONFIG_FS_ENCRYPTION) += crypto/ obj-$(CONFIG_FS_VERITY) += verity/ diff --git a/fs/ringbuffer_test.c b/fs/ringbuffer_test.c new file mode 100644 index 000000000000..01aa9c55120d --- /dev/null +++ b/fs/ringbuffer_test.c @@ -0,0 +1,209 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define pr_fmt(fmt) "%s() " fmt "\n", __func__ + +#include +#include +#include +#include +#include +#include + +struct ringbuffer_test_file { + struct ringbuffer_test_rw { + struct mutex lock; + struct ringbuffer *rb; + struct task_struct *thr; + } rw[2]; +}; + +#define BUF_NR 4 + +static int ringbuffer_test_writer(void *p) +{ + struct file *file = p; + struct ringbuffer_test_file *f = file->private_data; + struct ringbuffer *rb = f->rw[READ].rb; + u32 idx = 0; + u32 buf[BUF_NR]; + + while (!kthread_should_stop()) { + cond_resched(); + + struct kvec vec = { buf, sizeof(buf) }; + struct iov_iter iter; + iov_iter_kvec(&iter, ITER_SOURCE, &vec, 1, sizeof(buf)); + + for (unsigned i = 0; i < ARRAY_SIZE(buf); i++) + buf[i] = idx + i; + + ssize_t ret = ringbuffer_write_iter(rb, &iter, false); + if (ret < 0) + continue; + idx += ret / sizeof(buf[0]); + } + + return 0; +} + +static int ringbuffer_test_reader(void *p) +{ + struct file *file = p; + struct ringbuffer_test_file *f = file->private_data; + struct ringbuffer *rb = f->rw[WRITE].rb; + u32 idx = 0; + u32 buf[BUF_NR]; + + while (!kthread_should_stop()) { + cond_resched(); + + struct kvec vec = { buf, sizeof(buf) }; + struct iov_iter iter; + iov_iter_kvec(&iter, ITER_DEST, &vec, 1, sizeof(buf)); + + ssize_t ret = ringbuffer_read_iter(rb, &iter, false); + if (ret < 0) + continue; + + unsigned nr = ret / sizeof(buf[0]); + for (unsigned i = 0; i < nr; i++) + if (buf[i] != idx + i) + pr_err("read wrong data"); + idx += ret / sizeof(buf[0]); + } + + return 0; +} + +static void ringbuffer_test_free(struct ringbuffer_test_file *f) +{ + for (unsigned i = 0; i < ARRAY_SIZE(f->rw); i++) + if (!IS_ERR_OR_NULL(f->rw[i].thr)) + kthread_stop_put(f->rw[i].thr); + for (unsigned i = 0; i < ARRAY_SIZE(f->rw); i++) + if (!IS_ERR_OR_NULL(f->rw[i].rb)) + ringbuffer_free(f->rw[i].rb); + kfree(f); +} + +static int ringbuffer_test_open(struct inode *inode, struct file *file) +{ + static const char * const rw_str[] = { "reader", "writer" }; + int ret = 0; + + struct ringbuffer_test_file *f = kzalloc(sizeof(*f), GFP_KERNEL); + if (!f) + return -ENOMEM; + + for (struct ringbuffer_test_rw *i = f->rw; + i < f->rw + ARRAY_SIZE(f->rw); + i++) { + unsigned idx = i - f->rw; + + mutex_init(&i->lock); + + i->rb = ringbuffer_alloc(PAGE_SIZE * 4); + ret = PTR_ERR_OR_ZERO(i->rb); + if (ret) + goto err; + + i->thr = kthread_create(idx == READ + ? ringbuffer_test_reader + : ringbuffer_test_writer, + file, "ringbuffer_%s", rw_str[idx]); + ret = PTR_ERR_OR_ZERO(i->thr); + if (ret) + goto err; + get_task_struct(i->thr); + } + + file->private_data = f; + wake_up_process(f->rw[0].thr); + wake_up_process(f->rw[1].thr); + return 0; +err: + ringbuffer_test_free(f); + return ret; +} + +static int ringbuffer_test_release(struct inode *inode, struct file *file) +{ + ringbuffer_test_free(file->private_data); + return 0; +} + +static ssize_t ringbuffer_test_read_iter(struct kiocb *iocb, struct iov_iter *iter) +{ + struct file *file = iocb->ki_filp; + struct ringbuffer_test_file *f = file->private_data; + struct ringbuffer_test_rw *i = &f->rw[READ]; + + ssize_t ret = mutex_lock_interruptible(&i->lock); + if (ret) + return ret; + + ret = ringbuffer_read_iter(i->rb, iter, file->f_flags & O_NONBLOCK); + mutex_unlock(&i->lock); + return ret; +} + +static ssize_t ringbuffer_test_write_iter(struct kiocb *iocb, struct iov_iter *iter) +{ + struct file *file = iocb->ki_filp; + struct ringbuffer_test_file *f = file->private_data; + struct ringbuffer_test_rw *i = &f->rw[WRITE]; + + ssize_t ret = mutex_lock_interruptible(&i->lock); + if (ret) + return ret; + + ret = ringbuffer_write_iter(i->rb, iter, file->f_flags & O_NONBLOCK); + mutex_unlock(&i->lock); + return ret; +} + +static struct ringbuffer *ringbuffer_test_ringbuffer(struct file *file, int rw) +{ + struct ringbuffer_test_file *i = file->private_data; + + BUG_ON(rw > WRITE); + + return i->rw[rw].rb; +} + +static const struct file_operations ringbuffer_fops = { + .owner = THIS_MODULE, + .read_iter = ringbuffer_test_read_iter, + .write_iter = ringbuffer_test_write_iter, + .ringbuffer = ringbuffer_test_ringbuffer, + .open = ringbuffer_test_open, + .release = ringbuffer_test_release, +}; + +static int __init ringbuffer_test_init(void) +{ + int ringbuffer_major = register_chrdev(0, "ringbuffer-test", &ringbuffer_fops); + if (ringbuffer_major < 0) + return ringbuffer_major; + + static const struct class ringbuffer_class = { .name = "ringbuffer_test" }; + int ret = class_register(&ringbuffer_class); + if (ret) + goto major_out; + + struct device *ringbuffer_device = device_create(&ringbuffer_class, NULL, + MKDEV(ringbuffer_major, 0), + NULL, "ringbuffer-test"); + ret = PTR_ERR_OR_ZERO(ringbuffer_device); + if (ret) + goto class_out; + + return 0; + +class_out: + class_unregister(&ringbuffer_class); +major_out: + unregister_chrdev(ringbuffer_major, "ringbuffer-test"); + return ret; +} +__initcall(ringbuffer_test_init); diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 59b6765d86b8..bb16762af575 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2957,6 +2957,11 @@ config TEST_OBJPOOL If unsure, say N. +config RINGBUFFER_TEST + bool "Test driver for sys_ringbuffer" + default n + depends on RINGBUFFER + endif # RUNTIME_TESTING_MENU config ARCH_USE_MEMTEST