Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp3146432lqo; Tue, 21 May 2024 08:07:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVJJ6BD3MEws8KyMl1uDmbTqLyGcBIb3MV7FiHVzMsraEoUPHtwB2Y1aqZQOxej5TfPRL+yObaIqkIpL0zy+GDaS48xw6M6236Y5coWoQ== X-Google-Smtp-Source: AGHT+IFm3ipSEq0c2eaM4ExuTLx3MMaNAr9k6JMjQcnzkQIUykFPox4jyoZ52TPJPu/F/JrSlfaH X-Received: by 2002:a05:6830:1399:b0:6f1:2256:5d37 with SMTP id 46e09a7af769-6f122565f28mr26494565a34.14.1716304066708; Tue, 21 May 2024 08:07:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716304066; cv=pass; d=google.com; s=arc-20160816; b=ju/upQQ+CQ5ri+lp3mji8mmLCJ7J0zjyQkiGzcTyuTbMoF2ZVW3cEmZ9a9r6MGSSj9 54dC+U0ojCxBZXcnc5wqzsDCSULeoCYf68nVRMltRbeJOLAxPYfKrtUB9DAMcwb6+m4/ b0cVxHoz81p9erWu8BhEwg2EEKE8OuW7srpkPFDqmjqmB1OpwPAUJ5aB0+3Br1/RiBiZ V8EkhcWnIDR9pAm4QXTifVVuc8SVN6NgbBoGLLCz+5jEn0r5VOW2a90Bn7f03gCoWb6c ZvQj5kgxXjiQWo7NA3cdOLzGg6r6iRyki+UsGLodcdN5IXndAFK9e513Z/c1nxqkezvM kMng== 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=qAOewCQLgwWz0kHK72YTOt1hH5xfEC+UYb1BKwQbRIw=; fh=IRG/8dfgsHjFF+e71NzNAYSoDTaVuDdnvldFEoHwh2U=; b=Ezmirw8frqtulmLvwCPcFhP9RoEWaflM+886CdCmn2nPWofMCpJXO+1cQdg6e11snI 0AEr7qVpIEwySpNshsAhR0dQzQDVadh+MA/xYLjLIaGK3MF+Y65O6E8bV/ciAMY0B2z8 Sqh1yh6ITXCB4ssYHRp/GLMIR/ZxEfEuMf4LMKAjf4EG/gpra09HPCjLgVT4LzqQaOKC Oxwzs9oYeaz7P6+Ru+I7cxikguEMFxI7KPLDF4anw2UhVtei3CyCJxznu9lDBAZyV0rU aicUxDINkIaz2k3Ys9SV+tK4JQW2tG3ffwXYaC0JAp1KeeLYnO6L/lbLVYWXNqL6EheZ vsQA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@metaspace-dk.20230601.gappssmtp.com header.s=20230601 header.b="b7/anavp"; arc=pass (i=1 dkim=pass dkdomain=metaspace-dk.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-185047-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185047-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ada2fe7eead31-4806cb7e2e2si4593861137.7.2024.05.21.08.07.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 08:07:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-185047-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@metaspace-dk.20230601.gappssmtp.com header.s=20230601 header.b="b7/anavp"; arc=pass (i=1 dkim=pass dkdomain=metaspace-dk.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-185047-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185047-linux.lists.archive=gmail.com@vger.kernel.org" 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 0783D1C228B1 for ; Tue, 21 May 2024 14:02:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 273D91E502; Tue, 21 May 2024 14:02:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=metaspace-dk.20230601.gappssmtp.com header.i=@metaspace-dk.20230601.gappssmtp.com header.b="b7/anavp" Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 900ED77117 for ; Tue, 21 May 2024 14:02:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716300125; cv=none; b=MNmCkP9agfmmdPsS6WENQyv9yW8X4J/6H8DfBW+CnCOyklQv4hPCaEOiFCJduSOnu4MpUrgPEIuwIiqVOJ3qduptX+4PTgZy7Cfr7MzDp2VGXLeaGG+pT2bcMqAzbb2eRBWpr9aVT0k9hTsOIia4cjRCu9pUFGUpe9lnSI4+InI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716300125; c=relaxed/simple; bh=9QGBZCzlqj+FFJ1Y45J9aRl9xBFDHF+jLJOxIIOwF/k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TPx2wnJxCaarOzm6SkapztJA77OHZnFxzFCz2lrjdJQheoTKFa0uHHTH/0K9GBv8OOGpDN9qu5Dm2T2qmO15dK1WE4DlxJIvwUJsnH1E0BsUt/Uk/WNIHmLPwvzwN7wUbOar91qeZbhJ3KKE7J7+jQADGngmSsfE1RqhDuPwYzI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=metaspace.dk; spf=none smtp.mailfrom=metaspace.dk; dkim=pass (2048-bit key) header.d=metaspace-dk.20230601.gappssmtp.com header.i=@metaspace-dk.20230601.gappssmtp.com header.b=b7/anavp; arc=none smtp.client-ip=209.85.167.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=metaspace.dk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=metaspace.dk Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-51f4d2676d1so5094540e87.3 for ; Tue, 21 May 2024 07:02:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20230601.gappssmtp.com; s=20230601; t=1716300121; x=1716904921; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qAOewCQLgwWz0kHK72YTOt1hH5xfEC+UYb1BKwQbRIw=; b=b7/anavpfp7Q0tFuZWojlZKauaWewyVnUTjuGCZfA4IC9IfQaKk8lcmFnGxrKwnrwe IU3IwKZnaIlMlamGF2AP55mRS6ZyAnKt2SFhWR8b3RRXjcXrsM0PZlH18uMCyBqTT4bI hSnxTC2XPQO3GDGErp6sVa6Ma1GWZTlOWEPl1hzz0mjw+DUVs8r+YjzmhT6juzBd9q8j phQMdPzcMwAYrBYRQkKSdDhibeHf8DaPmnWHroHENzep6VmCucOewoNlybKQvfPe1WIi F/54rCBzqhTr48nEdSovfHDjCyqoG/251/x6KKdDx3l4N7f/J1vRPBiKC3YHxZk0JfBi DDOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716300121; x=1716904921; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qAOewCQLgwWz0kHK72YTOt1hH5xfEC+UYb1BKwQbRIw=; b=QbGOqY4QRLVVNW8iA1eaDx7kvnvUfbL1LymCKevJY/IYtoIASaJH2VbPCR/Go5SN6B fdd1lC3jwefcz2MQK3Rv+7g2Rg31eFlvYkKWi8UfHg8DzxgrIr2iYZeUYmy6PZdkijXK veatXzSD3XFcpjYh+asJZ/LWvYrL0aKpCJTPpKPTSsGaLfdutFu36n1XXBhO1E97I2QN rmNRvEevIRWYJNtdbKorHx5TOP08E9KKIKnsLmKQTGy1sRqQmGHaE4xv4SKtA9RT/Tw4 jOQxd08kN1ROsEhi+CNm8ejujny3xyLcUSTkXe/nxZBVvNLa3M7LLEi1Q3WnYK1LmkVT FuLA== X-Forwarded-Encrypted: i=1; AJvYcCWfmIPUiQPKkHWXaXGrQrxguIbTsJV90Ho2GboYfimdHAobvJrvZWAHeKvrDOluWQww2X8tTO3tl+fj+LpIC7oJoOXLXBplABy+onOt X-Gm-Message-State: AOJu0YzZpVgMxparVqHbCZvRbFBvMbnyz2cBHj30H1nZ48gX/nLSph4A Rkg7rAiEXvsYLvZNXOrZWuQmSLwW901P0zXYkc5bmu4ByXYtTJHqcRxycBaK6ac= X-Received: by 2002:a05:6512:3da1:b0:51f:1bf8:610c with SMTP id 2adb3069b0e04-5220fb77569mr26213481e87.11.1716300120353; Tue, 21 May 2024 07:02:00 -0700 (PDT) Received: from localhost ([79.142.230.34]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5733bed72bbsm16630154a12.57.2024.05.21.07.01.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 07:01:59 -0700 (PDT) From: Andreas Hindborg To: Jens Axboe , Christoph Hellwig , Keith Busch , Damien Le Moal , Bart Van Assche , Hannes Reinecke , Ming Lei , "linux-block@vger.kernel.org" Cc: Andreas Hindborg , Greg KH , Matthew Wilcox , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Chaitanya Kulkarni , Luis Chamberlain , Yexuan Yang <1182282462@bupt.edu.cn>, =?UTF-8?q?Sergio=20Gonz=C3=A1lez=20Collado?= , Joel Granados , "Pankaj Raghav (Samsung)" , Daniel Gomez , Niklas Cassel , Philipp Stanner , Conor Dooley , Johannes Thumshirn , =?UTF-8?q?Matias=20Bj=C3=B8rling?= , open list , "rust-for-linux@vger.kernel.org" , "lsf-pc@lists.linux-foundation.org" , "gost.dev@samsung.com" Subject: [PATCH v2 2/3] rust: block: add rnull, Rust null_blk implementation Date: Tue, 21 May 2024 16:03:21 +0200 Message-ID: <20240521140323.2960069-3-nmi@metaspace.dk> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240521140323.2960069-1-nmi@metaspace.dk> References: <20240521140323.2960069-1-nmi@metaspace.dk> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Andreas Hindborg This patch adds an initial version of the Rust null block driver. Signed-off-by: Andreas Hindborg --- drivers/block/Kconfig | 9 +++++ drivers/block/Makefile | 3 ++ drivers/block/rnull.rs | 86 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/block/mq.rs | 4 +- 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 drivers/block/rnull.rs diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig index 5b9d4aaebb81..ed209f4f2798 100644 --- a/drivers/block/Kconfig +++ b/drivers/block/Kconfig @@ -354,6 +354,15 @@ config VIRTIO_BLK This is the virtual block driver for virtio. It can be used with QEMU based VMMs (like KVM or Xen). Say Y or M. +config BLK_DEV_RUST_NULL + tristate "Rust null block driver (Experimental)" + depends on RUST + help + This is the Rust implementation of the null block driver. For now it + is only a minimal stub. + + If unsure, say N. + config BLK_DEV_RBD tristate "Rados block device (RBD)" depends on INET && BLOCK diff --git a/drivers/block/Makefile b/drivers/block/Makefile index 101612cba303..1105a2d4fdcb 100644 --- a/drivers/block/Makefile +++ b/drivers/block/Makefile @@ -9,6 +9,9 @@ # needed for trace events ccflags-y += -I$(src) +obj-$(CONFIG_BLK_DEV_RUST_NULL) += rnull_mod.o +rnull_mod-y := rnull.o + obj-$(CONFIG_MAC_FLOPPY) += swim3.o obj-$(CONFIG_BLK_DEV_SWIM) += swim_mod.o obj-$(CONFIG_BLK_DEV_FD) += floppy.o diff --git a/drivers/block/rnull.rs b/drivers/block/rnull.rs new file mode 100644 index 000000000000..1d6ab6f0f26f --- /dev/null +++ b/drivers/block/rnull.rs @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! This is a Rust implementation of the C null block driver. +//! +//! Supported features: +//! +//! - blk-mq interface +//! - direct completion +//! - block size 4k +//! +//! The driver is not configurable. + +use kernel::{ + alloc::flags, + block::mq::{ + self, + gen_disk::{self, GenDisk}, + Operations, TagSet, + }, + error::Result, + new_mutex, pr_info, + prelude::*, + sync::{Arc, Mutex}, + types::ARef, +}; + +module! { + type: NullBlkModule, + name: "rnull_mod", + author: "Andreas Hindborg", + license: "GPL v2", +} + +struct NullBlkModule { + _disk: Pin>>>, +} + +fn add_disk(tagset: Arc>) -> Result> { + let block_size: u16 = 4096; + if block_size % 512 != 0 || !(512..=4096).contains(&block_size) { + return Err(kernel::error::code::EINVAL); + } + + let mut disk = gen_disk::try_new(tagset)?; + disk.set_name(format_args!("rnullb{}", 0))?; + disk.set_capacity_sectors(4096 << 11); + disk.set_queue_logical_block_size(block_size.into()); + disk.set_queue_physical_block_size(block_size.into()); + disk.set_rotational(false); + disk.add() +} + +impl kernel::Module for NullBlkModule { + fn init(_module: &'static ThisModule) -> Result { + pr_info!("Rust null_blk loaded\n"); + let tagset = Arc::pin_init(TagSet::try_new(1, 256, 1), flags::GFP_KERNEL)?; + let disk = Box::pin_init( + new_mutex!(add_disk(tagset)?, "nullb:disk"), + flags::GFP_KERNEL, + )?; + + Ok(Self { _disk: disk }) + } +} + +struct NullBlkDevice; + +#[vtable] +impl Operations for NullBlkDevice { + #[inline(always)] + fn queue_rq(rq: ARef>, _is_last: bool) -> Result { + mq::Request::end_ok(rq) + .map_err(|_e| kernel::error::code::EIO) + .expect("Failed to complete request"); + + Ok(()) + } + + fn commit_rqs() {} + + fn complete(rq: ARef>) { + mq::Request::end_ok(rq) + .map_err(|_e| kernel::error::code::EIO) + .expect("Failed to complete request") + } +} diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index efbd2588791b..54e032bbdffd 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -51,6 +51,7 @@ //! //! ```rust //! use kernel::{ +//! alloc::flags, //! block::mq::*, //! new_mutex, //! prelude::*, @@ -77,7 +78,8 @@ //! } //! } //! -//! let tagset: Arc> = Arc::pin_init(TagSet::try_new(1, 256, 1))?; +//! let tagset: Arc> = +//! Arc::pin_init(TagSet::try_new(1, 256, 1), flags::GFP_KERNEL)?; //! let mut disk = gen_disk::try_new(tagset)?; //! disk.set_name(format_args!("myblk"))?; //! disk.set_capacity_sectors(4096); -- 2.44.0