# patch [patched]

## idella4

gentooers,

how do you apply a kernel patch, this patch?

I put in a bug re udev in a xen kernel.  I have kernel in a nutshell and it has some appropriate instructions for patching, a regular prepared update.

Say -r3 -r6 in a krenel release.  I've never touched manual patching since it' s done by portage and ebuilds.

This is from xensource created to address a submitted bug.  The reply from gentoo bugzilla ofcourse said apply and test.

There are still many things I haven;t got to do in gentoo; it's huge.  Hit the learing curve again.  I'm sure it's not hard. kernel content is still a huge mystery.

This addresses a xenbus.c

I've tried a few sinple options, one half worked..  Just to clarify, I've done

```

gentoo64 linux-2.6.34-xen # patch --strip < /home/idella/Documents/netback-xenbus-redone.diff

gentoo64 linux-2.6.34-xen # patch -u < /home/idella/Documents/netback-xenbus-redone.diff

gentoo64 linux-2.6.34-xen # patch -i  /home/idella/Documents/netback-xenbus-redone.diff

gentoo64 linux-2.6.34-xen # patch -p1  /home/idella/Documents/netback-xenbus-redone.diff

patching file drivers/xen/netback/xenbus.c                                               

Hunk #3 FAILED at 43.                                                                    

Hunk #4 FAILED at 194.                                                                   

Hunk #5 succeeded at 192 (offset -20 lines).                                             

Hunk #6 succeeded at 203 (offset -20 lines).                                             

2 out of 6 hunks FAILED -- saving rejects to file drivers/xen/netback/xenbus.c.rej  

gentoo64 linux-2.6.34-xen # diff --help

```

It's called a patch, it's a diff file.  This requires a simple one liner.

----------

## ssteinberg

patch -p1 < patch should work for this patch. Hunks failing meaning the patch was made for a different kernel version. Check the rejects log file and look for anything major, or just post it here.

----------

## idella4

ssteinberg

I was expecting a simple text file report.  This resembles a shell script.

```

gentoo64 linux-2.6.34-xen # patch -p1 < /home/idella/Documents/netback-xenbus-redone.diff

patching file drivers/xen/netback/xenbus.c                                               

Reversed (or previously applied) patch detected!  Assume -R? [n]                         

Apply anyway? [n]                                                                        

Skipping patch.                                                                          

6 out of 6 hunks ignored -- saving rejects to file drivers/xen/netback/xenbus.c.rej      

gentoo64 linux-2.6.34-xen # cat drivers/xen/netback/xenbus.c.rej                   

***************                                                                    

*** 19,24 ****                                                                     

                                                                                   

  #include <stdarg.h>                                                              

  #include <linux/module.h>                                                        

  #include <xen/xenbus.h>                                                          

  #include "common.h"                                                              

                                                                                   

--- 19,25 ----                                                                     

                                                                                   

  #include <stdarg.h>                                                              

  #include <linux/module.h>                                                        

+ #include <linux/rwsem.h>                                                         

  #include <xen/xenbus.h>                                                          

  #include "common.h"                                                              

                                                                                   

***************                                                                    

*** 28,33 ****                                                                     

      printk("netback/xenbus (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)  

  #endif                                                                           

                                                                                   

                                                                                   

  static int connect_rings(struct backend_info *);                                 

  static void connect(struct backend_info *);                                      

--- 29,35 ----                                                                     

      printk("netback/xenbus (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)  

  #endif                                                                           

                                                                                   

+ static DECLARE_RWSEM(teardown_sem);                                              

                                                                                   

  static int connect_rings(struct backend_info *);                                 

  static void connect(struct backend_info *);                                      

***************                                                                    

*** 41,54 ****                                                                     

        //netback_remove_accelerators(be, dev);                                    

                                                                                   

        unregister_hotplug_status_watch(be);                                       

-       if (be->netif) {                                                           

                kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);                      

                xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");               

                netif_disconnect(be->netif);                                       

                be->netif = NULL;                                                  

        }                                                                          

-       kfree(be);                                                                 

        dev_set_drvdata(&dev->dev, NULL);                                          

        return 0;                                                                  

  }                                                                                

                                                                                   

--- 43,61 ----                                                                     

        //netback_remove_accelerators(be, dev);                                    

                                                                                   

        unregister_hotplug_status_watch(be);                                       

+       if (be->netif)                                                             

                kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);                      

+                                                                                  

+       down_write(&teardown_sem);                                                 

+       if (be->netif) {                                                           

                xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");               

                netif_disconnect(be->netif);                                       

                be->netif = NULL;                                                  

        }                                                                          

        dev_set_drvdata(&dev->dev, NULL);                                          

+       up_write(&teardown_sem);                                                   

+       kfree(be);                                                                 

+                                                                                  

        return 0;                                                                  

  }                                                                                

                                                                                   

***************                                                                    

*** 187,195 ****                                                                   

                kfree(val);                                                        

        }                                                                          

                                                                                   

-       if (add_uevent_var(env, "vif=%s", netif->dev->name))                       

-               return -ENOMEM;                                                    

-                                                                                  

        return 0;                                                                  

  }                                                                                

                                                                                   

--- 194,208 ----                                                                   

                kfree(val);                                                        

        }                                                                          

                                                                                   

+       down_read(&teardown_sem);                                                  

+       be = dev_get_drvdata(&xdev->dev);                                          

+       if (be && be->netif) {                                                     

+               if (add_uevent_var(env, "vif=%s", netif->dev->name)) {             

+                       up_read(&teardown_sem);                                    

+                       return -ENOMEM;                                            

+               }                                                                  

+       }                                                                          

+       up_read(&teardown_sem);                                                    

        return 0;                                                                  

  }                                                                                

                                                                                   

***************                                                                    

*** 199,204 ****                                                                   

        int err;                                                                   

        long handle;                                                               

        struct xenbus_device *dev = be->dev;                                       

                                                                                   

        if (be->netif != NULL)                                                     

                return;                                                            

--- 212,218 ----

        int err;

        long handle;

        struct xenbus_device *dev = be->dev;

+       struct xen_netif *netif;

        if (be->netif != NULL)

                return;

***************

*** 209,221 ****

                return;

        }

-       be->netif = netif_alloc(&dev->dev, dev->otherend_id, handle);

-       if (IS_ERR(be->netif)) {

-               err = PTR_ERR(be->netif);

-               be->netif = NULL;

                xenbus_dev_fatal(dev, err, "creating interface");

                return;

        }

        kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);

  }

--- 223,235 ----

                return;

        }

+       netif = netif_alloc(&dev->dev, dev->otherend_id, handle);

+       if (IS_ERR(netif)) {

+               err = PTR_ERR(netif);

                xenbus_dev_fatal(dev, err, "creating interface");

                return;

        }

+       be->netif = netif;

        kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);

  }

```

