Retrieving current process/task_struct in linux kernel

As you know, processes are represented in the kernel by a structure called ‘task_struct’. All the processes in kernel got its own kernel stack. There is also an important structure called “thread_info” which is of size “52” bytes. .. Where “task” field in the thread_info structure is the association of process descriptor with the thread_info. The “rsp” or “esp” ( stack pointers) can be effectively used by the kernel to retrieve information about the active process on the CPU.

struct thread_info { struct task_struct *task; /* main task structure */

*****

‘thread_info’ and the ‘stack’ is a union as shown below.

union thread_union { struct thread_info thread_info; unsigned long stack[THREAD_SIZE/sizeof(long)]; };


Where THREAD_SIZE is equal ( After macro filling) to ((_AC(1,UL) << 12) << 1) which is 8192 ( for 8k stack).

To get ‘current’ process information, the kernel makes use of below macro in an effective way.

movl $0xffffe000,%ecx /* or 0xfffff000 for 4KB stacks */ andl %esp,%ecx movl (%ecx),p

line number “2” performs the main operation here. The stack pointer “esp”‘s last “13” or “12” bits are masked ( 8k and 4k stacks effectively) to get the thread_info structure. The thread_info structure’s “0th” offset holds a pointer to the ‘task_struct’. Thus kernel can easily retrieve the currently running process (line 3) from the CPU.

The “current” variable in kernel code refers to this task_struct.

#define get_current() (current_thread_info()->task)

grep/display number of lines before and after matching a pattern

I know lots of people are looking for a command-line method on grepping a particular line and some lines BEFORE OR AFTER that pattern matched line…. There are atleast 2 ways which I know :).. Method 1 : using GREP I think it will be self explanatory by the below example… [hchiramm@hchiramm tmp]$ cat /etc/sysconfig/network-scripts/ifcfg-eth0 …

Read more

How can we do pci pass-through or device assignment with kvm?

This is a very interesting feature which provides you the capability of passing a “PCI” device to a guest system bypassing the HOST . Below Red Hat kbase gives you information about doing pci pass-through with XEN. http://kbase.redhat.com/faq/docs/DOC-17364 With KVM , pci pass-through can be achieved by two ways. * virt-manager* Command line method. PCI …

Read more

Easy way to know, which command/process is sleeping and in which kernel function using WCHAN?..

I quite oftenly use below command to know the same..

# ps -eopid,tt,user,fname,tmout,f,wchan

The important bit here is “WCHAN” field and the “COMMAND” field.. “WCHAN” points to the “sleeping function” in kernel mode..

WCHAN : address of the kernel function where the process is sleeping (use wchan if you want the kernel
                           function name). Running tasks will display a dash (‘-‘) in this column.

[root@humbles-lap ~]$ ps -eopid,tt,user,fname,tmout,f,wchan
  PID TT       USER     COMMAND  TMOUT F WCHAN
1 ?        root     systemd      – 4 epoll_wait
2 ?        root     kthreadd     – 1 kthreadd
3 ?        root     ksoftirq     – 1 run_ksoftirqd
6 ?        root     migratio     – 1 cpu_stopper_thread
7 ?        root     watchdog     – 5 watchdog
21 ?        root     cpuset       – 1 rescuer_thread
22 ?        root     khelper      – 1 rescuer_thread
23 ?        root     kdevtmpf     – 5 devtmpfsd
24 ?        root     netns        – 1 rescuer_thread
25 ?        root     sync_sup     – 1 bdi_sync_supers
26 ?        root     bdi-defa     – 1 bdi_forker_thread
27 ?        root     kintegri     – 1 rescuer_thread
28 ?        root     kblockd      – 1 rescuer_thread
29 ?        root     ata_sff      – 1 rescuer_thread
30 ?        root     khubd        – 1 hub_thread
31 ?        root     md           – 1 rescuer_thread
55 ?        root     kswapd0      – 1 kswapd
56 ?        root     ksmd         – 1 ksm_scan_thread
57 ?        root     khugepag     – 1 khugepaged_alloc_sleep
58 ?        root     fsnotify     – 1 fsnotify_mark_destroy
59 ?        root     crypto       – 1 rescuer_thread
65 ?        root     kthrotld     – 1 rescuer_thread
66 ?        root     scsi_eh_     – 1 scsi_error_handler
67 ?        root     scsi_eh_     – 1 scsi_error_handler
68 ?        root     scsi_eh_     – 1 scsi_error_handler
69 ?        root     scsi_eh_     – 1 scsi_error_handler
70 ?        root     scsi_eh_     – 1 scsi_error_handler
71 ?        root     scsi_eh_     – 1 scsi_error_handler
79 ?        root     kpsmouse     – 1 rescuer_thread
130 ?        root     ttm_swap     – 1 rescuer_thread
311 ?        root     kdmflush     – 1 rescuer_thread
320 ?        root     jbd2/dm-     – 1 kjournald2
321 ?        root     ext4-dio     – 1 rescuer_thread
355 ?        root     kauditd      – 1 kauditd_thread
357 ?        root     udevd        – 4 poll_schedule_timeout
378 ?        root     systemd-     – 4 epoll_wait
391 ?        root     kvm-irqf     – 1 rescuer_thread
640 ?        root     ips-adju     – 1 ips_adjust
641 ?        root     ips-moni     – 1 ips_monitor
648 ?        root     cfg80211     – 1 rescuer_thread
661 ?        root     hci0         – 1 rescuer_thread
671 ?        root     iwlwifi      – 1 rescuer_thread
688 ?        root     hd-audio     – 1 rescuer_thread
736 ?        root     hd-audio     – 1 rescuer_thread
772 ?        root     flush-25     – 1 bdi_writeback_thread
775 ?        root     kdmflush     – 1 rescuer_thread
802 ?        root     kjournal     – 1 kjournald
811 ?        root     jbd2/sda     – 1 kjournald2
812 ?        root     ext4-dio     – 1 rescuer_thread
829 ?        root     bluetoot     – 4 poll_schedule_timeout
841 ?        avahi    avahi-da     – 4 poll_schedule_timeout
859 ?        root     abrtd        – 0 poll_schedule_timeout
862 ?        avahi    avahi-da     – 1 unix_stream_recvmsg
890 ?        root     gpm          – 5 hrtimer_nanosleep
893 ?        root     NetworkM     – 4 poll_schedule_timeout
895 ?        root     mcelog       – 4 poll_schedule_timeout

*********** Truncated

Hope this helps!!

Find integer array size in C programming

Someone asked me once “how can I get integer array size in C programming and use it in the code?”.Unfortunately there is no defined standard function to get that in C. Rather you could use below method .Suppose you have defined an integer array ( static ) called , int intArray[10];Its size can be derived …

Read more