From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 AFD1B3F8236 for ; Wed, 5 Aug 2026 19:20:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785957659; cv=none; b=THAWMJ4SdPaL3ra7pDQlvCYDvQHR0U1/pBVBfGhHMk8pJFns1BY1h+jo275r2feOT0MrYYs0EoccPTiE/kl62/eSLwaS+T4GNu/U/OgZPOqtJEUxY+RYR0NNviLeB6FwoHdrF2ow3lbltsn+gp5RHKvSq+yvuKLTVt7d69+cz1w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785957659; c=relaxed/simple; bh=M7+Xp5G3tZjyNJEGVQVB6k60p7Qw9FijqopxCE9sFOc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=tLLCP2tzZGVDWahCvYQX8GBkTwO27EH04kkQ1G2zrzncLmoZ0BCTeADJ+xqxn78pEIU/DtkZjtZy+lmi/UPs+NqoE+iMCJJFyNtMd2aN3aV3nQaV9GUOL2atctCJSTAS+WpIFwxrdgfQ5SyO7t1XYUcsanaurBZOEV6He8SlSSU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=qc/cEU7w; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="qc/cEU7w" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2ce98cb8165so2623845ad.1 for ; Wed, 05 Aug 2026 12:20:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785957648; x=1786562448; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=CJyM/9okkHYCeVF069aAYJMZJUD0kZWBXu8tJw0Mi1w=; b=qc/cEU7w42YEzgIaid8rWB0Bp+RokkA+FLzQ3J8+fxw+cSoTJtfNvWX//c4oz7yLuS l0Wm8Y/lKv22jbb3seFhvEe2tmRLUvzmV1HOcTdA8TgVZEU5JBwJx4FOvUJFlaaIwyv0 gaSD5QP+BRGdqDeVooIcdtvx2cxvrUBtJSu7CfPMzwBZcqZrLFTOy0bv2lYX3nfLbv0t jHwsS9INh/RHTy8uYbqyGItjS26pT+xGxzuVdfK4htD0Iesds2+0UThgETEaZiYn9BNM VzdixjRJ3rXF1zBBgSaozGtwx3RVDu4P2syxtb6GfrMh0RnwH1arAxP+i7AZG10rYO8P CcRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785957648; x=1786562448; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=CJyM/9okkHYCeVF069aAYJMZJUD0kZWBXu8tJw0Mi1w=; b=oueJN3cR58RI9suaEw/veDExqvH6sjMVx0axeU9VrE58AiKV+BeYjjLFPbpV/ZfjP5 KyxHiesTWgz+i0Xsj60tke1TKVQhGTrMXSdVR8UqsgKF+IAJMjsf9gkCRFB7KY0Wd39H VbXze03cyFSo6yAKTn8O9hj6zxjCfi+gHEiAiS6xb5wxHGqX70dOitzFN+RWNkMIyvmU 4fUkwne+ygjUyw/Xx0ZLrzEKMlJRhKexg+R3qKwaEvQeCwwg+Gbe68CQi0n6zRDbUMyJ t+PH0hFuzbx0f0nknncyZp8ij3ENOsoN4UMbDgB5a9VEoLLbR1/WT+4ZuM6T7+n84U+K /sDg== X-Forwarded-Encrypted: i=1; AHgh+RrCGF7JeLCio9GctIoyo1DuHvXPvsGxzYIsRUf4tJzLKm35r2bj2LySwzdZjEaPlv6hO9gjo0LmBmKZkEjMKw==@vger.kernel.org X-Gm-Message-State: AOJu0Yy9IQAKSnealVB8BivAOhpkS4StnuKPRu+P6AEc6RkNycIiKXK6 zqBtSz+o3s6c3h4Ob+APB+lM2izwG/BlKpoF1G487kHNhYvWo7zInrRK X-Gm-Gg: AR+sD12NfNN4Qr0b8lanq9mcoFd9qLAvJ2DxwY1LKESvqvPBfJbyNSUHPrQRNvli2wY CHxEiNcfMAcwSWpzW+C/B7VviF63kvWGKDMxD5aqXaQhHYyYGJWZZhINo3kFT48OYSXr5U2OfWO wPdgjUYhUPQ6LvC9FB3G8xgSj7f/szLZWBvLOu7T+ozEvHJfLLrbYNcCfea+wnPt46iTha3Q4jo As4csTQFcMRVB2g2Y3NDRCie+5Olp2rbzpsy9AGMwlMg+ljQ2i7y0i6aa7/HFHya59k4taI2+a9 taNCkj8cjZG6glcG/eXw/q0AXDUE6cnbqjTjHU+dPOche2mkXTJ30zFhB6yIVJLXmxVgFsBBp8S +K5YpEbYFwdV2jRyF3dbS+L9GSCCt3tXQzBlTDKLWf9LX8wYn919EtpdbUkjkZEC9UdJo8E6+C6 l+IMccya252C4gC7v7feZc8I0cvR8TqZeXEATAKOlCm+g23cxJcLl5dGRfIaRPX8k= X-Received: by 2002:a17:902:da87:b0:2d0:401c:2edc with SMTP id d9443c01a7336-2d0f185d642mr11951815ad.1.1785957648176; Wed, 05 Aug 2026 12:20:48 -0700 (PDT) Received: from archlinux ([2402:e280:411b:ec:aa93:b1ec:5a6b:5fce]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-315863b76cfsm18043913eec.4.2026.08.05.12.20.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Aug 2026 12:20:47 -0700 (PDT) From: Adarsh Das To: Andreas Hindborg Cc: Boqun Feng , Jens Axboe , Miguel Ojeda , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?UTF-8?q?Onur=20=C3=96zkan?= , linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Adarsh Das Subject: [PATCH] rust: block: set GenDisk block_device_operations.owner to THIS_MODULE Date: Thu, 6 Aug 2026 00:50:20 +0530 Message-ID: <20260805192020.107601-1-adarshdas950@gmail.com> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit GenDiskBuilder currently installs block_device_operations with owner set to NULL. The block layer uses this field to pin the driver module while block device file operations are in use. Take the driver's ThisModule in GenDiskBuilder::build() and store the operations table in GenDisk so gendisk->fops stays valid if the disk is moved, such as when rnull keeps it in an Option behind configfs. Update rnull as the in-tree caller. Signed-off-by: Adarsh Das --- drivers/block/rnull/configfs.rs | 1 + drivers/block/rnull/rnull.rs | 3 +- rust/kernel/block/mq.rs | 9 +++-- rust/kernel/block/mq/gen_disk.rs | 58 +++++++++++++++++++------------- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs.rs index 7c2eb5c0b722..bba30d590f68 100644 --- a/drivers/block/rnull/configfs.rs +++ b/drivers/block/rnull/configfs.rs @@ -147,6 +147,7 @@ fn store(this: &DeviceConfig, page: &[u8]) -> Result { if !guard.powered && power_op { guard.disk = Some(NullBlkDevice::new( + &THIS_MODULE, &guard.name, guard.block_size, guard.rotational, diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index 0ca8715febe8..4265a133cbf0 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -46,6 +46,7 @@ fn init(_module: &'static ThisModule) -> impl PinInit { impl NullBlkDevice { fn new( + this_module: &'static ThisModule, name: &CStr, block_size: u32, rotational: bool, @@ -61,7 +62,7 @@ fn new( .logical_block_size(block_size)? .physical_block_size(block_size)? .rotational(rotational) - .build(fmt!("{}", name.to_str()?), tagset, queue_data) + .build(this_module, fmt!("{}", name.to_str()?), tagset, queue_data) } } diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 1fd0d54dd549..33561e0f67af 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -8,8 +8,8 @@ //! - Implement [`Operations`] for a type `T`. //! - Create a [`TagSet`]. //! - Create a [`GenDisk`], via the [`GenDiskBuilder`]. -//! - Add the disk to the system by calling [`GenDiskBuilder::build`] passing in -//! the `TagSet` reference. +//! - Add the disk to the system by calling [`GenDiskBuilder::build`], passing in +//! the driver's [`ThisModule`], the disk name, the `TagSet`, and queue data. //! //! The types available in this module that have direct C counterparts are: //! @@ -86,9 +86,12 @@ //! //! let tagset: Arc> = //! Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?; +//! # // SAFETY: Dummy `ThisModule` for doctest compilation only. +//! # static THIS_MODULE: ThisModule = +//! # unsafe { ThisModule::from_ptr(core::ptr::null_mut()) }; //! let mut disk = gen_disk::GenDiskBuilder::new() //! .capacity_sectors(4096) -//! .build(fmt!("myblk"), tagset, ())?; +//! .build(&THIS_MODULE, fmt!("myblk"), tagset, ())?; //! //! # Ok::<(), kernel::error::Error>(()) //! ``` diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_disk.rs index fc97dd873974..ff079223d31a 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -95,8 +95,12 @@ pub fn capacity_sectors(mut self, capacity: u64) -> Self { } /// Build a new `GenDisk` and add it to the VFS. + /// + /// `this_module` must be the [`ThisModule`] for the kernel module registering + /// the disk. pub fn build( self, + this_module: &'static ThisModule, name: fmt::Arguments<'_>, tagset: Arc>, queue_data: T::QueueData, @@ -125,30 +129,32 @@ pub fn build( ) })?; - const TABLE: bindings::block_device_operations = bindings::block_device_operations { - submit_bio: None, - open: None, - release: None, - ioctl: None, - compat_ioctl: None, - check_events: None, - unlock_native_capacity: None, - getgeo: None, - set_read_only: None, - swap_slot_free_notify: None, - report_zones: None, - devnode: None, - alternative_gpt_sector: None, - get_unique_id: None, - // TODO: Set to `THIS_MODULE`. - owner: core::ptr::null_mut(), - pr_ops: core::ptr::null_mut(), - free_disk: None, - poll_bio: None, - }; - - // SAFETY: `gendisk` is a valid pointer as we initialized it above - unsafe { (*gendisk).fops = &TABLE }; + let fops = KBox::new( + bindings::block_device_operations { + submit_bio: None, + open: None, + release: None, + ioctl: None, + compat_ioctl: None, + check_events: None, + unlock_native_capacity: None, + getgeo: None, + set_read_only: None, + swap_slot_free_notify: None, + report_zones: None, + devnode: None, + alternative_gpt_sector: None, + get_unique_id: None, + owner: this_module.as_ptr(), + pr_ops: core::ptr::null_mut(), + free_disk: None, + poll_bio: None, + }, + GFP_KERNEL, + )?; + + // SAFETY: `gendisk` is a valid pointer as we initialized it above. + unsafe { (*gendisk).fops = core::ptr::from_ref(&*fops) }; let cleanup_failure = ScopeGuard::new_with_data((gendisk, data), |(gendisk, data)| { // SAFETY: `gendisk` came from `__blk_mq_alloc_disk()` above and @@ -193,6 +199,7 @@ pub fn build( // `__blk_mq_alloc_disk` above. Ok(GenDisk { _tagset: tagset, + _fops: fops, gendisk, }) } @@ -206,8 +213,11 @@ pub fn build( /// - `gendisk` was added to the VFS through a call to /// `bindings::device_add_disk`. /// - `self.gendisk.queue.queuedata` is initialized by a call to `ForeignOwnable::into_foreign`. +/// - `self._fops` stores the [`bindings::block_device_operations`] pointed to by +/// `gendisk.fops`. pub struct GenDisk { _tagset: Arc>, + _fops: KBox, gendisk: *mut bindings::gendisk, } -- 2.55.0