From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 171B5C433E1 for ; Tue, 23 Mar 2021 08:15:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DC5A5619C3 for ; Tue, 23 Mar 2021 08:15:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229692AbhCWIOt (ORCPT ); Tue, 23 Mar 2021 04:14:49 -0400 Received: from mx2.suse.de ([195.135.220.15]:54124 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229804AbhCWIOQ (ORCPT ); Tue, 23 Mar 2021 04:14:16 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1616487255; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=qg/9oH2DL28aA39SDUTzyvnM+v5iX+Jupkcgmnk0eP8=; b=HSjcd/rujZRJC9TgzQN1VgjKc5vA+MWvlPys3Xn1IcHPpszcqEi006OSV0P2i2XLL+qH39 Xcv9qYvi7Ip0b2h/CQOdx4aI+kajl260Erd0JwJA4hfaZMn6jKimoIpFlRCnHP1KN9e27D Y8kT4Tj9Gy9UapkTm0fMX+f3zeMogHI= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 5B2CEAB8A; Tue, 23 Mar 2021 08:14:15 +0000 (UTC) Date: Tue, 23 Mar 2021 09:14:12 +0100 From: Michal Hocko To: Peter Zijlstra Cc: Mike Kravetz , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt , Oscar Salvador , David Hildenbrand , Muchun Song , David Rientjes , Miaohe Lin , Matthew Wilcox , HORIGUCHI NAOYA , "Aneesh Kumar K . V" , Waiman Long , Peter Xu , Mina Almasry , Andrew Morton Subject: Re: [RFC PATCH 2/8] hugetlb: recompute min_count when dropping hugetlb_lock Message-ID: References: <20210319224209.150047-1-mike.kravetz@oracle.com> <20210319224209.150047-3-mike.kravetz@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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