The linux3.5 kernel allocates a 8k or 4k kernel stack for all processes (including kernel processes and user processes) (which can be selected at the time of kernel compilation), and the stack memory allocated in the module code is in the kernel stack, and if the module code is to allocate memory in the heap, it will be allocated using Kmalloc or Vmalloc, which is something.
The stack bottom of the kernel stack holds this thread_info, by acquiring the kernel stack to get thread_info information, thread_info inside a pointer to the TASK_STRUCT (process descriptor, which contains various information about the process), through which the pointer You can get a variety of information about the process.
The program that gets the name of the current process is written to better understand the kernel stack.
1#include <linux/init.h>2#include <linux/module.h>3#include <asm/thread_info.h>4#include <linux/sched.h>5 6Module_license ("GPL");7Module_author ("Bunfly");8 9 intTest_init ()Ten { One inti =0; A structThread_info *p =NULL; -p = (structThread_info *) ((unsignedLong) &i & ~0x1fff); - structTask_struct *t = p->task; thePrintk"Task Name is%s\n", t->comm); - - return 0; - } + - voidtest_exit () + { APrintk"Bye bye\n"); at } - - Module_init (test_init); -Module_exit (Test_exit);
Compiled into modules, the results of the operation on the Development Board are as follows;
The following key code is explained below:
Line 11th: Define an integer variable i on the kernel stack
Line 12th, define pointer p to thread_info struct
Line 13th: As shown, the known thread_info structure in the stack bottom of the kernel stack, integer variable i on the stack, I can get the address of the kernel stack size is 8k. Find the address of the thread_info structure . The method is the 13th line of code: The I is cast to unsigned long, then the back 13 position zero, and then strong to the thread_info structure pointer. It's not hard to understand why. Lenovo memory of the section page storage, memory address in front of the base address, followed by an offset, the size of the kernel stack is 8k. The description is offset in 8k size. The post 13 position zero is to find the base address, and the thread_info structure is at the bottom of the stack, that is, the base address is the address of the thread_info structure .
Line 14th defines the memory descriptor task_struct The struct pointer T, and saves the address of the task in the thread_info struct body .
The 15th line outputs the process name.
All the processes in the system are connected to a two-way circular list, and now we are outputting all the process names in the system by the process descriptor we have found.
1#include <linux/init.h>2#include <linux/module.h>3#include <asm/thread_info.h>4#include <linux/sched.h>5 6Module_license ("GPL");7Module_author ("Bunfly");8 9 intTest_init ()Ten { One //PRINTK ("name is%s\n", current->comm); A structThread_info *p =NULL; -p = (structThread_info *) ((unsignedLong) &p & ~ (8192-1)); - the structTask_struct *head = p->task; - structTask_struct *tmp =head; - Do{ -Printk"name is%s\n", tmp->comm); +TMP = container_of (Tmp->tasks.next,structtask_struct, tasks); -} while(TMP! =head); + A at return 0; - } - - voidtest_exit () - { -Printk"Bye bye\n"); in } - to Module_init (test_init); +Module_exit (Test_exit);
CONTAINER_OF macro is widely used in the kernel, it must be read.
Day 41st: Linux kernel stack