From bbeefddf88176098f541cdf12187408eb2cfa1c8 Mon Sep 17 00:00:00 2001 From: Vlad-Gabriel Serbu Date: Wed, 20 May 2026 11:48:44 +0100 Subject: [PATCH 1/2] Change to kcompat system Signed-off-by: Vlad-Gabriel Serbu --- .gitignore | 3 ++ driver/Makefile | 25 ++++++++++- driver/kcompat/module_import_ns_token.c | 30 +++++++++++++ driver/kcompat/probe.sh | 53 +++++++++++++++++++++++ driver/kcompat/vm_flags_set.c | 19 +++++++++ driver/slash_compat.h | 56 +++++++++++++++++++++++++ driver/slash_ctldev.c | 1 - driver/slash_dmabuf.c | 11 ++--- driver/slash_main.c | 8 +--- packaging/debian/slash-dkms.install | 1 + packaging/rpm/slash.spec | 2 + 11 files changed, 192 insertions(+), 17 deletions(-) create mode 100644 driver/kcompat/module_import_ns_token.c create mode 100755 driver/kcompat/probe.sh create mode 100644 driver/kcompat/vm_flags_set.c create mode 100644 driver/slash_compat.h diff --git a/.gitignore b/.gitignore index 7deb1aed..07e46f38 100644 --- a/.gitignore +++ b/.gitignore @@ -64,6 +64,9 @@ __pycache__/ *.pyc .venv/ +# Kernel module kcompat probe scratch (driver/kcompat/probe.sh) +driver/kcompat/.scratch/ + # Build files for package /pbuild/ /rpmbuild/ diff --git a/driver/Makefile b/driver/Makefile index 4e06c084..98a56815 100644 --- a/driver/Makefile +++ b/driver/Makefile @@ -44,6 +44,14 @@ endif SLASH_QDMA_OP_DEBUG ?= 0 +# Kcompat feature flags. Defaults are "n"; the all: recipe runs +# driver/kcompat/probe.sh against $(KDIR) to detect the actual values +# and passes them into the kbuild recursion. Each pair (modern API + +# legacy fallback) is covered by one probe — if the modern form is +# absent, the legacy form is the unconditional fallback in slash_compat.h. +SLASH_HAVE_VM_FLAGS_SET ?= n +SLASH_HAVE_MODULE_IMPORT_NS_TOKEN ?= n + # Set GCOV=1 to instrument the module for kernel gcov coverage. # Not set by default — never enable this in production builds. ifdef GCOV @@ -66,6 +74,14 @@ ccflags-y += \ -DSLASH_QDMA_OP_DEBUG=$(SLASH_QDMA_OP_DEBUG) \ -DSLASH_VERSION_STR=\"$(SLASH_VERSION)\" +ifeq ($(SLASH_HAVE_VM_FLAGS_SET),y) +ccflags-y += -DSLASH_HAVE_VM_FLAGS_SET +endif + +ifeq ($(SLASH_HAVE_MODULE_IMPORT_NS_TOKEN),y) +ccflags-y += -DSLASH_HAVE_MODULE_IMPORT_NS_TOKEN +endif + LIBQDMA_OBJS := \ $(LIBQDMA_PATH)/qdma_mbox.o \ @@ -102,11 +118,16 @@ QDMA_ACCESS_OBJS := \ $(MODULE)-objs += $(LIBQDMA_OBJS) $(QDMA_ACCESS_OBJS) +KCOMPAT := "$(SHELL)" "$(PWD)/kcompat/probe.sh" + all: - $(MAKE) -C $(KDIR) M=$(PWD) modules + @flags="$$($(KCOMPAT) "$(KDIR)" | tr '\n' ' ')"; \ + echo "slash: kcompat: $$flags"; \ + $(MAKE) -C "$(KDIR)" M="$(PWD)" $$flags modules clean: - $(MAKE) -C $(KDIR) M=$(PWD) clean + $(MAKE) -C "$(KDIR)" M="$(PWD)" clean + rm -rf "$(PWD)/kcompat/.scratch" install: all sudo install -d -m 755 /lib/modules/$(shell uname -r)/extra diff --git a/driver/kcompat/module_import_ns_token.c b/driver/kcompat/module_import_ns_token.c new file mode 100644 index 00000000..7ba56b0c --- /dev/null +++ b/driver/kcompat/module_import_ns_token.c @@ -0,0 +1,30 @@ +/* + * Token-form MODULE_IMPORT_NS probe. + * + * Pre-6.13: MODULE_IMPORT_NS(ns) = MODULE_INFO(import_ns, __stringify(ns)) + * -> token form is the documented usage. + * 6.13+: MODULE_IMPORT_NS(ns) = MODULE_INFO(import_ns, ns) + * -> token form fails to compile (DMA_BUF undefined). + * + * So this probe succeeds iff the token form is the right one to use. + * Compile success on a pre-6.13 kernel that still accepts the string + * form silently produces the wrong namespace string at runtime, which + * is why we probe the token form (precise) instead of the string form + * (ambiguous on older kernels). + */ +#include +#include + +static int __init conftest_init(void) +{ + return 0; +} + +static void __exit conftest_exit(void) +{ +} + +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS(DMA_BUF); +module_init(conftest_init); +module_exit(conftest_exit); diff --git a/driver/kcompat/probe.sh b/driver/kcompat/probe.sh new file mode 100755 index 00000000..fcaddc23 --- /dev/null +++ b/driver/kcompat/probe.sh @@ -0,0 +1,53 @@ +#!/bin/sh +# Probe the kernel build at $1 for SLASH API compatibility features. +# +# Each *.c file in this directory is built as a tiny standalone module +# against the target kernel headers; a successful build means the +# feature is available. One make-style assignment per feature is +# printed to stdout, e.g.: +# +# SLASH_HAVE_VM_FLAGS_SET=y +# SLASH_HAVE_MODULE_IMPORT_NS_STRING=n +# +# To add a new probe, drop another conftest .c file into this directory. +# The macro name is derived from the file basename (uppercased). + +set -eu + +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " >&2 + exit 2 +fi + +kdir="$1" +here="$(cd "$(dirname "$0")" && pwd)" + +if [ ! -d "$kdir" ]; then + echo "$0: kernel build dir '$kdir' not found" >&2 + exit 1 +fi + +scratch="$here/.scratch" +cleanup() { rm -rf "$scratch"; } +trap cleanup EXIT HUP INT TERM + +# Conftest builds must actually compile to be meaningful. Drop any +# flags the parent make may have set (notably -n / --dry-run, which +# would make every probe look successful but produce no real result). +unset MAKEFLAGS MFLAGS MAKEOVERRIDES + +rm -rf "$scratch" +mkdir -p "$scratch" +printf 'obj-m := conftest.o\n' > "$scratch/Makefile" + +for src in "$here"/*.c; do + [ -f "$src" ] || continue + feat=$(basename "$src" .c) + cp "$src" "$scratch/conftest.c" + if "${MAKE:-make}" -s -C "$kdir" M="$scratch" modules >/dev/null 2>&1; then + ans=y + else + ans=n + fi + printf 'SLASH_HAVE_%s=%s\n' "$(printf '%s' "$feat" | tr '[:lower:]' '[:upper:]')" "$ans" +done diff --git a/driver/kcompat/vm_flags_set.c b/driver/kcompat/vm_flags_set.c new file mode 100644 index 00000000..4149c46c --- /dev/null +++ b/driver/kcompat/vm_flags_set.c @@ -0,0 +1,19 @@ +#include +#include +#include + +static int __init conftest_init(void) +{ + struct vm_area_struct *vma = NULL; + + vm_flags_set(vma, (vm_flags_t)0); + return 0; +} + +static void __exit conftest_exit(void) +{ +} + +MODULE_LICENSE("GPL"); +module_init(conftest_init); +module_exit(conftest_exit); diff --git a/driver/slash_compat.h b/driver/slash_compat.h new file mode 100644 index 00000000..5b3a50c2 --- /dev/null +++ b/driver/slash_compat.h @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved. + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; if + * not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#ifndef SLASH_COMPAT_H +#define SLASH_COMPAT_H + +#include +#include + +/* + * Compat shims selected by the kcompat probes in driver/kcompat/. + * If the modern form is detected (SLASH_HAVE_*), use it; otherwise + * fall back to the legacy form. The probes are exhaustive, so no + * #error path is needed. + */ + +static inline void slash_vm_flags_set(struct vm_area_struct *vma, vm_flags_t flags) +{ +#if defined(SLASH_HAVE_VM_FLAGS_SET) + vm_flags_set(vma, flags); +#else + vma->vm_flags |= flags; +#endif +} + +/* + * MODULE_IMPORT_NS argument form. + * + * Pre-6.13: bare token, e.g. MODULE_IMPORT_NS(DMA_BUF). The kernel + * macro internally __stringify()s the argument, so passing a + * string literal would produce a runtime namespace mismatch. + * 6.13+: string literal, e.g. MODULE_IMPORT_NS("DMA_BUF"). The + * kernel macro stopped stringifying, so passing a bare token + * fails to compile. + * + * We probe the token form (precise cutover at 6.13) and stringify here + * when it's no longer accepted. + */ +#if defined(SLASH_HAVE_MODULE_IMPORT_NS_TOKEN) +#define SLASH_MODULE_IMPORT_NS(ns) MODULE_IMPORT_NS(ns) +#else +#define SLASH_MODULE_IMPORT_NS(ns) MODULE_IMPORT_NS(#ns) +#endif + +#endif /* SLASH_COMPAT_H */ diff --git a/driver/slash_ctldev.c b/driver/slash_ctldev.c index 3f529d23..dc69b137 100644 --- a/driver/slash_ctldev.c +++ b/driver/slash_ctldev.c @@ -41,7 +41,6 @@ #include #include #include -#include #include "slash.h" #include "slash_dmabuf.h" diff --git a/driver/slash_dmabuf.c b/driver/slash_dmabuf.c index a8f9439c..36f3ed53 100644 --- a/driver/slash_dmabuf.c +++ b/driver/slash_dmabuf.c @@ -39,13 +39,13 @@ #include "slash_dmabuf.h" #include "slash.h" +#include "slash_compat.h" #include #include #include #include #include -#include /** * struct slash_bar_dmabuf_data - Private data attached to each BAR dma-buf. @@ -181,13 +181,8 @@ static int slash_bar_dmabuf_mmap(struct dma_buf *dmabuf, struct vm_area_struct * * VM_DONTCOPY — do not inherit across fork(); BAR register * mappings should not be silently shared with children. */ -#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) - vm_flags_set(vma, VM_PFNMAP | VM_IO | VM_DONTDUMP | - VM_DONTEXPAND | VM_DONTCOPY); -#else - vma->vm_flags |= VM_PFNMAP | VM_IO | VM_DONTDUMP | - VM_DONTEXPAND | VM_DONTCOPY; -#endif + slash_vm_flags_set(vma, VM_PFNMAP | VM_IO | VM_DONTDUMP | + VM_DONTEXPAND | VM_DONTCOPY); wc = !!(pci_resource_flags(priv->pdev, priv->bar_number) & IORESOURCE_PREFETCH); vma->vm_page_prot = wc ? pgprot_writecombine(vma->vm_page_prot) diff --git a/driver/slash_main.c b/driver/slash_main.c index 7933fb18..5296e0e3 100644 --- a/driver/slash_main.c +++ b/driver/slash_main.c @@ -44,12 +44,12 @@ */ #include "slash.h" +#include "slash_compat.h" #include #include #include #include -#include #include "slash_pcie.h" #include "slash_hotplug_driver.h" @@ -131,8 +131,4 @@ MODULE_LICENSE("GPL"); MODULE_AUTHOR("AMD Inc."); MODULE_DESCRIPTION("SLASH/VRT module"); MODULE_VERSION(SLASH_VERSION_STR); -#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 13, 0) -MODULE_IMPORT_NS("DMA_BUF"); -#else -MODULE_IMPORT_NS(DMA_BUF); -#endif +SLASH_MODULE_IMPORT_NS(DMA_BUF); diff --git a/packaging/debian/slash-dkms.install b/packaging/debian/slash-dkms.install index 5958a3ed..d0496a33 100644 --- a/packaging/debian/slash-dkms.install +++ b/packaging/debian/slash-dkms.install @@ -21,6 +21,7 @@ driver/*.c usr/src/slash-@VERSION@/driver/ driver/*.h usr/src/slash-@VERSION@/driver/ driver/Makefile usr/src/slash-@VERSION@/driver/ +driver/kcompat usr/src/slash-@VERSION@/driver/ driver/libslash/include/slash/uapi usr/src/slash-@VERSION@/driver/libslash/include/slash/ submodules/qdma_drv/QDMA/linux-kernel/driver/libqdma/ usr/src/slash-@VERSION@/driver/ diff --git a/packaging/rpm/slash.spec b/packaging/rpm/slash.spec index 5ea6a128..a18ccd59 100644 --- a/packaging/rpm/slash.spec +++ b/packaging/rpm/slash.spec @@ -209,6 +209,8 @@ install -m 0644 driver/*.c %{buildroot}%{_usrsrc}/%{dkms_name}-%{dkms_versi install -m 0644 driver/*.h %{buildroot}%{_usrsrc}/%{dkms_name}-%{dkms_version}/driver/ install -m 0644 driver/Makefile %{buildroot}%{_usrsrc}/%{dkms_name}-%{dkms_version}/driver/ +cp -a driver/kcompat %{buildroot}%{_usrsrc}/%{dkms_name}-%{dkms_version}/driver/ + cp -a driver/libslash/include/slash/uapi \ %{buildroot}%{_usrsrc}/%{dkms_name}-%{dkms_version}/driver/libslash/include/slash/ From 62e2f6b1605381206399c032fb3bc27ab5a3209b Mon Sep 17 00:00:00 2001 From: Vlad-Gabriel Serbu Date: Wed, 20 May 2026 13:34:09 +0100 Subject: [PATCH 2/2] Missing license headers Signed-off-by: Vlad-Gabriel Serbu --- driver/kcompat/module_import_ns_token.c | 14 ++++++++++++++ driver/kcompat/probe.sh | 14 ++++++++++++++ driver/kcompat/vm_flags_set.c | 14 ++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/driver/kcompat/module_import_ns_token.c b/driver/kcompat/module_import_ns_token.c index 7ba56b0c..fa58d976 100644 --- a/driver/kcompat/module_import_ns_token.c +++ b/driver/kcompat/module_import_ns_token.c @@ -1,3 +1,17 @@ +/** + * Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved. + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; if + * not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + /* * Token-form MODULE_IMPORT_NS probe. * diff --git a/driver/kcompat/probe.sh b/driver/kcompat/probe.sh index fcaddc23..a8245e9a 100755 --- a/driver/kcompat/probe.sh +++ b/driver/kcompat/probe.sh @@ -1,4 +1,18 @@ #!/bin/sh +#/** +# * Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved. +# * This program is free software; you can redistribute it and/or modify it under the terms of the +# * GNU General Public License as published by the Free Software Foundation; version 2. +# * +# * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without +# * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# * General Public License for more details. +# * +# * You should have received a copy of the GNU General Public License along with this program; if +# * not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# * 02110-1301, USA. +# */ + # Probe the kernel build at $1 for SLASH API compatibility features. # # Each *.c file in this directory is built as a tiny standalone module diff --git a/driver/kcompat/vm_flags_set.c b/driver/kcompat/vm_flags_set.c index 4149c46c..dcc4edac 100644 --- a/driver/kcompat/vm_flags_set.c +++ b/driver/kcompat/vm_flags_set.c @@ -1,3 +1,17 @@ +/** + * Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved. + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; if + * not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + #include #include #include