kstrtol 导致 class 属性内循环

kstrtol causing looping within class attribute

我有一个简单的内核模块,它在 /sys/class 下创建一个 sysfs 节点并向 class 添加一个测试属性。 test 属性有一个存储函数,它只使用 kstrtol 将驻留在字符数组中的数字转换为长整数。它似乎运作良好;但是,该函数似乎陷入了无限循环。我对为什么感到非常困惑。有人有什么想法吗?

static ssize_t test_store(struct class *c,
    struct class_attribute *attr,
    const char *buf,
    size_t count)
{
    int ret;
    long test;

    char testbuf[10] = "10[=11=]";

    ret = kstrtol(testbuf, 10, &test);

    printk("Read test number%d", test);

    if (ret)
        return ret;

    return 0;
}

static CLASS_ATTR_WO(test);

终端输出:

 echo 10 > /sys/class/vehicle_control/test
[   57.633697] Creating vehicle_control sysfs node
[   60.270140] Read test number10
[   60.273221] Read test number10
[   60.279476] Read test number10
[   60.281071] Read test number10
[   60.284113] Read test number10
[   60.289285] Read test number10
[   60.290879] Read test number10
[   60.293921] Read test number10
[   60.299085] Read test number10
[   60.300678] Read test number10
[   60.303720] Read test number10
[   60.308967] Read test number10
[   60.310560] Read test number10
[   60.313601] Read test number10

来自documentation

store() should return the number of bytes used from the buffer. If the entire buffer has been used, just return the count argument.

所以在你的情况下你可以写 return count;.