Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp3145083rwe; Sun, 16 Apr 2023 12:48:51 -0700 (PDT) X-Google-Smtp-Source: AKy350Yl6/sYsyWM/cwl7IaXMuz/RsIzdjjgi6JcUOG+86dpm627R+QsxffTpvYdaiAsaPGVuFlr X-Received: by 2002:a62:8249:0:b0:594:1f1c:3d3a with SMTP id w70-20020a628249000000b005941f1c3d3amr19147424pfd.15.1681674531480; Sun, 16 Apr 2023 12:48:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681674531; cv=none; d=google.com; s=arc-20160816; b=hDYh5izK3f9HGjkEgXYRsx9FOLO37uMcX7xYTuAWaPvO9aOpmrWFiHFKJc7qcYe5Gq emyBzKHqYkHPsgLu2bCCVmckrQU6YyK5US7axEAKKqSvQjS72YQLvqK8yNj9dCZx/zGo F6uTSqXj7VUnk8M9iJmw/CvaoSSeqZBzopjAQbIk3iqfXBwXpDzemHSYBQ1SBfg8DVHq 7VliBz/KNfY+iu70U47NEJC9w4tjgbsJQdqW/HsJXFUVb4/gEzGavCSdfwGDwJw1WhZu YWOSv0FjLAXk9C1dreqaWfzfVXVMyXBo+OjpWCBsCc9k0rqMDhuYuRYBkeEDtlv/CKEL wwow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:message-id:in-reply-to :subject:cc:to:from:date:dkim-signature; bh=fE9QU8ssRHfrkp+Qs6Jt2adPN0B10qQBjFxitvUur5w=; b=EMkg4e/5zXaYi//2CE61veNdUxyeOTqKKUz9CR3vY8Xu2kyDSTdQcXAtvZCTzdv8Pz nOWBiH7WwNt7FzeIsTozpGbQ6Ka9nWb6YN5ekpYvXSi6gvo3ZKqsard8ojjAJCqk34JV JLQ8g0ZUxlqMmlG6VGprisAOd2Bm6V6ywPzhYccEcmJhzftfq0mYC5PWEY4InJMHdB6f 4gfbJy3KkQrSRVz4xdihsWiQIJBN9/hF72cqqlx0No/L2aqPK3cBDuk05bI2fHTKNdaz yQUvxjCHdG3gOtYTRJi8eLt9iN+2mKiv6Ls3YE4WtS/T4uuoAxTrDdDWwGx1M9uuh1zW yudw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=pTFh9YoY; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o13-20020aa7978d000000b00624e7c7bb07si9705272pfp.265.2023.04.16.12.48.38; Sun, 16 Apr 2023 12:48:51 -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=@google.com header.s=20221208 header.b=pTFh9YoY; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229762AbjDPTo6 (ORCPT + 99 others); Sun, 16 Apr 2023 15:44:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229446AbjDPTo5 (ORCPT ); Sun, 16 Apr 2023 15:44:57 -0400 Received: from mail-yw1-x1132.google.com (mail-yw1-x1132.google.com [IPv6:2607:f8b0:4864:20::1132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BCF282694 for ; Sun, 16 Apr 2023 12:44:56 -0700 (PDT) Received: by mail-yw1-x1132.google.com with SMTP id 00721157ae682-54fe82d8bf5so84861987b3.3 for ; Sun, 16 Apr 2023 12:44:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681674296; x=1684266296; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=fE9QU8ssRHfrkp+Qs6Jt2adPN0B10qQBjFxitvUur5w=; b=pTFh9YoYONT2cKveKNA6jETiwf92HtqU0CX8Fb+eU6YKNATL4+NSIe9LuB571YVQQ2 FvSwcvWgTww19tc9Rn0y+4sCK5LZLj6N0yzT1FA5tlasgFfufKUaYPfhYO9DEP9ZDcYL HchzUVzvPelw0Q4huckH+4NhHZRPQrzs+FZc7TS9g0u1uCXZ7Gh7Rx0t1vw6xeO3fjB1 ias0ybZKgaEzEMgV3MEOgwIBoeyu5n0/3eqYzjsLLft3tmdEYqDpNZ5I986azDXD11KD q/h6ljYEwHQnadmPQUM7nqloQwRDf9UjRpTnWnfKFqeIYHT89pXApVgoyTjabBqFP5wG a3Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681674296; x=1684266296; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fE9QU8ssRHfrkp+Qs6Jt2adPN0B10qQBjFxitvUur5w=; b=AFS7XeCCX1iFfKfh9HN61vFMNTXTpU0j4ccJKcBuM4bORTRq/nkskaPg7vcFJHUXhB 7i5I1+mLd5T/zr51gMdql9IV4E3zZKzuUrXB0iFC2ONYvw+nfDggZ+gSNxmfnTHryqG/ K6slU460IzTQXkvJdPSzNMI0FlezrkHZDz81L8+xU34pdoxx6ac7rWJNQlp2ZT5EObD7 ejZzp+SupMakMkyNlzSzbiXgZhDr7+iozu5qjku7HayFujWJimFedhBicvbLRGevGjVz CLpmdUe/ZH9V/RE+34DToDNd4JtG7ePYFUgRI8HlDDj0oDmmRTk2Bt2itbZDeT3VWyNe tCbA== X-Gm-Message-State: AAQBX9eskOdwrMW9WVf9xH1oIN5qMpQUSum7sGspv2u4RoPhwrvJ7iFI seEG2kIl3o2CCRxHmj9I58ibwQ== X-Received: by 2002:a81:7782:0:b0:543:b06a:19de with SMTP id s124-20020a817782000000b00543b06a19demr12158258ywc.3.1681674295681; Sun, 16 Apr 2023 12:44:55 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id b186-20020a811bc3000000b0054eff15530asm2650453ywb.90.2023.04.16.12.44.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Apr 2023 12:44:55 -0700 (PDT) Date: Sun, 16 Apr 2023 12:44:53 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Zi Yan cc: "Matthew Wilcox (Oracle)" , Yang Shi , Yu Zhao , linux-mm@kvack.org, "Kirill A . Shutemov" , Ryan Roberts , =?ISO-8859-15?Q?Michal_Koutn=FD?= , Roman Gushchin , Zach O'Keefe , Andrew Morton , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH v3 6/7] mm: truncate: split huge page cache page to a non-zero order if possible. In-Reply-To: <20230403201839.4097845-7-zi.yan@sent.com> Message-ID: <9dd96da-efa2-5123-20d4-4992136ef3ad@google.com> References: <20230403201839.4097845-1-zi.yan@sent.com> <20230403201839.4097845-7-zi.yan@sent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Mon, 3 Apr 2023, Zi Yan wrote: > From: Zi Yan > > To minimize the number of pages after a huge page truncation, we do not > need to split it all the way down to order-0. The huge page has at most > three parts, the part before offset, the part to be truncated, the part > remaining at the end. Find the greatest common divisor of them to > calculate the new page order from it, so we can split the huge > page to this order and keep the remaining pages as large and as few as > possible. > > Signed-off-by: Zi Yan > --- > mm/truncate.c | 21 +++++++++++++++++++-- > 1 file changed, 19 insertions(+), 2 deletions(-) > > diff --git a/mm/truncate.c b/mm/truncate.c > index 86de31ed4d32..817efd5e94b4 100644 > --- a/mm/truncate.c > +++ b/mm/truncate.c > @@ -22,6 +22,7 @@ > #include /* grr. try_to_release_page */ > #include > #include > +#include Really? > #include "internal.h" > > /* > @@ -211,7 +212,8 @@ int truncate_inode_folio(struct address_space *mapping, struct folio *folio) > bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) > { > loff_t pos = folio_pos(folio); > - unsigned int offset, length; > + unsigned int offset, length, remaining; > + unsigned int new_order = folio_order(folio); > > if (pos < start) > offset = start - pos; > @@ -222,6 +224,7 @@ bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) > length = length - offset; > else > length = end + 1 - pos - offset; > + remaining = folio_size(folio) - offset - length; > > folio_wait_writeback(folio); > if (length == folio_size(folio)) { > @@ -236,11 +239,25 @@ bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) > */ > folio_zero_range(folio, offset, length); > > + /* > + * Use the greatest common divisor of offset, length, and remaining > + * as the smallest page size and compute the new order from it. So we > + * can truncate a subpage as large as possible. Round up gcd to > + * PAGE_SIZE, otherwise ilog2 can give -1 when gcd/PAGE_SIZE is 0. > + */ > + new_order = ilog2(round_up(gcd(gcd(offset, length), remaining), > + PAGE_SIZE) / PAGE_SIZE); Gosh. In mm/readahead.c I can see "order = __ffs(index)", and I think something along those lines would be more appropriate here. But, if there's any value at all to choosing intermediate orders here in truncation, I don't think choosing a single order is the right approach - more easily implemented, yes, but is it worth doing? What you'd actually want (if anything) is to choose the largest orders possible, with smaller and smaller orders filling in the rest (I expect there's a technical name for this, but I don't remember - bin packing is something else, I think). As this code stands, truncate a 2M huge page at 1M and you get two 1M pieces (one then discarded) - nice; but truncate it at 1M+1 and you get lots of order 2 (forced up from 1) pieces. Seems weird, and not worth the effort. Hugh > + > + /* order-1 THP not supported, downgrade to order-0 */ > + if (new_order == 1) > + new_order = 0; > + > + > if (folio_has_private(folio)) > folio_invalidate(folio, offset, length); > if (!folio_test_large(folio)) > return true; > - if (split_folio(folio) == 0) > + if (split_huge_page_to_list_to_order(&folio->page, NULL, new_order) == 0) > return true; > if (folio_test_dirty(folio)) > return false; > -- > 2.39.2