Skip to content

Commit 71b9b0f

Browse files
Andreas Hindborgkawasaki
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. Signed-off-by: Andreas Hindborg <[email protected]>
1 parent 618b344 commit 71b9b0f

2 files changed

Lines changed: 80 additions & 13 deletions

File tree

drivers/block/rnull/configfs.rs

Lines changed: 59 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(kernel::error::code::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

@@ -126,6 +157,7 @@ impl configfs::AttributeOperations<0> for DeviceConfig {
126157
guard.block_size,
127158
guard.rotational,
128159
guard.capacity_mib,
160+
guard.irq_mode,
129161
)?);
130162
guard.powered = true;
131163
} else if guard.powered && !power_op {
@@ -218,3 +250,28 @@ impl configfs::AttributeOperations<3> for DeviceConfig {
218250
Ok(())
219251
}
220252
}
253+
254+
#[vtable]
255+
impl configfs::AttributeOperations<4> for DeviceConfig {
256+
type Data = DeviceConfig;
257+
258+
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
259+
let mut writer = kernel::str::Formatter::new(page);
260+
writer.write_fmt(fmt!("{}\n", this.data.lock().irq_mode))?;
261+
Ok(writer.bytes_written())
262+
}
263+
264+
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
265+
if this.data.lock().powered {
266+
return Err(EBUSY);
267+
}
268+
269+
let text = core::str::from_utf8(page)?.trim();
270+
let value = text
271+
.parse::<u8>()
272+
.map_err(|_| kernel::error::code::EINVAL)?;
273+
274+
this.data.lock().irq_mode = IRQMode::try_from(value)?;
275+
Ok(())
276+
}
277+
}

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
alloc::flags,
910
block::{
@@ -54,35 +55,44 @@ impl NullBlkDevice {
5455
block_size: u32,
5556
rotational: bool,
5657
capacity_mib: u64,
58+
irq_mode: IRQMode,
5759
) -> Result<GenDisk<Self>> {
5860
let tagset = Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?;
5961

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

73+
struct QueueData {
74+
irq_mode: IRQMode,
75+
}
76+
6977
#[vtable]
7078
impl Operations for NullBlkDevice {
71-
type QueueData = ();
79+
type QueueData = KBox<QueueData>;
7280

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

85-
fn commit_rqs(_queue_data: ()) {}
95+
fn commit_rqs(_queue_data: &QueueData) {}
8696

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

0 commit comments

Comments
 (0)