修改两个关于RCU锁的bug

RCU_TYPE(struct object *resource);

void get_resource_with_lock(void **get)
{
    *get = RCU_GET(resource);
}

void error_function()
{
    struct resource *get;
    get_resource_with_lock(&get);
    use(get);  // got error here
}

基本流程就是如上的代码段,经常间歇性crash,debug发现有时候get这个指针为NULL,百思不得其解。

后来想明白了,在“get_resource_with_lock”函数中,*get这个指针指向的内存段是受RCU锁保护的,但是出了其作用域(也就是出了这个函数),RCU锁不再保护,反而会释放这个资源,具体释放时间由RCU线程决定,所以就出现了上述的问题,解决方法就是两种:一是copy一份resource给get指针,另一个是将use函数放到get_resource_with_lock这个函数里面。



RCU_TYPE(struct object *resource);

void
error_modify_resource(struct object *new)
{
    struct object *old = RCU_GET(resource);
    remove(old);
    RCU_SET(resource, new);  // resource will point to NULL for a little time
}

void
correct_modify_resource(struct object *new)
{
    struct object *old = RCU_GET(resource);
    RCU_SET(resource, new);
    RCU_POSTPONE(remove, old);
}

上面这个例子是修改RCU保护变量中一种常出的错误。


猜你喜欢

转载自blog.csdn.net/batmancn/article/details/53195569
今日推荐