clinux-kernellockingrwlockseqlock

Why rwlock is more popular than seqlock in linux kernel?


After reading Robert Love's LKD, I learn rwlock and seqlock, both of them are based on spinlock.

When distinguish between reader and writer, rwlock is better than spinlock, it will get better performace. However, rwlock will make writer hungury.

seqlock solve rwlock making writer hungury problem, however, there are less use of seqlock than rwlock. So, why rwlock is more popular than seqlock?


Solution

  • A seqlock has a strong limitation, that readers should correctly work with inconsistent data.

    Not every processing algorithm allows inconstistent data. In most cases, such data can only be numbers: integers, booleans, etc. They rarely can be pointers, because a stale pointer may point to the memory which is already freed, so dereferencing such pointer is no-no.

    Locks (and rw-locks among them) doesn't have "inconsitent data" limitations, so they can be used in much more cases.

    Example of inconstisten data under seqlock

    Assume there are two structure's fields protected by the single seqlock. The first field, a is incremented by each "write", the second field, b is decremented by each "write". Both fields initially are 0.

    On may assume, that a reader will always find a + b to be 0.

    But in case of seqlock, this is not true. E.g., between reading a and b it could a "write", so a value will be old, and b value will be new, and a + b gives -1.