Applying Patches To The Linux Kernel

will explain this to you.

their specific patches) is also provided.

What is a patch?
different versions of a source tree. Patches are created with the `diff'
from the filename.

How do I apply or revert a patch?
You apply a patch with the `patch' program. The patch program reads a diff

Patches for the Linux kernel are generated relative to the parent directory
holding the kernel source dir.

kernel source directories it was generated against (or some other directory
unlabeled patch was generated against) you should change into your kernel
in the patch file when applying it (the -p1 argument to `patch' does this).

So, if you applied a patch like this:
patch -p1 < ../patch-x.y.z

You can revert (undo) it like this:
patch -R -p1 < ../patch-x.y.z

How do I feed a patch/diff file to `patch'?
done in several different ways.
via stdin using the following syntax:
patch -p1 < path/to/patch-x.y.z

section here.

patch -p1 -i path/to/patch-x.y.z

If your patch file is compressed with gzip or bzip2 and you don't want to
zcat path/to/patch-x.y.z.gz | patch -p1
bzcat path/to/patch-x.y.z.bz2 | patch -p1

gunzip or bunzip2 on the file -- like this:
gunzip patch-x.y.z.gz
bunzip2 patch-x.y.z.bz2

patch via stdin or the -i argument, as you prefer.

A few other nice arguments for patch are -s which causes patch to be silent
screen too fast, and --dry-run which causes patch to just print a listing of
what would happen, but doesn't actually make any changes. Finally --verbose
tells patch to print more information about the work being done.

Common errors when patching
file in different ways.
Checking that the file looks like a valid patch file & checking the code
just two of the basic sanity checks patch does.

If patch encounters something that doesn't look quite right it has two

usually adjust the line numbers and apply the patch.

it'll tell you about it by saying the patch applied with 'fuzz'.

outright and leaves a file with a .rej extension (a reject file). You can
go fix it up by hand if you wish.

only patches from kernel.org and you apply the patches in the correct order,
to start with a fresh tree downloaded in full from kernel.org.


If patch stops and presents a "File to patch:" prompt, then patch could not
applied with -p0 instead of -p1 (reading the patch file should reveal if
the patch but is not fatal).

If you get "Hunk #2 succeeded at 1887 with fuzz 2 (offset 7 lines)." or a
expected to make the change to make it fit).
was different than expected.
different kernel version than the one you are trying to patch.

If you get a message like "Hunk #3 FAILED at 2387.", then it means that the
fuzz its way through. This will generate a .rej file with the change that
caused the patch to fail and also a .orig file showing you the original
content that couldn't be changed.

If you get "Reversed (or previously applied) patch detected! Assume -R? [n]"
already been made.
then you can say [y]es here to make patch revert it for you.
destination directories when creating the patch, and in that case reverting
the patch will in fact apply it.

A message similar to "patch: **** unexpected end of file in patch" or "patch
unexpectedly ends in middle of line" means that patch could make no sense of
the file you fed to it. Either your download is broken, you tried to feed
two lines that had been split.

a patch from kernel.org to the correct version of an unmodified source tree.
So if you get these errors with kernel.org patches then you should probably
wish to apply.

Are there any alternatives to `patch'?
Yes there are alternatives.

You can use the `interdiff' program (http://cyberelk.net/tim/patchutils/) to
generate a patch representing the differences between two patches and then
apply the result.
This will let you move from something like to in a single

Here's how you'd go from to in a single step:
interdiff -z ../patch- ../patch- | patch -p1


