# Patch nvidia-drivers-331.17 for gentoo-sources-3.11.*

## Chopstix

The problem:

```
 * Gentoo supports kernels which are supported by NVIDIA

 * which are limited to the following kernels:

 * <sys-kernel/gentoo-sources-3.11

 * <sys-kernel/vanilla-sources-3.11

 * 

 * You are free to utilize epatch_user to provide whatever

 * support you feel is appropriate, but will not receive

 * support as a result of those changes.

 * 

 * Do not file a bug report about this.
```

The assumption:

You have already keyworded and emerged >=sys-kernel/gentoo-sources-3.11.6 and used eselect to select the new kernel.

The solution:

```
mkdir -p /etc/portage/patches/x11-drivers/nvidia-drivers-331.17/
```

Paste this into /etc/portage/patches/x11-drivers/nvidia-drivers-331.17/kernel.patch :

```
--- a/kernel/nv-linux.h 2013-10-27 23:23:30.290956283 +0100

+++ b/kernel/nv-linux.h 2013-10-27 23:25:11.366952961 +0100

@@ -956,7 +956,11 @@

 #endif

 #if !defined(NV_VMWARE)

+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)

+#define NV_NUM_PHYSPAGES                get_num_physpages()

+#else

 #define NV_NUM_PHYSPAGES                num_physpages

+#endif

 #define NV_GET_CURRENT_PROCESS()        current->tgid

 #define NV_IN_ATOMIC()                  in_atomic()

 #define NV_LOCAL_BH_DISABLE()           local_bh_disable()
```

Go ahead and emerge nvidia-drivers.Last edited by Chopstix on Mon Oct 28, 2013 11:07 am; edited 1 time in total

----------

## Jaglover

Chopstix,

great job!

Unfortunately ... many folks have posted it before, including myself ... and mine wasn't the first ...   :Embarassed: 

Google

----------

## Chopstix

Yes, I traced the original post to this nvidia devtalk forum post, and saw some mention of it here too, but I also saw many posts that people couldn't get the patch to apply, so I re-made the patch to work with the latest versions available, thereby saving some people time and denying them the pleasure of learning how the diff and patch commands work, and how to integrate those with ebuilds... oh why did I do it  :Razz: 

----------

## witul

Why we have to patch manually almost all versions?

----------

## Chopstix

The error message explains it: nvidia does not officially support the >=3.11 kernel yet.

----------

## platojones

 *witul wrote:*   

> Why we have to patch manually almost all versions?

 

To expand on what Chopstix said: The Gentoo dev who supports the nvidia-drivers has adopted the policy that Gentoo supports only what Nvidia officially supports.  And Nvidia doesn't officially support the 3.11 kernels yet.  That seems very reasonable to me, from a Gentoo dev perspective, otherwise, he's signed up for constantly digging up patches and handling all of the potential bugs that get filed for running the drivers with an unsupported kernel.  That's also why he added the ability for users to easily apply their own patches to the ebuild, using Gentoo's brilliant ebuild patch mechanism.

So, it's no big deal...when Nvidia releases the drivers to support the 3.11 kernels, we get them.  Until then, we just use the simple user patch mechanism.

----------

## Martux

Gnah  :Sad: 

For me the patch fails:

```

die "Failed Patch: ${patchname}!";

```

Completely ~amd64 here, so I wonder why?

----------

## megaflow

try this patch 

https://devtalk.nvidia.com/cmd/default/download-comment-attachment/59607/

and this link is to the site why u need this patch

https://devtalk.nvidia.com/default/topic/628864/unix-graphics-announcements-and-news/num_physpages-and-support-for-3-11-and-later-kernels/

----------

## Chopstix

Megaflow are you suggesting he ditch portage and use the installer? Bad advice. How can someone who doesn't know how to apply a simple patch know how to clean his system from the nvidia installer when he decides to move back to portage...

Martux either provide more info on why it failed, or patch it yourself, it's simple.

----------

## ulenrich

@megaflow 

very thanks! I have posted a notice at

https://bugs.gentoo.org/show_bug.cgi?id=447566#c175

@Chopstix 

you just put the one of the three patches you guess for example at:

 /etc/portage/patches/x11-drivers/nvidia-drivers-325.15/get_num_physpages_325-331.patch

The portage tree nvidia ebuild has epatch_user active!

----------

## Chopstix

I assumed they wouldn't be compatible.

