From: Alexander Potapenko <glider@google.com>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: David Hildenbrand <david@redhat.com>,
Andrew Morton <akpm@linux-foundation.org>,
Linux Memory Management List <linux-mm@kvack.org>,
LKML <linux-kernel@vger.kernel.org>,
Kees Cook <keescook@chromium.org>,
Michal Hocko <mhocko@kernel.org>,
Mateusz Nosek <mateusznosek0@gmail.com>,
Laura Abbott <labbott@kernel.org>
Subject: Re: [PATCH 3/3] mm, page_alloc: reduce static keys in prep_new_page()
Date: Thu, 29 Oct 2020 18:37:04 +0100 [thread overview]
Message-ID: <CAG_fn=VGOVGn3Vx177ACDqg8BaTS96B6Kx01_pxXG5R1D-cWRw@mail.gmail.com> (raw)
In-Reply-To: <81faf3d6-9536-ff00-447d-e964a010492d@suse.cz>
On Tue, Oct 27, 2020 at 2:32 PM Vlastimil Babka <vbabka@suse.cz> wrote:
>
> On 10/27/20 12:05 PM, Vlastimil Babka wrote:
> > On 10/27/20 10:10 AM, David Hildenbrand wrote:
> >> On 26.10.20 18:33, Vlastimil Babka wrote:
> >>> prep_new_page() will always zero a new page (regardless of __GFP_ZERO) when
> >>> init_on_alloc is enabled, but will also always skip zeroing if the page was
> >>> already zeroed on free by init_on_free or page poisoning.
> >>>
> >>> The latter check implemented by free_pages_prezeroed() can involve two
> >>> different static keys. As prep_new_page() is really a hot path, let's introduce
> >>> a single static key free_pages_not_prezeroed for this purpose and initialize it
> >>> in init_mem_debugging().
> >>
> >> Is this actually observable in practice? This smells like
> >> micro-optimization to me.
> >>
> >> Also, I thought the whole reason for static keys is to have basically no
> >> overhead at runtime, so I wonder if replacing two static key checks by a
> >> single one actually makes *some* difference.
> >
> > You're right, the difference seems to be just a single NOP. The static key
> > infrastructure seems to be working really well.
> > (At least the asm inspection made me realize that kernel_poison_pages() is
> > called unconditionally and the static key is checked inside, not inline so I'll
> > be amending patch 2...)
> >
> > Initially I thought I would be reducing 3 keys to 1 in this patch, but I got the
> > code wrong. So unless others think it's a readability improvements, we can drop
> > this patch.
I agree with David that replacing two static keys with one is probably
a micro-optimization.
Also, if someone is enabling both init_on_alloc and init_on_free, they
are already paying so much that no one is going to notice an extra
static key.
> > Or we can also reconsider this whole optimization. If the point is to be
> > paranoid and enable both init_on_free and init_on_alloc, should we trust that
> > nobody wrote something after the clearing on free via use-after-free? :) Kees/Alex?
I think we must trust the kernel to not overwrite zeroed pages.
After all, this could theoretically happen at any time, not only while
the memory chunk is freed.
> More thoughts...
>
> PAGE_POISONING_NO_SANITY skips the check on "unpoisoning" whether poison was
> corrupted
> PAGE_POISONING_ZERO uses zero instead of 0xAA as poison pattern
>
> the point of enabling both of these seems to be moot now that init_on_free
> exists, as that zeroes pages that are being freed, without checking on alloc
> that they are still zeroed.
>
> What if only one is enabled?
> - PAGE_POISONING_NO_SANITY without PAGE_POISONING_ZERO - we poison with the 0xAA
> pattern but nobody checks it, so does it give us anything over init_on_free
> writing zeroes? I don't think so?
>
> - PAGE_POISONING_ZERO without PAGE_POISONING_NO_SANITY - we use zeroes (like
> init_on_free) but also check that it wasn't corrupted. We save some time on
> writing zeroes again on alloc, but the check is still expensive. And writing
> 0xAA would possibly detect more corruptions than writing zero (a stray write of
> NULL is more likely to happen than of 0xAA?).
>
> So my conclusion:
> - We can remove PAGE_POISONING_NO_SANITY because it only makes sense with
> PAGE_POISONING_ZERO, and we can use init_on_free instead
Agreed.
> - We can also probably remove PAGE_POISONING_ZERO, because if we want to do the
> unpoisoning sanity check, then we also most likely want the 0xAA pattern and not
> zero.
Agreed.
It might also make sense to somehow merge page poisoning and
init_on_free together and have one config dimension instead of two
(providing something similar to the
INIT_STACK_NONE/INIT_STACK_ALL_ZERO/INIT_STACK_ALL_PATTERN configs)
> Thoughts?
>
--
Alexander Potapenko
Software Engineer
Google Germany GmbH
Erika-Mann-Straße, 33
80636 München
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
prev parent reply other threads:[~2020-10-29 17:37 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-26 17:33 [PATCH 0/3] optimize handling of memory debugging parameters Vlastimil Babka
2020-10-26 17:33 ` [PATCH 1/3] mm, page_alloc: do not rely on the order of page_poison and init_on_alloc/free parameters Vlastimil Babka
2020-10-27 9:03 ` David Hildenbrand
2020-10-27 9:58 ` Vlastimil Babka
2020-10-27 9:58 ` David Hildenbrand
2020-10-28 8:31 ` Mike Rapoport
2020-10-26 17:33 ` [PATCH 2/3] mm, page_poison: use static key more efficiently Vlastimil Babka
2020-10-27 9:07 ` David Hildenbrand
2020-10-30 16:27 ` Luis Chamberlain
2020-10-30 22:56 ` Vlastimil Babka
2020-11-11 13:29 ` Luis Chamberlain
2020-10-26 17:33 ` [PATCH 3/3] mm, page_alloc: reduce static keys in prep_new_page() Vlastimil Babka
2020-10-27 9:10 ` David Hildenbrand
2020-10-27 11:05 ` Vlastimil Babka
2020-10-27 13:32 ` Vlastimil Babka
2020-10-27 17:41 ` Vlastimil Babka
2020-10-28 8:38 ` David Hildenbrand
2020-10-29 17:37 ` Alexander Potapenko [this message]
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='CAG_fn=VGOVGn3Vx177ACDqg8BaTS96B6Kx01_pxXG5R1D-cWRw@mail.gmail.com' \
--to=glider@google.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=keescook@chromium.org \
--cc=labbott@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mateusznosek0@gmail.com \
--cc=mhocko@kernel.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;
as well as URLs for NNTP newsgroup(s).