Kernel Korner - Allocating Memory in the Kernel
With a little understanding, getting a hold of memory in the kernel is demystified and not too much more difficult to do than it is in user space. A few simple rules of thumb can go a long way:
Decide whether you can sleep (that is, whether the call to kmalloc() can block). If you are in an interrupt handler, in a bottom half, or if you hold a lock, you cannot. If you are in process context and do not hold a lock, you probably can.
If you can sleep, specify GFP_KERNEL.
If you cannot sleep, specify GFP_ATOMIC.
If you need DMA-capable memory (for example, for an ISA or broken PCI device), specify GFP_DMA.
Always check for and handle a NULL return value from kmalloc().
Do not leak memory; make sure you call kfree() somewhere.
Ensure that you do not race and call kfree() multiple times and that you never access a block of memory after you free it.
For more information, check out these files in your kernel source tree.
include/linux/gfp.h: home of the allocation flags.
include/linux/slab.h: definitions of kmalloc(), et al.
mm/page_alloc.c: page allocation functions.
mm/slab.c: implementation of kmalloc(), et al.
Robert Love (firstname.lastname@example.org) is a kernel hacker at MontaVista Software and a student at the University of Florida. He is the author of Linux Kernel Development. Robert enjoys fine wine and lives in Gainesville, Florida.
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
|Trying to Tame the Tablet||May 08, 2013|
|Dart: a New Web Programming Experience||May 07, 2013|
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Validate an E-Mail Address with PHP, the Right Way
- New Products
- Trying to Tame the Tablet
- Tech Tip: Really Simple HTTP Server with Python
- git-annex assistant
1 hour 26 min ago
- direct cable connection
1 hour 49 min ago
- Agreed on AirDroid. With my
1 hour 59 min ago
- I just learned this
2 hours 3 min ago
2 hours 33 min ago
- not living upto the mobile revolution
5 hours 25 min ago
- Deceptive Advertising and
6 hours 50 sec ago
- Let\'s declare that you have
6 hours 1 min ago
- Alterations in Contest Due
6 hours 2 min ago
- At a numbers mindset, your
6 hours 4 min ago