Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
25 changes: 23 additions & 2 deletions driver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 \
Expand Down Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions driver/kcompat/module_import_ns_token.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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.
*
* 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 <linux/init.h>
#include <linux/module.h>

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);
67 changes: 67 additions & 0 deletions driver/kcompat/probe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/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
# 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 <kernel-build-dir>" >&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
33 changes: 33 additions & 0 deletions driver/kcompat/vm_flags_set.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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 <linux/init.h>
#include <linux/mm.h>
#include <linux/module.h>

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);
56 changes: 56 additions & 0 deletions driver/slash_compat.h
Original file line number Diff line number Diff line change
@@ -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 <linux/mm.h>
#include <linux/module.h>

/*
* 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 */
1 change: 0 additions & 1 deletion driver/slash_ctldev.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/version.h>

#include "slash.h"
#include "slash_dmabuf.h"
Expand Down
11 changes: 3 additions & 8 deletions driver/slash_dmabuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
#include "slash_dmabuf.h"

#include "slash.h"
#include "slash_compat.h"

#include <linux/err.h>
#include <linux/mm.h>
#include <linux/pci.h>
#include <linux/printk.h>
#include <linux/slab.h>
#include <linux/version.h>

/**
* struct slash_bar_dmabuf_data - Private data attached to each BAR dma-buf.
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions driver/slash_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
*/

#include "slash.h"
#include "slash_compat.h"

#include <linux/init.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/printk.h>
#include <linux/version.h>

#include "slash_pcie.h"
#include "slash_hotplug_driver.h"
Expand Down Expand Up @@ -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);
1 change: 1 addition & 0 deletions packaging/debian/slash-dkms.install
Original file line number Diff line number Diff line change
Expand Up @@ -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/
2 changes: 2 additions & 0 deletions packaging/rpm/slash.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
Loading