Re: [PATCH 1/5] seqlock: introduce SEQLOCK_READ_SECTION()

From: Linus Torvalds
Date: Sun Oct 05 2025 - 12:36:22 EST


On Sun, 5 Oct 2025 at 09:09, Oleg Nesterov <oleg@xxxxxxxxxx> wrote:
>
> OK. But if you don't object I'd like to avoid another DEFINE_LOCK_GUARD()
> or something like it in this case. To me it won't buy anything.

Oh, absolutely. I didn't mean that you actually *use* the fancy GUARD
infrastructure we have: this code doesn't do the whole '__cleanup()'
thing at all.

I only mean that as far as users are concerned, this all looks
similar, so please use a similar interface even if from an
implementation standpoint it is very different.

> The problem is that you can't declare "int lockless/seq" and
> "unsigned long flags" inside "for (...)", but I'll try to think about
> it more.

I agree, it's an annoying limitation of the for-loop variable
declaration thing that you can only declare one type.

There's a somewhat strange solution to it, though: declare a structure
with multiple members of the types you want:

for (struct { char *a; int b; } c = {NULL, 1}; ... }

and then you use 'c.a' and 'c.b' and friends inside the loop.

It looks pretty odd, but it actually works fine and we should probably
use it more.

[ The *really* hacky solution is to just use one type and pick the
biggest type and then possibly mis-use that type.

That's effectively what you did with 'lockless': it's declared as an
'int', but it's really effectively used as a 'bool' anyway.

Of course, in that 'lockless' case it's not as noticeable, since we
often use int/bool rather interchangeably in the kernel for historical
reasons anyway, so you probably didn't think of it as a hacky solution
]

Linus