Messages in this thread | | | From | Linus Torvalds <> | Date | Mon, 25 Oct 2021 17:02:18 -0700 | Subject | Re: [PATCH] secretmem: Prevent secretmem_users from wrapping to zero |
| |
On Mon, Oct 25, 2021 at 4:37 PM Linus Torvalds <torvalds@linux-foundation.org> wrote: > > Stop thinking that refcount_t is a good type. Start realizing the > downsides. Start understanding that saturation is a HORRENDOUSLY BAD > solution, and horrible QoI.
Basically, refcount_t should be used purely for internal kernel data structures - it makes perfect sense for things like the 'struct device' resource handling, for example.
In fact, there it is good for two reasons:
- it's not counting some user resource, so users shouldn't have any way to trigger overflow and saturation which causes problems
- it's used by random driver stuff, which is often where kernel bugs happen and testing is fundamentally limited by hw availability etc
but in general, anything that is user-accountable needs to have _limits_, not saturation. It's why the page count is a "atomic_t" even if the name of the field is "_refcount". Because refcount_t is the INFERIOR TYPE.
Using an atomic_t properly is actually the much better option. It's just that "properly" might be a bit more code, involving actual limit checking.
'refcount_t' is basically a shorthand for "I didn't bother doing this right, so I'm using this type that adds debugging, warns and stops working and might DoS the kernel".
It's a crutch. It's not the alpha and the omega of counting types. It has its place, but I really want to stress how people should ABSOLUTELY not think "oh, refcount_t is better than atomic_t".
Linus
| |