kmalloc()可以返回无效的内存吗?

时间:2013-12-27 12:29:14

标签: c linux memory-management kernel-module kmalloc

我正在编写一个linux内核模块,我在其中实现了一个链表。我知道Linux内核中有一个列表API,但是当我实现它时,我不知道如此实现它使用kmalloc()处理原始指针。运行几个小时后,内核崩溃,在崩溃日志中显示“常规保护错误”。日志还显示它是从我的搜索链表的功能中发生的。显然搜索功能如下,没有逻辑错误。

/*
 * Searches in a certain index of hash table for a data
 * returns NULL if not found else returns pointer of that element in the table
 */

struct queue_data * search_table(unsigned int hash_index, struct queue_data *new_data)
{
        /* Taking a new queue pointer. Which will be pointing to the queue represented
         * by new_data at last. */
        struct queue_data *ret;
        /* First initializing it with first queue on the list */
        ret = table[hash_index].next;
        /* Iterating through the list to find the desired queue */
        while(ret != NULL) {
                /* Checking if current queue matches our criteria */
                if(ret->saddr == new_data->saddr &&
                        ret->daddr == new_data->daddr &&
                        ret->dest == new_data->dest &&
                        ret->ssrc == new_data->ssrc) {
                        /* It matched. So I can return it */
                        return ret;
                }
                /* It didn't match so I need to go to next queue */
                ret = ret->next;
        }

        /* No queue matched out criteria. Because if it matched it would have not
         * come this far. It would have returned before.
         * So I need to return a NULL. Now value of 'ret' is NULL.
         * I can return 'ret'
         */
        return ret;
}

从逻辑的角度来看,插入功能也很明显。由于一般保护错误通常发生在无效的内存访问发生时,我从未使用过除kmalloc()以外的内存。现在我的问题是,如果我使用kmalloc分配的内存,那么有可能使用无效内存,我应该在使用之前检查吗?

崩溃的分数日志在这里:

[ffff8804130cb690] general_protection at ffffffff81661c85
    [exception RIP: search_table+52]
    RIP: ffffffffa00bc854  RSP: ffff8804130cb748  RFLAGS: 00010286
    RAX: d6d4575455d55555  RBX: ffff88040f46db00  RCX: 0000000000000018
    RDX: 02b53202ab706c17  RSI: ffff8803fccaaa00  RDI: 00000000000c2568
    RBP: ffff8804130cb748   R8: ffffffff8180cb80   R9: 000000000000016d
    R10: a3d70a3d70a3d70b  R11: ffff8803fccaab58  R12: ffffc9001262cc38
    R13: 000000000000079f  R14: ffff8803fccaaa00  R15: ffffffffa00cbee8
    ORIG_RAX: ffffffffffffffff  CS: 0010  SS: 0018

插入时,我用kmalloc检查分配的内存:

   /* Allocating and initializing a new queue.
    * If a queue corresponding to it already exists then it's data will
    * copied and this queue will be dropped.
    * Else this queue will be inserted to the hash table that manages the queues.
    */
    new_data = (struct queue_data *)kmalloc(sizeof(struct queue_data), GFP_ATOMIC);
    if (!new_data) {
        //printk(KERN_ALERT "pkt_queue EXCEPTION: new_data\n");
        return NULL;
    }

1 个答案:

答案 0 :(得分:3)

查看您发布的代码,我能看到的常规保护错误的唯一可能来源是这一行:

ret = table[hash_index].next;

你没有检查table的大小,所以也许你正在访问越界内存?无法确定,不知道table的声明方式,地点和内容,以及如何初始化它。

在查看您的评论后,hash_indexunsigned int,是HASH_PRIME宏的模数的结果,可能是有一点,你遇到了可能的有符号无符号算术问题,所以,尽管HASH_PRIME上有模数,但 实际上超出了界限。也许补充一下:

if (hash_index >= HASH_PRIME) hash_index = HASH_PRIME-1;//or error

为了完整起见:正如我在评论中指出的那样,您使用的函数都使用内核的u32类型。事实证明,这就是您的代码仍然访问错误的内存的原因。 (在手机上输入此更新...讨厌它)