From: Kees Cook <kees@kernel.org>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Christoph Lameter <cl@linux.com>,
Pekka Enberg <penberg@kernel.org>,
David Rientjes <rientjes@google.com>,
Joonsoo Kim <iamjoonsoo.kim@lge.com>,
Andrew Morton <akpm@linux-foundation.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Hyeonggon Yoo <42.hyeyoo@gmail.com>,
"Gustavo A . R . Silva" <gustavoars@kernel.org>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>,
Jann Horn <jannh@google.com>,
Przemek Kitszel <przemyslaw.kitszel@intel.com>,
Marco Elver <elver@google.com>,
linux-mm@kvack.org, Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH v3] slab: Introduce kmalloc_obj() and family
Date: Tue, 27 Aug 2024 17:19:39 -0700 [thread overview]
Message-ID: <202408271709.31D322019@keescook> (raw)
In-Reply-To: <5c3852e6-4a6a-42d8-85ff-8c1605939454@suse.cz>
On Tue, Aug 27, 2024 at 11:32:14PM +0200, Vlastimil Babka wrote:
> +Cc Linus
>
> On 8/23/24 01:13, Kees Cook wrote:
> > Introduce type-aware kmalloc-family helpers to replace the common
> > idioms for single, array, and flexible object allocations:
> >
> > ptr = kmalloc(sizeof(*ptr), gfp);
> > ptr = kzalloc(sizeof(*ptr), gfp);
> > ptr = kmalloc_array(count, sizeof(*ptr), gfp);
> > ptr = kcalloc(count, sizeof(*ptr), gfp);
> > ptr = kmalloc(struct_size(ptr, flex_member, count), gfp);
> >
> > These become, respectively:
> >
> > kmalloc_obj(ptr, gfp);
> > kzalloc_obj(ptr, gfp);
> > kmalloc_objs(ptr, count, gfp);
> > kzalloc_objs(ptr, count, gfp);
> > kmalloc_flex(ptr, flex_member, count, gfp);
>
> This is indeed better than the previous version. The hidden assignment to
> ptr seems still very counter-intuitive, but if it's the only way to do those
> validations, the question is then just whether it's worth the getting used
> to it, or not.
We could make the syntax require "&ptr"?
As for alternatives, one thing I investigated for a while that made
several compiler people unhappy was to introduce an builtin named
something like __builtin_lvalue() which could be used on the RHS of an
assignment to discover the lvalue in some way. Then we could, for
example, add alignment discovery like so:
#define kmalloc(_size, _gfp) \
__kmalloc(_size, __alignof(typeof(__builtin_lvalue())), _gfp)
or do the FAM struct allocations:
#define kmalloc_flex(_member, _count, _gfp) \
__kmalloc(sizeof(*typeof(__builtin_lvalue())) +
sizeof(*__builtin_lvalue()->_member) * (_count), gfp)
Compiler folks seems very unhappy with this, though. As I can recognize
it creates problems for stuff like:
return kmalloc(...)
Of course the proposed macros still have the above problem, and both to
use a temporary variable to deal with it.
So, really it's a question of "how best to introspect the lvalue?"
> [...]
> > by GCC[1] and Clang[2]. The internal use of __flex_count() allows for
> > automatically setting the counter member of a struct's flexible array
>
> But if it's a to-be-implemented feature, perhaps it would be too early to
> include it here? Were you able to even test that part right now?
There are RFC patches for both GCC and Clang that I tested against.
However, yes, it is still pretty early. I just wanted to show that it
can work, etc. (i.e. not propose a macro with no "real" benefit over the
existing assignments).
> [...]
> > Replacing all existing simple code patterns found via Coccinelle[3]
> > shows what could be replaced immediately (saving roughly 1,500 lines):
> >
> > 7040 files changed, 14128 insertions(+), 15557 deletions(-)
>
> Since that could be feasible to apply only if Linus ran that directly
> himself, including him now. Because doing it any other way would leave us
> semi-converted forever and not bring the full benefits?
Right -- I'd want to do a mass conversion and follow it up with any
remaining ones. There are a lot in the style of "return k*alloc(...)"
for example.
> [...]
> > +#define kvmalloc_obj(P, FLAGS) \
> > + __alloc_objs(kvmalloc, P, 1, FLAGS, NULL)
>
> Wonder if there is really a single struct (not array) with no flex array
> that could need kvmalloc? :)
Ah, yes, Good point. I was going for "full" macro coverage. :P
Thanks for looking at this!
-Kees
--
Kees Cook
next prev parent reply other threads:[~2024-08-28 0:37 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-22 23:13 Kees Cook
2024-08-23 4:27 ` Przemek Kitszel
2024-10-04 17:23 ` Kees Cook
2024-08-27 21:32 ` Vlastimil Babka
2024-08-28 0:19 ` Kees Cook [this message]
2024-08-28 15:37 ` Christoph Lameter (Ampere)
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=202408271709.31D322019@keescook \
--to=kees@kernel.org \
--cc=42.hyeyoo@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=cl@linux.com \
--cc=elver@google.com \
--cc=gustavoars@kernel.org \
--cc=iamjoonsoo.kim@lge.com \
--cc=jannh@google.com \
--cc=justinstitt@google.com \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=llvm@lists.linux.dev \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=penberg@kernel.org \
--cc=przemyslaw.kitszel@intel.com \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=torvalds@linux-foundation.org \
--cc=vbabka@suse.cz \
/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