From: Arvind Sankar <nivedita@alum.mit.edu>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Arvind Sankar <nivedita@alum.mit.edu>,
Matthew Wilcox <willy@infradead.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
Dennis Zhou <dennis@kernel.org>, Tejun Heo <tj@kernel.org>,
Christoph Lameter <cl@linux.com>, Linux-MM <linux-mm@kvack.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Kees Cook <keescook@chromium.org>
Subject: Re: [GIT PULL] percpu fix for v5.9-rc6
Date: Fri, 18 Sep 2020 17:00:50 -0400 [thread overview]
Message-ID: <20200918210050.GA2953017@rani.riverdale.lan> (raw)
In-Reply-To: <CAHk-=wh-ryuY7KBNWr1n+kgQ5_CHB3-X+od-djBV4W-1kQFokA@mail.gmail.com>
On Fri, Sep 18, 2020 at 01:40:44PM -0700, Linus Torvalds wrote:
> On Fri, Sep 18, 2020 at 1:29 PM Arvind Sankar <nivedita@alum.mit.edu> wrote:
> >
> > In general (i.e. outside the implementation of the macro itself), what
> > is the preferred way of getting the size of just the header?
> > 1) offsetof(typeof(s),flex)
> > 2) struct_size(s, flex, 0)
>
> I think those two should end up being equivalent.
Yeah, but it would be good to standardize on one of them.
>
> > 3) sizeof(s)
>
> This works right now, but exactly *because* it works, we're not seeing
> the questionable cases.
>
> Of course, _also_ exactly because it just silently works, I also don't
> know if there may be thousands of perfectly fine uses where people
> really do want the header, and a "sizeof()" is simpler than
> alternatives 1-2.
>
> It's possible that there really are a lot of "I want to know just the
> header size" cases. It sounds odd, but I could _imagine_ situations
> like that, even though no actual case comes to mind.
I'm asking because I just added an instance of (3) and want to know if I
should change it :)
The case was when you have a function that got passed a pointer and a
size, and wants to verify that the size covers the structure before
accessing its fields. If the function only needs the "fixed" fields, it
feels a little unnatural to use (1) or (2) when the flex member is
otherwise not going be accessed at all.
>
> > 4) new macro that's easier to read than 1 or 2, but makes it clear
> > what you're doing?
>
> I don't think this would have any real advantage, would it?
The advantage is documenting that you do mean the header size, i.e.
something like struct_header_size(s).
>
> Now what might be good is if we can make "struct_size()" also actually
> verify that the member that is passed in is that last non-sized
> member. I'm not sure how to do that.
>
> I know how to check that it's *not* that last unsized member (just do
> "sizeof(s->flex)", and it should error), but I don't see how to assert
> the reverse of that).
>
> Because that kind of "yes, we actually pass in the right member" check
> would be good to have too.
>
> Linus
You could just assert that offsetof(typeof(s),flex) == sizeof(s), no? It
would also make sure that someone doesn't try to use struct_size() with
a 1-sized array member.
next prev parent reply other threads:[~2020-09-18 21:00 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-17 20:45 [GIT PULL] percpu fix for v5.9-rc6 Dennis Zhou
2020-09-18 1:05 ` Linus Torvalds
2020-09-18 16:23 ` Gustavo A. R. Silva
2020-09-18 17:23 ` Linus Torvalds
2020-09-18 19:34 ` Gustavo A. R. Silva
2020-09-18 19:37 ` Linus Torvalds
2020-09-18 20:02 ` Matthew Wilcox
2020-09-18 20:14 ` Linus Torvalds
2020-09-18 20:29 ` Arvind Sankar
2020-09-18 20:40 ` Linus Torvalds
2020-09-18 21:00 ` Arvind Sankar [this message]
2020-09-18 21:18 ` Linus Torvalds
2020-09-18 22:39 ` Arvind Sankar
2020-09-19 1:28 ` Linus Torvalds
2020-09-19 2:53 ` Arvind Sankar
2020-09-19 3:02 ` Matthew Wilcox
2020-09-19 3:04 ` Linus Torvalds
2020-09-19 2:45 ` Matthew Wilcox
2020-09-19 3:37 ` Arvind Sankar
2020-09-19 15:15 ` David Laight
2020-09-18 20:03 ` Gustavo A. R. Silva
2020-09-18 1:10 ` pr-tracker-bot
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=20200918210050.GA2953017@rani.riverdale.lan \
--to=nivedita@alum.mit.edu \
--cc=cl@linux.com \
--cc=dennis@kernel.org \
--cc=gustavoars@kernel.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=tj@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=willy@infradead.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;
as well as URLs for NNTP newsgroup(s).