Looking at the patch content, where does it stipulate a kernel version? I can't pick one.  I have 3 xen kernels.  It's likely I have the matching kernel if I kneww what it is.

Has the pastch taken despite the hunk fail?  I've never touched this before.

----------

## drescherjm

If you get Hunk failed then the patch was not fully applied. You probably want to run it again and let it reverse so that it removes the patch since the patch is incompatible with your kernel version.

You could also look at the .rej file and try  to fix the patch.

----------

## ssteinberg

It doesn't, whoever posted the patch should stay against what it was made.

You posted all 6 ignored hunks (probably because of trying to repatch the kernel again). Either way seem like 3 and 4 are important to whatever that patch does.

----------

## idella4

drescherjm

```

gentoo64 linux-2.6.34-xen # patch -p1 < /home/idella/Documents/netback-xenbus-redone.diff

patching file drivers/xen/netback/xenbus.c

Reversed (or previously applied) patch detected!  Assume -R? [n] y

Hunk #3 FAILED at 41.

Hunk #4 FAILED at 187.

Hunk #5 succeeded at 168 (offset -31 lines).

Hunk #6 succeeded at 178 (offset -31 lines).

2 out of 6 hunks FAILED -- saving rejects to file drivers/xen/netback/xenbus.c.rej

```

ok, just needed to return to the bug to get the info.  kernel version

```

You could also look at the .rej file and try to fix the patch.

```

Hmmm, who do you think I am?. Maybe you could.  The link is there to access the patch. However, I'm not without progress.

Is this doable or did the dev at bug-wranglers@gentoo.org or udev@gentoo direct me to apply a patch with a lack of consideration of what I would be applying it to?  Then, can portage provide its patches to upgrade it to xen-5?  Remembering that the kernel used by the reporter of the xen bug was using a xen0source prepared xen kernel.  I have four gentoo xens and one xen-source one which worked.

----------

## idella4

just to refloat this for one further question.

I've worked the gentoo and the xensource kernels through another thread which lead to this

The patch I was applying was made to suit the xensource kernel.  Knowing nothing about patches, I'd like to check this out;

The patch was made to adjust the content of the file linux-2.6-xen//drivers/xen/netback/xenbus.c.

I have two instances of linux-2.6-xen.  To one, I followed the instruct to upgrade it to a xen-2.6.32.

```

idella@genny /mnt/gentoo/linux-2.6.32.17-xen $ sudo patch -p1 < /home/idella/Documents/netback-xenbus-redone.diff 

patching file drivers/xen/netback/xenbus.c

Hunk #4 succeeded at 189 (offset -5 lines).

Hunk #5 succeeded at 207 (offset -5 lines).

Hunk #6 succeeded at 218 (offset -5 lines).

```

