configfs-for-v6.16

-----BEGIN PGP SIGNATURE-----
 
 iQJKBAABCAA0FiEEEsH5R1a/fCoV1sAS4bgaPnkoY3cFAmgm4zAWHGEuaGluZGJv
 cmdAa2VybmVsLm9yZwAKCRDhuBo+eShjd8+nD/4xLZRdP5/Sq8boG/ZjPwja8ATv
 MjSrakeOHFPJx+ac/QNmhkWmVQrpazA72YU9NN5VL9UdM+eFHJ4EmFIUQFcKsYhU
 edj5Fyegc2/CzY4wPcJQcY4ck9n6xoWqR//75uEtHRfMVAwJguQ7/qg7XR+Skhy7
 RSuJ96qi2uf6ktrZUGVilkF+tjhg0j9eoiJUnU+2d5khT/vqlezv/x6xC1Vxr5WK
 JgsIhg9NzEc3BjZotmurZ7WaAIFey83ta8r7fwQM+SfSqMbLbdXu3XNQ5v9MBbBO
 YDsxl2LD2ukCzucbY5h6/h1TjQ+IBUSx9ExEq0ATG46HzMGBc7EFMYb3Q2klaSX2
 lfK5YKvZVFPZUp1Yr94+YIvyhU6g0dtDELOBVB5lj1FHv7wqDOUY9wofh+tLgyMT
 PTp/sYHc7UUvPghFMNhrkbGaIrUI2fRZviTgAJGOp2zE2gvV2lz4Jg9UvBkbpezc
 q8X6xx+3r1dj5MhlrjeLifLWiXaZhumT2D5TEwYl3LjKLzekFKCMoqJ5eqFPSty9
 vvp5uBk7siErAlAi3xq19HWmrGoacMg0tPqe9QElkA2jzBzNBcEdo7NQAAOzjkg+
 NA58fKyrcmKrI7Y1NQmHTmx1RIUOWPXyW7ddZS8CGgsDzlVH35gIBPFqmj235tSq
 67Rtb/Iev7KtPL0z8w==
 =d1MQ
 -----END PGP SIGNATURE-----

Merge tag 'configfs-for-v6.16' of git://git.kernel.org/pub/scm/linux/kernel/git/a.hindborg/linux

Pull configfs updates from Andreas Hindborg:

 - Allow creation of rw files with custom permissions. This allows
   drivers to better protect secrets written through configfs

 - Fix a bug where an error condition did not cause an early return
   while populating attributes

 - Report ENOMEM rather than EFAULT when kvasprintf() fails in
   config_item_set_name()

 - Add a Rust API for configfs. This allows Rust drivers to use configfs
   through a memory safe interface

* tag 'configfs-for-v6.16' of git://git.kernel.org/pub/scm/linux/kernel/git/a.hindborg/linux:
  MAINTAINERS: add configfs Rust abstractions
  rust: configfs: add a sample demonstrating configfs usage
  rust: configfs: introduce rust support for configfs
  configfs: Correct error value returned by API config_item_set_name()
  configfs: Do not override creating attribute file failure in populate_attrs()
  configfs: Delete semicolon from macro type_print() definition
  configfs: Add CONFIGFS_ATTR_PERM helper
This commit is contained in:
Linus Torvalds 2025-05-26 12:28:55 -07:00
commit a56d3133bd
11 changed files with 1272 additions and 5 deletions

View File

@ -5983,7 +5983,9 @@ S: Supported
T: git git://git.kernel.org/pub/scm/linux/kernel/git/a.hindborg/linux.git configfs-next
F: fs/configfs/
F: include/linux/configfs.h
F: rust/kernel/configfs.rs
F: samples/configfs/
F: samples/rust/rust_configfs.rs
CONGATEC BOARD CONTROLLER MFD DRIVER
M: Thomas Richard <thomas.richard@bootlin.com>

View File

