lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <YFmjVNnnHHpyxqKe@dhcp22.suse.cz>
Date:   Tue, 23 Mar 2021 09:14:12 +0100
From:   Michal Hocko <mhocko@...e.com>
To:     Peter Zijlstra <peterz@...radead.org>
Cc:     Mike Kravetz <mike.kravetz@...cle.com>, linux-mm@...ck.org,
        linux-kernel@...r.kernel.org, Shakeel Butt <shakeelb@...gle.com>,
        Oscar Salvador <osalvador@...e.de>,
        David Hildenbrand <david@...hat.com>,
        Muchun Song <songmuchun@...edance.com>,
        David Rientjes <rientjes@...gle.com>,
        Miaohe Lin <linmiaohe@...wei.com>,
        Matthew Wilcox <willy@...radead.org>,
        HORIGUCHI NAOYA <naoya.horiguchi@....com>,
        "Aneesh Kumar K . V" <aneesh.kumar@...ux.ibm.com>,
        Waiman Long <longman@...hat.com>, Peter Xu <peterx@...hat.com>,
        Mina Almasry <almasrymina@...gle.com>,
        Andrew Morton <akpm@...ux-foundation.org>
Subject: Re: [RFC PATCH 2/8] hugetlb: recompute min_count when dropping
 hugetlb_lock

On Tue 23-03-21 09:01:02, Peter Zijlstra wrote:
> On Tue, Mar 23, 2021 at 08:50:53AM +0100, Michal Hocko wrote:
> 
> > > >> +static inline unsigned long min_hp_count(struct hstate *h, unsigned long count)
> > > >> +{
> > > >> +	unsigned long min_count;
> > > >> +
> > > >> +	min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
> > > >> +	return max(count, min_count);
> > > > 
> > > > Just out of curiousity, is compiler allowed to inline this piece of code
> > > > and then cache the value? In other words do we need to make these
> > > > READ_ONCE or otherwise enforce the no-caching behavior?
> > > 
> > > I honestly do not know if the compiler is allowed to do that.  The
> > > assembly code generated by my compiler does not cache the value, but
> > > that does not guarantee anything.  I can add READ_ONCE to make the
> > > function look something like:
> > > 
> > > static inline unsigned long min_hp_count(struct hstate *h, unsigned long count)
> > > {
> > > 	unsigned long min_count;
> > > 
> > > 	min_count = READ_ONCE(h->resv_huge_pages) + READ_ONCE(h->nr_huge_pages)
> > > 					- READ_ONCE(h->free_huge_pages);
> > > 	return max(count, min_count);
> > > }
> > 
> > Maybe just forcing to never inline the function should be sufficient.
> > This is not a hot path to micro optimize for no function call. But there
> > are much more qualified people on the CC list on this matter who could
> > clarify. Peter?
> 
> I'm not sure I understand the code right.

We need to ensure the function is evaluated each time it is called
because it will be used after a lock is dropped and reacquired so
numbers could have changed. The point of wrapping this into a function
is to reduce the code duplication IIUC.

> But inline or not doesn't
> matter, LTO completely ruins that game. Just like if it was a static
> function, then the compiler is free to inline it, even if the function
> lacks an inline attribute.

OK

> Basically, without READ_ONCE() the compiler is allowed to entirely elide
> the load (and use a previous load), or to duplicate the load and do it
> again later (reaching a different result).
> 
> Similarly, the compiler is allowed to byte-wise load the variable in any
> random order and re-assemble.
> 
> If any of that is a problem, you have to use READ_ONCE().

Thanks for the confirmation!
-- 
Michal Hocko
SUSE Labs

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