Skip to contentSkip to conversation

module: expose imported namespaces via sysfs

PATCH Superseded Nicholas Sielicki 2 participants 5 messages Jan 8 – Mar 5

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 module: expose imported namespaces via sysfs

Currently, the only way for userspace to determine which symbol namespaces a module imports is to locate the .ko file on disk (which may not match the loaded module), then either parsing the binary manually and handling any potential compression, or shelling-out to modinfo.

This is painful in cases where userspace wants to distinguish between module variants that share the same name but import different namespaces. For example, the nvidia-uvm module exists in both open and closed source variants; the open source version imports the DMA_BUF namespace while the closed source version does not, and networking middleware may want to initialize itself differently depending on that.

Add /sys/module/*/import_ns to expose imported namespaces for loaded modules. The file contains one namespace per line and only exists for modules that import at least one namespace.

---
 Documentation/ABI/testing/sysfs-module |  9 ++++
 include/linux/module.h                 |  1 +
 kernel/module/main.c                   | 62 +++++++++++++++++++++++++-
 3 files changed, 71 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-module +9 -0
index 6bc9af6229f0..d8e2a33fd514 100644
@@ -48,6 +48,15 @@ Contact:	Kay Sievers <[email protected]>
 Description:	Show the initialization state(live, coming, going) of
 		the module.
 
+What:		/sys/module/*/import_ns
+Date:		January 2026
+KernelVersion:	6.20
+Contact:	[email protected]
+Description:	List of symbol namespaces imported by this module via
+		MODULE_IMPORT_NS(). Each namespace appears on a separate line.
+		This file only exists for modules that import at least one
+		namespace.
+
 What:		/sys/module/*/taint
 Date:		Jan 2012
 KernelVersion:	3.3
include/linux/module.h +1 -0
index d80c3ea57472..f1bcca03f90b 100644
@@ -419,6 +419,7 @@ struct module {
 	struct module_attribute *modinfo_attrs;
 	const char *version;
 	const char *srcversion;
+	const char *imported_namespaces;
 	struct kobject *holders_dir;
 
 	/* Exported symbols */
kernel/module/main.c +61 -1

96 lines not on this page · raw message

Signature · 1 line
2.52.0
#1 lore raw

PATCH 2/2 docs: symbol-namespaces: mention sysfs attribute

Reference the new /sys/module/*/import_ns sysfs attribute in docs as an alternative to modinfo for inspecting imported namespaces of loaded modules.

---
 Documentation/core-api/symbol-namespaces.rst | 5 +++++
 1 file changed, 5 insertions(+)
Documentation/core-api/symbol-namespaces.rst +5 -0
index 034898e81ba2..2304d5bffcce 100644
@@ -114,6 +114,11 @@ inspected with modinfo::
 	import_ns:      USB_STORAGE
 	[...]
 
+For modules that are currently loaded, imported namespaces are also available
+via sysfs::
+
+	$ cat /sys/module/ums_karma/import_ns
+	USB_STORAGE
 
 It is advisable to add the MODULE_IMPORT_NS() statement close to other module
 metadata definitions like MODULE_AUTHOR() or MODULE_LICENSE().
Signature · 1 line
2.52.0
maintainer
#2 lore raw
In reply to Nicholas Sielicki

Hi Nicholas,

Quoted 13 lines · Nicholas Sielicki

Currently, the only way for userspace to determine which symbol namespaces a module imports is to locate the .ko file on disk (which may not match the loaded module), then either parsing the binary manually and handling any potential compression, or shelling-out to modinfo.

This is painful in cases where userspace wants to distinguish between module variants that share the same name but import different namespaces. For example, the nvidia-uvm module exists in both open and closed source variants; the open source version imports the DMA_BUF namespace while the closed source version does not, and networking middleware may want to initialize itself differently depending on that.

Quoted 3 lines

Add /sys/module/*/import_ns to expose imported namespaces for loaded modules. The file contains one namespace per line and only exists for modules that import at least one namespace.

The patches look reasonable to me, but I do wonder if examining imported namespaces is the best way to distinguish between module variants. Is looking at /sys/module/*/taint not sufficient for your use case?

Sami

#3 lore raw
Quoted 4 lines · Sami Tolvanen

The patches look reasonable to me, but I do wonder if examining imported namespaces is the best way to distinguish between module variants. Is looking at /sys/module/*/taint not sufficient for your use case?

You're right that the example in the commit message is bad; it's true that the taint bit is strictly mutually exclusive with dmabuf support, at least for the way that this evolved in the nvidia open module. This is a patch I had in the back of my head for a while, and the details had become murkier since.

The actual problem I wanted to solve with this was not with dmabuf being conditionally available on the exporter side (where the taint bit, albeit imprecise, would have worked), but with determining whether the ioctl was present on the import side, which was only true from 5.12 onward for uverbs (commit bfe0cc6).

So on eg: a system with untainted nvidia-open on 5.10, it was viable to export dmabufs from device memory, but ibv_reg_dmabuf_mr would fail at runtime because the ioctl didn't exist yet. The workaround for my specific piece of middleware was just to call uname and check for 5.12+ before advertising dmabuf support back to the caller, but this left me feeling gross.

In reality, this patch doesn't solve that problem, because any system running a new enough kernel to have this patch will already have support on both import/export sides. But it's what made me wish this functionality existed.

Why I still hope you'll take this patch:

1. cheaply exposing module namespace imports under sysfs can be useful beyond this specific situation. The kernel is already tracking this and validating it for other reasons, so it's easy to also just expose it.

2. taint may not always be mutually exclusive with a module namespace import,
eg: in the case of uverbs above.

