Skip to content

Commit 34585dc

Browse files
Andreas Hindborgaxboe
authored andcommitted
rnull: add soft-irq completion support
rnull currently only supports direct completion. Add option for completing requests across CPU nodes via soft IRQ or IPI. Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Daniel Almeida <[email protected]> Signed-off-by: Andreas Hindborg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 4ec0528 commit 34585dc

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

drivers/block/rnull/configfs.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
use super::{NullBlkDevice, THIS_MODULE};
4-
use core::fmt::Write;
4+
use core::fmt::{Display, Write};
55
use kernel::{
66
block::mq::gen_disk::{GenDisk, GenDiskBuilder},
77
c_str,
@@ -36,7 +36,7 @@ impl AttributeOperations<0> for Config {
3636

3737
fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
3838
let mut writer = kernel::str::Formatter::new(page);
39-
writer.write_str("blocksize,size,rotational\n")?;
39+
writer.write_str("blocksize,size,rotational,irqmode\n")?;
4040
Ok(writer.bytes_written())
4141
}
4242
}
@@ -58,6 +58,7 @@ impl configfs::GroupOperations for Config {
5858
blocksize: 1,
5959
rotational: 2,
6060
size: 3,
61+
irqmode: 4,
6162
],
6263
};
6364

@@ -72,13 +73,42 @@ impl configfs::GroupOperations for Config {
7273
rotational: false,
7374
disk: None,
7475
capacity_mib: 4096,
76+
irq_mode: IRQMode::None,
7577
name: name.try_into()?,
7678
}),
7779
}),
7880
))
7981
}
8082
}
8183

84+
#[derive(Debug, Clone, Copy)]
85+
pub(crate) enum IRQMode {
86+
None,
87+
Soft,
88+
}
89+
90+
impl TryFrom<u8> for IRQMode {
91+
type Error = kernel::error::Error;
92+
93+
fn try_from(value: u8) -> Result<Self> {
94+
match value {
95+
0 => Ok(Self::None),
96+
1 => Ok(Self::Soft),
97+
_ => Err(EINVAL),
98+
}
99+
}
100+
}
101+
102+
impl Display for IRQMode {
103+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
104+
match self {
105+
Self::None => f.write_str("0")?,
106+
Self::Soft => f.write_str("1")?,
107+
}
108+
Ok(())
109+
}
110+
}
111+
82112
#[pin_data]
83113
pub(crate) struct DeviceConfig {
84114
#[pin]
@@ -92,6 +122,7 @@ struct DeviceConfigInner {
92122
block_size: u32,
93123
rotational: bool,
94124
capacity_mib: u64,
125+
irq_mode: IRQMode,
95126
disk: Option<GenDisk<NullBlkDevice>>,
96127
}
97128

@@ -121,6 +152,7 @@ impl configfs::AttributeOperations<0> for DeviceConfig {
121152
guard.block_size,
122153
guard.rotational,
123154
guard.capacity_mib,
155+
guard.irq_mode,
124156
)?);
125157
guard.powered = true;
126158
} else if guard.powered && !power_op {
@@ -205,3 +237,26 @@ impl configfs::AttributeOperations<3> for DeviceConfig {
205237
Ok(())
206238
}
207239
}
240+
241+
#[vtable]
242+
impl configfs::AttributeOperations<4> for DeviceConfig {
243+
type Data = DeviceConfig;
244+
245+
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
246+
let mut writer = kernel::str::Formatter::new(page);
247+
writer.write_fmt(fmt!("{}\n", this.data.lock().irq_mode))?;
248+
Ok(writer.bytes_written())
249+
}
250+
251+
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
252+
if this.data.lock().powered {
253+
return Err(EBUSY);
254+
}
255+
256+
let text = core::str::from_utf8(page)?.trim();
257+
let value = text.parse::<u8>().map_err(|_| EINVAL)?;
258+
259+
this.data.lock().irq_mode = IRQMode::try_from(value)?;
260+
Ok(())
261+
}
262+
}

drivers/block/rnull/rnull.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
mod configfs;
66

7+
use configfs::IRQMode;
78
use kernel::{
89
block::{
910
self,
@@ -53,35 +54,44 @@ impl NullBlkDevice {
5354
block_size: u32,
5455
rotational: bool,
5556
capacity_mib: u64,
57+
irq_mode: IRQMode,
5658
) -> Result<GenDisk<Self>> {
5759
let tagset = Arc::pin_init(TagSet::new(1, 256, 1), GFP_KERNEL)?;
5860

61+
let queue_data = Box::new(QueueData { irq_mode }, GFP_KERNEL)?;
62+
5963
gen_disk::GenDiskBuilder::new()
6064
.capacity_sectors(capacity_mib << (20 - block::SECTOR_SHIFT))
6165
.logical_block_size(block_size)?
6266
.physical_block_size(block_size)?
6367
.rotational(rotational)
64-
.build(fmt!("{}", name.to_str()?), tagset, ())
68+
.build(fmt!("{}", name.to_str()?), tagset, queue_data)
6569
}
6670
}
6771

72+
struct QueueData {
73+
irq_mode: IRQMode,
74+
}
75+
6876
#[vtable]
6977
impl Operations for NullBlkDevice {
70-
type QueueData = ();
78+
type QueueData = KBox<QueueData>;
7179

7280
#[inline(always)]
73-
fn queue_rq(_queue_data: (), rq: ARef<mq::Request<Self>>, _is_last: bool) -> Result {
74-
mq::Request::end_ok(rq)
75-
.map_err(|_e| kernel::error::code::EIO)
76-
// We take no refcounts on the request, so we expect to be able to
77-
// end the request. The request reference must be unique at this
78-
// point, and so `end_ok` cannot fail.
79-
.expect("Fatal error - expected to be able to end request");
80-
81+
fn queue_rq(queue_data: &QueueData, rq: ARef<mq::Request<Self>>, _is_last: bool) -> Result {
82+
match queue_data.irq_mode {
83+
IRQMode::None => mq::Request::end_ok(rq)
84+
.map_err(|_e| kernel::error::code::EIO)
85+
// We take no refcounts on the request, so we expect to be able to
86+
// end the request. The request reference must be unique at this
87+
// point, and so `end_ok` cannot fail.
88+
.expect("Fatal error - expected to be able to end request"),
89+
IRQMode::Soft => mq::Request::complete(rq),
90+
}
8191
Ok(())
8292
}
8393

84-
fn commit_rqs(_queue_data: ()) {}
94+
fn commit_rqs(_queue_data: &QueueData) {}
8595

8696
fn complete(rq: ARef<mq::Request<Self>>) {
8797
mq::Request::end_ok(rq)

0 commit comments

Comments
 (0)