【问题标题】:Linux SLAB_CTOR_VERIFY and cache undeclaredLinux SLAB_CTOR_VERIFY 和缓存未声明
【发布时间】:2012-08-22 22:05:20
【问题描述】:

我尝试编译一个程序,作为存储分配工作原理的示例。

编译器给我一个错误,“缓存”和“SLAB_CTOR_VERIFY”未声明。

我在整个内核项目(Linux Kernel 2.6.32)中找不到声明。

也许有一些替代品或类似的东西。

我在网上没有找到任何关于此的内容,但也许你们中的一些人可以给我一个线索。

这里是源代码:

#include <linux/module.h>
#include <linux/completion.h>
#include <linux/slab.h>

static int thread_id=0;
static DECLARE_COMPLETION( on_exit );
static kmem_cache_t *cache;

struct linobj {
    int dummy_i, dummy_j, dummy_k;
    char dummy_feld[250];
    wait_queue_head_t wq;
};

static void linobj_destructor(void *objp, kmem_cache_t *cache,
    unsigned long flags)
{
    printk("linobj_destructor( %p )\n", objp);
    return;
}

static void linobj_constructor(void *objp, kmem_cache_t *cache,
       unsigned long flags)
{
    struct linobj *ptr = (struct linobj *)objp;
    if( flags & SLAB_CTOR_VERIFY )
        return;
    printk("linobj_constructor( %p )\n", objp);
    init_waitqueue_head(&ptr->wq);
    return;
}

static int thread_code( void *data )
{
    unsigned long timeout, retvalue;
    int i;
    struct linobj *obj;

    daemonize("linobj-test");
    allow_signal( SIGTERM );
    for( i=0; i<5; i++ ) {
        obj = (struct linobj *)kmem_cache_alloc( cache, GFP_KERNEL );
        printk("objadr=%p\n", obj );
        timeout=HZ;
        retvalue=schedule_timeout_interruptible(timeout);
        kmem_cache_free( cache, obj );
        if( retvalue )
            break;
    }
    complete_and_exit( &on_exit, 0 );
}

static int __init slab_init(void)
{
    cache = kmem_cache_create( "linobj", sizeof(struct linobj),
        0, 0, linobj_constructor, linobj_destructor );
    if( !cache )
        return -EFAULT;
    thread_id=kernel_thread( thread_code, NULL, CLONE_KERNEL );
    if( thread_id==0 ) {
        kmem_cache_destroy( cache );
        return -EIO;
    }
    return 0;
}

static void __exit slab_exit(void)
{
    kill_pid( find_vpid(thread_id), SIGTERM, 1 );
    wait_for_completion( &on_exit );
    if( cache )
        kmem_cache_destroy( cache );
}

module_init( slab_init );
module_exit( slab_exit );

MODULE_LICENSE("GPL");

提前致谢

彼得

【问题讨论】:

    标签: linux linux-kernel linux-device-driver embedded-linux kernel


    【解决方案1】:

    SLAB_CTOR_VERIFY 在2.6.22 中被删除

    【讨论】:

    • 感谢您的回答,但有没有替代品?
    • 来自我链接的更改日志:有一个相关标志 SLAB_CTOR_VERIFY 经常检查以清除 fs inode 缓存。从 fs 构造函数中删除无意义的检查(如果不删除 SLAB_DEBUG_INITIAL,它们甚至会毫无意义)。
    【解决方案2】:

    通过一些更改让它编译。 SLAB.. 已被删除,并且 kmem_cache 函数已更改。 kmem_cache_t 已替换为 struct kmem_cache:

    #include <linux/module.h>
    #include <linux/completion.h>
    #include <linux/slab.h>
    #include <linux/sched.h> // daemonize
    
    static int thread_id=0;
    static DECLARE_COMPLETION( on_exit );
    typedef struct kmem_cache kmem_cache_t; //
    static kmem_cache_t *cache;
    
    struct linobj {
        int dummy_i, dummy_j, dummy_k;
        char dummy_feld[250];
        wait_queue_head_t wq; 
    };
    
    static void linobj_destructor(void *objp, kmem_cache_t *cache,
        unsigned long flags)
    {
        printk("linobj_destructor( %p )\n", objp);
        return;
    }
    
    static void linobj_constructor(void *objp) {
        struct linobj *ptr = (struct linobj *)objp;
        printk("linobj_constructor( %p )\n", objp);
        init_waitqueue_head(&ptr->wq);
        return;
    }
    
    static int thread_code( void *data )
    {
        unsigned long timeout, retvalue;
        int i;
        struct linobj *obj;
    
        daemonize("linobj-test");
        allow_signal( SIGTERM );
    
        for( i=0; i<5; i++ ) {
            obj = (struct linobj *)kmem_cache_alloc( cache, GFP_KERNEL );
            printk("objadr=%p\n", obj );
            timeout=HZ;
            retvalue=schedule_timeout_interruptible(timeout);
            kmem_cache_free( cache, obj );
            if( retvalue )
                break;
        }
        complete_and_exit( &on_exit, 0 );
    }
    
    static int __init slab_init(void)
    {
        cache = kmem_cache_create( "linobj", sizeof(struct linobj),
              0, 0, linobj_constructor);
        if( !cache )
            return -EFAULT;
        thread_id=kernel_thread( thread_code, NULL, CLONE_KERNEL );
        if( thread_id==0 ) {
            kmem_cache_destroy( cache );
            return -EIO;
        }
        return 0;
    }
    
    static void __exit slab_exit(void)
    {
        kill_pid( find_vpid(thread_id), SIGTERM, 1 );
        wait_for_completion( &on_exit );
        if( cache )
            kmem_cache_destroy( cache );
    }
    
    module_init( slab_init );
    module_exit( slab_exit );
    
    MODULE_LICENSE("GPL");
    

    【讨论】:

    • 但我认为我需要if( flags &amp; SLAB_CTOR_VERIFY ) 来控制标志?!这不是必须的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    相关资源
    最近更新 更多