Linux Kernel & Device Driver Programming

Miscellaneous Linux Kernel Programming Patterns & Anti-Patterns

 

This file uses the W3C HTML Slidy format. The "a" key toggles between one-slide-at-a-time and single-page mode, and the "c" key toggles on and off the table of contents. The ← and → keys can be used to page forward and backward. For more help on controls see the "help?" link at the bottom.

See also the introductory page on patterns and the page on locking patterns.

Image from http://kadermannan.wordpress.com/2009/04/

Anti-Pattern: Locking with Un-Intentional Recursion

   static int i_am_active = 0;
   if (i_am_active++) goto handle_error;
   mutex_lock(&m);
   ...code that may call obscure functions...
   mutex_unlock(&m);
   i_am_active--

Anti-Pattern: Registration Before Ready

Anti-Pattern: Unnecessary Memory Allocation

Anti-Pattern: Allocating Large Local Objects

   int my_function (int my_arg) {
      char * my_buffer[some_size];
      ...maybe call some more functions...
   }

Anti-Pattern: Intentional Recursion

Anti-Pattern: Failure to Recover Memory

   if ((p = kmalloc (sizeof (*p), GFP_KERNEL))) return -ENOMEM;
   ....         
   if (some_test(some_object)) return;
   ...
   kfree (p);

Anti-Pattern: Failure to Check Return Status

   p = kmalloc (sizeof (*p), GFP_KERNEL);
   p->member = some_value;

See also the introductory page on patterns and the page on locking patterns.