mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	dan.j.williams@intel.com, linux-kernel@vger.kernel.org,
	Nick Desaulniers <ndesaulniers@google.com>
Subject: Re: cleanup: Make no_free_ptr() __must_check
Date: Tue, 15 Aug 2023 15:53:39 +0200	[thread overview]
Message-ID: <20230815135339.GA966323@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <fcc8a158-f6e4-8963-782f-ba04b47350b8@rasmusvillemoes.dk>

On Tue, Aug 15, 2023 at 01:28:37PM +0200, Rasmus Villemoes wrote:
> On 15/08/2023 12.52, Peter Zijlstra wrote:
> > 
> > recent discussion brought about the realization that it makes sense for
> > no_free_ptr() to have __must_check semantics in order to avoid leaking
> > the resource.
> > 
> 
> > +static inline __must_check void * __no_free_ptr(void **pp)
> > +{ void *p = *pp; *pp = NULL; return p; }
> > +
> >  #define no_free_ptr(p) \
> > -	({ __auto_type __ptr = (p); (p) = NULL; __ptr; })
> > +	(({ void * __maybe_unused ___t = (p); }), \
> > +	 ((typeof(p))__no_free_ptr((void **)&(p))))
> 
> So this does seem to work as advertised, but it could perhaps use some
> comments. Because at first I read this as one big statement expression,
> and I had this memory of a __must_check function call being the last
> expression in such had no effect at all [1]. But this is actually a
> comma expression.

Right, I can into that as well, that was infact the first thing I tried.
Most vexing indeed.

> 
> Also, isn't it more complicated than necessary? Can we get rid of the
> inner stmt expr and tmp var by just making it
> 
>   ((void) (p), ((typeof(p))__no_free_ptr((void **)&(p)))
> 
> which is more or less the whole reason comma expressions is a thing.

Ah, so the point of the statement expression before the comma is to
validate that (p) is in fact a pointer, and to that effect we assign it
to a 'void *' temporary.

If that case is invalid, we'll get a compile fail with a dodgy message.

I did this, because (void **)&(p) looses type integrity due to the cast.

But yeah, I suppose it all needs a wee comment.

  reply	other threads:[~2023-08-15 13:54 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-15 10:52 Peter Zijlstra
2023-08-15 11:28 ` Rasmus Villemoes
2023-08-15 13:53   ` Peter Zijlstra [this message]
2023-08-15 15:16     ` David Laight
2023-08-16  7:23     ` Rasmus Villemoes
2023-08-16  8:00       ` Linus Torvalds
2023-08-16  8:58         ` Rasmus Villemoes
2023-08-16  9:17         ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230815135339.GA966323@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=dan.j.williams@intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=ndesaulniers@google.com \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®