Building NVidia Driver 340.76 with Linux 4.0.4 on Fedora 21

By | 15 June 2015

This evening I updated Fedora 21 on my Thinkpad W510 laptop and the kernel upgrade brought the laptop to version 4.0.4. The previous 3.x kernel worked perfectly with my Nvidia drivers but installing the Nvidia drivers in the 4.0.4 kernel failed. I was unable to find anything online via Google or the Nvidia web site. I looked at the Nvidia install log (/var/log/nvidia-installer.log) and noticed that the kernel module build was failing on “write_cr4.” After a bit of Google searching I found a post on the Nvidia developer forums about some issues with read_cr4 and write_cr4 being changed to __read_cr4 and __write_cr4 in Linux 4.x kernels.
I usually update my Nvidia drivers by running ‘./NVIDIA-Linux-x86_64-340.76.run’ but adding a ‘-x’ to the command extracts the full package so the contents can be viewed, and in this case modified. All that needs to be done is edit the file kernel/nv-pat.c in the extracted directory from:
{
unsigned long cr0 = read_cr0();
write_cr0(((cr0 & (0xdfffffff)) | 0x40000000));
wbinvd();
*cr4 = read_cr4();
if (*cr4 & 0x80) write_cr4(*cr4 & ~0x80);
__flush_tlb();
}

static inline void nv_enable_caches(unsigned long cr4)
{
unsigned long cr0 = read_cr0();
wbinvd();
__flush_tlb();
write_cr0((cr0 & 0x9fffffff));
if (cr4 & 0x80) write_cr4(cr4);
}

To:
{
unsigned long cr0 = read_cr0();
write_cr0(((cr0 & (0xdfffffff)) | 0x40000000));
wbinvd();
*cr4 = __read_cr4();
if (*cr4 & 0x80) __write_cr4(*cr4 & ~0x80);
__flush_tlb();
}

static inline void nv_enable_caches(unsigned long cr4)
{
unsigned long cr0 = read_cr0();
wbinvd();
__flush_tlb();
write_cr0((cr0 & 0x9fffffff));
if (cr4 & 0x80) __write_cr4(cr4);
}

You can then run ‘./nvidia-installer’ in the extracted directory as root and the module will build just fine. I am assuming the next release of the Nvidia driver will contain a fix but until then, this will get you working if you have a 4.x kernel and the driver fails to build.

2 thoughts on “Building NVidia Driver 340.76 with Linux 4.0.4 on Fedora 21

  1. John Shaw

    Chris, thanks… this solution worked for me and I am pretty much a newbie to linux; very clearly explained.

    Reply

Leave a Reply to John Shaw Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.