Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp676021lqj; Sun, 2 Jun 2024 17:34:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXQ3jJhNueR7VFXxU/bxK+y3juUaQvlrqVmkpndUB46SXf/X1lNh8g5+fK9x4Nni6MvCtGanXsmVNDChs4RC2/t7sO9eH/jlhroDrr7ow== X-Google-Smtp-Source: AGHT+IF49PfpLG+C0yyM7P/DdtvP5kTLEEAQFi/SshQWKsGV34uO4B86hQ77qbRi6ykNCPvfybxV X-Received: by 2002:a25:6fc5:0:b0:de6:556:337b with SMTP id 3f1490d57ef6-dfaa62bdb2fmr1192823276.7.1717374883766; Sun, 02 Jun 2024 17:34:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717374883; cv=pass; d=google.com; s=arc-20160816; b=gVFRBrcU5wGppsBJVd8aL3W0JNs1K/G3MrTlkKxFOCZT+YddIYk4i1Tr3rQ6Z7d0cW kHbga63qUcIALwxKqKpbrRAO6nLNAS55fTR+CRaJ664V6IrfelF0L4g337Q8yS2b9I4q oQX7QNflk42hdoaB3GdF/p93pmJ/jGwl4ZGoetKqaWsymUTt1SMyDnAYwQb9dhjXLjWq pFKK7ggwZ7fmpbGJfOx0e4O5zT8m5RFGXr3dZAFJ3c9QSgKczPvuGgquucblWwY4yxjp KaIf01UdxQdfNSuLTgkSgTy6H9J0rsqoKYa78j92NCNzaXUJ+KSgwWHBjLpmISr+HTuZ vxIw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=qtDVS9A12/FhM6gLI1GwHXngLSArqAMBUGSjtT60l7Y=; fh=zHmvfkjdNfBuJ8M3ivq9Vd8L3gqi7cNjIbsvZkBSDEk=; b=iIv2t1LYs/oJNrEPgffkKvMaacxPPz4f6w6mOGrujiuKhMl6lgitv3Qcmc1+3FmmFR Ro7Tn9ErGwiAWvVsOOuMX49UBfYgD32eiK/Dt94qDMYS5f8u4imOB1/ly5iubGtdhJ2t PX4uVkdaJyjhStAYKt1BHbOz3GO0/MWp6F3WnIiFD4WKtp/Cd2an7XVaBqrF29vn1TdP zC5MV+lRl46wMGJEso/Py90wqm7tBenoWKPB1ez4sj4rHzLA94jf9k+4ttA2MfiucpjO H3XQBH7pCGTukeWy5B9ER1e8V1LvvFG8pnnBK7N4QpICNYxuz1kK4Dkz57yE7wSHf+pe QUag==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="h9/irGBP"; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-198504-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-198504-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 6a1803df08f44-6ae4a733c43si57294406d6.82.2024.06.02.17.34.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Jun 2024 17:34:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-198504-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="h9/irGBP"; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-198504-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-198504-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 6F2241C20A62 for ; Mon, 3 Jun 2024 00:34:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7525B125D6; Mon, 3 Jun 2024 00:33:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="h9/irGBP" Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 078DB63D5 for ; Mon, 3 Jun 2024 00:33:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717374806; cv=none; b=lQwg6vcUwnyKDWWt8+0hFfUH49VREK13JYb4FhWNiXGURXg8bB9WgzVtbpU6uL58qrXsvfYC06OQnijWVK5XqPMInD/l//P6BBoiKCvWihP6gBpqHHKhhEpWFDHpyJrWNqTVWlrNJJp/4pNSRN4rCHFca525zlujmB+hjm8j7ds= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717374806; c=relaxed/simple; bh=MbreANTvOFsf+HoMZMv881QbCHhV7LHQF9AJXemvdRM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aFwrGJArAt2W0n/FRwpysutzLDvb+aIFDZWtqoE3YAUJzb6gW180dysBxoAiDD7QAhiMcDGWmmHgZzDd8+GThwuI819jJ/CbVS+WKdZieo+uKrFF3ClC3nWZxCnKiMecdc4u6wOT1zA0oYDFr3IEu9JMJoqvRYM4ZLbuECpVPVs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=h9/irGBP; arc=none smtp.client-ip=91.218.175.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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 -- 2.45.1