Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4898337pxu; Thu, 10 Dec 2020 08:03:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJxmac3cLciNdmyfB629pOzz3Oif8DVGmspZYQhTzUsm25idrYfmdwKhepKIb8HGfsdei83x X-Received: by 2002:a17:906:378f:: with SMTP id n15mr6861501ejc.263.1607616210409; Thu, 10 Dec 2020 08:03:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607616210; cv=none; d=google.com; s=arc-20160816; b=lLLDR3Wt5qxQjlXcUT67RZqf9miLwQkCgKu09uwIp7alpJ4MLH1Ik8/4vwWh0hSw6O TJ2RI8xmvTRrzlGt/3yXj6suScK/+BUprbGNl+n05QNs5WQ4n8HzkYUZ+0729DFPWKAP 9KSkY2weydR92h9eoHCWBQ2iYtctOn4c7f14GOpECo7DYrOivIaZuU2vON0ycHmx89K1 HsP28/y+vWZ72NACIygBWrTyNBWzLz0vxo8djv6u42HRM24/MITCFgbWyjMpw3P4tDdU 2+6wrA+MEalR0eQZq0taSs6IEQ5X87gkzAdgcF6EarMuD1yWAK2emE/n76YOcFcdHSit HczA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=xKjSMYSu+Z18egYl0Qu1TrUiaOZoWdBxJJgAeszoW0s=; b=M/3rsuH4A85vDJ7SPpYmvhpuQdqrnhfROEcM26zg+s6FjGWSAGCxTy0ClrfSj21lXT KPCMhX6NKampIgwFtjcfH+kbX5dI8lXGZWxI8Gaez5D3uTc9Z7V00GaDLBo64rvrsmlU jquhS4pY4NGLazx3Odmt3e2luwJ8HgI1+qVyqANJ/zp3347McCDj4a60IeRL82/65YwD OkPcgLFGCE9qO9pYkBLlZLVV7R/zlKUbcOBdK3Y/3W+IWK9zZ04S3IJbPSPrcXUSUvyd wM/HBvGjZ3goQga5WZDYeq14+aX5W80GFZFvNNqfX6dHQfnUUw4nIyMlW3sWzPO+M/Bx p8MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Q4/KIDHt"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o23si2932632edt.543.2020.12.10.08.03.02; Thu, 10 Dec 2020 08:03:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Q4/KIDHt"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392114AbgLJQA0 (ORCPT + 99 others); Thu, 10 Dec 2020 11:00:26 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:58492 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403854AbgLJQAE (ORCPT ); Thu, 10 Dec 2020 11:00:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1607615917; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=xKjSMYSu+Z18egYl0Qu1TrUiaOZoWdBxJJgAeszoW0s=; b=Q4/KIDHt4HEZsEV6aPQX1FZdUgcig8GLdSBuhhyN1FS07+ZVZKZgtq4ynBMqZVXFqS0mNU 68xZGINwlBknKpr+vJXHS2mS9JEPqv4H4hbH4NmipN30fieRxQ7MHTx0pVARDko6N5gN0o IcU6aYKyHR228eMgU8gTtRYhJL7MYWg= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-436-XJ-9TNLrOFOCL9NKwKRRTA-1; Thu, 10 Dec 2020 10:58:32 -0500 X-MC-Unique: XJ-9TNLrOFOCL9NKwKRRTA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 083758049C9; Thu, 10 Dec 2020 15:58:31 +0000 (UTC) Received: from localhost (unknown [10.18.25.174]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2D19419C78; Thu, 10 Dec 2020 15:58:27 +0000 (UTC) Date: Thu, 10 Dec 2020 10:58:26 -0500 From: Mike Snitzer To: Sergei Shtepa Cc: "axboe@kernel.dk" , "johannes.thumshirn@wdc.com" , "koct9i@gmail.com" , "ming.lei@redhat.com" , "hare@suse.de" , "josef@toxicpanda.com" , "steve@sk2.org" , "linux-block@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Pavel Tide Subject: Re: [PATCH 2/3] block: blk_interposer - sample Message-ID: <20201210155826.GA10154@redhat.com> References: <1607518911-30692-1-git-send-email-sergei.shtepa@veeam.com> <1607518911-30692-3-git-send-email-sergei.shtepa@veeam.com> <20201209143606.GA494@redhat.com> <20201210155405.GB31521@veeam.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201210155405.GB31521@veeam.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Dec 10 2020 at 10:54am -0500, Sergei Shtepa wrote: > The 12/09/2020 17:36, Mike Snitzer wrote: > > On Wed, Dec 09 2020 at 8:01am -0500, > > Sergei Shtepa wrote: > > > > > This sample demonstrates how to use blk_interposer. > > > It show how to properly connect the interposer module to kernel, > > > and perform the simplest monitoring of the number of bio. > > > > > > Signed-off-by: Sergei Shtepa > > > --- > > > samples/blk_interposer/Makefile | 2 + > > > samples/blk_interposer/blk-interposer.c | 276 ++++++++++++++++++++++++ > > > 2 files changed, 278 insertions(+) > > > create mode 100644 samples/blk_interposer/Makefile > > > create mode 100644 samples/blk_interposer/blk-interposer.c > > > > > > diff --git a/samples/blk_interposer/Makefile b/samples/blk_interposer/Makefile > > > new file mode 100644 > > > index 000000000000..b11aefde2b1c > > > --- /dev/null > > > +++ b/samples/blk_interposer/Makefile > > > @@ -0,0 +1,2 @@ > > > +# SPDX-License-Identifier: GPL-2.0 > > > +obj-$(CONFIG_SAMPLE_BLK_INTERPOSER) += blk-interposer.o > > > diff --git a/samples/blk_interposer/blk-interposer.c b/samples/blk_interposer/blk-interposer.c > > > new file mode 100644 > > > index 000000000000..92b4c1fcf8f7 > > > --- /dev/null > > > +++ b/samples/blk_interposer/blk-interposer.c > > > @@ -0,0 +1,276 @@ > > > +// SPDX-License-Identifier: GPL-2.0 > > > + > > > +/* > > > + * Block layer interposer allow to interpose bio requests from kernel module. > > > + * This allows you to monitor requests, modify requests, add new request, > > > + * or even redirect requests to another devices. > > > + * > > > + * This sample demonstrates how to use blk_interposer. > > > + * It show how to properly connect the interposer module to kernel, > > > + * and perform the simplest monitoring of the number of bio. > > > + */ > > > + > > > +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt > > > + > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > + > > > +int device_major = 8; > > > +int device_minor; > > > +int fmode = FMODE_READ | FMODE_WRITE; > > > + > > > +/* > > > + * Each interposer must have a common part in the form of the blk_interposer structure, > > > + * as well as its own unique data. > > > + */ > > > +struct my_interposer { > > > + /* > > > + * Common part of block device interposer. > > > + */ > > > + struct blk_interposer interposer; > > > + /* > > > + * Specific part for our interposer data. > > > + */ > > > + atomic_t counter; > > > +}; > > > + > > > +struct my_interposer my_ip; > > > + > > > +/** > > > + * blk_interposer_attach - Attach interposer to disk > > > + * @disk: target disk > > > + * @interposer: block device interposer > > > + */ > > > +static int blk_interposer_attach(struct gendisk *disk, struct blk_interposer *interposer) > > > +{ > > > + int ret = 0; > > > + > > > + /* > > > + * Stop disks queue processing. > > > + */ > > > + blk_mq_freeze_queue(disk->queue); > > > + blk_mq_quiesce_queue(disk->queue); > > > + > > > + /* > > > + * Check if the interposer is already busy. > > > + * The interposer will only connect if it is not busy. > > > + */ > > > + if (blk_has_interposer(disk)) { > > > + pr_info("The interposer is already busy.\n"); > > > + ret = -EBUSY; > > > + goto out; > > > + } > > > + > > > + /* > > > + * Attach the interposer. > > > + */ > > > + disk->interposer = interposer; > > > + /* > > > + * And while the queue is stopped, we can do something specific for our module. > > > + */ > > > + pr_info("Block device interposer attached successfully.\n"); > > > + > > > +out: > > > + /* > > > + * Resume disks queue processing > > > + */ > > > + blk_mq_unquiesce_queue(disk->queue); > > > + blk_mq_unfreeze_queue(disk->queue); > > > + > > > + return ret; > > > +} > > > + > > > +/** > > > + * blk_interposer_detach - Detach interposer from disk > > > + * @disk: target disk > > > + * @interposer: block device interposer > > > + */ > > > +static int blk_interposer_detach(struct gendisk *disk, struct blk_interposer *interposer) > > > +{ > > > + int ret = 0; > > > + > > > + if (WARN_ON(!disk)) > > > + return -EINVAL; > > > + > > > + /* > > > + * Stop disks queue processing. > > > + */ > > > + blk_mq_freeze_queue(disk->queue); > > > + blk_mq_quiesce_queue(disk->queue); > > > + > > > + /* > > > + * Check if the interposer is still available. > > > + */ > > > + if (!disk->interposer) { > > > + pr_info("The interposer is not available.\n"); > > > + return -ENOENT; > > > + goto out; > > > + } > > > + /* > > > + * Check if it is really our interposer. > > > + */ > > > + if (disk->interposer->ip_submit_bio != interposer->ip_submit_bio) { > > > + pr_info("The interposer found is not ours.\n"); > > > + return -EPERM; > > > + goto out; > > > + } > > > + > > > + /* > > > + * Detach interposer. > > > + */ > > > + disk->interposer = NULL; > > > + /* > > > + * And while the queue is stopped, we can do something specific for our module. > > > + */ > > > + pr_info("Block device interposer detached successfully.\n"); > > > + > > > +out: > > > + /* > > > + * Resume disks queue processing. > > > + */ > > > + blk_mq_unquiesce_queue(disk->queue); > > > + blk_mq_unfreeze_queue(disk->queue); > > > + > > > + return ret; > > > +} > > > > This attach and detach code needs to be elevated out of samples so that > > any future consumer of blk_interposer doesn't reinvent it. It is far > > too fundamental. > > > > The way you've proposed this be merged is very much unacceptable. > > > > Nacked-by: Mike Snitzer > > > Yes, but on the other hand, while the queue is suspended, the module can perform > some other actions specific to it. > > And since the functions blk_mq_freeze_queue(), blk_mq_quiesce_queue(), > blk_mq_unquiesce_queue() and blk_mq_unfreeze_queue() are public, > the module creator can implement its module connection functionality regardless of > whether we make the functions blk_interposer_attach() and blk_interposer_detach() > in the kernel or not. > > I'll think about it and try to come up with a better solution. This practice of sprinkling code in samples/ subdir and getting a minimalist hook into the kernel (with no actual in-kernel consumer) is _not_ how we do things. I'm not well-versed on the samples/ subdir nor when it came into existence; but it should die in a fire! Mike