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 23103C433E1 for ; Tue, 23 Mar 2021 08:02:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E8BEB619D2 for ; Tue, 23 Mar 2021 08:02:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229716AbhCWICH (ORCPT ); Tue, 23 Mar 2021 04:02:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229500AbhCWIBp (ORCPT ); Tue, 23 Mar 2021 04:01:45 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F024C061574 for ; Tue, 23 Mar 2021 01:01:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=lx3J6Y5Q3le0nZDa4hURyeJThg9+xLXsxyVyB1tvv0U=; b=h+sXw7FNjiA7THYFgpctWo8GVv a0g9V03KoAnatTs0p68hMqbimfiJY5KD2IJxzSedlDb/cRXM5V7NzIIKZ37XXHDZFT4Z2OnhQNDNf V2Jiq8jRYE4llLIrHlQEWHZOIPNCgvqFli6upWuhw+mZlm7vcZ/hhVvNXgtuHJoZgHVJj0VlLcg/C e1laS1PPsC5sVIYLRgukn1ysJOb/1yPX1ThzPImbhxGn43E0U8DlP/64dG/2kbR8wQngDhefR3yNm cMz6RbI8wwOCQJX/PY18/tRE9CZKzix5OYMW+eE7hN14hWbsZo5X5TwHkuHZYzVyirRFPSrpP04po d1sPhz4A==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lObyN-00EFl3-54; Tue, 23 Mar 2021 08:01:09 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 5C63C3010C8; Tue, 23 Mar 2021 09:01:03 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id F343A2053DA99; Tue, 23 Mar 2021 09:01:02 +0100 (CET) Date: Tue, 23 Mar 2021 09:01:02 +0100 From: Peter Zijlstra To: Michal Hocko 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, 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. 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. 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().