@ -619,7 +619,7 @@ static int populate_attrs(struct config_item *item)
break;
}
}
if (t->ct_bin_attrs) {
if (!error && t->ct_bin_attrs) {
for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
if (ops && ops->is_bin_visible && !ops->is_bin_visible(item, bin_attr, i))
continue;
@ -970,7 +970,7 @@ static void configfs_dump_one(struct configfs_dirent *sd, int level)
{
pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
#define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
#define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type)
type_print(CONFIGFS_ROOT);
type_print(CONFIGFS_DIR);
type_print(CONFIGFS_ITEM_ATTR);

View File

@ -66,7 +66,7 @@ int config_item_set_name(struct config_item *item, const char *fmt, ...)
name = kvasprintf(GFP_KERNEL, fmt, args);
va_end(args);
if (!name)
return -EFAULT;
return -ENOMEM;
}
/* Free the old name, if necessary. */

View File

@ -120,15 +120,19 @@ struct configfs_attribute {
ssize_t (*store)(struct config_item *, const char *, size_t);
};
#define CONFIGFS_ATTR(_pfx, _name) \
#define CONFIGFS_ATTR_PERM(_pfx, _name, _perm) \
static struct configfs_attribute _pfx##attr_##_name = { \
.ca_name = __stringify(_name), \
.ca_mode = S_IRUGO | S_IWUSR, \
.ca_mode = _perm, \
.ca_owner = THIS_MODULE, \
.show = _pfx##_name##_show, \
.store = _pfx##_name##_store, \
}
#define CONFIGFS_ATTR(_pfx, _name) CONFIGFS_ATTR_PERM( \
_pfx, _name, S_IRUGO | S_IWUSR \
)
#define CONFIGFS_ATTR_RO(_pfx, _name) \
static struct configfs_attribute _pfx##attr_##_name = { \
.ca_name = __stringify(_name), \

View File

@ -10,6 +10,7 @@
#include <linux/blk-mq.h>
#include <linux/blk_types.h>
#include <linux/blkdev.h>
#include <linux/configfs.h>
#include <linux/cpumask.h>
#include <linux/cred.h>
#include <linux/device/faux.h>

View File

@ -17,3 +17,8 @@ void rust_helper_mutex_assert_is_held(struct mutex *mutex)
{
lockdep_assert_held(mutex);
}
void rust_helper_mutex_destroy(struct mutex *lock)
{
mutex_destroy(lock);
}

1049
rust/kernel/configfs.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -42,6 +42,8 @@
pub mod block;
#[doc(hidden)]
pub mod build_assert;
#[cfg(CONFIG_CONFIGFS_FS)]
pub mod configfs;
pub mod cred;
pub mod device;
pub mod device_id;

View File

@ -10,6 +10,17 @@ menuconfig SAMPLES_RUST
if SAMPLES_RUST
config SAMPLE_RUST_CONFIGFS
tristate "Configfs sample"
depends on CONFIGFS_FS
help
This option builds the Rust configfs sample.
To compile this as a module, choose M here:
the module will be called rust_configfs.
If unsure, say N.
config SAMPLE_RUST_MINIMAL
tristate "Minimal"
help

View File

@ -8,6 +8,7 @@ obj-$(CONFIG_SAMPLE_RUST_DMA) += rust_dma.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o
obj-$(CONFIG_SAMPLE_RUST_CONFIGFS) += rust_configfs.o
rust_print-y := rust_print_main.o rust_print_events.o

View File

@ -0,0 +1,192 @@
// SPDX-License-Identifier: GPL-2.0
//! Rust configfs sample.
use kernel::alloc::flags;
use kernel::c_str;
use kernel::configfs;
use kernel::configfs_attrs;
use kernel::new_mutex;
use kernel::page::PAGE_SIZE;
use kernel::prelude::*;
use kernel::sync::Mutex;
module! {
type: RustConfigfs,
name: "rust_configfs",
author: "Rust for Linux Contributors",
description: "Rust configfs sample",
license: "GPL",
}
#[pin_data]
struct RustConfigfs {
#[pin]
config: configfs::Subsystem<Configuration>,
}
#[pin_data]
struct Configuration {
message: &'static CStr,
#[pin]
bar: Mutex<(KBox<[u8; PAGE_SIZE]>, usize)>,
}
impl Configuration {
fn new() -> impl PinInit<Self, Error> {
try_pin_init!(Self {
message: c_str!("Hello World\n"),
bar <- new_mutex!((KBox::new([0; PAGE_SIZE], flags::GFP_KERNEL)?, 0)),
})
}
}
impl kernel::InPlaceModule for RustConfigfs {
fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
pr_info!("Rust configfs sample (init)\n");
// Define a subsystem with the data type `Configuration`, two
// attributes, `message` and `bar` and child group type `Child`. `mkdir`
// in the directory representing this subsystem will create directories
// backed by the `Child` type.
let item_type = configfs_attrs! {
container: configfs::Subsystem<Configuration>,
data: Configuration,
child: Child,
attributes: [
message: 0,
bar: 1,
],
};
try_pin_init!(Self {
config <- configfs::Subsystem::new(
c_str!("rust_configfs"), item_type, Configuration::new()
),
})
}
}
#[vtable]
impl configfs::GroupOperations for Configuration {
type Child = Child;
fn make_group(&self, name: &CStr) -> Result<impl PinInit<configfs::Group<Child>, Error>> {
// Define a group with data type `Child`, one attribute `baz` and child
// group type `GrandChild`. `mkdir` in the directory representing this
// group will create directories backed by the `GrandChild` type.
let tpe = configfs_attrs! {
container: configfs::Group<Child>,
data: Child,
child: GrandChild,
attributes: [
baz: 0,
],
};
Ok(configfs::Group::new(name.try_into()?, tpe, Child::new()))
}
}
#[vtable]
impl configfs::AttributeOperations<0> for Configuration {
type Data = Configuration;
fn show(container: &Configuration, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
pr_info!("Show message\n");
let data = container.message;
page[0..data.len()].copy_from_slice(data);
Ok(data.len())
}
}
#[vtable]
impl configfs::AttributeOperations<1> for Configuration {
type Data = Configuration;
fn show(container: &Configuration, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
pr_info!("Show bar\n");
let guard = container.bar.lock();
let data = guard.0.as_slice();
let len = guard.1;
page[0..len].copy_from_slice(&data[0..len]);
Ok(len)
}
fn store(container: &Configuration, page: &[u8]) -> Result {
pr_info!("Store bar\n");
let mut guard = container.bar.lock();
guard.0[0..page.len()].copy_from_slice(page);
guard.1 = page.len();
Ok(())
}
}
// `pin_data` cannot handle structs without braces.
#[pin_data]
struct Child {}
impl Child {
fn new() -> impl PinInit<Self, Error> {
try_pin_init!(Self {})
}
}
#[vtable]
impl configfs::GroupOperations for Child {
type Child = GrandChild;
fn make_group(&self, name: &CStr) -> Result<impl PinInit<configfs::Group<GrandChild>, Error>> {
// Define a group with data type `GrandChild`, one attribute `gc`. As no
// child type is specified, it will not be possible to create subgroups
// in this group, and `mkdir`in the directory representing this group
// will return an error.
let tpe = configfs_attrs! {
container: configfs::Group<GrandChild>,
data: GrandChild,
attributes: [
gc: 0,
],
};
Ok(configfs::Group::new(
name.try_into()?,
tpe,
GrandChild::new(),
))
}
}
#[vtable]
impl configfs::AttributeOperations<0> for Child {
type Data = Child;
fn show(_container: &Child, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
pr_info!("Show baz\n");
let data = c"Hello Baz\n".to_bytes();
page[0..data.len()].copy_from_slice(data);
Ok(data.len())
}
}
// `pin_data` cannot handle structs without braces.
#[pin_data]
struct GrandChild {}
impl GrandChild {
fn new() -> impl PinInit<Self, Error> {
try_pin_init!(Self {})
}
}
#[vtable]
impl configfs::AttributeOperations<0> for GrandChild {
type Data = GrandChild;
fn show(_container: &GrandChild, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
pr_info!("Show grand child\n");
let data = c"Hello GC\n".to_bytes();
page[0..data.len()].copy_from_slice(data);
Ok(data.len())
}
}