Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3404157rwb; Mon, 19 Sep 2022 21:45:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7vXFcjmAnL6+aKJ16BiZIODt3ZnXnt+khj9xOkhUJNzFa1x62x2aK3+RCMPqhW57193EmD X-Received: by 2002:a17:906:4fc9:b0:780:da33:2e95 with SMTP id i9-20020a1709064fc900b00780da332e95mr10560332ejw.682.1663649142618; Mon, 19 Sep 2022 21:45:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663649142; cv=none; d=google.com; s=arc-20160816; b=TOn2kRpQW4bmQHWJbvU6KRgruWIMK3mC7DT9kG/BF5fIdgGTb0gSIrXquRvbx2/6di /A0oNsRSR5q6Tm/FN5yYW1dObJc7MlGxyv7nvMVXaUhptWisg9yrvikNNfV4bE6sZtwI fW4AK649T6EREKSQR1KRretA+tvnha7PnK5yPab+SiCBBICDaoTrMNnud+5xSK4lbfHb ojX3sQAShiCyVy/zikYfjtqRBZ82TJ8WghXosCvL5dakiL4TAGwkNUdlRyiClI+Nxsfr eke9+oECp6jVfzJ4VeE+8dhtYZdc0lX8xYz4Ly0grhzQEwbSUGUZqg/VJpld1rovR4uG xv5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :dkim-signature; bh=+lv+1pLscsnrBbnFu732MJDcdMkpx4lhLo+NKm9WmeA=; b=vAVfg7M/rTy6RTlrD6/wJOUgde80YpklQyP+L58idxj0vYOWD+75uODOmGWrETfASB bXa4tyS+qZM00u4fxoAPxwNtAM8rou2ycrnPC4AKrxU5UdYEBAS49CL7Qyy7hukmHAvh NY65YqzbT8/NEkUK+bHV5Oj+AvzJHfhxR3eUcrjaHOEkXCMEIDoUjUnhkxsx6na/1Nd0 9mPO+Hs9K75jo04CvHl5Nkzyn/vg9PFCsdABn6aUgaKMBS+ckYJ2pX5wifRKlwsMCPYt 15lv4u9QOdSPeY3S3wgy0qzzQBybJziilXXOmB3VNg8ceh/4UiqSl3NT1wc3fZrNIqvV yGCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=1k+hZz9C; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 10-20020a170906308a00b0073317952891si248506ejv.777.2022.09.19.21.45.17; Mon, 19 Sep 2022 21:45:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=1k+hZz9C; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229684AbiITEDF (ORCPT + 99 others); Tue, 20 Sep 2022 00:03:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229572AbiITEDC (ORCPT ); Tue, 20 Sep 2022 00:03:02 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 878FB632B for ; Mon, 19 Sep 2022 21:03:01 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 3BA571F388; Tue, 20 Sep 2022 04:03:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1663646580; 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=+lv+1pLscsnrBbnFu732MJDcdMkpx4lhLo+NKm9WmeA=; b=1k+hZz9Cx4oQ8CUogRqpFgdCDpxlbOWirCrEmYdOU6PaoZ05Eyr+VcGfAL4YXELauLBlW9 tWDapyWnG/CDnU/HnsA01wvUjmKS66xWVccLkq9kh7SEdV/r3ykAISEl+qy6yajgpfGl0o kVIsJ/c/oMjUOpyJRrpRKca9eL6bRT4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1663646580; 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=+lv+1pLscsnrBbnFu732MJDcdMkpx4lhLo+NKm9WmeA=; b=PYcT9LQHQFl/X6jtNGJymqbsnpaOVlva7lCu4SrTn/6gCPCMCvDJhPZjUsLeGJ5iiQfDeA lbqfeDAer9McPICA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C0A2813ABD; Tue, 20 Sep 2022 04:02:58 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id FVIsLHI7KWOyfwAAMHmgww (envelope-from ); Tue, 20 Sep 2022 04:02:58 +0000 Date: Tue, 20 Sep 2022 06:02:56 +0200 From: Oscar Salvador To: Mike Kravetz Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Joao Martins , Matthew Wilcox , Michal Hocko , Peter Xu , Miaohe Lin , Vlastimil Babka , Andrew Morton Subject: Re: [PATCH v2] hugetlb: freeze allocated pages before creating hugetlb pages Message-ID: References: <20220916214638.155744-1-mike.kravetz@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220916214638.155744-1-mike.kravetz@oracle.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 16, 2022 at 02:46:38PM -0700, Mike Kravetz wrote: > When creating hugetlb pages, the hugetlb code must first allocate > contiguous pages from a low level allocator such as buddy, cma or > memblock. The pages returned from these low level allocators are > ref counted. This creates potential issues with other code taking > speculative references on these pages before they can be transformed to > a hugetlb page. This issue has been addressed with methods and code > such as that provided in [1]. > > Recent discussions about vmemmap freeing [2] have indicated that it > would be beneficial to freeze all sub pages, including the head page > of pages returned from low level allocators before converting to a > hugetlb page. This helps avoid races if we want to replace the page > containing vmemmap for the head page. > > There have been proposals to change at least the buddy allocator to > return frozen pages as described at [3]. If such a change is made, it > can be employed by the hugetlb code. However, as mentioned above > hugetlb uses several low level allocators so each would need to be > modified to return frozen pages. For now, we can manually freeze the > returned pages. This is done in two places: > 1) alloc_buddy_huge_page, only the returned head page is ref counted. > We freeze the head page, retrying once in the VERY rare case where > there may be an inflated ref count. > 2) prep_compound_gigantic_page, for gigantic pages the current code > freezes all pages except the head page. New code will simply freeze > the head page as well. > > In a few other places, code checks for inflated ref counts on newly > allocated hugetlb pages. With the modifications to freeze after > allocating, this code can be removed. > > After hugetlb pages are freshly allocated, they are often added to the > hugetlb free lists. Since these pages were previously ref counted, this > was done via put_page() which would end up calling the hugetlb > destructor: free_huge_page. With changes to freeze pages, we simply > call free_huge_page directly to add the pages to the free list. > > In a few other places, freshly allocated hugetlb pages were immediately > put into use, and the expectation was they were already ref counted. In > these cases, we must manually ref count the page. > > [1] https://lore.kernel.org/linux-mm/20210622021423.154662-3-mike.kravetz@oracle.com/ > [2] https://lore.kernel.org/linux-mm/20220802180309.19340-1-joao.m.martins@oracle.com/ > [3] https://lore.kernel.org/linux-mm/20220809171854.3725722-1-willy@infradead.org/ > > Signed-off-by: Mike Kravetz Hi Mike, this looks great and simplifies the code much more. I got a question though: > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -1787,9 +1787,8 @@ static bool __prep_compound_gigantic_page(struct page *page, unsigned int order, > > /* we rely on prep_new_huge_page to set the destructor */ > set_compound_order(page, order); > - __ClearPageReserved(page); > __SetPageHead(page); > - for (i = 1; i < nr_pages; i++) { > + for (i = 0; i < nr_pages; i++) { > p = nth_page(page, i); > > /* > @@ -1830,17 +1829,19 @@ static bool __prep_compound_gigantic_page(struct page *page, unsigned int order, > } else { > VM_BUG_ON_PAGE(page_count(p), p); > } > - set_compound_head(p, page); > + if (i != 0) > + set_compound_head(p, page); Sure I am missing something here, but why we only freeze refcount here in case it is for demote? We seem to be doing it inconditionally in alloc_buddy_huge_page. -- Oscar Salvador SUSE Labs