Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp67339pxb; Tue, 5 Apr 2022 00:15:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJlSf8Tw2FiPgqkX5XkZfIGm99D2Ccxmk3ODeaHQEGMkD70YTKiU9uk+tiP7YtN8lOErNb X-Received: by 2002:a17:90a:430d:b0:1bc:f340:8096 with SMTP id q13-20020a17090a430d00b001bcf3408096mr2383959pjg.93.1649142918339; Tue, 05 Apr 2022 00:15:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649142918; cv=none; d=google.com; s=arc-20160816; b=s2/0Pi1HZnXTYQ4Jzs282wxbXxMZqEXz2j48kYN/B5aCCMaeQ5ZjkKGQrdpSUgPVO4 DoE4l0O1OCkz3ZS8j5AishLZLU3OD2CyDA2J7swwZsl8A0BAR5EyLGtn/8Xjrknzsqay PGam+duLhNDNszmkYxdtwLE8CZ7dAL4HnHRxEUFa8Z54gvQh/0HTz+j2Kji2QIjUepXd So3CFWQdnbaQIJ4dDXnWcOdXhmess1uF8SlXk+FjFJAxX8zE5KuwYEt3PcV8cyuiy6Fv W7Mrhid6arXjxWfbmHKstpZbGxgYzdbr2FSdaUctSVRjEFqJ9BgbLUPtToxS4qvV4+T6 SIvA== 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; bh=/QFKu+8iDYXzD2S59SJGtCM/zLkV8LQizrbN9+1y8pU=; b=Uwy8z4mRWvZpi9ts5/wU1A+BcB5L5GXIiw7LcrXbFzOLGWNRlHVzR2r9bT9vepN/f1 I3rdQ8W/W82dsQcID5J74hECkL4Q/MvnopZ9yzcSpcqTztGlpP3U6UzMT08m5fH/6/EY wDmu50dj53HS4mR032qQxQUYbar7XHWLIED9YxC5OqBzL+L42hC4pGb7HyV9srDjKVgp D7EqCJTZVN88r9evepeb5du4FpHfUX9Ywaqky+fCvMbq+Ml3+9+0vL6bIFXSg6cwVPKc dHYJReclyD0OvX0jzsSyNOooeawosjqioLVNAJQsHhsSztrc/d1kNDYMNhXCMz7Sm6tB HokA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q37-20020a635065000000b00398fe641fd3si8428180pgl.705.2022.04.05.00.15.05; Tue, 05 Apr 2022 00:15:18 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229684AbiDEFTU (ORCPT + 99 others); Tue, 5 Apr 2022 01:19:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229516AbiDEFTP (ORCPT ); Tue, 5 Apr 2022 01:19:15 -0400 Received: from mail105.syd.optusnet.com.au (mail105.syd.optusnet.com.au [211.29.132.249]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8DAB294 for ; Mon, 4 Apr 2022 22:17:16 -0700 (PDT) Received: from dread.disaster.area (pa49-180-43-123.pa.nsw.optusnet.com.au [49.180.43.123]) by mail105.syd.optusnet.com.au (Postfix) with ESMTPS id D18F310E5639; Tue, 5 Apr 2022 15:17:12 +1000 (AEST) Received: from dave by dread.disaster.area with local (Exim 4.92.3) (envelope-from ) id 1nbbZ0-00DwF0-DI; Tue, 05 Apr 2022 15:17:10 +1000 Date: Tue, 5 Apr 2022 15:17:10 +1000 From: Dave Chinner To: Roman Gushchin Cc: Hillf Danton , MM , Matthew Wilcox , Mel Gorman , Stephen Brennan , Yu Zhao , David Hildenbrand , LKML Subject: Re: [RFC] mm/vmscan: add periodic slab shrinker Message-ID: <20220405051710.GW1609613@dread.disaster.area> References: <20220402072103.5140-1-hdanton@sina.com> <20220403005618.5263-1-hdanton@sina.com> <20220404010948.GV1609613@dread.disaster.area> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.4 cv=VuxAv86n c=1 sm=1 tr=0 ts=624bd0db a=MV6E7+DvwtTitA3W+3A2Lw==:117 a=MV6E7+DvwtTitA3W+3A2Lw==:17 a=kj9zAlcOel0A:10 a=z0gMJWrwH1QA:10 a=4ao_m9BdAAAA:8 a=7-415B0cAAAA:8 a=RB2aauR-4WVBKZjbOhwA:9 a=CjuIK1q_8ugA:10 a=MsgbvsnFcLE8lbaOJhbn:22 a=biEYGPWJfzWAr4FL6Ov7:22 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE 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, Apr 04, 2022 at 12:08:25PM -0700, Roman Gushchin wrote: > On Mon, Apr 04, 2022 at 11:09:48AM +1000, Dave Chinner wrote: > > i.e. the amount of work that shrinkers need to do in a periodic scan > > is largerly determined by the rate of shrinkable cache memory usage > > growth rather than memory reclaim priority as it is now. Hence there > > needs to be different high level "shrinker needs to do X amount of > > work" calculation for periodic reclaim than there is now. > > > > e.g. we calculate a rolling average of the size of the cache and a > > rate of change over a series of polling operations (i.e. calling > > ->scan_count) and then when sustained growth is detected we start > > trying to shrink the cache to limit the rate of growth of the cache. > > > > If the cache keeps growing, then it's objects are being repeatedly > > referenced and it *should* keep growing. If it's one-off objects > > that are causing the growth of the cache and so objects are being > > reclaimed by the shrinker, then matching the periodic shrink scan to > > the growth rate will substantially reduce the rate of growth of that > > cache. > > A clever idea! > > It seems like we need to add some stats to the list_lru API or maybe to > the shrinker API (and let list_lru to use it). > E.g. total/scanned/reclaimed, maybe with a time decay > > I'm also thinking about: > 1) adding a sysfs/debugfs interface to expose shrinkers current size and > statistics, with an ability to call into the reclaim manually. I've thought about it, too, and can see where it could be useful. However, when I consider the list_lru memcg integration, I suspect it becomes a "can't see the forest for the trees" problem. We're going to end up with millions of sysfs objects with no obvious way to navigate, iterate or search them if we just take the naive "sysfs object + stats per list_lru instance" approach. Also, if you look at commit 6a6b7b77cc0f mm: ("list_lru: transpose the array of per-node per-memcg lru lists") that went into 5.18-rc1, you'll get an idea of the amount of memory overhead just tracking the list_lru x memcg infrastructure consumes at scale: I had done a easy test to show the optimization. I create 10k memory cgroups and mount 10k filesystems in the systems. We use free command to show how many memory does the systems comsumes after this operation (There are 2 numa nodes in the system). +-----------------------+------------------------+ | condition | memory consumption | +-----------------------+------------------------+ | without this patchset | 24464 MB | +-----------------------+------------------------+ | after patch 1 | 21957 MB | <--------+ +-----------------------+------------------------+ | | after patch 10 | 6895 MB | | +-----------------------+------------------------+ | | after patch 12 | 4367 MB | | +-----------------------+------------------------+ | | The more the number of nodes, the more obvious the effect---+ If we now add sysfs objects and stats arrays to each of the list_lrus that we initiate even now on 5.18-rc1, we're going to massively blow out the memory footprint again. So, nice idea, but I'm not sure we can make it useful and not consume huge amounts of memory.... What might be more useful is a way of getting the kernel to tell us what the, say, 20 biggest slab caches are in the system and then provide a way to selectively shrink them. We generally don't really care about tiny caches, just the ones consuming all the memory. This isn't my idea - Kent has been looking at this because of how useless OOM kill output is for debugging slab cache based OOM triggers. See this branch for an example: https://evilpiepirate.org/git/bcachefs.git/log/?h=shrinker_to_text Even a sysfs entry that you echo a number into and it returns the "top N" largest LRU lists. echo -1 into it and it returns every single one if you want all the information. The whole "one sysfs file, one value" architecture falls completely apart when we might have to indexing *millions* of internal structures with many parameters per structure... > 2) formalizing a reference bit/counter API on the shrinkers level, so that > shrinker users can explicitly mark objects (re)-"activation". Not 100% certain what you are refering to here - something to do with active object rotation? Or an active/inactive list split with period demotion like we have for the page LRUs? Or workingset refault detection? Can you explain in more detail? > 3) _maybe_ we need to change the shrinkers API from a number of objects to > bytes, so that releasing a small number of large objects can compete with > a releasing on many small objects. But I'm not sure. I think I suggested something similar a long time ago. We have shrinkers that track things other than slab objects. e.g. IIRC the ttm graphics allocator shrinker tracks sets of pages and frees pages, not slab objects. The XFS buffer cache tracks variable sized objects, from 512 bytes to 64KB in length, so the amount of memory it frees is variable even if the number of handles it scans and reclaims is fixed and consistent. Other subsystems have different "non object" shrinker needs as well. The long and short of it is that two shrinkers might have the same object count, but one might free 10x the amount of memory than the other for the same amount of shrinking work. Being able to focus reclaim work on caches that can free a lot of memory much more quickly would be a great idea. It also means that a shrinker that scans a fragmented slab can keep going until a set number of slab pages have been freed, rather than a set number of slab objects. We can push reclaim of fragmented slab caches much harder when necessary if we are reclaiming by freed byte counts... So, yeah, byte-count based reclaim definitely has merit compared to what we currently do. It's more generic and more flexible... > 4) also some shrinkers (e.g. shadow nodes) lying about the total size of > objects and I have an uneasy feeling about this approach. Yeah, never been a fan about use cases like that, but IIRC it was really the only option at the time to only trigger reclaim when the kernel was absolutely desperate because we really need the working set shadow nodes to do their job well when memory is very low... Cheers, Dave. -- Dave Chinner david@fromorbit.com