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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3CFDAECAAD8 for ; Tue, 20 Sep 2022 04:03:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92A2A80009; Tue, 20 Sep 2022 00:03:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D9BB80007; Tue, 20 Sep 2022 00:03:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A26B80009; Tue, 20 Sep 2022 00:03:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6C78580007 for ; Tue, 20 Sep 2022 00:03:03 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 33AF481CAE for ; Tue, 20 Sep 2022 04:03:03 +0000 (UTC) X-FDA: 79931118246.13.35F42A4 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf31.hostedemail.com (Postfix) with ESMTP id 8CEE320006 for ; Tue, 20 Sep 2022 04:03:01 +0000 (UTC) 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> ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663646581; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+lv+1pLscsnrBbnFu732MJDcdMkpx4lhLo+NKm9WmeA=; b=VygGq9VVT2K2LBa5xQgtf4ATr5E7JXe+B1vfE6UqtXVI1GMy9MOKDtL/i0306Vsv0dLqu5 41s0VAgXuBIFR3BgbJVpB3x9BgNqZS4Io+3udXQMjNIK8RsAwo7RWheKeIuJ+9gPNvV8vB YzFtMg3LMeFfpqckwjpuZC5datsbleA= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=1k+hZz9C; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=PYcT9LQH; spf=pass (imf31.hostedemail.com: domain of osalvador@suse.de designates 195.135.220.29 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663646581; a=rsa-sha256; cv=none; b=iPxBSifOZcFBrlSEill+g0YWR8aXFbXDDl1Jf8XhtfB6yaiREXLOBldj4Bl+qucKILhMuo yBkfdbeR8z/TuBLOtXOjVxDliBQsKSWxnUkO3yktps/wtN9SoH8wsIdqqqa8/C4/Yg4Yi6 42psybbmjL7XvMIxPMzl+18aVLzRjEI= X-Rspam-User: Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=1k+hZz9C; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=PYcT9LQH; spf=pass (imf31.hostedemail.com: domain of osalvador@suse.de designates 195.135.220.29 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de X-Rspamd-Server: rspam04 X-Stat-Signature: t478n7jo77qqz8sk48eed9g65edf9u6r X-Rspamd-Queue-Id: 8CEE320006 X-HE-Tag: 1663646581-463430 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 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