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 C2340C433C1 for ; Tue, 23 Mar 2021 08:14:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6566C619B6 for ; Tue, 23 Mar 2021 08:14:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6566C619B6 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BEEB46B0126; Tue, 23 Mar 2021 04:14:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B9E886B0129; Tue, 23 Mar 2021 04:14:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A17E16B012B; Tue, 23 Mar 2021 04:14:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0156.hostedemail.com [216.40.44.156]) by kanga.kvack.org (Postfix) with ESMTP id 81F606B0126 for ; Tue, 23 Mar 2021 04:14:18 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 3EAE187F8 for ; Tue, 23 Mar 2021 08:14:18 +0000 (UTC) X-FDA: 77950426596.24.8F99549 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf23.hostedemail.com (Postfix) with ESMTP id 2CE12A0009E0 for ; Tue, 23 Mar 2021 08:14:16 +0000 (UTC) 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: X-Stat-Signature: pnd1xm8kffqopb3jkijptetqu6ycy6ye X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2CE12A0009E0 Received-SPF: none (suse.com>: No applicable sender policy available) receiver=imf23; identity=mailfrom; envelope-from=""; helo=mx2.suse.de; client-ip=195.135.220.15 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616487256-95499 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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