How to Apply Basler Camera USB zerocopy patch to Linux kernel (beginner)


  • This is my very first attempt in apply a kernel patch ever for Linux.
  • Do not recommend to try this on your day-to-day use OS/PC. Try on a virtual machine or a spare pc which you can afford to lose data.

Download the patch/source codes:

System I’m using: Ubuntu 16.04, Kernel 4.15.0, 30GB disk space (virtual machine)

Trial #1:

Certainly, here I have very little idea what I’m doing. However, my motivation to do this is after reading this post of Building Realtime Linux for ROS2.

Typically, if you google “how to apply patch for linux kernel”, the common instruction is to use a ready patch file that one can download somewhere. My (or our) problem is there is no such file here but a full kernel source codes with some modifications that are too much to track.

After some more reading (here0 & here1), I learned how to generate a patch file. I went ahead and download the source codes for linux-4.15 kernel at linux public github Create kernel folder and download + un-compress linux-4.15 and linux-4.2.y-usb-zerocopyto this folder. To create the patch file:

cd ~/kernel
diff -uNr linux-4.15 linux-4.2.y-usb-zerocopy > patch-4.15-zerocopy

After the step above, a patch file name patch-4.15-zerocopy is generated inside kernel folder. However, this file is rather large by hundred of MBs. But do I know this is flagging something? No, not really. So I went ahead to apply the patching process.

cd ~/kernel/linux-4.15
patch -p1 < ../patch-4.15-zerocopy

Side note: the idea here is to compile kernel 4.15 with the patch details. The patch step happen when we are in the kernel 4.15 folder and call for the patch file from there. And -p1 tag for patch file is necessary for the setup…read the here1 link I posted above for explanation of -p1 tag.

After the patch step is successful, the next step is to compile the patched kernel. First, we need to make a copy of current config file and compile the oldconfig (check out the ROS2 link after the patch step) with:

cp /boot/config-`uname -r` .config
yes '' | make oldconfig

Right about here, I ran into problem. make oldconfig was not compiling. It complained for missing file. So I went to check for the file it complained that was not found in linux-4.15 folder. Some files were indeed missing. So what happened was, the 4.15 kernel is so far ahead of the 4.2.y-usb-zerocopy version. A lot of changes have happened. By applying a patch with source code of an old kernel, I also deleted a lot of new changes. Therefore, I got many files not found error.

Trial #2:

I re-did trial #1 with the only difference that I downloaded linux-4.2 source code to be closer to the linux-4.2.y-usb-zerocopy. I created a new patch file name patch-4.2-zerocopy and this file is significantly smaller than the last file with kbs size. So that means, there weren’t a lot of changes between two versions.

I made it through the last failing step which was

yes '' | make oldconfig

Then I continued with actually building the kernel with:

make -j4

make only should also work. -j4 is to utilize 4 cpu cores to build faster. At this step, you can sit back, relax or go do something else because this step would take awhile. Check back once in awhile to see if anything failing.

After the previous step complete, we can go ahead to build kernel modules and install kernel to boot with:

sudo make modules_install
sudo make install

If everything went well, congrats, we are done applying the zerocopy patch for Basler camera. If you have dual boot, then you don’t have to worry about this. But one last step to do is to update grub so we can boot into the kernel we just built. I use the steps in this video. Select linux-4.2.3 when you come to the list of option

After getting into Linux, you can enable and disable zerocopy with instruction here:

Next implementation: I want to add the changes to newer kernel

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s