使用 struct 会导致内核恐慌吗?

发布于 2024-08-14 03:24:41 字数 1459 浏览 5 评论 0原文

我第一次尝试编写一些 Linux 内核代码,并且遇到了奇怪的内核恐慌。

我有一个使用内核内置宏(include/linux/list.h)维护的链接列表。如果列表为空,我分配以下结构的一个实例:

struct time_span
{
   struct timeval start;
   struct timeval end;
};

并用名为“tmp”的指针指向它。我将 tmp 添加到我使用 list_add_tail() 维护的列表中。

稍后,如果列表不为空(我尝试使用一个列表项进行测试以简化调试),我用 tmp 指向列表中的第一项,并尝试打印出 tmp->end.tv_sec 的内容。不幸的是,这会导致内核恐慌。

tmp 不是 NULL(我在运行时检查),“tmp->end”也不是(我可以打印两者)。只有当我尝试访问“end”中的字段之一时,才会出现内核恐慌。我以前从未见过这样的事情——有人有什么想法吗?

感谢您的帮助!

-------编辑------

代码示例(这位于将被重复调用的函数中):

// .........
struct timeval now_tv;
do_gettimeofday(&now_tv);
if(!list_empty(&(my_list.time_list)))
    {
        tmp = list_first_entry(&(my_list.time_list), struct time_span, time_list);
        if(tmp != NULL)
        {
                    tmp->end.tv_sec = now_tv.tv_sec; // THIS BREAKS
                                                     // Attempting to print "tmp->end.tv_sec" also breaks.
            tmp->end.tv_usec = now_tv.tv_usec;
        }
    }

        // .........

    if(list_empty(&(my_list.time_list)))
        {
        new_time_span = (struct time_span *) kmalloc(sizeof(struct time_span), GFP_KERNEL);
        INIT_LIST_HEAD(&(new_time_span->time_list));
        list_add_tail(&(new_time_span->time_list), &(my_list.time_list));
            do_gettimeofday(&(new_time_span->start));
    }

    // ........

I'm taking my first crack at writing some linux kernel code, and I'm hitting a weird kernel panic.

I have a linked list I am maintaining with the kernel's built-in macros (include/linux/list.h). If the list is empty, I allocate an instance of the following structure:

struct time_span
{
   struct timeval start;
   struct timeval end;
};

and point to it with a pointer called "tmp". I add tmp to the list I'm maintaining with list_add_tail().

Later, if the list is not empty (I'm trying to test with one list item to simplify debugging), I point to the first item in the list with tmp and try to print out the contents of tmp->end.tv_sec. Unfortunately, this causes a kernel panic.

tmp is not NULL (I check at run-time) and neither is "tmp->end" (I am able to print both). It's only when I try to access one of the fields in "end" that I get a kernel panic. I've never seen something like this before -- does anyone have any ideas?

Thanks for any assistance!

-------EDIT------

Code example (this lives in a function that will be called repeatedly):

// .........
struct timeval now_tv;
do_gettimeofday(&now_tv);
if(!list_empty(&(my_list.time_list)))
    {
        tmp = list_first_entry(&(my_list.time_list), struct time_span, time_list);
        if(tmp != NULL)
        {
                    tmp->end.tv_sec = now_tv.tv_sec; // THIS BREAKS
                                                     // Attempting to print "tmp->end.tv_sec" also breaks.
            tmp->end.tv_usec = now_tv.tv_usec;
        }
    }

        // .........

    if(list_empty(&(my_list.time_list)))
        {
        new_time_span = (struct time_span *) kmalloc(sizeof(struct time_span), GFP_KERNEL);
        INIT_LIST_HEAD(&(new_time_span->time_list));
        list_add_tail(&(new_time_span->time_list), &(my_list.time_list));
            do_gettimeofday(&(new_time_span->start));
    }

    // ........

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2024-08-21 03:24:41

您缺少一些有关 Linux 链表的基础知识。
以下内容应更改

struct time_span
{
   struct timeval start;
   struct timeval end;
};

为:

struct time_span
{
   struct timeval start;
   struct timeval end;
   struct list_head time_list;
}

当使用 Linux 链表时,您应该将 struct list_head 放入您想要列表的结构中。
在下面的代码中,您分配一个类型 struct time_span 并在分配的变量 new_time_span 内引用名为 time_list 的变量...但是您还没有将其添加到上面的结构中。

// .........
struct timeval now_tv;
do_gettimeofday(&now_tv);
if(!list_empty(&(my_list.time_list)))
    {
        tmp = list_first_entry(&(my_list.time_list), struct time_span, time_list);
        if(tmp != NULL)
        {
                    tmp->end.tv_sec = now_tv.tv_sec; // THIS BREAKS
                                                     // Attempting to print "tmp->end.tv_sec" also breaks.
                tmp->end.tv_usec = now_tv.tv_usec;
        }
    }

根据您提供的信息,我不知道为什么会出现上述情况。也许只是 tmp 是一个指向垃圾的指针,这就是它崩溃的原因?如果您有内核调试器设置,则很容易验证。

        // .........

    if(list_empty(&(my_list.time_list)))
        {
        new_time_span = (struct time_span *) kmalloc(sizeof(struct time_span), GFP_KERNEL);
        INIT_LIST_HEAD(&(new_time_span->time_list));
        list_add_tail(&(new_time_span->time_list), &(my_list.time_list));
            do_gettimeofday(&(new_time_span->start));
    }

    // ........

以下是一些应该有所帮助的好文章:

http://kernelnewbies.org/FAQ/LinkedLists
http://sumanadak.blogspot.com/2006/09/ linux-kernel-linked-list.html

You're missing some fundamentals about Linux linked lists.
The following should change:

struct time_span
{
   struct timeval start;
   struct timeval end;
};

To:

struct time_span
{
   struct timeval start;
   struct timeval end;
   struct list_head time_list;
}

When using Linux linked lists you should put the struct list_head inside your struct that you want a list of.
In the code below, you're allocating a type struct time_span and referencing a variable named time_list inside the allocated variable new_time_span... but you haven't added that to your struct above.

// .........
struct timeval now_tv;
do_gettimeofday(&now_tv);
if(!list_empty(&(my_list.time_list)))
    {
        tmp = list_first_entry(&(my_list.time_list), struct time_span, time_list);
        if(tmp != NULL)
        {
                    tmp->end.tv_sec = now_tv.tv_sec; // THIS BREAKS
                                                     // Attempting to print "tmp->end.tv_sec" also breaks.
                tmp->end.tv_usec = now_tv.tv_usec;
        }
    }

Based on the information you've provided, I don't know why the above breaks. Maybe it's just that tmp is a pointer pointing to garbage and that's why it crashes? If you have a kernel debugger setup it's easy to verify.

        // .........

    if(list_empty(&(my_list.time_list)))
        {
        new_time_span = (struct time_span *) kmalloc(sizeof(struct time_span), GFP_KERNEL);
        INIT_LIST_HEAD(&(new_time_span->time_list));
        list_add_tail(&(new_time_span->time_list), &(my_list.time_list));
            do_gettimeofday(&(new_time_span->start));
    }

    // ........

Here are some good articles that should help:

http://kernelnewbies.org/FAQ/LinkedLists
http://sumanadak.blogspot.com/2006/09/linux-kernel-linked-list.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文