----------

## ulenrich

 *Chopstix wrote:*   

> I assumed they wouldn't be compatible.

 

If any patch is not compatible there is a very,very high chance it 

simply doesn't patch and emerge errors out the build process.

Just try  :Smile: 

----------

## megaflow

@Chopstix

i try the patch myself before posting it.

I assumed u know how to use epatch , to patch it

i stumble on the patch by a problem with world of warcraft using all my system memory. and make my system run very slow

so i made a quike response on this tread 

sorry for that

----------

## Chopstix

I meant I thought the patch provided by nvidia would not work with portage only with nvidia's installer, which is why I assumed megaflow was encouraging Martux to ditch portage and use nvidia's installer.

----------

## Wallsandfences

I'm not sure what your experiences are, but I have the same problem with the patch:

```
 *   /var/tmp/portage/x11-drivers/nvidia-drivers-331.17/temp/kernel.patch.out

 * ERROR: x11-drivers/nvidia-drivers-331.17::gentoo failed (prepare phase):

 *   Failed Patch: kernel.patch!

 * 

 * Call stack:

 *     ebuild.sh, line   93:  Called src_prepare

 *   environment, line 4234:  Called epatch_user

 *   environment, line 1650:  Called epatch

 *   environment, line 1623:  Called die

 * The specific snippet of code:

 *               die "Failed Patch: ${patchname}!";

```

Rüdiger

----------

## Martux

It just worked for me with the patch from the nvidia-site, megaflow provided earlier.

----------

## Wallsandfences

this is with megaflow's patch...

----------

## Martux

Hm, you did an

```

mkdir -p /etc/portage/patches/x11-drivers/nvidia-drivers-331.17/

```

first?

Also made sure, you're only using the one appropriate patch, namely the get_num_physpages_325-331.patch one?

----------

## Wallsandfences

```
 cat /etc/portage/patches/x11-drivers/nvidia-drivers-331.17/kernel.patch

This patch will work on installers for the following versions of the

NVIDIA Linux driver:

325.15

331.13

331.17

It will not apply cleanly against older 325 drivers, but can be made to

work with some minor changes.

--- /dev/null

+++ kernel/patch-get_num_physpages.h

@@ -0,0 +1,10 @@

+#define NV_PATCH_get_num_physpages_DESCRIPTION \

+"This patch adds support for kernels such as Linux 3.11, which use the "\

+"get_num_physpages() function instead of the num_physpages variable to "\

+"report the number of physical pages. In order to avoid compatibility "\

+"problems that would otherwise result from differences between what "\

+"the removed num_physpages variable reported versus what is reported "\

+"by get_num_physpages(), the driver will fall back to a different memory "\

+"allocation strategy on systems where num_phsypages is not available, when "\

+"the amount of system memory matches or exceeds the upper bound of a given "\

+"GPU's physical address space.\n"

--- kernel/Makefile

+++ kernel/Makefile

@@ -125,6 +125,7 @@ COMPILE_TESTS = \

    file_operations \

    sg_init_table \

    pci_get_domain_bus_and_slot \

+   get_num_physpages \

    efi_enabled \

    dom0_kernel_present \

    drm_available \

--- kernel/conftest.sh

+++ kernel/conftest.sh

@@ -1676,6 +1676,30 @@ compile_test() {

             fi

         ;;

 

+        get_num_physpages)

+            #

+            # Determine if the get_num_physpages() function is

+            # present.

+            #

+            echo "$CONFTEST_PREAMBLE

+            #include <linux/mm.h>

+            void conftest_get_num_physpages(void) {

+                get_num_physpages(NULL);

+            }" > conftest$$.c

+

+            $CC $CFLAGS -c conftest$$.c > /dev/null 2>&1

+            rm -f conftest$$.c

+

+            if [ -f conftest$$.o ]; then

+                echo "#undef NV_GET_NUM_PHYSPAGES_PRESENT" | append_conftest "functions"

+                rm -f conftest$$.o

+                return

+            else

+                echo "#define NV_GET_NUM_PHYSPAGES_PRESENT" | append_conftest "functions"

+                return

+            fi

+        ;;

```

----------

## ulenrich

That is a corrupted patch file. Ca 60percentage of the original.

A patch typically should not end with a '+' sign.

----------

## Wallsandfences

Yeah, you were right, the file wasn't complete. It works now.

Rüdiger

----------

