<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Linux Kernel Internals on Linux-Kernel-Internals</title><link>https://ankitkdev.com/blog/</link><description>Recent content in Linux Kernel Internals on Linux-Kernel-Internals</description><generator>Hugo</generator><language>en</language><atom:link href="https://ankitkdev.com/blog/index.xml" rel="self" type="application/rss+xml"/><item><title>Char Driver</title><link>https://ankitkdev.com/blog/linux-kernel/device-driver/char-driver/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/linux-kernel/device-driver/char-driver/</guid><description>&lt;h2 id="dev_t"&gt;&lt;code&gt;dev_t&lt;/code&gt;&lt;a class="anchor" href="#dev_t"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;32 bit unsigned integer where first 20 used to store minor and rest 12 bit used to store major.&lt;/p&gt;
&lt;p&gt;see:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;include/linux/kdev_t.h&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;include/linux/types.h&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="char-devices"&gt;Char Devices&lt;a class="anchor" href="#char-devices"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;blockquote class='book-hint '&gt;
&lt;p&gt;There are 2 ways to create a char device node:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Automatically, via &lt;code&gt;class_create()&lt;/code&gt; / &lt;code&gt;device_create()&lt;/code&gt; (udev creates the &lt;code&gt;/dev&lt;/code&gt; entry).&lt;/li&gt;
&lt;li&gt;Manually, via the &lt;code&gt;mknod&lt;/code&gt; command.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;&lt;p&gt;We&amp;rsquo;ll go through both.&lt;/p&gt;
&lt;blockquote class='book-hint '&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; you must populate &lt;code&gt;struct file_operations&lt;/code&gt; &lt;em&gt;before&lt;/em&gt; binding it to &lt;code&gt;struct cdev&lt;/code&gt; (i.e. before calling &lt;code&gt;cdev_init()&lt;/code&gt;).
See &lt;a href="https://github.com/niekiran/linux-device-driver-1/blob/54e818e345cc507730fe02008749f89eda262121/custom_drivers/002pseudo_char_driver/pcd.c#L146"&gt;example&lt;/a&gt;&lt;/p&gt;</description></item><item><title>unsigned :\ 0 Bit-Fields in c</title><link>https://ankitkdev.com/blog/c/zero-width-bit-field/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/c/zero-width-bit-field/</guid><description>&lt;h3 id="unsigned--0-in-c-bit-fields"&gt;&lt;code&gt;unsigned : 0&lt;/code&gt; in C Bit-Fields&lt;a class="anchor" href="#unsigned--0-in-c-bit-fields"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;blockquote class='book-hint '&gt;
&lt;p&gt;Sometimes We have particular case, where a field must begin at the next word boundary, so a zero-width bit-field is used.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Consider the following structure:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; test {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; a : &lt;span style="color:#ae81ff"&gt;3&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; b : &lt;span style="color:#ae81ff"&gt;5&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; c : &lt;span style="color:#ae81ff"&gt;6&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;member of this struct uses only &lt;strong&gt;14 bits&lt;/strong&gt; in total (3 + 5 + 6). However, on most systems, &lt;code&gt;sizeof(struct test)&lt;/code&gt; gives &lt;strong&gt;4 bytes&lt;/strong&gt;, not 14 bits.
Because unsigned bit-fields are allocated inside an unsigned int storage unit. If unsigned int is 32 bits, all 14 bits fit into a single 32-bit storage unit.&lt;/p&gt;</description></item><item><title>Completion-Variable</title><link>https://ankitkdev.com/blog/linux-kernel/concurrency/completion-variable/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/linux-kernel/concurrency/completion-variable/</guid><description>&lt;h1 id="completion-variable"&gt;Completion-Variable&lt;a class="anchor" href="#completion-variable"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Completions are a simple synchronization mechanism that is preferable to
sleeping and waking up in some situations. If you have a task that must
simply sleep until some process has run its course, completions can do it
easily and without race conditions&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/completion.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; completion_dev {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; cdev cdev;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; completion completion; 
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; completion_dev completion_dev;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;static&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;completion_open&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; inode &lt;span style="color:#f92672"&gt;*&lt;/span&gt;inode, &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; file &lt;span style="color:#f92672"&gt;*&lt;/span&gt;filp)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	filp&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;private_data &lt;span style="color:#f92672"&gt;=&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#a6e22e"&gt;container_of&lt;/span&gt;(inode&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;i_cdev, &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; completion_dev, cdev);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;static&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;ssize_t&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;completion_read&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; file &lt;span style="color:#f92672"&gt;*&lt;/span&gt;filp, &lt;span style="color:#66d9ef"&gt;char&lt;/span&gt; __user &lt;span style="color:#f92672"&gt;*&lt;/span&gt;buf,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;			 &lt;span style="color:#66d9ef"&gt;size_t&lt;/span&gt; count, &lt;span style="color:#66d9ef"&gt;loff_t&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt;pos)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; completion_dev &lt;span style="color:#f92672"&gt;*&lt;/span&gt;dev &lt;span style="color:#f92672"&gt;=&lt;/span&gt; filp&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;private_data;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#a6e22e"&gt;pr_debug&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;process %d(%s) going to sleep&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;\n&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;, current&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;pid, current&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;comm);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#a6e22e"&gt;wait_for_completion&lt;/span&gt;(&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;dev&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;completion);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#a6e22e"&gt;pr_debug&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;awoken %d(%s)&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;\n&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;, current&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;pid, current&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;comm);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;static&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;ssize_t&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;completion_write&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; file &lt;span style="color:#f92672"&gt;*&lt;/span&gt;filp, &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;char&lt;/span&gt; __user &lt;span style="color:#f92672"&gt;*&lt;/span&gt;buf,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;				&lt;span style="color:#66d9ef"&gt;size_t&lt;/span&gt; count, &lt;span style="color:#66d9ef"&gt;loff_t&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt;pos)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; completion_dev &lt;span style="color:#f92672"&gt;*&lt;/span&gt;dev &lt;span style="color:#f92672"&gt;=&lt;/span&gt; filp&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;private_data;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#a6e22e"&gt;pr_debug&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;process %d(%s) awakening the readers...&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;\n&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;, current&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;pid, current&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;comm);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#a6e22e"&gt;complete&lt;/span&gt;(&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;dev&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;completion);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; count;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;static&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; file_operations completion_fops &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	.owner &lt;span style="color:#f92672"&gt;=&lt;/span&gt; THIS_MODULE,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	.open &lt;span style="color:#f92672"&gt;=&lt;/span&gt; completion_open,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	.read &lt;span style="color:#f92672"&gt;=&lt;/span&gt; completion_read,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	.write &lt;span style="color:#f92672"&gt;=&lt;/span&gt; completion_write,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;static&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; __init &lt;span style="color:#a6e22e"&gt;m_init&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; ret;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#a6e22e"&gt;init_completion&lt;/span&gt;(&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;completion_dev.completion);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	ret &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;alloc_chrdev_region&lt;/span&gt;(&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;dev__t&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, MODULE_NAME);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (ret) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#a6e22e"&gt;pr_debug&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Error: %d -Cant&amp;#39;t get major&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;\n&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;, ret);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; ret;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#a6e22e"&gt;cdev_init&lt;/span&gt;(&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;completion_dev.cdev, &lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;completion_fops);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	ret &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;cdev_add&lt;/span&gt;(&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;completion_dev.cdev, &lt;span style="color:#66d9ef"&gt;dev__t&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	[...],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;:&lt;/p&gt;</description></item><item><title>container_of()</title><link>https://ankitkdev.com/blog/linux-kernel/macros/container_of/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/linux-kernel/macros/container_of/</guid><description>&lt;h3 id="container_of"&gt;&lt;code&gt;container_of()&lt;/code&gt;&lt;a class="anchor" href="#container_of"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;h4 id="accessing-a-custom-struct-from-struct-inode-via-struct-cdev"&gt;Accessing a custom struct from &lt;code&gt;struct inode&lt;/code&gt; via &lt;code&gt;struct cdev&lt;/code&gt;&lt;a class="anchor" href="#accessing-a-custom-struct-from-struct-inode-via-struct-cdev"&gt;#&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;&lt;code&gt;container_of()&lt;/code&gt; lets you walk backward from an embedded member (here, &lt;code&gt;struct cdev&lt;/code&gt;) to the containing struct. This is how a driver recovers its private device context inside &lt;code&gt;file_operations&lt;/code&gt; callbacks.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/kernel.h&amp;gt; /* container_of() */&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/fs.h&amp;gt; /* struct inode, struct file */&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/cdev.h&amp;gt; /* struct cdev */&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; scull_dev {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#75715e"&gt;/* ... */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; cdev cdev;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;scull_open&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; inode &lt;span style="color:#f92672"&gt;*&lt;/span&gt;inode, &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; file &lt;span style="color:#f92672"&gt;*&lt;/span&gt;filp)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; scull_dev &lt;span style="color:#f92672"&gt;*&lt;/span&gt;dev;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	dev &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;container_of&lt;/span&gt;(inode&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;i_cdev, &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; scull_dev, cdev);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	filp&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;private_data &lt;span style="color:#f92672"&gt;=&lt;/span&gt; dev;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#75715e"&gt;/* ... */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;ssize_t&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;scull_read&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; file &lt;span style="color:#f92672"&gt;*&lt;/span&gt;filp, &lt;span style="color:#66d9ef"&gt;char&lt;/span&gt; __user &lt;span style="color:#f92672"&gt;*&lt;/span&gt;buff, &lt;span style="color:#66d9ef"&gt;size_t&lt;/span&gt; count,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		 &lt;span style="color:#66d9ef"&gt;loff_t&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt;f_pos)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; scull_dev &lt;span style="color:#f92672"&gt;*&lt;/span&gt;dev &lt;span style="color:#f92672"&gt;=&lt;/span&gt; filp&lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt;private_data;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#75715e"&gt;/* ... */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;blockquote class='book-hint '&gt;
&lt;p&gt;Pattern: stash the recovered pointer in &lt;code&gt;filp-&amp;gt;private_data&lt;/code&gt; on &lt;code&gt;open()&lt;/code&gt; so every later callback (&lt;code&gt;read&lt;/code&gt;, &lt;code&gt;write&lt;/code&gt;, &lt;code&gt;ioctl&lt;/code&gt;, &lt;code&gt;release&lt;/code&gt;, &amp;hellip;) can grab it back in one line.&lt;/p&gt;</description></item><item><title>do { ... } while (0)</title><link>https://ankitkdev.com/blog/c/do-while-loop/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/c/do-while-loop/</guid><description>&lt;h3 id="the-structural-wrapper-do----while-0"&gt;The Structural Wrapper: &lt;code&gt;do { ... } while (0)&lt;/code&gt;&lt;a class="anchor" href="#the-structural-wrapper-do----while-0"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Standard macro design pattern. Forces the macro to behave like a single C statement, so it can&amp;rsquo;t break syntax when used inside an &lt;code&gt;if/else&lt;/code&gt; without braces.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define LOG_DEBUG(msg) do { \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; printk(KERN_DEBUG msg); \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; debug_count++; \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;} while (0)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (tmp)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;LOG_DEBUG&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;checking condition&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;\n&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;do_something&lt;/span&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;blockquote class='book-hint '&gt;
&lt;p&gt;Two statements is where this trick stops being a nice-to-have and becomes necessary. Without the wrapper:&lt;/p&gt;</description></item><item><title>Error Handling with Pointers</title><link>https://ankitkdev.com/blog/kernal-internals/error-handling/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/kernal-internals/error-handling/</guid><description>&lt;h2 id="error-handling-with-pointers-in-the-kernel"&gt;Error Handling with Pointers in the Kernel&lt;a class="anchor" href="#error-handling-with-pointers-in-the-kernel"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Many kernel functions return either a valid pointer on success or an encoded error code on failure. this lets a single return value carry both success and failure info, keeping the function signature clean.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;include/linux/err.h&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define MAX_ERRNO 4095&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is the largest error number the kernel uses. No kernel pointer will ever legitimately point into the very top of the address space,
that range is reserved to encode error codes instead, Because kernel reserves a small range of addresses at the very top of the virtual address space and these addresses are invalid as actual memory locations.&lt;/p&gt;</description></item><item><title>Ioctl Interface</title><link>https://ankitkdev.com/blog/linux-kernel/device-driver/ioctl/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/linux-kernel/device-driver/ioctl/</guid><description>&lt;h1 id="ioctl"&gt;ioctl&lt;a class="anchor" href="#ioctl"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The ioctl() function manipulates the underlying device parameters of special
files. Most devices can perform operations beyond simple data transfers; user
space must often be able to request, for example, that the device lock its door,
eject its media, report error information, change a baud rate, or self destruct.
These operations are usually supported via the ioctl method.&lt;/p&gt;
&lt;blockquote class='book-hint '&gt;
&lt;p&gt;&lt;code&gt;lwn.net&lt;/code&gt; have wonderful explaination on ioctl, i suggest you read it, &lt;a href="https://lwn.net/Kernel/LDD2/ch05.lwn#t1"&gt;here&lt;/a&gt; to know the internals.&lt;/p&gt;</description></item><item><title>Kbuild Makefile</title><link>https://ankitkdev.com/blog/kernal-internals/kbuild/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/kernal-internals/kbuild/</guid><description>&lt;h2 id="kbuild-makefile"&gt;Kbuild Makefile&lt;a class="anchor" href="#kbuild-makefile"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;blockquote class='book-hint '&gt;
&lt;p&gt;These aren&amp;rsquo;t plain GNU Make, Kbuild (the kernel&amp;rsquo;s build system) reads them with special meaning when you run &lt;code&gt;make -C /lib/modules/$(uname -r)/build M=$(pwd) modules&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-makefile" data-lang="makefile"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;CFLAGS_main.o &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; -DDEBUG
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ccflags-y &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; -std&lt;span style="color:#f92672"&gt;=&lt;/span&gt;gnu99 -DENABLE_DEBUG
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;proc_fs_basic-objs &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; main.o
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;obj-m &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; proc_fs_basic.o
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;CFLAGS_main.o&lt;/code&gt;&lt;/strong&gt; - per-object compiler flags. Only &lt;code&gt;main.o&lt;/code&gt; gets &lt;code&gt;-DDEBUG&lt;/code&gt;; if you had &lt;code&gt;main.o&lt;/code&gt; and &lt;code&gt;helper.o&lt;/code&gt;, only the first sees the macro. Useful for debug-gating one file without recompiling the whole module noisily.&lt;/p&gt;</description></item><item><title>Module Parameters</title><link>https://ankitkdev.com/blog/linux-kernel/macros/kernel-module/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/linux-kernel/macros/kernel-module/</guid><description>&lt;h2 id="module-parameters"&gt;Module Parameters&lt;a class="anchor" href="#module-parameters"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/module.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/moduleparam.h&amp;gt; /* not strictly needed, linux/module.h already pulls it in */&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/kernel.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;module_param&lt;/span&gt;(name, type, perm);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;MODULE_PARM_DESC&lt;/span&gt;(myarr, &lt;span style="color:#e6db74"&gt;&amp;#34;this is my array of int&amp;#34;&lt;/span&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Pass the value at load time:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;insmod module.ko param&lt;span style="color:#f92672"&gt;=&lt;/span&gt;value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>pr_fmt()</title><link>https://ankitkdev.com/blog/linux-kernel/macros/pr-family/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/linux-kernel/macros/pr-family/</guid><description>&lt;h2 id="pr_fmt"&gt;&lt;code&gt;pr_fmt()&lt;/code&gt;&lt;a class="anchor" href="#pr_fmt"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="prefixing-the-pr_-calls"&gt;Prefixing the &lt;code&gt;pr_*()&lt;/code&gt; calls&lt;a class="anchor" href="#prefixing-the-pr_-calls"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;pr_fmt()&lt;/code&gt; is a macro that rewrites the format string of every &lt;code&gt;pr_*()&lt;/code&gt; call (&lt;code&gt;pr_info&lt;/code&gt;, &lt;code&gt;pr_warn&lt;/code&gt;, &lt;code&gt;pr_err&lt;/code&gt;, &amp;hellip;) in the file. It has to be defined &lt;strong&gt;before&lt;/strong&gt; any header that pulls in &lt;code&gt;&amp;lt;linux/printk.h&amp;gt;&lt;/code&gt;, so it always sits at the very top, above your includes.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define pr_fmt(fmt) KBUILD_MODNAME &amp;#34;:%s: &amp;#34; fmt, __func__
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/kernel.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/module.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;static&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;func&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#a6e22e"&gt;pr_warn&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;hello-world&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;\n&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;pr_fmt()&lt;/code&gt; line auto-adjusts to whichever function it&amp;rsquo;s expanded in.&lt;/p&gt;</description></item><item><title>Stringification</title><link>https://ankitkdev.com/blog/linux-kernel/macros/string/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/linux-kernel/macros/string/</guid><description>&lt;h2 id="stringification"&gt;Stringification&lt;a class="anchor" href="#stringification"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;include/linux/stringify.h&lt;/code&gt; defines:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define __stringify_1(x...) #x
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define __stringify(x...) __stringify_1(x)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Both use &lt;code&gt;#&lt;/code&gt; to turn an argument into a string literal. They differ only when the argument is a macro.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;#&lt;/code&gt; stringifies tokens &lt;em&gt;before&lt;/em&gt; expanding any macros in them. So &lt;code&gt;__stringify_1(x)&lt;/code&gt; gives you the literal text you passed, macro name included, unexpanded.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;__stringify(x)&lt;/code&gt; adds one layer of indirection. Since &lt;code&gt;x&lt;/code&gt; isn&amp;rsquo;t touched directly by &lt;code&gt;#&lt;/code&gt; inside &lt;code&gt;__stringify&lt;/code&gt;, the preprocessor expands &lt;code&gt;x&lt;/code&gt; first when it&amp;rsquo;s passed down to &lt;code&gt;__stringify_1&lt;/code&gt;, which then stringifies the &lt;em&gt;expanded&lt;/em&gt; result.&lt;/p&gt;</description></item><item><title>Error Handling</title><link>https://ankitkdev.com/blog/c/error-handling/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/c/error-handling/</guid><description>&lt;h3 id="error-handling-for-complex-cases"&gt;Error Handling for complex cases&lt;a class="anchor" href="#error-handling-for-complex-cases"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Standard kernel pattern for unwinding partial allocations when a loop fails mid-way.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; ret;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; (i &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;; i &lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt; count; i&lt;span style="color:#f92672"&gt;++&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; arr1[i] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;kmalloc&lt;/span&gt;(size1, GFP_KERNEL);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (&lt;span style="color:#f92672"&gt;!&lt;/span&gt;arr1[i]) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ret &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#f92672"&gt;-&lt;/span&gt;ENOMEM;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;goto&lt;/span&gt; err;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; arr2[i] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;kmalloc&lt;/span&gt;(size2, GFP_KERNEL);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (&lt;span style="color:#f92672"&gt;!&lt;/span&gt;arr2[i]) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#75715e"&gt;/* error handling in mid-way loop */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;kfree&lt;/span&gt;(arr1[i]);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; arr1[i] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; NULL;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ret &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#f92672"&gt;-&lt;/span&gt;ENOMEM;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;goto&lt;/span&gt; err;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;err:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;while&lt;/span&gt; (&lt;span style="color:#f92672"&gt;--&lt;/span&gt;i &lt;span style="color:#f92672"&gt;&amp;gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#a6e22e"&gt;kfree&lt;/span&gt;(arr2[i]);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#a6e22e"&gt;kfree&lt;/span&gt;(arr1[i]);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; ret;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>Proc-Interface</title><link>https://ankitkdev.com/blog/linux-kernel/device-driver/proc/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/linux-kernel/device-driver/proc/</guid><description>&lt;h2 id="proc-interface"&gt;Proc Interface&lt;a class="anchor" href="#proc-interface"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;/proc&lt;/code&gt; is the classic way a driver hands kernel-side information to userspace without going through a real block device. There are three ways to implement the read side of a proc entry, depends on whether you&amp;rsquo;re exposing a single fixed value or walking a list.&lt;/p&gt;
&lt;blockquote class='book-hint '&gt;
&lt;p&gt;Keep a note that this method is &lt;strong&gt;depreciated&lt;/strong&gt; for device driver.&lt;/p&gt;
&lt;/blockquote&gt;&lt;h3 id="1-raw-file_operations---the-old-way"&gt;1. Raw &lt;code&gt;file_operations&lt;/code&gt; - the old way&lt;a class="anchor" href="#1-raw-file_operations---the-old-way"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;This is the original method: you wire up &lt;code&gt;.read&lt;/code&gt;, &lt;code&gt;.write&lt;/code&gt;, &lt;code&gt;.open&lt;/code&gt;, &lt;code&gt;.llseek&lt;/code&gt;, and &lt;code&gt;.release&lt;/code&gt; yourself, exactly like a normal character device.&lt;/p&gt;</description></item><item><title>programming concepts</title><link>https://ankitkdev.com/blog/theory/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/theory/</guid><description>&lt;p&gt;In this section, I&amp;rsquo;ll try to cover the programming concepts.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="thundering-herd-problem"&gt;&amp;lsquo;Thundering Herd problem&amp;rsquo;&lt;a class="anchor" href="#thundering-herd-problem"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;where hundreds of processes are woken up at once to fight for the same lock.&lt;/p&gt;
&lt;p&gt;We can observe this situation when &lt;code&gt;wait_queue_head&lt;/code&gt; is waked up by &lt;code&gt;wake_up_all&lt;/code&gt;; multiple list element trying to access the same lock/resources.
To Handle these situation its better to try exclusive wake up - &lt;code&gt;wake_up{,_nr}&lt;/code&gt;.&lt;/p&gt;
&lt;hr&gt;</description></item><item><title>Build &amp; Install Linux Kernel for BBB</title><link>https://ankitkdev.com/blog/build-linux-kernel-BBB/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/build-linux-kernel-BBB/</guid><description>&lt;h1 id="build-and-install-linux-kernel-for-beaglebone-black"&gt;Build and Install Linux Kernel For Beaglebone Black&lt;a class="anchor" href="#build-and-install-linux-kernel-for-beaglebone-black"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="overview"&gt;Overview&lt;a class="anchor" href="#overview"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The BeagleBone Black (BBB) uses an ARM Cortex-A8 32 bit processor, so the kernel must be cross-compiled on an x86 machine and then deployed to the board. This blog will assume that you already have a os[debian/ubuntu] installed on your sd card.&lt;/p&gt;
&lt;p&gt;We’ll:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;set up toolchain&lt;/li&gt;
&lt;li&gt;fetch kernel source&lt;/li&gt;
&lt;li&gt;configure for BBB&lt;/li&gt;
&lt;li&gt;build kernel + modules&lt;/li&gt;
&lt;li&gt;deploy to SD card / board&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="1-prerequisites"&gt;1. Prerequisites&lt;a class="anchor" href="#1-prerequisites"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Make sure you have:&lt;/p&gt;</description></item><item><title>Build &amp; Install Linux Kernel for BBB</title><link>https://ankitkdev.com/blog/misc/build-linux-kernel-BBB/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/misc/build-linux-kernel-BBB/</guid><description>&lt;h1 id="build-and-install-linux-kernel-for-beaglebone-black"&gt;Build and Install Linux Kernel For Beaglebone Black&lt;a class="anchor" href="#build-and-install-linux-kernel-for-beaglebone-black"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="overview"&gt;Overview&lt;a class="anchor" href="#overview"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The BeagleBone Black (BBB) uses an ARM Cortex-A8 32 bit processor, so the kernel must be cross-compiled on an x86 machine and then deployed to the board. This blog will assume that you already have a os[debian/ubuntu] installed on your sd card.&lt;/p&gt;
&lt;p&gt;We’ll:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;set up toolchain&lt;/li&gt;
&lt;li&gt;fetch kernel source&lt;/li&gt;
&lt;li&gt;configure for BBB&lt;/li&gt;
&lt;li&gt;build kernel + modules&lt;/li&gt;
&lt;li&gt;deploy to SD card / board&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="1-prerequisites"&gt;1. Prerequisites&lt;a class="anchor" href="#1-prerequisites"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Make sure you have:&lt;/p&gt;</description></item><item><title>C Memory Layout</title><link>https://ankitkdev.com/blog/c-memory-layout/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/c-memory-layout/</guid><description>&lt;h1 id="c-memory-layout-structs-unions-and-a-linux-kernel-patch"&gt;C Memory Layout: Structs, Unions, and a Linux Kernel Patch&lt;a class="anchor" href="#c-memory-layout-structs-unions-and-a-linux-kernel-patch"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="overview"&gt;Overview&lt;a class="anchor" href="#overview"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C memory layout looks simple in isolation: structs store fields sequentially, unions overlap memory, and flexible array members allow variable-sized trailing storage. But combining these features can create subtle layout issues.&lt;/p&gt;
&lt;p&gt;While working on a Linux kernel selftest, I encountered a case where a union contained multiple structures, each ending with a flexible array member at different offsets. Although the code was functionally correct, this confused the compiler into treating the union as effectively variable-sized, triggering a layout warning.&lt;/p&gt;</description></item><item><title>C Memory Layout</title><link>https://ankitkdev.com/blog/c/c-memory-layout/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/c/c-memory-layout/</guid><description>&lt;h1 id="c-memory-layout-structs-unions-and-a-linux-kernel-patch"&gt;C Memory Layout: Structs, Unions, and a Linux Kernel Patch&lt;a class="anchor" href="#c-memory-layout-structs-unions-and-a-linux-kernel-patch"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="overview"&gt;Overview&lt;a class="anchor" href="#overview"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C memory layout looks simple in isolation: structs store fields sequentially, unions overlap memory, and flexible array members allow variable-sized trailing storage. But combining these features can create subtle layout issues.&lt;/p&gt;
&lt;p&gt;While working on a Linux kernel selftest, I encountered a case where a union contained multiple structures, each ending with a flexible array member at different offsets. Although the code was functionally correct, this confused the compiler into treating the union as effectively variable-sized, triggering a layout warning.&lt;/p&gt;</description></item><item><title>Configure U-boot for BBB</title><link>https://ankitkdev.com/blog/configure-uboot-bbb/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/configure-uboot-bbb/</guid><description>&lt;h1 id="configure-u-boot-for-beaglebone-black"&gt;Configure U-boot for BeagleBone Black&lt;a class="anchor" href="#configure-u-boot-for-beaglebone-black"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="overview"&gt;Overview&lt;a class="anchor" href="#overview"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;U-Boot (Universal Boot Loader) is the go-to bootloader for embedded Linux systems. If you&amp;rsquo;re working with the BeagleBone Black (BBB), building U-Boot from source gives you full control over the boot process, from initializing hardware to loading your kernel. This guide walks you through cross-compiling and installing U-Boot on the BBB from scratch.&lt;/p&gt;
&lt;p&gt;This blog will explain how to compile and install U-Boot, and then either manually boot from the U-Boot prompt or use &lt;strong&gt;&lt;code&gt;uEnv.txt&lt;/code&gt;&lt;/strong&gt; to autoboot the kernel.
You can also use &lt;code&gt;extlinux/extlinux.conf&lt;/code&gt; to boot the kernel.&lt;/p&gt;</description></item><item><title>Configure U-boot for BBB</title><link>https://ankitkdev.com/blog/misc/configure-uboot-bbb/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/misc/configure-uboot-bbb/</guid><description>&lt;h1 id="configure-u-boot-for-beaglebone-black"&gt;Configure U-boot for BeagleBone Black&lt;a class="anchor" href="#configure-u-boot-for-beaglebone-black"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="overview"&gt;Overview&lt;a class="anchor" href="#overview"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;U-Boot (Universal Boot Loader) is the go-to bootloader for embedded Linux systems. If you&amp;rsquo;re working with the BeagleBone Black (BBB), building U-Boot from source gives you full control over the boot process, from initializing hardware to loading your kernel. This guide walks you through cross-compiling and installing U-Boot on the BBB from scratch.&lt;/p&gt;
&lt;p&gt;This blog will explain how to compile and install U-Boot, and then either manually boot from the U-Boot prompt or use &lt;strong&gt;&lt;code&gt;uEnv.txt&lt;/code&gt;&lt;/strong&gt; to autoboot the kernel.
You can also use &lt;code&gt;extlinux/extlinux.conf&lt;/code&gt; to boot the kernel.&lt;/p&gt;</description></item><item><title>Debugging Macros</title><link>https://ankitkdev.com/blog/linux-kernel/macros/debug/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/linux-kernel/macros/debug/</guid><description>&lt;h1 id="debugging-macros"&gt;Debugging Macros&lt;a class="anchor" href="#debugging-macros"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;This section will contain all macros related to kernel debugging.&lt;/p&gt;
&lt;h2 id="might_sleep"&gt;&lt;code&gt;might_sleep&lt;/code&gt;&lt;a class="anchor" href="#might_sleep"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;configs&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CONFIG_DEBUG_ATOMIC_SLEEP&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;this macros is useful to detect sleeping function called in Non sleeping code-paths,
and implies that code path should not be executed in atomic context, since it can sleep.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;/**
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; * might_sleep - annotation for functions that can sleep
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; *
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; * this macro will print a stack trace if it is executed in an atomic
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; * context (spinlock, irq-handler, ...). Additional sections where blocking is
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; * not allowed can be annotated with non_block_start() and non_block_end()
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; * pairs.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; *
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; * This is a useful debugging help to be able to catch problems early and not
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; * be bitten later when the calling function happens to sleep when it is not
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; * supposed to.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# define might_sleep() \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>Forward Declarations</title><link>https://ankitkdev.com/blog/c/forward-declaration-in-c/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/c/forward-declaration-in-c/</guid><description>&lt;h1 id="understanding-forward-declarations-in-c"&gt;Understanding Forward Declarations in C&lt;a class="anchor" href="#understanding-forward-declarations-in-c"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;One of the best ways to learn systems programming is by reading real-world code. Recently, while reading the Linux kernel source, I noticed something interesting inside &lt;code&gt;drivers/pinctrl/core.h&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/kref.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/list.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/mutex.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/radix-tree.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/types.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;linux/pinctrl/machine.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; dentry;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; device;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; device_node;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; module;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;At first glance, these lines seem incomplete. We see declarations like &lt;code&gt;struct device;&lt;/code&gt; but no actual definition. Where is the complete structure with all its members? Why didn&amp;rsquo;t file defining this structure isn&amp;rsquo;t included?&lt;/p&gt;</description></item><item><title>Install Ubuntu Server on Qemu</title><link>https://ankitkdev.com/blog/misc/install-ubuntu-server-on-qemu/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/misc/install-ubuntu-server-on-qemu/</guid><description>&lt;h1 id="install-ubuntu-server-on-qemu"&gt;Install Ubuntu Server on Qemu&lt;a class="anchor" href="#install-ubuntu-server-on-qemu"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="installation-of-ubuntu-server-on-qemu-x86_64"&gt;Installation of ubuntu server on qemu-x86_64&lt;a class="anchor" href="#installation-of-ubuntu-server-on-qemu-x86_64"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;At First I want to run the custom modules on linux. But this is something that is not good to do on host machine. So i searched a lot of stuff. Tinkering with buildroot, after some days i was able to run the build on qemu.&lt;/p&gt;
&lt;p&gt;But i found out build actually didn’t have my basic tool like &lt;code&gt;gnu make&lt;/code&gt;. Everytime i try to do something i need to find particular tool on &lt;code&gt;menuconfig&lt;/code&gt; then make the build again. Learning was so slow.&lt;/p&gt;</description></item><item><title>Linux Kernel Mentorship Program</title><link>https://ankitkdev.com/blog/linux-kernel-mentorship-program/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/linux-kernel-mentorship-program/</guid><description>&lt;h1 id="linux-kernel-mentorship-program"&gt;Linux Kernel Mentorship Program&lt;a class="anchor" href="#linux-kernel-mentorship-program"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="introduction"&gt;Introduction&lt;a class="anchor" href="#introduction"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I have been using Linux for the last two years and gradually developed an interest in systems programming and C. The mentorship prerequisites includes tasks like building and booting the Linux kernel, writing a basic kernel module, decoding stack traces, and modifying and booting a custom kernel build.&lt;/p&gt;
&lt;p&gt;Somehow, my application was accepted ;-).
&lt;label class="book-image" for="book-image-toggle-2"&gt;&lt;input class="hidden toggle" type="checkbox" id="book-image-toggle-2" /&gt;&lt;img src="https://ankitkdev.com/blog/lfx-acceptance.png" alt="Description" loading="lazy" title="lfx-acceptance" /&gt;&lt;/label&gt;&lt;/p&gt;
&lt;p&gt;At first, the Linux Kernel seemed quite difficult to understand what was going on. We were initially told to choose two subsystems to work on.&lt;/p&gt;</description></item><item><title>Linux Kernel Mentorship Program</title><link>https://ankitkdev.com/blog/misc/linux-kernel-mentorship-program/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/misc/linux-kernel-mentorship-program/</guid><description>&lt;h1 id="linux-kernel-mentorship-program"&gt;Linux Kernel Mentorship Program&lt;a class="anchor" href="#linux-kernel-mentorship-program"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="introduction"&gt;Introduction&lt;a class="anchor" href="#introduction"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I have been using Linux for the last two years and gradually developed an interest in systems programming and C. The mentorship prerequisites includes tasks like building and booting the Linux kernel, writing a basic kernel module, decoding stack traces, and modifying and booting a custom kernel build.&lt;/p&gt;
&lt;p&gt;Somehow, my application was accepted ;-).
&lt;label class="book-image" for="book-image-toggle-2"&gt;&lt;input class="hidden toggle" type="checkbox" id="book-image-toggle-2" /&gt;&lt;img src="https://ankitkdev.com/blog/lfx-acceptance.png" alt="Description" loading="lazy" title="lfx-acceptance" /&gt;&lt;/label&gt;&lt;/p&gt;
&lt;p&gt;At first, the Linux Kernel seemed quite difficult to understand what was going on. We were initially told to choose two subsystems to work on.&lt;/p&gt;</description></item><item><title>Return value by Macro</title><link>https://ankitkdev.com/blog/c/return-by-macro/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/c/return-by-macro/</guid><description>&lt;p&gt;code example of macro which returns a value.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define check(condition) \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; ({ \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; int __ret = 0; \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; if (!(condition)) \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; { \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; printf(&amp;#34;Condition failed!\n&amp;#34;); \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; __ret = -1; \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; } \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; __ret; \
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;main&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; x &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;10&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; result &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;check&lt;/span&gt;(x &lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;15&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;printf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;result: %d&amp;#34;&lt;/span&gt;, result);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>SLUB debug</title><link>https://ankitkdev.com/blog/kernel-debugging/debug-slub-memory/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/kernel-debugging/debug-slub-memory/</guid><description>&lt;h1 id="basics-of-debugging-slab-memory-corruption-via-slub-debug"&gt;Basics of debugging slab memory corruption via SLUB debug&lt;a class="anchor" href="#basics-of-debugging-slab-memory-corruption-via-slub-debug"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="introduction"&gt;Introduction&lt;a class="anchor" href="#introduction"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Memory corruption can occur due to various bugs or defects: Uninitialized Memory
Reads (UMR), Use After Free (UAF), Use After Return (UAR), double-free, memory
leakage, or illegal Out Of Bounds (OOB) accesses that attempt to work upon (read/write/
execute) illegal memory regions.&lt;/p&gt;
&lt;p&gt;Since memory is dynamically allocated and freed via the kernel&amp;rsquo;s engine – the
page allocator. This can lead to serious wastage (internal fragmentation) of memory.
To mitigate this, the slab allocator (or slab cache) is layered upon it, serving two
primary tasks – providing fragments of pages efficiently (within the kernel, allocation
requests for small pieces of memory, from a few bytes to a couple of kilobytes), and serving as a cache for commonly used data structures.&lt;/p&gt;</description></item><item><title>Variadic Macro</title><link>https://ankitkdev.com/blog/c/variadic-macro/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://ankitkdev.com/blog/c/variadic-macro/</guid><description>&lt;p&gt;In kernel you&amp;rsquo;ll see two type of variaidic macro&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define __stringify(x...)	 __stringify_1(x)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;this is same as&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#define __stringify(...)	 __stringify_1(__VA_ARGS__)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In standard C99 and later, a variadic macro must look like above one,
However, GCC introduced an extension that allows you to give a name to the variable arguments by placing the ellipsis immediately after an identifier &lt;code&gt;(x...)&lt;/code&gt;.
When you write &lt;code&gt;x...&lt;/code&gt;, the compiler treats x as a named parameter that bundles everything passed into the macro (including any commas separating multiple arguments).&lt;/p&gt;</description></item></channel></rss>