Skip to content
This repository has been archived by the owner on Apr 11, 2022. It is now read-only.

Commit

Permalink
Add nv_register_user_pages/os_lock_user_pages from the Linux driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
shkhln committed Oct 30, 2020
1 parent 0c7a480 commit a68f5a4
Show file tree
Hide file tree
Showing 4 changed files with 429 additions and 0 deletions.
11 changes: 11 additions & 0 deletions x11/nvidia-driver/files/patch-src-nvidia-Makefile.patch
@@ -0,0 +1,11 @@
--- src/nvidia/Makefile.orig 2020-03-17 04:21:12.818538000 +0300
+++ src/nvidia/Makefile 2020-03-17 04:21:26.485226000 +0300
@@ -23,6 +23,8 @@
CFLAGS+= -D__KERNEL__ -DNVRM -Wno-unused-function -Wuninitialized -O2 -fno-strict-aliasing -mno-red-zone -mcmodel=kernel -UDEBUG -U_DEBUG -DNDEBUG
CFLAGS+= -DNV_SPECTRE_V2=$(SPECTRE_V2_RETPOLINE)

+CFLAGS+= -I${SYSDIR}/compat/linuxkpi/common/include
+
include ../common/optional-cflags.mk

OBJS+= ${RMOBJ}
123 changes: 123 additions & 0 deletions x11/nvidia-driver/files/patch-src-nvidia-nvidia_os.c.patch
@@ -0,0 +1,123 @@
--- src/nvidia/nvidia_os.c.orig 2020-02-21 03:55:08.000000000 +0300
+++ src/nvidia/nvidia_os.c 2020-03-17 23:41:54.697875000 +0300
@@ -953,13 +953,94 @@
{
}

+// source: common/inc/nv-mm.h
+
+#include <linux/mm.h>
+
+static inline long NV_GET_USER_PAGES(unsigned long start,
+ unsigned long nr_pages,
+ int write,
+ int force,
+ struct page **pages,
+ struct vm_area_struct **vmas)
+{
+ unsigned int flags = 0;
+
+ if (write)
+ flags |= FOLL_WRITE;
+ if (force)
+ flags |= FOLL_FORCE;
+
+ return get_user_pages(start, nr_pages, flags, pages, vmas);
+}
+
+// !common/inc/nv-mm.h
+
+#include <linux/preempt.h> // in_interrupt
+#include <linux/kthread.h> // current, in_atomic
+
+#define irqs_disabled() (curthread->td_critnest != 0 || curthread->td_intr_nesting_level != 0) // source: kms-drm repo
+
+// source: common/inc/nv-linux.h
+
+#define NV_IN_ATOMIC() in_atomic()
+#define NV_MAY_SLEEP() (!irqs_disabled() && !in_interrupt() && !NV_IN_ATOMIC())
+
+// !common/inc/nv-linux.h
+
+// source: nvidia/os-mlock.c
+
NV_STATUS NV_API_CALL os_lock_user_pages(
void *address,
NvU64 page_count,
void **page_array
)
{
- return NV_ERR_NOT_SUPPORTED;
+ NV_STATUS rmStatus;
+ struct mm_struct *mm = current->mm;
+ struct page **user_pages;
+ NvU64 i, pinned;
+ NvBool write = 1, force = 0;
+ int ret;
+
+ if (!NV_MAY_SLEEP())
+ {
+ nv_printf(NV_DBG_ERRORS,
+ "NVRM: %s(): invalid context!\n", __FUNCTION__);
+ return NV_ERR_NOT_SUPPORTED;
+ }
+
+ rmStatus = os_alloc_mem((void **)&user_pages,
+ (page_count * sizeof(*user_pages)));
+ if (rmStatus != NV_OK)
+ {
+ nv_printf(NV_DBG_ERRORS,
+ "NVRM: failed to allocate page table!\n");
+ return rmStatus;
+ }
+
+ down_read(&mm->mmap_sem);
+ ret = NV_GET_USER_PAGES((unsigned long)address,
+ page_count, write, force, user_pages, NULL);
+ up_read(&mm->mmap_sem);
+ pinned = ret;
+
+ if (ret < 0)
+ {
+ os_free_mem(user_pages);
+ return NV_ERR_INVALID_ADDRESS;
+ }
+ else if (pinned < page_count)
+ {
+ for (i = 0; i < pinned; i++)
+ put_page(user_pages[i]);
+ os_free_mem(user_pages);
+ return NV_ERR_INVALID_ADDRESS;
+ }
+
+ *page_array = user_pages;
+
+ return NV_OK;
}

NV_STATUS NV_API_CALL os_unlock_user_pages(
@@ -967,8 +1048,23 @@
void *page_array
)
{
- return NV_ERR_NOT_SUPPORTED;
+ NvBool write = 1;
+ struct page **user_pages = page_array;
+ NvU32 i;
+
+ for (i = 0; i < page_count; i++)
+ {
+ if (write)
+ set_page_dirty_lock(user_pages[i]);
+ put_page(user_pages[i]);
+ }
+
+ os_free_mem(user_pages);
+
+ return NV_OK;
}
+
+// !nvidia/os-mlock.c

NV_STATUS NV_API_CALL os_lookup_user_io_memory(
void *address,
8 changes: 8 additions & 0 deletions x11/nvidia-driver/files/patch-src-nvidia-nvidia_pci.c.patch
@@ -0,0 +1,8 @@
--- src/nvidia/nvidia_pci.c.orig 2020-03-17 04:12:33.637145000 +0300
+++ src/nvidia/nvidia_pci.c 2020-03-17 04:12:38.637179000 +0300
@@ -361,3 +361,5 @@

MODULE_DEPEND(nvidia, mem, 1, 1, 1);
MODULE_DEPEND(nvidia, io, 1, 1, 1);
+
+MODULE_DEPEND(nvidia, linuxkpi, 1, 1, 1);

0 comments on commit a68f5a4

Please sign in to comment.