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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E9E18C4332F for ; Wed, 18 May 2022 01:47:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234004AbiERBrW (ORCPT ); Tue, 17 May 2022 21:47:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233820AbiERBrC (ORCPT ); Tue, 17 May 2022 21:47:02 -0400 Received: from mail-il1-x149.google.com (mail-il1-x149.google.com [IPv6:2607:f8b0:4864:20::149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13DA6546A8 for ; Tue, 17 May 2022 18:47:00 -0700 (PDT) Received: by mail-il1-x149.google.com with SMTP id g11-20020a056e021a2b00b002cf48b48824so424976ile.21 for ; Tue, 17 May 2022 18:47:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc:content-transfer-encoding; bh=z51FPcNwNvxNlVX8smn5vw9si1mnZZRQUKLtiLkXezM=; b=bCWp0sxEaC4g9CBSBbnutqBccAuxHsQs55of/Bt/jQWSINgjftl7w0Qt/PscXsiDqm zo8beAASkyshZHQ/ySdD5c9QF2x4BrjCGYmJvRIWAhiyUIgN1LhHmdXB/deeb15U3Tof tsHB+3K36syq88/p+mLTk0E+rKdS4VUlpmSb7BXaN4Grd3kC8N39Xew3W2+xiAJduUS9 p5s7aLH7cPd6cBs31D0FdGB5VV3317OlZ+lXjPOnmjlO3V6JtdBmJGAEyfE7GlHFS8PA qgIqTztNPP+Hk3oqchsF9QItKApyWLPPMxpDQfIkFOWX9dkx74wWxgvcRLr78juMnh/V 1lEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=z51FPcNwNvxNlVX8smn5vw9si1mnZZRQUKLtiLkXezM=; b=F/LuGmUEdk1I31xzEjvMtsiQrE4/qk0Z9WoexW04RFIAyl0YM1qrwr7PJBXXCpMVUQ nIeOOfwgPzS/lzjUkqRiftORf6PoYO4onFZZDKjlg1esWJC4d9Fcpn5XSrsYQg14dUCZ FNSegnQsODIGHhrEfZO1FtpFOuDKTma3WV+h5hc/Mp+RH+hv6MKCbHhZgJh/L+fXEOOn NEgG7IUenyohSXvM5+6PzbgziAkyJPpy61J8K9AUqHCXk9QqpFIDGrttr6RzruAPq0Aa tRCkUiwVIZXgmp8ccwJgoKoS6LlobqbL8vNHHOZBuqGpnHMF6mMI/cxfuqK/32sxC/Wd Vxqg== X-Gm-Message-State: AOAM531B6ZpWDlebHmBy+s91swAY55M13s6jBXjjQWMh0zxoBA2VsNSR TWAJRcZA9IjGsg0LHgs5F3B+RUNnrP4= X-Google-Smtp-Source: ABdhPJyaU1KModtGurKuZbvm5GQ4iXAgX0NK43+WD6/J/2/U88t/+Dj+unOyz4fkoFhBf5nRjHUp6XY00V0= X-Received: from yuzhao.bld.corp.google.com ([2620:15c:183:200:f7bc:90c9:d86e:4ea8]) (user=yuzhao job=sendgmr) by 2002:a6b:3fc2:0:b0:65a:4236:bb42 with SMTP id m185-20020a6b3fc2000000b0065a4236bb42mr11514573ioa.215.1652838419082; Tue, 17 May 2022 18:46:59 -0700 (PDT) Date: Tue, 17 May 2022 19:46:22 -0600 In-Reply-To: <20220518014632.922072-1-yuzhao@google.com> Message-Id: <20220518014632.922072-4-yuzhao@google.com> Mime-Version: 1.0 References: <20220518014632.922072-1-yuzhao@google.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog Subject: [PATCH v11 03/14] mm/vmscan.c: refactor shrink_node() From: Yu Zhao To: Andrew Morton , linux-mm@kvack.org Cc: Andi Kleen , Aneesh Kumar , Catalin Marinas , Dave Hansen , Hillf Danton , Jens Axboe , Johannes Weiner , Jonathan Corbet , Linus Torvalds , Matthew Wilcox , Mel Gorman , Michael Larabel , Michal Hocko , Mike Rapoport , Peter Zijlstra , Tejun Heo , Vlastimil Babka , Will Deacon , linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, page-reclaim@google.com, Yu Zhao , Barry Song , Miaohe Lin , Brian Geffon , Jan Alexander Steffens , Oleksandr Natalenko , Steven Barrett , Suleiman Souhlal , Daniel Byrne , Donald Carr , "=?UTF-8?q?Holger=20Hoffst=C3=A4tte?=" , Konstantin Kharlamov , Shuang Zhai , Sofia Trinh , Vaibhav Jain Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch refactors shrink_node() to improve readability for the upcoming changes to mm/vmscan.c. Signed-off-by: Yu Zhao Reviewed-by: Barry Song Reviewed-by: Miaohe Lin Acked-by: Brian Geffon Acked-by: Jan Alexander Steffens (heftig) Acked-by: Oleksandr Natalenko Acked-by: Steven Barrett Acked-by: Suleiman Souhlal Tested-by: Daniel Byrne Tested-by: Donald Carr Tested-by: Holger Hoffst=C3=A4tte Tested-by: Konstantin Kharlamov Tested-by: Shuang Zhai Tested-by: Sofia Trinh Tested-by: Vaibhav Jain --- mm/vmscan.c | 198 +++++++++++++++++++++++++++------------------------- 1 file changed, 104 insertions(+), 94 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 1678802e03e7..2232cb55af41 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2644,6 +2644,109 @@ enum scan_balance { SCAN_FILE, }; =20 +static void prepare_scan_count(pg_data_t *pgdat, struct scan_control *sc) +{ + unsigned long file; + struct lruvec *target_lruvec; + + target_lruvec =3D mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); + + /* + * Flush the memory cgroup stats, so that we read accurate per-memcg + * lruvec stats for heuristics. + */ + mem_cgroup_flush_stats(); + + /* + * Determine the scan balance between anon and file LRUs. + */ + spin_lock_irq(&target_lruvec->lru_lock); + sc->anon_cost =3D target_lruvec->anon_cost; + sc->file_cost =3D target_lruvec->file_cost; + spin_unlock_irq(&target_lruvec->lru_lock); + + /* + * Target desirable inactive:active list ratios for the anon + * and file LRU lists. + */ + if (!sc->force_deactivate) { + unsigned long refaults; + + refaults =3D lruvec_page_state(target_lruvec, + WORKINGSET_ACTIVATE_ANON); + if (refaults !=3D target_lruvec->refaults[0] || + inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) + sc->may_deactivate |=3D DEACTIVATE_ANON; + else + sc->may_deactivate &=3D ~DEACTIVATE_ANON; + + /* + * When refaults are being observed, it means a new + * workingset is being established. Deactivate to get + * rid of any stale active pages quickly. + */ + refaults =3D lruvec_page_state(target_lruvec, + WORKINGSET_ACTIVATE_FILE); + if (refaults !=3D target_lruvec->refaults[1] || + inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) + sc->may_deactivate |=3D DEACTIVATE_FILE; + else + sc->may_deactivate &=3D ~DEACTIVATE_FILE; + } else + sc->may_deactivate =3D DEACTIVATE_ANON | DEACTIVATE_FILE; + + /* + * If we have plenty of inactive file pages that aren't + * thrashing, try to reclaim those first before touching + * anonymous pages. + */ + file =3D lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); + if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) + sc->cache_trim_mode =3D 1; + else + sc->cache_trim_mode =3D 0; + + /* + * Prevent the reclaimer from falling into the cache trap: as + * cache pages start out inactive, every cache fault will tip + * the scan balance towards the file LRU. And as the file LRU + * shrinks, so does the window for rotation from references. + * This means we have a runaway feedback loop where a tiny + * thrashing file LRU becomes infinitely more attractive than + * anon pages. Try to detect this based on file LRU size. + */ + if (!cgroup_reclaim(sc)) { + unsigned long total_high_wmark =3D 0; + unsigned long free, anon; + int z; + + free =3D sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); + file =3D node_page_state(pgdat, NR_ACTIVE_FILE) + + node_page_state(pgdat, NR_INACTIVE_FILE); + + for (z =3D 0; z < MAX_NR_ZONES; z++) { + struct zone *zone =3D &pgdat->node_zones[z]; + + if (!managed_zone(zone)) + continue; + + total_high_wmark +=3D high_wmark_pages(zone); + } + + /* + * Consider anon: if that's low too, this isn't a + * runaway file reclaim problem, but rather just + * extreme pressure. Reclaim as per usual then. + */ + anon =3D node_page_state(pgdat, NR_INACTIVE_ANON); + + sc->file_is_tiny =3D + file + free <=3D total_high_wmark && + !(sc->may_deactivate & DEACTIVATE_ANON) && + anon >> sc->priority; + } +} + /* * Determine how aggressively the anon and file LRU lists should be * scanned. The relative value of each set of LRU lists is determined @@ -3114,109 +3217,16 @@ static void shrink_node(pg_data_t *pgdat, struct s= can_control *sc) unsigned long nr_reclaimed, nr_scanned; struct lruvec *target_lruvec; bool reclaimable =3D false; - unsigned long file; =20 target_lruvec =3D mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); =20 again: - /* - * Flush the memory cgroup stats, so that we read accurate per-memcg - * lruvec stats for heuristics. - */ - mem_cgroup_flush_stats(); - memset(&sc->nr, 0, sizeof(sc->nr)); =20 nr_reclaimed =3D sc->nr_reclaimed; nr_scanned =3D sc->nr_scanned; =20 - /* - * Determine the scan balance between anon and file LRUs. - */ - spin_lock_irq(&target_lruvec->lru_lock); - sc->anon_cost =3D target_lruvec->anon_cost; - sc->file_cost =3D target_lruvec->file_cost; - spin_unlock_irq(&target_lruvec->lru_lock); - - /* - * Target desirable inactive:active list ratios for the anon - * and file LRU lists. - */ - if (!sc->force_deactivate) { - unsigned long refaults; - - refaults =3D lruvec_page_state(target_lruvec, - WORKINGSET_ACTIVATE_ANON); - if (refaults !=3D target_lruvec->refaults[0] || - inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) - sc->may_deactivate |=3D DEACTIVATE_ANON; - else - sc->may_deactivate &=3D ~DEACTIVATE_ANON; - - /* - * When refaults are being observed, it means a new - * workingset is being established. Deactivate to get - * rid of any stale active pages quickly. - */ - refaults =3D lruvec_page_state(target_lruvec, - WORKINGSET_ACTIVATE_FILE); - if (refaults !=3D target_lruvec->refaults[1] || - inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) - sc->may_deactivate |=3D DEACTIVATE_FILE; - else - sc->may_deactivate &=3D ~DEACTIVATE_FILE; - } else - sc->may_deactivate =3D DEACTIVATE_ANON | DEACTIVATE_FILE; - - /* - * If we have plenty of inactive file pages that aren't - * thrashing, try to reclaim those first before touching - * anonymous pages. - */ - file =3D lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); - if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) - sc->cache_trim_mode =3D 1; - else - sc->cache_trim_mode =3D 0; - - /* - * Prevent the reclaimer from falling into the cache trap: as - * cache pages start out inactive, every cache fault will tip - * the scan balance towards the file LRU. And as the file LRU - * shrinks, so does the window for rotation from references. - * This means we have a runaway feedback loop where a tiny - * thrashing file LRU becomes infinitely more attractive than - * anon pages. Try to detect this based on file LRU size. - */ - if (!cgroup_reclaim(sc)) { - unsigned long total_high_wmark =3D 0; - unsigned long free, anon; - int z; - - free =3D sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); - file =3D node_page_state(pgdat, NR_ACTIVE_FILE) + - node_page_state(pgdat, NR_INACTIVE_FILE); - - for (z =3D 0; z < MAX_NR_ZONES; z++) { - struct zone *zone =3D &pgdat->node_zones[z]; - if (!managed_zone(zone)) - continue; - - total_high_wmark +=3D high_wmark_pages(zone); - } - - /* - * Consider anon: if that's low too, this isn't a - * runaway file reclaim problem, but rather just - * extreme pressure. Reclaim as per usual then. - */ - anon =3D node_page_state(pgdat, NR_INACTIVE_ANON); - - sc->file_is_tiny =3D - file + free <=3D total_high_wmark && - !(sc->may_deactivate & DEACTIVATE_ANON) && - anon >> sc->priority; - } + prepare_scan_count(pgdat, sc); =20 shrink_node_memcgs(pgdat, sc); =20 --=20 2.36.0.550.gb090851708-goog