Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 05-25-2018, 02:10 PM   #1
LQ Newbie
Registered: May 2018
Posts: 1

Rep: Reputation: Disabled
Linux kernel linked lists - and sorting

I'm implementing some system calls which are mainly supposed to maintain a list of processes, and a list of open files with each process. However I'm having a hard time debugging the code, which requires recompiling over and over again.

Here are probably my main problems:

When and how can I initialize the list keeping the processes? Currently I'm checking if the list is empty before initializing but it doesn't work. I also tried head == head->prev->next but failed.

What's the correct way to call the list_sort considering the related 'compare' function? I'm getting the error

[ 1827.710802] BUG: unable to handle kernel paging request at fffff550

[ 1827.710808] IP: [] compare+0x8/0x20

[ 1827.710814] *pde = 00a32067 *pte = 00000000
This is the code. I'd really appreciate you reviewing it, especially the init system call whose correct behavior the others rely on.
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/pid.h>
#include <linux/list.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/fdtable.h>
#include <linux/list_sort.h>
#include <linux/slab.h>

struct fileDescriptor {
    int fd;
    struct list_head list;

struct processInfo {
    struct task_struct task;
    int len_files;
    struct fileDescriptor listHead;
    struct list_head list;

struct processInfo listHead;

int compare(void* priv, struct list_head *a, struct list_head *b)
    struct processInfo *p1 = container_of(a, struct processInfo, list);
    struct processInfo *p2 = container_of(b, struct processInfo, list);

    if (p1-> > p2-> return -1;
    else return 1;

asmlinkage long sys_init_process_list(pid_t p)
    struct pid* pid;
    struct task_struct *task;
    struct files_struct *processFiles;
    struct fdtable *filesTable;
    struct processInfo *newProcess;


    pid = find_get_pid(p);
    if (pid == NULL)
        return 1;
        struct list_head *list;
        struct task_struct *childTask;
        struct fileDescriptor *newfd;
        int i = 0;
        task = pid_task(pid, PIDTYPE_PID);
        newProcess = (struct processInfo*) kmalloc(sizeof(*newProcess), GFP_KERNEL);
        //copy task structure
        newProcess->task = *task;
        //adding file descriptors
        processFiles = task->files;
        filesTable = files_fdtable(processFiles);
        while (filesTable->fd[i] != NULL)
            newfd = (struct fileDescriptor*) kmalloc(sizeof(*newfd), GFP_KERNEL);
            newfd->fd = i;
            list_add_tail(&(newfd->list), &(newProcess->listHead).list);
        newProcess->len_files = i;
        // add the new node to mylist
        list_add_tail(&(newProcess->list), &(listHead.list));        
        //adding childern
        list_for_each(list, &task->children)
            childTask = list_entry(list, struct task_struct, sibling);
        return 0;
asmlinkage long sys_sort_process_list(void)
        printk("empty list\n");
        return 1;
    list_sort(NULL, &listHead.list, compare);
    return 0;
asmlinkage long sys_print_process_list(void)

    struct processInfo *aProcess;

    if (sys_sort_process_list())
        printk("empty list\n");
        return 1;

    list_for_each_entry(aProcess, &listHead.list, list)
        printk("%d, %d\n", (aProcess->task).pid, aProcess->len_files);
    return 0;
asmlinkage long sys_clear_process_list(void)
    struct processInfo *aProcess, *tmp;

        printk("empty list\n");
        return 1;

    printk("deleting the list");

    list_for_each_entry_safe(aProcess, tmp, &listHead.list, list)
        printk("freeing %d", (aProcess->task).pid);

    return 0;
Old 05-26-2018, 12:07 AM   #2
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003
Here's the way you submit code for analysis:

Program description.
What is it supposed to do?
What does it actually do?
How is it defective?
What have you tried to fix it?
What happened then?
And put your notes in the code, explaining what each part is supposed to do.

If you do those things, many times the answer is found. But just plunking a glob of code in front of us shows that you yourself might not even understand what each part is supposed to do. And complaining that you have to recompile a lot is totally lame. If you need to compile less than 100 times you got off easy!
Old 06-03-2018, 11:32 PM   #3
Registered: Apr 2016
Posts: 423

Rep: Reputation: Disabled
for one thing you need to know if your code runs in the same time and space as the kernel

if it doesn't you have to lock the list, or its' stale perhaps while your reading it

(note i'm not saying if you lock it that your system won't grind to a halt. i didn't read all the code to give you such an answer)

grep for the same (structure/name variable) in the whole source tree to see what other modules used the same (structure/name) in "stable drivers" and see what they did

Last edited by X-LFS-2010; 06-03-2018 at 11:35 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python - Sorting Lists inside of Lists LXer Syndicated Linux News 0 06-06-2013 01:00 PM
Question about sorting and linked lists Blackened Justice Programming 4 12-15-2011 07:53 PM
LXer: Unique Sorting Of Lists And Lists Of Lists With Perl For Linux Or Unix LXer Syndicated Linux News 0 09-05-2008 01:50 PM
LXer: Sorting Perl Lists And Removing Duplicates On Linux Or Unix LXer Syndicated Linux News 0 09-04-2008 05:20 AM
linked lists in linux kernel how they works? cranium2004 Programming 3 03-05-2005 03:27 PM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 05:50 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration