Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp302038pxt; Wed, 4 Aug 2021 23:30:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxKVm7iRGetKESWzK70Vq9nOjFssPdB7fiUMJfmK1FXp39FexlXVSOuWjCXR3z0sZ3EiE3Z X-Received: by 2002:a02:a390:: with SMTP id y16mr3176776jak.120.1628145037517; Wed, 04 Aug 2021 23:30:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628145037; cv=none; d=google.com; s=arc-20160816; b=dn+vq7TyNDmXH2N08LJONGTKHo495BljGU2YpMYbDgXG6ASKd8EqdJbYWk7rdPLJrQ GHCFoF/pE/RpvEmBL46fUOB5IZYQQ+nHgK4JztCoxKQXs71vRHRjFXd3xtyrmEhGV/rZ T/ptXf8bqz2YXK5aKbwymPRqTsr+2eQvtvBay5M0Rl0eq9BxYKKdsQCAJhryEMgjYEyx gP4IA+8kXthfmoI7Z5mO1UYxigYeXpjcBWIlabJsMw326prpdyUkRweQazr3Jy4PHv3M sFeF3U4sD4ubgxcHudFJVgW64Qb9QpFGD4j7bMzS7NbZrWYAHAbgk9uSvLmwqT5XybGi 37sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=94iHnb+/irUo9Hib68Z88vXnda7LIvHLlcB+6a3kJg0=; b=lOSjVwST+Z5IKRKKOGJQw2hTKToWnSRMtFBdEU+Z9TRKcAICewE5bKaVM6BKV5bvw7 OFQfpvS64wi767KSPCmxgPsXU0Q+20qclYI7xPqwZCxpvr7bbZMc+/FPD0Ul8yJIDxOU R533+ibHSPhPHWRhM4vK1v5cu9SUN7l9gCeQjukESVhUwFfug6vutlhAKFuwE5Un29gs zsopTJRFQGZoJU73Pcz2buGG5MK8f+OUd/LMIgP4+E7ASyZgwf/Vs10JrYSsPoGFvAYt EqfuMZSr630XLZcPpk/Cb9A7i36Uk+27pQv82WeCbspZaLoLlnLb3NtM7s75jgyEjWPk Y/rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=fXytiDtQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d187si5200551iog.56.2021.08.04.23.30.25; Wed, 04 Aug 2021 23:30:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=fXytiDtQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236015AbhHEANj (ORCPT + 99 others); Wed, 4 Aug 2021 20:13:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229991AbhHEANi (ORCPT ); Wed, 4 Aug 2021 20:13:38 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48A03C0613D5 for ; Wed, 4 Aug 2021 17:13:24 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id gs8so6339899ejc.13 for ; Wed, 04 Aug 2021 17:13:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=94iHnb+/irUo9Hib68Z88vXnda7LIvHLlcB+6a3kJg0=; b=fXytiDtQb8tuZ8wXEhe+dfUtI7/iEsklS0EH8jNn9Y8LDfUTdSb72m5g2opH81hxzB e9jvgl6fdMGLGo87fDbY+mm0/BEfnpRNsLy1cPmuNp3kVWHn/HzKWswZC31oVGU/Gdfj S41xL+hwFTWz/vdAABO+Ypp8S6S8N9MQA5ZHFBjl+1LYRTp86ONCKD/7GsyUQs91aqX5 OPkeuesPW8FQuan0SNTvxsgp4bV4I6sDixj+hIcXdCiYYGH69XOycpEK/Y4gIvtWSXwi FNZRvCpf1eLQ9Syxb9asQfDc9ZvrArR1VjEEiTubMklqzuQRbMxuOB+Cx4ZFv2nExTWn HzWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=94iHnb+/irUo9Hib68Z88vXnda7LIvHLlcB+6a3kJg0=; b=YU2xcIw6SpJV3xARmo/3eJPyHf075CZSOF5v2bneX2n3pHOWBVGtINs5S9QRD6EapJ 4UDALrt2cbeheUh8lfpq1ILdan9nkjQkrW1R4nNjSgh8mDfJJVcjFqLKlaY7ZzyMujSh srkSYQ/5s0AZQUHyuSRO6KSbwE08efZt+92iVRehuoJeuIASbE+TUr67T+g3TAVIv8hL V+R3eaKgEwJbMvXuDv+7l5WcFHgp7Ebx2WCGGHVcYjS2/NXd1Ou480Dg2hojro2r0Tz/ Oupem1K4lMkDcaRGrrbBNHpLfcFwTYIyF7moFvlIWUzHeqmjlOukr5kZLnacQLO/CHcy PHyg== X-Gm-Message-State: AOAM5335jk4zQsB0qL9JRuLNgi7YWvXrmor5jetpojh/R/5W6Mrdu84+ ic7hI+mzP8z37PKdXQX610wOeU906wkBivOj4Og= X-Received: by 2002:a17:906:1919:: with SMTP id a25mr1670121eje.161.1628122402854; Wed, 04 Aug 2021 17:13:22 -0700 (PDT) MIME-Version: 1.0 References: <20210731063938.1391602-1-yuzhao@google.com> <20210731063938.1391602-3-yuzhao@google.com> In-Reply-To: <20210731063938.1391602-3-yuzhao@google.com> From: Yang Shi Date: Wed, 4 Aug 2021 17:13:11 -0700 Message-ID: Subject: Re: [PATCH 2/3] mm: free zapped tail pages when splitting isolated thp To: Yu Zhao Cc: Linux MM , Andrew Morton , Hugh Dickins , "Kirill A . Shutemov" , Matthew Wilcox , Vlastimil Babka , Zi Yan , Linux Kernel Mailing List , Shuang Zhai Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 30, 2021 at 11:39 PM Yu Zhao wrote: > > If a tail page has only two references left, one inherited from the > isolation of its head and the other from lru_add_page_tail() which we > are about to drop, it means this tail page was concurrently zapped. > Then we can safely free it and save page reclaim or migration the > trouble of trying it. > > Signed-off-by: Yu Zhao > Tested-by: Shuang Zhai > --- > include/linux/vm_event_item.h | 1 + > mm/huge_memory.c | 28 ++++++++++++++++++++++++++++ > mm/vmstat.c | 1 + > 3 files changed, 30 insertions(+) > > diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h > index ae0dd1948c2b..829eeac84094 100644 > --- a/include/linux/vm_event_item.h > +++ b/include/linux/vm_event_item.h > @@ -99,6 +99,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, > #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD > THP_SPLIT_PUD, > #endif > + THP_SPLIT_FREE, > THP_ZERO_PAGE_ALLOC, > THP_ZERO_PAGE_ALLOC_FAILED, > THP_SWPOUT, > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index d8b655856e79..5120478bca41 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -2432,6 +2432,8 @@ static void __split_huge_page(struct page *page, struct list_head *list, > struct address_space *swap_cache = NULL; > unsigned long offset = 0; > unsigned int nr = thp_nr_pages(head); > + LIST_HEAD(pages_to_free); > + int nr_pages_to_free = 0; > int i; > > VM_BUG_ON_PAGE(list && PageLRU(head), head); > @@ -2506,6 +2508,25 @@ static void __split_huge_page(struct page *page, struct list_head *list, > continue; > unlock_page(subpage); > > + /* > + * If a tail page has only two references left, one inherited > + * from the isolation of its head and the other from > + * lru_add_page_tail() which we are about to drop, it means this > + * tail page was concurrently zapped. Then we can safely free it > + * and save page reclaim or migration the trouble of trying it. > + */ > + if (list && page_ref_freeze(subpage, 2)) { > + VM_BUG_ON_PAGE(PageLRU(subpage), subpage); > + VM_BUG_ON_PAGE(PageCompound(subpage), subpage); > + VM_BUG_ON_PAGE(page_mapped(subpage), subpage); > + > + ClearPageActive(subpage); > + ClearPageUnevictable(subpage); > + list_move(&subpage->lru, &pages_to_free); > + nr_pages_to_free++; > + continue; > + } Yes, such page could be freed instead of swapping out. But I'm wondering if we could have some simpler implementation. Since such pages will be re-added to page list, so we should be able to check their refcount in shrink_page_list(). If the refcount is 1, the refcount inc'ed by lru_add_page_tail() has been put by later put_page(), we know it is freed under us since the only refcount comes from isolation, we could just jump to "keep" (the label in shrink_page_list()), then such page will be freed later by shrink_inactive_list(). For MADV_PAGEOUT, I think we could add some logic to handle such page after shrink_page_list(), just like what shrink_inactive_list() does. Migration already handles refcount == 1 page, so should not need any change. Is this idea feasible? > + > /* > * Subpages may be freed if there wasn't any mapping > * like if add_to_swap() is running on a lru page that > @@ -2515,6 +2536,13 @@ static void __split_huge_page(struct page *page, struct list_head *list, > */ > put_page(subpage); > } > + > + if (!nr_pages_to_free) > + return; > + > + mem_cgroup_uncharge_list(&pages_to_free); > + free_unref_page_list(&pages_to_free); > + count_vm_events(THP_SPLIT_FREE, nr_pages_to_free); > } > > int total_mapcount(struct page *page) > diff --git a/mm/vmstat.c b/mm/vmstat.c > index b0534e068166..f486e5d98d96 100644 > --- a/mm/vmstat.c > +++ b/mm/vmstat.c > @@ -1300,6 +1300,7 @@ const char * const vmstat_text[] = { > #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD > "thp_split_pud", > #endif > + "thp_split_free", > "thp_zero_page_alloc", > "thp_zero_page_alloc_failed", > "thp_swpout", > -- > 2.32.0.554.ge1b32706d8-goog >