3. currently, the only way to determine whether a module speaks dmabuf at runtime is to hardcode module-specific priors around the kernel version and/or the taint bit, or to link proprietary vendor libraries and check for things like CU_DEVICE_ATTRIBUTE_DMA_BUF_SUPPORTED. Exposing this under sysfs provides a cheaper way for networking middleware (that doesn't usually allocate device memory itself and largely wishes to just blindly plumb dmabufs fds from callers into some importer in a vendor agnostic way) to determine what modules are in-play during initialization:

Quoted 3 lines
user@host$ grep DMA_BUF /sys/module/*/import_ns
/sys/module/amdgpu/import_ns:DMA_BUF
/sys/module/ib_uverbs/import_ns:DMA_BUF

Critically, it can do this check without needing to link any vendor libraries.

As an example, libfabric currently contains an enum for heterogeneous memory types, spanning ZE/ROCM/CUDA/SYNAPSEAI/NEURON... but libfabric doesn't need to allocate device memory itself (it is provided by the caller, hopefully as a dmabuf), and regardless of what module exported it, all roads will eventually lead back to a generic dmabuf fd import ioctl for the NIC.

Despite that, to prevent memory registration errors at runtime before they happen, it needs to probe support for these memory types at initialization time. Today, that essentially means needing to link a new proprietary vendor library to query dmabuf export support using a vendor-specific API. But without any existing mechanism in the kernel to query known dmabuf importers or exporters, even a suboptimal one, this is what is done in practice.

Best,
Nicholas

maintainer
#4 lore raw
In reply to Nicholas Sielicki
Quoted 8 lines · Nicholas Sielicki
kernel/module/main.c +7 -0
+static ssize_t show_modinfo_import_ns(const struct module_attribute *mattr,
+				      struct module_kobject *mk, char *buffer)
+{
+	return sysfs_emit(buffer, "%s\n", mk->mod->imported_namespaces);
+}
+
+static int modinfo_import_ns_exists(struct module *mod)

kernel/module/main.c static const struct module_attribute modinfo_##field = { \ · line 626

Quoted 10 lines
kernel/module/main.c +10 -0
+{
+	return mod->imported_namespaces != NULL;
+}
+
+static const struct module_attribute modinfo_import_ns = {
+	.attr = { .name = "import_ns", .mode = 0444 },
+	.show = show_modinfo_import_ns,
+	.test = modinfo_import_ns_exists,
+};
+

Don't we need a .setup function that initializes mod->imported_namespaces to NULL? Currently, if setup_modinfo returns an error, the pointer remains initialized to whatever value we read from .gnu.linkonce.this_module, and we'll pass that arbitrary pointer to kfree.

This isn't normally a problem since modpost zero-initializes the field, but we don't want to rely on userspace to initialize our pointers.

Also, define .free to release the buffer instead of adding a direct call to free_modinfo.

Quoted 22 lines

static struct {

kernel/module/main.c +8 -0
 	char name[MODULE_NAME_LEN];
 	char taints[MODULE_FLAGS_BUF_SIZE];
@@ -1058,6 +1075,7 @@ const struct module_attribute *const modinfo_attrs[] = {
 	&module_uevent,
 	&modinfo_version,
 	&modinfo_srcversion,
+	&modinfo_import_ns,
 	&modinfo_initstate,
 	&modinfo_coresize,
 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
@@ -1753,11 +1771,48 @@ static void module_license_taint_check(struct module *mod, const char *license)
 	}
 }
 
+static int copy_modinfo_import_ns(struct module *mod, struct load_info *info)
+{
+	char *ns;
+	size_t len, total_len = 0;
+	char *buf, *p;
+
+	for_each_modinfo_entry(ns, info, "import_ns")

kernel/module/main.c static void module_license_taint_check(struct module *mod, const char *license) · line 1790

Quoted 10 lines
kernel/module/main.c +10 -0
+		total_len += strlen(ns) + 1;
+
+	if (!total_len) {
+		mod->imported_namespaces = NULL;
+		return 0;
+	}
+
+	buf = kmalloc(total_len, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;

For example, if kmalloc fails, mod->imported_namespaces isn't initialized.

Quoted 8 lines
kernel/module/main.c +8 -0
+
+	p = buf;
+	for_each_modinfo_entry(ns, info, "import_ns") {
+		len = strlen(ns);
+		memcpy(p, ns, len);
+		p += len;
+		*p++ = '\n';
+	}

kernel/module/main.c static void module_license_taint_check(struct module *mod, const char *license) · line 1808

Quoted 10 lines
kernel/module/main.c +10 -0
+	/* Replace trailing newline with null terminator. */
+	*(p - 1) = '\0';
+
+	mod->imported_namespaces = buf;
+	return 0;
+}
+
+static void free_modinfo_import_ns(struct module *mod)
+{
+	kfree(mod->imported_namespaces);

mod->imported_namespaces = NULL;

Quoted 12 lines
kernel/module/main.c +3 -1
+}
+
 static int setup_modinfo(struct module *mod, struct load_info *info)
 {
 	const struct module_attribute *attr;
 	char *imported_namespace;
-	int i;
+	int i, err;
 
 	for (i = 0; (attr = modinfo_attrs[i]); i++) {
 		if (attr->setup)

kernel/module/main.c static int setup_modinfo(struct module *mod, struct load_info *info) · line 1832

Quoted 2 lines
kernel/module/main.c +0 -0
@@ -1776,6 +1831,10 @@ static int setup_modinfo(struct module *mod, struct load_info *info)
 		}
 	}

Also setup_modinfo can fail before copy_modinfo_import_ns is even called.

kernel/module/main.c static int setup_modinfo(struct module *mod, struct load_info *info) · line 1837

Quoted 4 lines
kernel/module/main.c +4 -0
+	err = copy_modinfo_import_ns(mod, info);
+	if (err)
+		return err;
+

Sami