Re: [PATCH v3 06/10] lib/refcount: Move saturation warnings out of line

From: Will Deacon
Date: Fri Oct 11 2019 - 08:09:52 EST


On Thu, Oct 10, 2019 at 01:48:20PM -0700, Kees Cook wrote:
> On Mon, Oct 07, 2019 at 04:46:59PM +0100, Will Deacon wrote:
> > Having the refcount saturation and warnings inline bloats the text,
> > despite the fact that these paths should never be executed in normal
> > operation.
> >
> > Move the refcount saturation and warnings out of line to reduce the
> > image size when refcount_t checking is enabled. Relative to an x86_64
> > defconfig, the sizes reported by bloat-o-meter are:
> >
> > # defconfig+REFCOUNT_FULL, inline saturation (i.e. before this patch)
> > Total: Before=14762076, After=14915442, chg +1.04%
> >
> > # defconfig+REFCOUNT_FULL, out-of-line saturation (i.e. after this patch)
> > Total: Before=14762076, After=14835497, chg +0.50%
>
> The downside of this change is that this means we get one warning per
> refcount_saturation_type, where as before it was once per refcount
> usage. I think, ultimately, this is okay, but it is a behavioral change
> that might be worth pointing out.

Good point; I'll add something to the commit message.

> > diff --git a/lib/refcount.c b/lib/refcount.c
> > index 3a534fbebdcc..6a61d39f9390 100644
> > --- a/lib/refcount.c
> > +++ b/lib/refcount.c
> > @@ -8,6 +8,34 @@
> > #include <linux/spinlock.h>
> > #include <linux/bug.h>
> >
> > +#define REFCOUNT_WARN(str) WARN_ONCE(1, "refcount_t: " str ".\n")
>
> This define seems like overkill for just adding a prefix to 5 uses...

I dunno. It doesn't hurt and I did get bored of typing that prefix.

> > +void refcount_warn_saturate(refcount_t *r, enum refcount_saturation_type t)
> > +{
> > + refcount_set(r, REFCOUNT_SATURATED);
> > +
> > + switch (t) {
> > + case REFCOUNT_ADD_NOT_ZERO_OVF:
> > + REFCOUNT_WARN("saturated; leaking memory");
> > + break;
> > + case REFCOUNT_ADD_OVF:
> > + REFCOUNT_WARN("saturated; leaking memory");
> > + break;
> > + case REFCOUNT_ADD_UAF:
> > + REFCOUNT_WARN("addition on 0; use-after-free");
> > + break;
> > + case REFCOUNT_SUB_UAF:
> > + REFCOUNT_WARN("underflow; use-after-free");
> > + break;
> > + case REFCOUNT_DEC_LEAK:
> > + REFCOUNT_WARN("decrement hit 0; leaking memory");
>
> Even the longest doesn't line wrap:
>
> WARN_ONCE(1, "refcount_t: decrement hit 0; leaking memory\n");
>
> > + break;
> > + default:
> > + WARN_ON(1);
>
> This deserves a string too, yes?
>
> WARN_ONCE(1, "refcount_t: unknown saturation event!?\n");

Ok.

>
> > + }
> > +}
> > +EXPORT_SYMBOL(refcount_warn_saturate);
> > +
> > /**
> > * refcount_dec_if_one - decrement a refcount if it is 1
> > * @r: the refcount
> > --
> > 2.23.0.581.g78d2f28ef7-goog
> >
>
> Otherwise, okay, I grudgingly accept the loss of warnings when running
> the lkdtm tests in order to gain back some text size... :)

Thanks :)

Will