I doesn't draw an error, but it has only seemingly done 3 out of 6.  Subsequently,

```

[code]

idella@genny /mnt/gentoo/linux-2.6.32.17-xen $ sudo make CC="ccache gcc"

  CHK     include/linux/version.h

  CHK     include/linux/utsrelease.h

  SYMLINK include/asm -> include/asm-x86

  CALL    scripts/checksyscalls.sh

  CHK     include/linux/compile.h

  CC      drivers/xen/netback/xenbus.o

drivers/xen/netback/xenbus.c: In function 'connect_rings':

drivers/xen/netback/xenbus.c:471: error: 'struct xen_netif' has no member named 'features'

drivers/xen/netback/xenbus.c:481: error: 'struct xen_netif' has no member named 'features'

drivers/xen/netback/xenbus.c:489: error: 'struct xen_netif' has no member named 'features'

make[3]: *** [drivers/xen/netback/xenbus.o] Error 1

make[2]: *** [drivers/xen/netback] Error 2

make[1]: *** [drivers/xen] Error 2

make: *** [drivers] Error 2

[/code]

however, to another instance,

```

idella@genny /mnt/ubuntu/home/idella/xen/xen-3.4-testing.hg/linux-2.6-xen $ sudo patch -p1 < /home/idella/Documents/netback-xenbus-redone.diff 

patching file drivers/xen/netback/xenbus.c

Reversed (or previously applied) patch detected!  Assume -R? [n] y

Hunk #4 succeeded at 182 (offset -5 lines).

Hunk #5 succeeded at 194 (offset -5 lines).

Hunk #6 succeeded at 204 (offset -5 lines).

[code]

also half. 

Does the patch need to be redone to apply to 2.6.32?  What happened to the first 3?  Why was error not drawn?

Applying the patch to the gentoo xen kernel didn't take.  Can the content of the patch be applied to these other kernels using some manual approach or not?

The instance that is in its original 2.6.31-13 form has compiled.

----------

## Hu

 *idella4 wrote:*   

> I doesn't draw an error, but it has only seemingly done 3 out of 6.

 Why do you say that? *idella4 wrote:*   

> 
> 
> ```
> 
> idella@genny /mnt/gentoo/linux-2.6.32.17-xen $ sudo make CC="ccache gcc"
> ...

 The patch is incompatible with the kernel version to which you applied it.

 *idella4 wrote:*   

> Does the patch need to be redone to apply to 2.6.32?

 Yes.  If it were compatible as-is with 2.6.32, it would produce code that compiled.

 *idella4 wrote:*   

> What happened to the first 3?  Why was error not drawn?

 As far as I can see, the first three worked fine.  What error were you expecting to be drawn?

 *idella4 wrote:*   

> Can the content of the patch be applied to these other kernels using some manual approach or not?
> 
> The instance that is in its original 2.6.31-13 form has compiled.

 Yes.  Kernel developers can often work out the intent of a patch and construct a patch that mirrors the intent, rather than the literal text, of a failed patch.

----------

## idella4

Hu,

I doesn't draw an error, but it has only seemingly done 3 out of 6.

 *Quote:*   

> 
> 
> Why do you say that?
> 
> 

 

Since it reported 3 out of 6 succeeded, why doesn't it report on the success or failure of the first three?

 *Quote:*   

> 
> 
> What error were you expecting to be drawn? 
> 
> 

 

The patch when applied to 2.6.32 reported 2 succeeded, 2 failed.  If only half reported successful, that ought imply the other half were failures.  The result though proves the first three were successful despite not being reported successful.

Well anyway, that completes lesson 1 in patches.

----------

## Hu

 *idella4 wrote:*   

> Since it reported 3 out of 6 succeeded, why doesn't it report on the success or failure of the first three?

 That would be noisy.  Consider:

```
$ echo -e 'abc\ndef' >foo

$ echo -e 'abc\nde' >bar

$ diff -u foo bar >x.patch

$ patch -p0 --dry-run foo <x.patch 

patching file foo

```

The patch applied successfully, so there were no failures to report.  If you want specific confirmation of success, add in --verbose to patch.

 *idella4 wrote:*   

> The patch when applied to 2.6.32 reported 2 succeeded, 2 failed.  If only half reported successful, that ought imply the other half were failures.  The result though proves the first three were successful despite not being reported successful.

 I think you misunderstand the output.  The first three were completely successful, so they were not reported.  The second three were applied, but patch did not apply them in exactly the location that the patch file specified, so patch printed a diagnostic warning you that it was forced to offset the changes from where the patch file said they should go.  When this occurs, you should be a bit more suspicious about whether patch produced an output file that does exactly what the original author intended.

----------

