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

Notices


Reply
  Search this Thread
Old 05-25-2018, 03:10 PM   #1
bishopP
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

Quote:
[ 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.
Code:
#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->task.pid > p2->task.pid) 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;

    if(list_empty(&listHead.list))
    {
        printk("____list_empty_____\n");
        INIT_LIST_HEAD(&listHead.list);
    }
    else
        printk("____list_not_empty_____\n");


    pid = find_get_pid(p);
    if (pid == NULL)
    {
        sys_init_process_list(1);
        return 1;
    }
    else 
    {
        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
        INIT_LIST_HEAD(&(newProcess->listHead).list);
        processFiles = task->files;
        filesTable = files_fdtable(processFiles);
        while (filesTable->fd[i] != NULL)
        {
            newfd = (struct fileDescriptor*) kmalloc(sizeof(*newfd), GFP_KERNEL);
            newfd->fd = i;
            INIT_LIST_HEAD(&newfd->list);
            list_add_tail(&(newfd->list), &(newProcess->listHead).list);
            i++;
        }
        newProcess->len_files = i;
        INIT_LIST_HEAD(&newProcess->list);
        // 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);
            sys_init_process_list(childTask->pid);
        }
        return 0;
    }
}
//////////////////////////////////////////
asmlinkage long sys_sort_process_list(void)
{
    if(list_empty(&listHead.list))
    {
        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;

    if(list_empty(&listHead.list))
    {
        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);
        list_del(&aProcess->list);
        kfree(aProcess);
    }

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

Rep: Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999
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-04-2018, 12:32 AM   #3
X-LFS-2010
Member
 
Registered: Apr 2016
Posts: 382

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-04-2018 at 12:35 AM.
 
  


Reply


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 02:00 PM
Question about sorting and linked lists Blackened Justice Programming 4 12-15-2011 08: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 02:50 PM
LXer: Sorting Perl Lists And Removing Duplicates On Linux Or Unix LXer Syndicated Linux News 0 09-04-2008 06:20 AM
linked lists in linux kernel how they works? cranium2004 Programming 3 03-05-2005 04:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 04:45 PM.

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