summaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
Diffstat (limited to 'samples')
-rw-r--r--samples/damon/mtier.c16
-rw-r--r--samples/ftrace/ftrace-ops.c4
-rw-r--r--samples/landlock/sandboxer.c175
-rw-r--r--samples/rust/rust_debugfs.rs11
-rw-r--r--samples/rust/rust_dma.rs18
-rw-r--r--samples/rust/rust_driver_auxiliary.rs79
-rw-r--r--samples/rust/rust_driver_i2c.rs13
-rw-r--r--samples/rust/rust_driver_pci.rs90
-rw-r--r--samples/rust/rust_driver_platform.rs9
-rw-r--r--samples/rust/rust_driver_usb.rs15
-rw-r--r--samples/rust/rust_i2c_client.rs14
-rw-r--r--samples/rust/rust_misc_device.rs34
-rw-r--r--samples/rust/rust_soc.rs9
-rw-r--r--samples/trace_events/trace-events-sample.c4
14 files changed, 359 insertions, 132 deletions
diff --git a/samples/damon/mtier.c b/samples/damon/mtier.c
index 775838a23d93..dae6929f827c 100644
--- a/samples/damon/mtier.c
+++ b/samples/damon/mtier.c
@@ -75,11 +75,11 @@ static struct damon_ctx *damon_sample_mtier_build_ctx(bool promote)
struct damon_ctx *ctx;
struct damon_attrs attrs;
struct damon_target *target;
- struct damon_region *region;
struct damos *scheme;
struct damos_quota_goal *quota_goal;
struct damos_filter *filter;
struct region_range addr;
+ struct damon_addr_range range;
int ret;
ctx = damon_new_ctx();
@@ -120,10 +120,15 @@ static struct damon_ctx *damon_sample_mtier_build_ctx(bool promote)
addr.end = promote ? node1_end_addr : node0_end_addr;
}
- region = damon_new_region(addr.start, addr.end);
- if (!region)
+ if (addr.start >= addr.end)
+ goto free_out;
+
+ range.start = addr.start;
+ range.end = addr.end;
+
+ ret = damon_set_regions(target, &range, 1, DAMON_MIN_REGION_SZ);
+ if (ret)
goto free_out;
- damon_add_region(region, target);
scheme = damon_new_scheme(
/* access pattern */
@@ -151,6 +156,9 @@ static struct damon_ctx *damon_sample_mtier_build_ctx(bool promote)
if (!scheme)
goto free_out;
damon_set_schemes(ctx, &scheme, 1);
+ /* zero target value causes division by zero in damos_quota_store() */
+ if (!node0_mem_used_bp || !node0_mem_free_bp)
+ goto free_out;
quota_goal = damos_new_quota_goal(
promote ? DAMOS_QUOTA_NODE_MEM_USED_BP :
DAMOS_QUOTA_NODE_MEM_FREE_BP,
diff --git a/samples/ftrace/ftrace-ops.c b/samples/ftrace/ftrace-ops.c
index 68d6685c80bd..152ffc1a30b6 100644
--- a/samples/ftrace/ftrace-ops.c
+++ b/samples/ftrace/ftrace-ops.c
@@ -232,8 +232,8 @@ static int __init ftrace_ops_sample_init(void)
ops_destroy(ops_irrelevant, nr_ops_irrelevant);
/*
- * The benchmark completed sucessfully, but there's no reason to keep
- * the module around. Return an error do the user doesn't have to
+ * The benchmark completed successfully, but there's no reason to keep
+ * the module around. Return an error so the user doesn't have to
* manually unload the module.
*/
return -EINVAL;
diff --git a/samples/landlock/sandboxer.c b/samples/landlock/sandboxer.c
index 66e56ae275c6..ac71019e6212 100644
--- a/samples/landlock/sandboxer.c
+++ b/samples/landlock/sandboxer.c
@@ -58,10 +58,15 @@ static inline int landlock_restrict_self(const int ruleset_fd,
#define ENV_FS_RO_NAME "LL_FS_RO"
#define ENV_FS_RW_NAME "LL_FS_RW"
+#define ENV_FS_QUIET_NAME "LL_FS_QUIET"
#define ENV_TCP_BIND_NAME "LL_TCP_BIND"
#define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
+#define ENV_NET_QUIET_NAME "LL_NET_QUIET"
#define ENV_SCOPED_NAME "LL_SCOPED"
+#define ENV_QUIET_ACCESS_NAME "LL_QUIET_ACCESS"
#define ENV_FORCE_LOG_NAME "LL_FORCE_LOG"
+#define ENV_UDP_BIND_NAME "LL_UDP_BIND"
+#define ENV_UDP_CONNECT_SEND_NAME "LL_UDP_CONNECT_SEND"
#define ENV_DELIMITER ":"
static int str2num(const char *numstr, __u64 *num_dst)
@@ -117,7 +122,7 @@ static int parse_path(char *env_path, const char ***const path_list)
/* clang-format on */
static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
- const __u64 allowed_access)
+ const __u64 allowed_access, __u32 flags)
{
int num_paths, i, ret = 1;
char *env_path_name;
@@ -167,7 +172,7 @@ static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
if (!S_ISDIR(statbuf.st_mode))
path_beneath.allowed_access &= ACCESS_FILE;
if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0)) {
+ &path_beneath, flags)) {
fprintf(stderr,
"Failed to update the ruleset with \"%s\": %s\n",
path_list[i], strerror(errno));
@@ -185,7 +190,7 @@ out_free_name:
}
static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
- const __u64 allowed_access)
+ const __u64 allowed_access, __u32 flags)
{
int ret = 1;
char *env_port_name, *env_port_name_next, *strport;
@@ -213,7 +218,7 @@ static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
}
net_port.port = port;
if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
- &net_port, 0)) {
+ &net_port, flags)) {
fprintf(stderr,
"Failed to update the ruleset with port \"%llu\": %s\n",
net_port.port, strerror(errno));
@@ -301,7 +306,70 @@ out_unset:
/* clang-format on */
-#define LANDLOCK_ABI_LAST 9
+/*
+ * Parses ENV_QUIET_ACCESS_NAME and sets the quiet_access_fs, quiet_access_net
+ * and quiet_scoped masks of @ruleset_attr accordingly.
+ */
+static int add_quiet_access(const char *const env_var,
+ struct landlock_ruleset_attr *const ruleset_attr)
+{
+ char *env_quiet_access, *env_quiet_access_next, *str_access;
+
+ env_quiet_access = getenv(env_var);
+ if (!env_quiet_access)
+ return 0;
+
+ env_quiet_access = strdup(env_quiet_access);
+ env_quiet_access_next = env_quiet_access;
+ unsetenv(env_var);
+
+ while ((str_access = strsep(&env_quiet_access_next, ENV_DELIMITER))) {
+ if (strcmp(str_access, "") == 0)
+ continue;
+ else if (strcmp(str_access, "all") == 0) {
+ ruleset_attr->quiet_access_fs =
+ ruleset_attr->handled_access_fs;
+ ruleset_attr->quiet_access_net =
+ ruleset_attr->handled_access_net;
+ ruleset_attr->quiet_scoped = ruleset_attr->scoped;
+ } else if (strcmp(str_access, "read") == 0)
+ ruleset_attr->quiet_access_fs |= ACCESS_FS_ROUGHLY_READ;
+ else if (strcmp(str_access, "write") == 0)
+ ruleset_attr->quiet_access_fs |=
+ ACCESS_FS_ROUGHLY_WRITE;
+ else if (strcmp(str_access, "tcp_bind") == 0)
+ ruleset_attr->quiet_access_net |=
+ LANDLOCK_ACCESS_NET_BIND_TCP;
+ else if (strcmp(str_access, "tcp_connect") == 0)
+ ruleset_attr->quiet_access_net |=
+ LANDLOCK_ACCESS_NET_CONNECT_TCP;
+ else if (strcmp(str_access, "udp_bind") == 0)
+ ruleset_attr->quiet_access_net |=
+ LANDLOCK_ACCESS_NET_BIND_UDP;
+ else if (strcmp(str_access, "udp_connect") == 0)
+ ruleset_attr->quiet_access_net |=
+ LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
+ else if (strcmp(str_access, "abstract_unix_socket") == 0)
+ ruleset_attr->quiet_scoped |=
+ LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET;
+ else if (strcmp(str_access, "signal") == 0)
+ ruleset_attr->quiet_scoped |= LANDLOCK_SCOPE_SIGNAL;
+ else {
+ fprintf(stderr, "Unknown quiet access \"%s\"\n",
+ str_access);
+ free(env_quiet_access);
+ return -1;
+ }
+ }
+
+ free(env_quiet_access);
+ ruleset_attr->quiet_access_fs &= ruleset_attr->handled_access_fs;
+ ruleset_attr->quiet_access_net &= ruleset_attr->handled_access_net;
+ ruleset_attr->quiet_scoped &= ruleset_attr->scoped;
+ return 0;
+}
+
+#define LANDLOCK_ABI_LAST 10
#define XSTR(s) #s
#define STR(s) XSTR(s)
@@ -324,18 +392,37 @@ static const char help[] =
"means an empty list):\n"
"* " ENV_TCP_BIND_NAME ": ports allowed to bind (server)\n"
"* " ENV_TCP_CONNECT_NAME ": ports allowed to connect (client)\n"
+ "* " ENV_UDP_BIND_NAME ": local UDP ports allowed to bind (server: "
+ "prepare to receive on port / client: set as source port)\n"
+ "* " ENV_UDP_CONNECT_SEND_NAME ": remote UDP ports allowed to connect "
+ "or send to (client: use as destination port / server: receive only from it)\n"
+ "(caution: sending requires being able to bind to a local source port)\n"
"* " ENV_SCOPED_NAME ": actions denied on the outside of the landlock domain\n"
" - \"a\" to restrict opening abstract unix sockets\n"
" - \"s\" to restrict sending signals\n"
"\n"
"A sandboxer should not log denied access requests to avoid spamming logs, "
"but to test audit we can set " ENV_FORCE_LOG_NAME "=1\n"
+ ENV_FS_QUIET_NAME " and " ENV_NET_QUIET_NAME ", both optional, can then be used "
+ "to make access to some denied paths or network ports not trigger audit logging.\n"
+ ENV_QUIET_ACCESS_NAME " can be used to specify which accesses should be quieted "
+ "(required when " ENV_FS_QUIET_NAME " or " ENV_NET_QUIET_NAME " is set):\n"
+ " - \"all\" to quiet all of the accesses below\n"
+ " - \"read\" to quiet all file/dir read accesses\n"
+ " - \"write\" to quiet all file/dir write accesses\n"
+ " - \"tcp_bind\" to quiet tcp bind denials\n"
+ " - \"tcp_connect\" to quiet tcp connect denials\n"
+ " - \"udp_bind\" to quiet udp bind denials\n"
+ " - \"udp_connect\" to quiet udp connect / send denials\n"
+ " - \"abstract_unix_socket\" to quiet abstract unix socket denials\n"
+ " - \"signal\" to quiet signal denials\n"
"\n"
"Example:\n"
ENV_FS_RO_NAME "=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
ENV_FS_RW_NAME "=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
ENV_TCP_BIND_NAME "=\"9418\" "
ENV_TCP_CONNECT_NAME "=\"80:443\" "
+ ENV_UDP_CONNECT_SEND_NAME "=\"53\" "
ENV_SCOPED_NAME "=\"a:s\" "
"%1$s bash -i\n"
"\n"
@@ -356,10 +443,16 @@ int main(const int argc, char *const argv[], char *const *const envp)
struct landlock_ruleset_attr ruleset_attr = {
.handled_access_fs = access_fs_rw,
.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
- LANDLOCK_ACCESS_NET_CONNECT_TCP,
+ LANDLOCK_ACCESS_NET_CONNECT_TCP |
+ LANDLOCK_ACCESS_NET_BIND_UDP |
+ LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP,
.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
LANDLOCK_SCOPE_SIGNAL,
+ .quiet_access_fs = 0,
+ .quiet_access_net = 0,
+ .quiet_scoped = 0,
};
+ bool quiet_supported = true;
int supported_restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON;
int set_restrict_flags = 0;
@@ -444,6 +537,15 @@ int main(const int argc, char *const argv[], char *const *const envp)
/* Removes LANDLOCK_ACCESS_FS_RESOLVE_UNIX for ABI < 9 */
ruleset_attr.handled_access_fs &=
~LANDLOCK_ACCESS_FS_RESOLVE_UNIX;
+ __attribute__((fallthrough));
+ case 9:
+ /* Removes UDP support for ABI < 10 */
+ ruleset_attr.handled_access_net &=
+ ~(LANDLOCK_ACCESS_NET_BIND_UDP |
+ LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
+ /* Removes quiet flags for ABI < 10 later on. */
+ quiet_supported = false;
+
/* Must be printed for any ABI < LANDLOCK_ABI_LAST. */
fprintf(stderr,
"Hint: You should update the running kernel "
@@ -475,6 +577,18 @@ int main(const int argc, char *const argv[], char *const *const envp)
ruleset_attr.handled_access_net &=
~LANDLOCK_ACCESS_NET_CONNECT_TCP;
}
+ /* Removes UDP bind access control if not supported by a user. */
+ env_port_name = getenv(ENV_UDP_BIND_NAME);
+ if (!env_port_name) {
+ ruleset_attr.handled_access_net &=
+ ~LANDLOCK_ACCESS_NET_BIND_UDP;
+ }
+ /* Removes UDP connect/send access control if not supported by a user. */
+ env_port_name = getenv(ENV_UDP_CONNECT_SEND_NAME);
+ if (!env_port_name) {
+ ruleset_attr.handled_access_net &=
+ ~LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
+ }
if (check_ruleset_scope(ENV_SCOPED_NAME, &ruleset_attr))
return 1;
@@ -497,6 +611,25 @@ int main(const int argc, char *const argv[], char *const *const envp)
unsetenv(ENV_FORCE_LOG_NAME);
}
+ /* Set the quiet access masks. */
+ if (quiet_supported) {
+ if ((getenv(ENV_FS_QUIET_NAME) || getenv(ENV_NET_QUIET_NAME)) &&
+ !getenv(ENV_QUIET_ACCESS_NAME)) {
+ fprintf(stderr,
+ "%s must be set (e.g. to \"all\") when %s or %s is used\n",
+ ENV_QUIET_ACCESS_NAME, ENV_FS_QUIET_NAME,
+ ENV_NET_QUIET_NAME);
+ return 1;
+ }
+ if (add_quiet_access(ENV_QUIET_ACCESS_NAME, &ruleset_attr))
+ return 1;
+ } else if (getenv(ENV_FS_QUIET_NAME) || getenv(ENV_NET_QUIET_NAME) ||
+ getenv(ENV_QUIET_ACCESS_NAME)) {
+ fprintf(stderr,
+ "Quiet flags not supported by current kernel\n");
+ return 1;
+ }
+
ruleset_fd =
landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
if (ruleset_fd < 0) {
@@ -504,21 +637,41 @@ int main(const int argc, char *const argv[], char *const *const envp)
return 1;
}
- if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro)) {
+ if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro, 0))
goto err_close_ruleset;
- }
- if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw)) {
+ if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw, 0))
goto err_close_ruleset;
+
+ /* Don't require this env to be present. */
+ if (quiet_supported && getenv(ENV_FS_QUIET_NAME)) {
+ if (populate_ruleset_fs(ENV_FS_QUIET_NAME, ruleset_fd, 0,
+ LANDLOCK_ADD_RULE_QUIET))
+ goto err_close_ruleset;
}
if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd,
- LANDLOCK_ACCESS_NET_BIND_TCP)) {
+ LANDLOCK_ACCESS_NET_BIND_TCP, 0)) {
goto err_close_ruleset;
}
if (populate_ruleset_net(ENV_TCP_CONNECT_NAME, ruleset_fd,
- LANDLOCK_ACCESS_NET_CONNECT_TCP)) {
+ LANDLOCK_ACCESS_NET_CONNECT_TCP, 0)) {
+ goto err_close_ruleset;
+ }
+ if (populate_ruleset_net(ENV_UDP_BIND_NAME, ruleset_fd,
+ LANDLOCK_ACCESS_NET_BIND_UDP, 0)) {
goto err_close_ruleset;
}
+ if (populate_ruleset_net(ENV_UDP_CONNECT_SEND_NAME, ruleset_fd,
+ LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP, 0)) {
+ goto err_close_ruleset;
+ }
+
+ if (quiet_supported) {
+ if (populate_ruleset_net(ENV_NET_QUIET_NAME, ruleset_fd, 0,
+ LANDLOCK_ADD_RULE_QUIET)) {
+ goto err_close_ruleset;
+ }
+ }
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
perror("Failed to restrict privileges");
diff --git a/samples/rust/rust_debugfs.rs b/samples/rust/rust_debugfs.rs
index 0963efe19f93..1f59e08aaa4b 100644
--- a/samples/rust/rust_debugfs.rs
+++ b/samples/rust/rust_debugfs.rs
@@ -117,13 +117,14 @@ kernel::acpi_device_table!(
impl platform::Driver for RustDebugFs {
type IdInfo = ();
+ type Data<'bound> = Self;
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
- fn probe(
- pdev: &platform::Device<Core>,
- _info: Option<&Self::IdInfo>,
- ) -> impl PinInit<Self, Error> {
+ fn probe<'bound>(
+ pdev: &'bound platform::Device<Core<'_>>,
+ _info: Option<&'bound Self::IdInfo>,
+ ) -> impl PinInit<Self, Error> + 'bound {
RustDebugFs::new(pdev).pin_chain(|this| {
this.counter.store(91, Relaxed);
{
@@ -146,7 +147,7 @@ impl RustDebugFs {
dir.read_write_file(c"pair", new_mutex!(Inner { x: 3, y: 10 }))
}
- fn new(pdev: &platform::Device<Core>) -> impl PinInit<Self, Error> + '_ {
+ fn new<'a>(pdev: &'a platform::Device<Core<'_>>) -> impl PinInit<Self, Error> + 'a {
let debugfs = Dir::new(c"sample_debugfs");
let dev = pdev.as_ref();
diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs
index 129bb4b39c04..5046b4628d0e 100644
--- a/samples/rust/rust_dma.rs
+++ b/samples/rust/rust_dma.rs
@@ -58,9 +58,13 @@ kernel::pci_device_table!(
impl pci::Driver for DmaSampleDriver {
type IdInfo = ();
+ type Data<'bound> = Self;
const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
- fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
+ fn probe<'bound>(
+ pdev: &'bound pci::Device<Core<'_>>,
+ _info: &'bound Self::IdInfo,
+ ) -> impl PinInit<Self, Error> + 'bound {
pin_init::pin_init_scope(move || {
dev_info!(pdev, "Probe DMA test driver.\n");
@@ -73,7 +77,7 @@ impl pci::Driver for DmaSampleDriver {
Coherent::zeroed_slice(pdev.as_ref(), TEST_VALUES.len(), GFP_KERNEL)?;
for (i, value) in TEST_VALUES.into_iter().enumerate() {
- kernel::dma_write!(ca, [i]?, MyStruct::new(value.0, value.1));
+ kernel::dma_write!(ca, [try: i], MyStruct::new(value.0, value.1));
}
let size = 4 * page::PAGE_SIZE;
@@ -91,16 +95,14 @@ impl pci::Driver for DmaSampleDriver {
}
impl DmaSampleDriver {
- fn check_dma(&self) -> Result {
+ fn check_dma(&self) {
for (i, value) in TEST_VALUES.into_iter().enumerate() {
- let val0 = kernel::dma_read!(self.ca, [i]?.h);
- let val1 = kernel::dma_read!(self.ca, [i]?.b);
+ let val0 = kernel::dma_read!(self.ca, [panic: i].h);
+ let val1 = kernel::dma_read!(self.ca, [panic: i].b);
assert_eq!(val0, value.0);
assert_eq!(val1, value.1);
}
-
- Ok(())
}
}
@@ -109,7 +111,7 @@ impl PinnedDrop for DmaSampleDriver {
fn drop(self: Pin<&mut Self>) {
dev_info!(self.pdev, "Unload DMA test driver.\n");
- assert!(self.check_dma().is_ok());
+ self.check_dma();
for (i, entry) in self.sgt.iter().enumerate() {
dev_info!(
diff --git a/samples/rust/rust_driver_auxiliary.rs b/samples/rust/rust_driver_auxiliary.rs
index 5c5a5105a3ff..2c1351040e45 100644
--- a/samples/rust/rust_driver_auxiliary.rs
+++ b/samples/rust/rust_driver_auxiliary.rs
@@ -10,15 +10,13 @@ use kernel::{
Bound,
Core, //
},
- devres::Devres,
driver,
pci,
prelude::*,
+ types::ForLt,
InPlaceModule, //
};
-use core::any::TypeId;
-
const MODULE_NAME: &CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
const AUXILIARY_NAME: &CStr = c"auxiliary";
@@ -33,10 +31,14 @@ kernel::auxiliary_device_table!(
impl auxiliary::Driver for AuxiliaryDriver {
type IdInfo = ();
+ type Data<'bound> = Self;
const ID_TABLE: auxiliary::IdTable<Self::IdInfo> = &AUX_TABLE;
- fn probe(adev: &auxiliary::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
+ fn probe<'bound>(
+ adev: &'bound auxiliary::Device<Core<'_>>,
+ _info: &'bound Self::IdInfo,
+ ) -> impl PinInit<Self, Error> + 'bound {
dev_info!(
adev,
"Probing auxiliary driver for auxiliary device with id={}\n",
@@ -49,13 +51,17 @@ impl auxiliary::Driver for AuxiliaryDriver {
}
}
-#[pin_data]
-struct ParentDriver {
- private: TypeId,
- #[pin]
- _reg0: Devres<auxiliary::Registration>,
- #[pin]
- _reg1: Devres<auxiliary::Registration>,
+struct Data<'bound> {
+ index: u32,
+ parent: &'bound pci::Device<Bound>,
+}
+
+struct ParentDriver;
+
+#[allow(clippy::type_complexity)]
+struct ParentData<'bound> {
+ _reg0: auxiliary::Registration<'bound, ForLt!(Data<'_>)>,
+ _reg1: auxiliary::Registration<'bound, ForLt!(Data<'_>)>,
}
kernel::pci_device_table!(
@@ -67,26 +73,53 @@ kernel::pci_device_table!(
impl pci::Driver for ParentDriver {
type IdInfo = ();
+ type Data<'bound> = ParentData<'bound>;
const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
- fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
- try_pin_init!(Self {
- private: TypeId::of::<Self>(),
- _reg0 <- auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 0, MODULE_NAME),
- _reg1 <- auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 1, MODULE_NAME),
+ fn probe<'bound>(
+ pdev: &'bound pci::Device<Core<'_>>,
+ _info: &'bound Self::IdInfo,
+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
+ Ok(ParentData {
+ // SAFETY: `ParentData` is the driver's private data, which is dropped when the
+ // device is unbound; i.e. `mem::forget()` is never called on it.
+ _reg0: unsafe {
+ auxiliary::Registration::new_with_lt(
+ pdev.as_ref(),
+ AUXILIARY_NAME,
+ 0,
+ MODULE_NAME,
+ Data {
+ index: 0,
+ parent: pdev,
+ },
+ )?
+ },
+ // SAFETY: See `_reg0` above.
+ _reg1: unsafe {
+ auxiliary::Registration::new_with_lt(
+ pdev.as_ref(),
+ AUXILIARY_NAME,
+ 1,
+ MODULE_NAME,
+ Data {
+ index: 1,
+ parent: pdev,
+ },
+ )?
+ },
})
}
}
impl ParentDriver {
fn connect(adev: &auxiliary::Device<Bound>) -> Result {
- let dev = adev.parent();
- let pdev: &pci::Device<Bound> = dev.try_into()?;
- let drvdata = dev.drvdata::<Self>()?;
+ let data = adev.registration_data::<ForLt!(Data<'_>)>()?;
+ let pdev = data.parent;
dev_info!(
- dev,
+ pdev,
"Connect auxiliary {} with parent: VendorID={}, DeviceID={:#x}\n",
adev.id(),
pdev.vendor_id(),
@@ -94,9 +127,9 @@ impl ParentDriver {
);
dev_info!(
- dev,
- "We have access to the private data of {:?}.\n",
- drvdata.private
+ pdev,
+ "Connected to auxiliary device with index {}.\n",
+ data.index
);
Ok(())
diff --git a/samples/rust/rust_driver_i2c.rs b/samples/rust/rust_driver_i2c.rs
index 6be79f9e9fb5..ead8263a7d48 100644
--- a/samples/rust/rust_driver_i2c.rs
+++ b/samples/rust/rust_driver_i2c.rs
@@ -35,15 +35,16 @@ kernel::of_device_table! {
impl i2c::Driver for SampleDriver {
type IdInfo = u32;
+ type Data<'bound> = Self;
const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE);
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
- fn probe(
- idev: &i2c::I2cClient<Core>,
- info: Option<&Self::IdInfo>,
- ) -> impl PinInit<Self, Error> {
+ fn probe<'bound>(
+ idev: &'bound i2c::I2cClient<Core<'_>>,
+ info: Option<&'bound Self::IdInfo>,
+ ) -> impl PinInit<Self, Error> + 'bound {
let dev = idev.as_ref();
dev_info!(dev, "Probe Rust I2C driver sample.\n");
@@ -55,11 +56,11 @@ impl i2c::Driver for SampleDriver {
Ok(Self)
}
- fn shutdown(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) {
+ fn shutdown<'bound>(idev: &'bound i2c::I2cClient<Core<'_>>, _this: Pin<&Self>) {
dev_info!(idev.as_ref(), "Shutdown Rust I2C driver sample.\n");
}
- fn unbind(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) {
+ fn unbind<'bound>(idev: &'bound i2c::I2cClient<Core<'_>>, _this: Pin<&Self>) {
dev_info!(idev.as_ref(), "Unbind Rust I2C driver sample.\n");
}
}
diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci.rs
index 47d3e84fab63..1aa8197d8698 100644
--- a/samples/rust/rust_driver_pci.rs
+++ b/samples/rust/rust_driver_pci.rs
@@ -9,7 +9,6 @@ use kernel::{
Bound,
Core, //
},
- devres::Devres,
io::{
register,
register::Array,
@@ -17,8 +16,7 @@ use kernel::{
},
num::Bounded,
pci,
- prelude::*,
- sync::aref::ARef, //
+ prelude::*, //
};
mod regs {
@@ -45,7 +43,7 @@ mod regs {
pub(super) const END: usize = 0x10;
}
-type Bar0 = pci::Bar<{ regs::END }>;
+type Bar0<'bound> = pci::Bar<'bound, { regs::END }>;
#[derive(Copy, Clone, Debug)]
struct TestIndex(u8);
@@ -66,14 +64,14 @@ impl TestIndex {
const NO_EVENTFD: Self = Self(0);
}
-#[pin_data(PinnedDrop)]
-struct SampleDriver {
- pdev: ARef<pci::Device>,
- #[pin]
- bar: Devres<Bar0>,
+struct SampleDriverData<'bound> {
+ pdev: &'bound pci::Device,
+ bar: Bar0<'bound>,
index: TestIndex,
}
+struct SampleDriver;
+
kernel::pci_device_table!(
PCI_TABLE,
MODULE_PCI_TABLE,
@@ -84,8 +82,8 @@ kernel::pci_device_table!(
)]
);
-impl SampleDriver {
- fn testdev(index: &TestIndex, bar: &Bar0) -> Result<u32> {
+impl SampleDriverData<'_> {
+ fn testdev(index: &TestIndex, bar: &Bar0<'_>) -> Result<u32> {
// Select the test.
bar.write_reg(regs::TEST::zeroed().with_index(*index));
@@ -140,51 +138,49 @@ impl SampleDriver {
impl pci::Driver for SampleDriver {
type IdInfo = TestIndex;
+ type Data<'bound> = SampleDriverData<'bound>;
const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
- fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> impl PinInit<Self, Error> {
- pin_init::pin_init_scope(move || {
- let vendor = pdev.vendor_id();
- dev_dbg!(
- pdev,
- "Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n",
- vendor,
- pdev.device_id()
- );
-
- pdev.enable_device_mem()?;
- pdev.set_master();
-
- Ok(try_pin_init!(Self {
- bar <- pdev.iomap_region_sized::<{ regs::END }>(0, c"rust_driver_pci"),
- index: *info,
- _: {
- let bar = bar.access(pdev.as_ref())?;
-
- dev_info!(
- pdev,
- "pci-testdev data-match count: {}\n",
- Self::testdev(info, bar)?
- );
- Self::config_space(pdev);
- },
- pdev: pdev.into(),
- }))
+ fn probe<'bound>(
+ pdev: &'bound pci::Device<Core<'_>>,
+ info: &'bound Self::IdInfo,
+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
+ let vendor = pdev.vendor_id();
+ dev_dbg!(
+ pdev,
+ "Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n",
+ vendor,
+ pdev.device_id()
+ );
+
+ pdev.enable_device_mem()?;
+ pdev.set_master();
+
+ let bar = pdev.iomap_region_sized::<{ regs::END }>(0, c"rust_driver_pci")?;
+
+ dev_info!(
+ pdev,
+ "pci-testdev data-match count: {}\n",
+ SampleDriverData::testdev(info, &bar)?
+ );
+ SampleDriverData::config_space(pdev);
+
+ Ok(SampleDriverData {
+ pdev,
+ bar,
+ index: *info,
})
}
- fn unbind(pdev: &pci::Device<Core>, this: Pin<&Self>) {
- if let Ok(bar) = this.bar.access(pdev.as_ref()) {
- // Reset pci-testdev by writing a new test index.
- bar.write_reg(regs::TEST::zeroed().with_index(this.index));
- }
+ fn unbind<'bound>(_pdev: &'bound pci::Device<Core<'_>>, this: Pin<&Self::Data<'bound>>) {
+ this.bar
+ .write_reg(regs::TEST::zeroed().with_index(this.index));
}
}
-#[pinned_drop]
-impl PinnedDrop for SampleDriver {
- fn drop(self: Pin<&mut Self>) {
+impl Drop for SampleDriverData<'_> {
+ fn drop(&mut self) {
dev_dbg!(self.pdev, "Remove Rust PCI driver sample.\n");
}
}
diff --git a/samples/rust/rust_driver_platform.rs b/samples/rust/rust_driver_platform.rs
index f2229d176fb9..ec0d6cac4f57 100644
--- a/samples/rust/rust_driver_platform.rs
+++ b/samples/rust/rust_driver_platform.rs
@@ -101,13 +101,14 @@ kernel::acpi_device_table!(
impl platform::Driver for SampleDriver {
type IdInfo = Info;
+ type Data<'bound> = Self;
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
- fn probe(
- pdev: &platform::Device<Core>,
- info: Option<&Self::IdInfo>,
- ) -> impl PinInit<Self, Error> {
+ fn probe<'bound>(
+ pdev: &'bound platform::Device<Core<'_>>,
+ info: Option<&'bound Self::IdInfo>,
+ ) -> impl PinInit<Self, Error> + 'bound {
let dev = pdev.as_ref();
dev_dbg!(dev, "Probe Rust Platform driver sample.\n");
diff --git a/samples/rust/rust_driver_usb.rs b/samples/rust/rust_driver_usb.rs
index ab72e99e1274..02bd5085f9bc 100644
--- a/samples/rust/rust_driver_usb.rs
+++ b/samples/rust/rust_driver_usb.rs
@@ -26,21 +26,22 @@ kernel::usb_device_table!(
impl usb::Driver for SampleDriver {
type IdInfo = ();
+ type Data<'bound> = Self;
const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
- fn probe(
- intf: &usb::Interface<Core>,
+ fn probe<'bound>(
+ intf: &'bound usb::Interface<Core<'_>>,
_id: &usb::DeviceId,
- _info: &Self::IdInfo,
- ) -> impl PinInit<Self, Error> {
- let dev: &device::Device<Core> = intf.as_ref();
+ _info: &'bound Self::IdInfo,
+ ) -> impl PinInit<Self, Error> + 'bound {
+ let dev: &device::Device<Core<'_>> = intf.as_ref();
dev_info!(dev, "Rust USB driver sample probed\n");
Ok(Self { _intf: intf.into() })
}
- fn disconnect(intf: &usb::Interface<Core>, _data: Pin<&Self>) {
- let dev: &device::Device<Core> = intf.as_ref();
+ fn disconnect<'bound>(intf: &'bound usb::Interface<Core<'_>>, _data: Pin<&Self>) {
+ let dev: &device::Device<Core<'_>> = intf.as_ref();
dev_info!(dev, "Rust USB driver sample disconnected\n");
}
}
diff --git a/samples/rust/rust_i2c_client.rs b/samples/rust/rust_i2c_client.rs
index 8d2c12e535b0..2d876f4e3ee0 100644
--- a/samples/rust/rust_i2c_client.rs
+++ b/samples/rust/rust_i2c_client.rs
@@ -106,13 +106,14 @@ const BOARD_INFO: i2c::I2cBoardInfo =
impl platform::Driver for SampleDriver {
type IdInfo = ();
+ type Data<'bound> = Self;
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
- fn probe(
- pdev: &platform::Device<device::Core>,
- _info: Option<&Self::IdInfo>,
- ) -> impl PinInit<Self, Error> {
+ fn probe<'bound>(
+ pdev: &'bound platform::Device<device::Core<'_>>,
+ _info: Option<&'bound Self::IdInfo>,
+ ) -> impl PinInit<Self, Error> + 'bound {
dev_info!(
pdev.as_ref(),
"Probe Rust I2C Client registration sample.\n"
@@ -129,7 +130,10 @@ impl platform::Driver for SampleDriver {
})
}
- fn unbind(pdev: &platform::Device<device::Core>, _this: Pin<&Self>) {
+ fn unbind<'bound>(
+ pdev: &'bound platform::Device<device::Core<'_>>,
+ _this: Pin<&Self::Data<'bound>>,
+ ) {
dev_info!(
pdev.as_ref(),
"Unbind Rust I2C Client registration sample.\n"
diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
index 87a1fe63533a..41e26c825060 100644
--- a/samples/rust/rust_misc_device.rs
+++ b/samples/rust/rust_misc_device.rs
@@ -97,14 +97,36 @@
use kernel::{
device::Device,
- fs::{File, Kiocb},
- ioctl::{_IO, _IOC_SIZE, _IOR, _IOW},
- iov::{IovIterDest, IovIterSource},
- miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration},
+ fs::{
+ File,
+ Kiocb, //
+ },
+ ioctl::{
+ _IO,
+ _IOC_SIZE,
+ _IOR,
+ _IOW, //
+ },
+ iov::{
+ IovIterDest,
+ IovIterSource, //
+ },
+ miscdevice::{
+ MiscDevice,
+ MiscDeviceOptions,
+ MiscDeviceRegistration, //
+ },
new_mutex,
prelude::*,
- sync::{aref::ARef, Mutex},
- uaccess::{UserSlice, UserSliceReader, UserSliceWriter},
+ sync::{
+ aref::ARef,
+ Mutex, //
+ },
+ uaccess::{
+ UserSlice,
+ UserSliceReader,
+ UserSliceWriter, //
+ },
};
const RUST_MISC_DEV_HELLO: u32 = _IO('|' as u32, 0x80);
diff --git a/samples/rust/rust_soc.rs b/samples/rust/rust_soc.rs
index 8079c1c48416..808d58200eb6 100644
--- a/samples/rust/rust_soc.rs
+++ b/samples/rust/rust_soc.rs
@@ -37,13 +37,14 @@ kernel::acpi_device_table!(
impl platform::Driver for SampleSocDriver {
type IdInfo = ();
+ type Data<'bound> = Self;
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
- fn probe(
- pdev: &platform::Device<Core>,
- _info: Option<&Self::IdInfo>,
- ) -> impl PinInit<Self, Error> {
+ fn probe<'bound>(
+ pdev: &'bound platform::Device<Core<'_>>,
+ _info: Option<&'bound Self::IdInfo>,
+ ) -> impl PinInit<Self, Error> + 'bound {
dev_dbg!(pdev, "Probe Rust SoC driver sample.\n");
let pdev = pdev.into();
diff --git a/samples/trace_events/trace-events-sample.c b/samples/trace_events/trace-events-sample.c
index ecc7db237f2e..0b7a6efdb247 100644
--- a/samples/trace_events/trace-events-sample.c
+++ b/samples/trace_events/trace-events-sample.c
@@ -107,6 +107,10 @@ int foo_bar_reg(void)
* for consistency sake, we still take the thread_mutex.
*/
simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
+ if (IS_ERR_OR_NULL(simple_tsk_fn)) {
+ pr_err("Failed to create simple_thread_fn\n");
+ simple_tsk_fn = NULL;
+ }
out:
mutex_unlock(&thread_mutex);
return 0;