Apply custom Kernel Patch

I have to modify the /drivers/bus/mhi/pci_generic.c file. I am building my own image for an x86/64 target. I read the guide about using quilt to create and apply .patch file, but I don't know what I have to write in the file.
I need to change two lines of code:

from:
- err = mhi_sync_power_up(mhi_cntrl);
to:
+ err = mhi_async_power_up(mhi_cntrl);

Can someone help me and explain me how to do it? In the meantime, I decided to modify the pci_generic.c file in build_dir directory using an editor, and then:

make target/linux/compile
make target/linux/install
make

Is this correct? Are the modifications I made in the text editor applied to the openwrt .img file?

  1. Make a patch that modifies the file/files you want
  2. Drop the patch into target/linux/x86/patches-6.1/ (or 6.6 if you're using the testing kernel)
  3. Build

How do I create the patch to modify the file drivers/bus/mhi/pci_generic.c at a specific line?

from
- err = mhi_sync_power_up(mhi_cntrl);
to:
+ err = mhi_async_power_up(mhi_cntrl);

Might be an easier way but I would do it with git like this:

  1. extract the linux source and cd into that dir
  2. git init then git add git add drivers/bus/mhi/host/pci_generic.c then git commit -m "baseline"
  3. Modify that file to you liking then git commit -am "my patch title"
  4. Make the patch with git format-patch -1

Example:

From b5a957d62351ce22af11be282919a1f34b210f9c Mon Sep 17 00:00:00 2001
From: graysky
Date: Sun, 14 Apr 2024 07:42:12 -0400
Subject: [PATCH] my patch title

---
 drivers/bus/mhi/host/pci_generic.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/bus/mhi/host/pci_generic.c b/drivers/bus/mhi/host/pci_generic.c
index 08f3f03..000cb87 100644
--- a/drivers/bus/mhi/host/pci_generic.c
+++ b/drivers/bus/mhi/host/pci_generic.c
@@ -870,7 +870,7 @@ static void mhi_pci_recovery_work(struct work_struct *work)
 	if (err)
 		goto err_try_reset;
 
-	err = mhi_sync_power_up(mhi_cntrl);
+	err = mhi_async_power_up(mhi_cntrl);
 	if (err)
 		goto err_unprepare;
 
@@ -974,7 +974,7 @@ static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		goto err_unregister;
 	}
 
-	err = mhi_sync_power_up(mhi_cntrl);
+	err = mhi_async_power_up(mhi_cntrl);
 	if (err) {
 		dev_err(&pdev->dev, "failed to power up MHI controller\n");
 		goto err_unprepare;
@@ -1073,7 +1073,7 @@ static void mhi_pci_reset_done(struct pci_dev *pdev)
 		return;
 	}
 
-	err = mhi_sync_power_up(mhi_cntrl);
+	err = mhi_async_power_up(mhi_cntrl);
 	if (err) {
 		dev_err(&pdev->dev, "failed to power up MHI controller\n");
 		mhi_unprepare_after_power_down(mhi_cntrl);
-- 
2.44.0

Drop that patch into target/linux/x86/patches-6.1/100-patchtitle.patch

If the build system detects that the patch is dirty you might need to run update_kernel.sh to clean it.

EDIT: yep, needed to clean it.

Here is the cleaned version:

From b5a957d62351ce22af11be282919a1f34b210f9c Mon Sep 17 00:00:00 2001
From: graysky
Date: Sun, 14 Apr 2024 07:42:12 -0400
Subject: [PATCH] my patch title

---
 drivers/bus/mhi/host/pci_generic.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

--- a/drivers/bus/mhi/host/pci_generic.c
+++ b/drivers/bus/mhi/host/pci_generic.c
@@ -892,7 +892,7 @@ static void mhi_pci_recovery_work(struct
 	if (err)
 		goto err_try_reset;
 
-	err = mhi_sync_power_up(mhi_cntrl);
+	err = mhi_async_power_up(mhi_cntrl);
 	if (err)
 		goto err_unprepare;
 
@@ -996,7 +996,7 @@ static int mhi_pci_probe(struct pci_dev
 		goto err_unregister;
 	}
 
-	err = mhi_sync_power_up(mhi_cntrl);
+	err = mhi_async_power_up(mhi_cntrl);
 	if (err) {
 		dev_err(&pdev->dev, "failed to power up MHI controller\n");
 		goto err_unprepare;
@@ -1095,7 +1095,7 @@ static void mhi_pci_reset_done(struct pc
 		return;
 	}
 
-	err = mhi_sync_power_up(mhi_cntrl);
+	err = mhi_async_power_up(mhi_cntrl);
 	if (err) {
 		dev_err(&pdev->dev, "failed to power up MHI controller\n");
 		mhi_unprepare_after_power_down(mhi_cntrl);

I have tried creating the patch with quilt. These are the commands I used:

make target/linux/{clean,prepare} V=s QUILT=1
cd build_dir/target-x86_64_musl/linux-x86_64/linux-6.1.82
quilt new generic/010-main_code_fix.patch
quilt edit drivers/bus/mhi/host/pci_generic.c
quilt diff
quilt refresh
cd /home/lele/openwrt
make target/linux/update package/index V=s

I can see the patch here build_dir/target-x86_64_musl/linux-x86_64/linux-6.1.82/patches/generic and with quilt series
But I can't see the patch in target/linux/x86/patches-6.1/
What do I need to do? How can be sure the patch is applied at image building?

IDK, my method is how I did it and got the desired result.

the patch won't go magically in the target/generic directory... you need to move it from your linux/generic to target/linux/generic patches directory... maybe in hack directory.

also remember that anything in build_dir might be wiped and recompiled again

I tried copying it, but then I have an error while compiling kernel because there are two same patch (mine) in two different location

@Ansuel said this.

Ok, thanks, but it works even without moving it.

I have moved to target/linux/x86/patches-*, but how can I be sure that the patch have been applied to the image?
If I try: quilt series. It doesn't list the patch. Why?

Propably because you didn't tell quilt that there is a new patch? quilt refresh and make .../update would do that. As stated in the wiki:

https://openwrt.org/docs/guide-developer/toolchain/use-patches-with-buildsystem

If a patch is applied or not you would recognize only if there is an error and it does not build because of this. You could enable logging (V=sc) also. As described in the wiki. Another option would be flashing your image and look if it works as intended (with proper tools if available/needed).

I had done those commands

Then check the log (for the patch filename) either during build time or writing it into a file and check after build. Patches are applied very early in build process.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.