<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Every&#39;s Blog</title>
    <link rel="self" type="application/atom+xml" href="https://every2.codeberg.page/blog/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://every2.codeberg.page/blog/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-04-22T00:00:00+00:00</updated>
    <id>https://every2.codeberg.page/blog/atom.xml</id>
    <entry xml:lang="en">
        <title>My First Code Patch in the Linux Kernel</title>
        <published>2026-04-22T00:00:00+00:00</published>
        <updated>2026-04-22T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://every2.codeberg.page/blog/posts/my-first-code-patch-in-the-linux-kernel/"/>
        <id>https://every2.codeberg.page/blog/posts/my-first-code-patch-in-the-linux-kernel/</id>
        
        <content type="html" xml:base="https://every2.codeberg.page/blog/posts/my-first-code-patch-in-the-linux-kernel/">&lt;h1 id=&quot;overview&quot;&gt;Overview&lt;/h1&gt;
&lt;p&gt;This patch is about the process to convert a stream of bytes into
specific data types.&lt;/p&gt;
&lt;h2 id=&quot;modeling-the-problem&quot;&gt;Modeling the problem&lt;/h2&gt;
&lt;p&gt;Let&#39;s think of a stream of bytes as a block, so if I have &lt;em&gt;one byte&lt;/em&gt; (aka
8 bits) I&#39;ll use &lt;em&gt;one block&lt;/em&gt; and in this block I&#39;ve the data I wanna
transform, in this case imagine it as the number 2 (0b10).&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://codeberg.org/Every2/blog/raw/branch/main/images/stream-byte-representation.png&quot; alt=&quot;Visual representation of a stream of bytes as a block&quot; /&gt;&lt;/p&gt;
&lt;h3 id=&quot;shaping-the-block&quot;&gt;Shaping the block&lt;/h3&gt;
&lt;p&gt;So far we&#39;ve worked with the abstract form of the data which&#39;s a block
with the content inside, but what I want is to shape and manipulate
this data. In high level languages like Rust, we&#39;ve types that accept
&lt;em&gt;bit patterns&lt;/em&gt;, so if I have one block I can call it as u8 data type.&lt;/p&gt;
&lt;h1 id=&quot;getting-the-hands-dirty&quot;&gt;Getting the hands dirty&lt;/h1&gt;
&lt;p&gt;Now we understood the problem, so we&#39;re capable of starting
translating it into Rust. Remember that I called it a block? Now we
represent the blocks as a &lt;a rel=&quot;external&quot; href=&quot;https://doc.rust-lang.org/std/primitive.slice.html&quot;&gt;Slice&lt;/a&gt; of Bytes (u8).&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B1D6; background-color: #1A1B26;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;use&lt;/span&gt;&lt;span style=&quot;color: #0DB9D7;&quot;&gt; core&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #0DB9D7;&quot;&gt;mem&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span&gt;size_of&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;unsafe impl&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; FromBytes&lt;/span&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; u8&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    fn&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt; from_bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt;bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;: &amp;amp;[&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt;u8&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;]) -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; Option&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #F7768E;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt;        let&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; slice_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt;as_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt;cast&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F7768E;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #0DB9D7;&quot;&gt; core&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #0DB9D7;&quot;&gt;mem&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt;size_of&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F7768E;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;            unsafe {&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; Some&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&amp;amp;*&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt;slice_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        }&lt;/span&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt;            None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;span style=&quot;color: #51597D;font-style: italic;&quot;&gt; /// Mutable implementation below&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Wow, great job! It&#39;s finished, right? Not yet!!&lt;/p&gt;
&lt;h2 id=&quot;alignment&quot;&gt;Alignment&lt;/h2&gt;
&lt;p&gt;Rust doesn&#39;t have the &lt;em&gt;unsafe&lt;/em&gt; keyword for nothing. When you convert
the slice to a type. it&#39;s possible that the data isn&#39;t aligned with
its original form for any reason, such something going wrong during
the cast. So we need to check!&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B1D6; background-color: #1A1B26;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;use&lt;/span&gt;&lt;span style=&quot;color: #0DB9D7;&quot;&gt; core&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #0DB9D7;&quot;&gt;mem&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span&gt;size_of&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;unsafe impl&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; FromBytes&lt;/span&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; u8&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    fn&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt; from_bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt;bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;: &amp;amp;[&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt;u8&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;]) -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; Option&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #F7768E;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt;        let&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; slice_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt;as_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt;cast&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F7768E;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #0DB9D7;&quot;&gt; core&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #0DB9D7;&quot;&gt;mem&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt;size_of&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F7768E;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;()&lt;/span&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; slice_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt;is_aligned&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;            unsafe {&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; Some&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&amp;amp;*&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt;slice_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        }&lt;/span&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt;            None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;span style=&quot;color: #51597D;font-style: italic;&quot;&gt; /// Mutable implementation below&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If for some reason, you wanna do it manually just change the if to:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B1D6; background-color: #1A1B26;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #0DB9D7;&quot;&gt; size_of&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F7768E;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;()&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #BB9AF7;&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt;ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color: #C0CAF5;&quot;&gt; usize&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) %&lt;/span&gt;&lt;span style=&quot;color: #7AA2F7;&quot;&gt; align_of&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F7768E;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;() {}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h1 id=&quot;the-end-and-acknowledgements&quot;&gt;The end and acknowledgements&lt;/h1&gt;
&lt;p&gt;For the last part of the patch, I wrote the documentation that was
probably the hardest part to me and it&#39;s very important, so if you plan to
contribute to Rust for Linux know that it&#39;s a big point for your patchs. I
have to thanks a lot people from Nvidia and &lt;strong&gt;Alexandre Corbout&lt;/strong&gt; in
special that give me a lot of support in the patch approval process.&lt;/p&gt;
&lt;p&gt;Thanks for &lt;a rel=&quot;external&quot; href=&quot;https://lkcamp.dev/&quot;&gt;lkcamp&lt;/a&gt; too, I began my journey there.&lt;/p&gt;
&lt;p&gt;A huge thanks for my girlfriend that show lkcamp to me and give me
inconditional support.&lt;/p&gt;
&lt;p&gt;If you wanna see the code working &lt;a rel=&quot;external&quot; href=&quot;https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2024&amp;amp;gist=a153d3d64fe535cc466f2ebd46f6be0b&quot;&gt;click here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Link for the patch &lt;a rel=&quot;external&quot; href=&quot;https://lore.kernel.org/rust-for-linux/20250824213134.27079-1-christiansantoslima21@gmail.com/&quot;&gt;Discussion&lt;/a&gt;&lt;/p&gt;
</content>
        
    </entry>
</feed>