Another alternative is `ketchup', which is a python script for automatic
downloading and applying of patches (http://www.selenic.com/ketchup/).

the patch contains a given regular expression.

Where can I download the patches?
The patches are available at http://kernel.org/
specific homes.

The 2.6.x.y (-stable) and 2.6.x patches live at

The -rc patches live at

The -git patches live at

The -mm kernels live at

In place of ftp.kernel.org you can use ftp.cc.kernel.org, where cc is a
country code. This way you'll be downloading from a mirror site that's most
likely geographically closer to you, resulting in faster downloads for you,
less bandwidth used globally and less load on the main kernel.org servers --
these are good things, so do use mirrors when possible.

The 2.6.x kernels
release is the most recent.

will be released (see below) on top of this base. Once a new 2.6.x base
previous 2.6.x kernel and the new one.

To apply a patch moving from 2.6.11 to 2.6.12, you'd do the following (note
that such patches do *NOT* apply on top of 2.6.x.y kernels but on top of the
base 2.6.x kernel -- if you need to move from 2.6.x.y to 2.6.x+1 you need to
first revert the 2.6.x.y patch).

Here are some examples:

# moving from 2.6.11 to 2.6.12
$ cd ~/linux-2.6.11 # change to kernel source dir
$ patch -p1 < ../patch-2.6.12 # apply the 2.6.12 patch
$ cd ..
$ mv linux-2.6.11 linux-2.6.12 # rename source dir

# moving from to 2.6.12
$ cd ~/linux- # change to kernel source dir
$ patch -p1 -R < ../patch- # revert the patch
# source dir is now 2.6.11
$ patch -p1 < ../patch-2.6.12 # apply new 2.6.12 patch
$ cd ..
$ mv linux- linux-2.6.12 # rename source dir

The 2.6.x.y kernels
Kernels with 4-digit versions are -stable kernels. They contain small(ish)
critical fixes for security problems or significant regressions discovered
in a given 2.6.x kernel.

kernel and are not interested in helping test development/experimental

If no 2.6.x.y kernel is available, then the highest numbered 2.6.x kernel is
the current stable kernel.

note: the -stable team usually do make incremental patches available as well

These patches are not incremental, meaning that for example the
patch does not apply on top of the kernel source, but rather on top
of the base 2.6.12 kernel source .
So, in order to apply the patch to your existing kernel
source you have to first back out the patch (so you are left with a
base 2.6.12 kernel source) and then apply the new patch.

Here's a small example:

$ cd ~/linux- # change into the kernel source dir
$ patch -p1 < ../patch- # apply the new patch
$ cd ..
$ mv linux- linux- # rename the kernel source dir

The -rc kernels
These are release-candidate kernels. These are development kernels released

These kernels are not stable and you should expect occasional breakage if
development branches and is also what will eventually turn into the next

stuff (such people should see the sections about -git and -mm kernels below).

The -rc patches are not incremental, they apply to a base 2.6.x kernel, just
like the 2.6.x.y patches described above. The kernel version before the -rcN
turn into.
So, 2.6.13-rc5 means that this is the fifth release candidate for the 2.6.13
kernel and the patch should be applied on top of the 2.6.12 kernel source.

Here are 3 examples of how to apply these patches:

# first an example of moving from 2.6.12 to 2.6.13-rc3
$ cd ~/linux-2.6.12 # change into the 2.6.12 source dir
$ patch -p1 < ../patch-2.6.13-rc3 # apply the 2.6.13-rc3 patch
$ cd ..
$ mv linux-2.6.12 linux-2.6.13-rc3 # rename the source dir

# now let's move from 2.6.13-rc3 to 2.6.13-rc5
$ cd ~/linux-2.6.13-rc3 # change into the 2.6.13-rc3 dir
$ patch -p1 -R < ../patch-2.6.13-rc3 # revert the 2.6.13-rc3 patch
$ patch -p1 < ../patch-2.6.13-rc5 # apply the new 2.6.13-rc5 patch
$ cd ..
$ mv linux-2.6.13-rc3 linux-2.6.13-rc5 # rename the source dir

# finally let's try and move from to 2.6.13-rc5
$ cd ~/linux- # change to the kernel source dir
$ patch -p1 < ../patch-2.6.13-rc5 # apply new 2.6.13-rc5 patch
$ cd ..
$ mv linux- linux-2.6.13-rc5 # rename the kernel source dir

The -git kernels
These are daily snapshots of Linus' kernel tree (managed in a git
repository, hence the name).

These patches are usually released daily and represent the current state of
Linus's tree. They are more experimental than -rc kernels since they are
generated automatically without even a cursory glance to see if they are

-git patches are not incremental and apply either to a base 2.6.x kernel or
a base 2.6.x-rc kernel -- you can see which from their name.
A patch named 2.6.12-git1 applies to the 2.6.12 kernel source and a patch
named 2.6.13-rc3-git2 applies to the source of the 2.6.13-rc3 kernel.

Here are some examples of how to apply these patches:

# moving from 2.6.12 to 2.6.12-git1
$ cd ~/linux-2.6.12 # change to the kernel source dir
$ patch -p1 < ../patch-2.6.12-git1 # apply the 2.6.12-git1 patch
$ cd ..
$ mv linux-2.6.12 linux-2.6.12-git1 # rename the kernel source dir

# moving from 2.6.12-git1 to 2.6.13-rc2-git3
$ cd ~/linux-2.6.12-git1 # change to the kernel source dir
$ patch -p1 -R < ../patch-2.6.12-git1 # revert the 2.6.12-git1 patch
# we now have a 2.6.12 kernel
$ patch -p1 < ../patch-2.6.13-rc2 # apply the 2.6.13-rc2 patch
# the kernel is now 2.6.13-rc2
$ patch -p1 < ../patch-2.6.13-rc2-git3 # apply the 2.6.13-rc2-git3 patch
# the kernel is now 2.6.13-rc2-git3
$ cd ..
$ mv linux-2.6.12-git1 linux-2.6.13-rc2-git3 # rename source dir

The -mm kernels
These are experimental kernels released by Andrew Morton.

The -mm tree serves as a sort of proving ground for new features and other
experimental patches.
Once a patch has proved its worth in -mm for a while Andrew pushes it on to
Linus for inclusion in mainline.

Although it's encouraged that patches flow to Linus via the -mm tree, this
is not always enforced.
Subsystem maintainers (or individuals) sometimes push their patches directly
to Linus, even though (or after) they have been merged and tested in -mm (or
sometimes even without prior testing in -mm).

You should generally strive to get your patches into mainline via -mm to
ensure maximum testing.

This branch is in constant flux and contains many experimental features, a
experimental of the branches described in this document.

These kernels are not appropriate for use on systems that are supposed to be
even more so for -mm kernels).

These kernels in addition to all the other experimental patches they contain
usually also contain any changes in the mainline -git kernels available at
the time of release.

Testing of -mm kernels is greatly appreciated since the whole point of the
tree is to weed out regressions, crashes, data corruption bugs, build
breakage (and any other bug in general) before changes are merged into the
more stable mainline Linus tree.
But testers of -mm should be aware that breakage in this tree is more common
than in any other tree.

The -mm kernels are not released on a fixed schedule, but usually a few -mm
kernels are released in between each -rc kernel (1 to 3 is common).
The -mm kernels apply to either a base 2.6.x kernel (when no -rc kernels
have been released yet) or to a Linus -rc kernel.

Here are some examples of applying the -mm patches:

# moving from 2.6.12 to 2.6.12-mm1
$ cd ~/linux-2.6.12 # change to the 2.6.12 source dir
$ patch -p1 < ../2.6.12-mm1 # apply the 2.6.12-mm1 patch
$ cd ..
$ mv linux-2.6.12 linux-2.6.12-mm1 # rename the source appropriately

# moving from 2.6.12-mm1 to 2.6.13-rc3-mm3
$ cd ~/linux-2.6.12-mm1
$ patch -p1 -R < ../2.6.12-mm1 # revert the 2.6.12-mm1 patch
# we now have a 2.6.12 source
# we now have a 2.6.13-rc3 source
$ patch -p1 < ../2.6.13-rc3-mm3 # apply the 2.6.13-rc3-mm3 patch
$ cd ..
$ mv linux-2.6.12-mm1 linux-2.6.13-rc3-mm3 # rename the source dir

This concludes this list of explanations of the various kernel trees.
the kernel.

forgotten for their reviews and contributions to this document.