[SOLVED] NVIDIA 340.107 - Linux 5.0-rc5 : Failed to build kernel module

NVIDIA installer for driver 340.107 fails to build a module nvidia.ko for Linux kernel 5.0-rc5.

Here is where the build fails (from the nvidia-installer.log):

/home/ejm/NVIDIA-Linux-x86_64-340.107/kernel/nv-drm.c: In function ‘nv_alloc_os_descriptor_handle’:
/home/ejm/NVIDIA-Linux-x86_64-340.107/kernel/nv-drm.c:255:5: error: implicit declaration of function ‘drm_gem_object_unreference_unlocked’; did you mean ‘drm_gem_object_put_unlocked’? [-Werror=implicit-function-declaration]

nvidia-installer.log (182 KB)

Working patch for NVIDIA driver 340.107 & Linux Kernel 5.0.

Thanks to Alberto Milone, NVIDIA driver maintainer for Ubuntu.

Pastebin link: https://pastebin.com/raw/qsqxsBmZ

Zip file of patch attached: nv_patch.zip

Note: Use -l or --ignore-whitespace in the patch command

Example: patch -p0 -l < nv_patch_340.107_linux_kernel_5.0
nv_patch.zip (2.15 KB)

This isn’t enough if DKMS is used (which is rather handy), for that the nv-time.h needs to be added to the top-level .manifest file

Encountered the same problem on Linux kernel 5.1.2. Applied the patch from topic, added nv-time.h to top-level .manifest file as suggested in a post above, but still encountering the original error, just on different line (the one from the patch):

/home/lzhev/NVIDIA-Linux-x86_64-340.107/kernel/nv-drm.c: In function ‘nv_drm_gem_object_unreference_unlocked’:
   /home/lzhev/NVIDIA-Linux-x86_64-340.107/kernel/nv-drm.c:46:5: error: implicit declaration of function ‘drm_gem_object_unreference_unlocked’; did you mean ‘nv_drm_gem_object_unreference_unlocked’? [-Werror=implicit-function-declaration]
   cc1: some warnings being treated as errors

Any suggestions?

UPDATE: solved the problem, there was my mistake applying the patch (broken diff), confirm patch solving this problem.
nvidia-installer.log (158 KB)

This also worked with the 340.107 driver and the latest CentOS 7.7 kernel - 3.10.0-1062.1.1.el7.x86_64.