Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp12438201rwd; Fri, 23 Jun 2023 06:13:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6tc8AFuS/l4hRu/yhQCx7p3+pq7abuTB8I2aCqZSE4nlOBm3hWeoZEWIkoSF2wApclLF7M X-Received: by 2002:a05:6e02:804:b0:342:7bea:5f3c with SMTP id u4-20020a056e02080400b003427bea5f3cmr13770550ilm.18.1687526026927; Fri, 23 Jun 2023 06:13:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687526026; cv=none; d=google.com; s=arc-20160816; b=YvMaDyDuZpixwxL3lYPTCdXGCUOjym5W4YAl/PjZktZIlyGAX4dxf7hB9MVFGkqO3P rXU2VdfxeYkku/dmCfmqE+59KimHYTreBoyLLqqJCcKxbrR7SYtXnsDpO+FQmFcVr9bx bLodRIiQGO89vI5gk638DE3fyFm7uTQ2XnlHC3IviVwxgSVNPgrrYe8y+bN7q+yd/oc2 kFDTod98OTuuwYWKlix6XzHKfrtmDahUUYPgC7/nMoN6eljOkf41S1KFqL2QYd/cpFxm 9AyL5sKLa+6+PRGwX8hAguejya6uu9Esz5ogNiJ+Ty04Bx7VSsZsKkgZ7wkOa8rRANPu 0DXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=/7x6DiXSOkypZgSRa+BISNq5WosZP8Hu0i35XNPtJQQ=; b=h73xFAbPZR2NLm4ZGFIVS8b6dvv/ei18BsejkQNUs3Exyakwc/m5gawy13MN43DXL2 WqEflgiCoaiYGUrfPYEBr37XaivapgGnK/ezySSxrlxFlp8HagEHbzFGfQ1o1OM/PA6+ ddrvs6lvMakd9ptLrtgxaBZBIbUE8MyOYtbNhp9neoLjomZhFUpeUyGyMEFfBjDddn0K aAj+JQha4foXlwK6IP3zqZKeGK29ehsdhgLE93fgT8rRSzGuAhLHnQhPZfuM99aouPnb rby8drOz4d4+u7XaMT2FWWVl0v3CaRljlzWnk5VUaS28tQ9QqzGmjQz3PJb3v9YhbJiu wIrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=c4Lc90e3; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r20-20020a6560d4000000b005573c4dd590si3405709pgv.457.2023.06.23.06.13.28; Fri, 23 Jun 2023 06:13:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-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=@bytedance.com header.s=google header.b=c4Lc90e3; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231286AbjFWNLM (ORCPT + 99 others); Fri, 23 Jun 2023 09:11:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230060AbjFWNLL (ORCPT ); Fri, 23 Jun 2023 09:11:11 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 701BC2688 for ; Fri, 23 Jun 2023 06:11:08 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-25edb2433b8so123013a91.1 for ; Fri, 23 Jun 2023 06:11:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1687525868; x=1690117868; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=/7x6DiXSOkypZgSRa+BISNq5WosZP8Hu0i35XNPtJQQ=; b=c4Lc90e3q/J/WKx3HQ+z7NN2l1qZRKlelFYboQ0nNA7dtkVHpa39U+pQO2zrVLopHR tQb8FdbybA7ZY5EJuz7Bj8lc58TkEH/aUZXL9aZwXUN962/6l/NewngXKCisO7gAhI9A 6y1k+r3pXZ/9NOPCcEIwbQmxJcwu17xkFr7LC37JSENPSJtDN4GEFRuoB+dm8iAKWIp9 DFutxWv0gIG1WvIBQtrHwdJ+U3wemUIH6AIx7XIJDjUmh1K9SXXWWzWNhP9Z793nQfgc /81DZq+4PM5+K/vGcsN5FzVIevuwRHMFrPp0/v2gAPq8CuK9bUHMh/q7eJ4T5hUbjVvx g0uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687525868; x=1690117868; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/7x6DiXSOkypZgSRa+BISNq5WosZP8Hu0i35XNPtJQQ=; b=QJfHKZWfWM+l4lkCuXWAHrIon0eCq+odn6Suf9U0D5GD1Tsuqopt4KDuCJbncI7sbg 6x+pEhiW7/n55WOXhGmpIC9ljJzPf9lutxkYIgh0gmpClMuO89b6ENdQUOIeuz1ZFkNj 7V1o4/FfDm7tyG0vsxwY1RO0dvxv8GOx6NurtoT4vkI1GwSwfB6kO1GZU3k7FklvlsIg Qfkrmj5OYCju7agj/bB4TMKjHut8BA7zZV3LmFn2ptbLuY2Yy1TLkxVP4WiKn/rF9bS+ x6wFHGN6s2LG21Sqspty185PiTg6P2eqOwT0K3UBMgJJKYm0ZgGoIPQw/DIdAhGXnpOZ Hekg== X-Gm-Message-State: AC+VfDyBU8yDuDa+QJ+80V6P4stl25J26/cqlHl6MzYJU0V+JifRAUIf D3Vvsl1dpMH3g+xwCHp0vLGnxQ== X-Received: by 2002:a17:90b:1bc2:b0:258:9621:913f with SMTP id oa2-20020a17090b1bc200b002589621913fmr25470832pjb.3.1687525867887; Fri, 23 Jun 2023 06:11:07 -0700 (PDT) Received: from [10.4.168.167] ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id 1-20020a17090a198100b0024e49b53c24sm1568646pji.10.2023.06.23.06.11.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 23 Jun 2023 06:11:07 -0700 (PDT) Message-ID: Date: Fri, 23 Jun 2023 21:10:57 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [PATCH 24/29] mm: vmscan: make global slab shrink lockless Content-Language: en-US To: Dave Chinner , Vlastimil Babka , paulmck@kernel.org Cc: akpm@linux-foundation.org, tkhai@ya.ru, roman.gushchin@linux.dev, djwong@kernel.org, brauner@kernel.org, tytso@mit.edu, linux-kernel@vger.kernel.org, linux-mm@kvack.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-arm-msm@vger.kernel.org, dm-devel@redhat.com, linux-raid@vger.kernel.org, linux-bcache@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-nfs@vger.kernel.org, linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org References: <20230622085335.77010-1-zhengqi.arch@bytedance.com> <20230622085335.77010-25-zhengqi.arch@bytedance.com> From: Qi Zheng In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable 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-nfs@vger.kernel.org On 2023/6/23 14:29, Dave Chinner wrote: > On Thu, Jun 22, 2023 at 05:12:02PM +0200, Vlastimil Babka wrote: >> On 6/22/23 10:53, Qi Zheng wrote: >>> @@ -1067,33 +1068,27 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid, >>> if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) >>> return shrink_slab_memcg(gfp_mask, nid, memcg, priority); >>> >>> - if (!down_read_trylock(&shrinker_rwsem)) >>> - goto out; >>> - >>> - list_for_each_entry(shrinker, &shrinker_list, list) { >>> + rcu_read_lock(); >>> + list_for_each_entry_rcu(shrinker, &shrinker_list, list) { >>> struct shrink_control sc = { >>> .gfp_mask = gfp_mask, >>> .nid = nid, >>> .memcg = memcg, >>> }; >>> >>> + if (!shrinker_try_get(shrinker)) >>> + continue; >>> + rcu_read_unlock(); >> >> I don't think you can do this unlock? >> >>> + >>> ret = do_shrink_slab(&sc, shrinker, priority); >>> if (ret == SHRINK_EMPTY) >>> ret = 0; >>> freed += ret; >>> - /* >>> - * Bail out if someone want to register a new shrinker to >>> - * prevent the registration from being stalled for long periods >>> - * by parallel ongoing shrinking. >>> - */ >>> - if (rwsem_is_contended(&shrinker_rwsem)) { >>> - freed = freed ? : 1; >>> - break; >>> - } >>> - } >>> >>> - up_read(&shrinker_rwsem); >>> -out: >>> + rcu_read_lock(); >> >> That new rcu_read_lock() won't help AFAIK, the whole >> list_for_each_entry_rcu() needs to be under the single rcu_read_lock() to be >> safe. > > Yeah, that's the pattern we've been taught and the one we can look > at and immediately say "this is safe". > > This is a different pattern, as has been explained bi Qi, and I > think it *might* be safe. > > *However.* > > Right now I don't have time to go through a novel RCU list iteration > pattern it one step at to determine the correctness of the > algorithm. I'm mostly worried about list manipulations that can > occur outside rcu_read_lock() section bleeding into the RCU > critical section because rcu_read_lock() by itself is not a memory > barrier. > > Maybe Paul has seen this pattern often enough he could simply tell > us what conditions it is safe in. But for me to work that out from > first principles? I just don't have the time to do that right now. Hi Paul, can you help to confirm this? > >> IIUC this is why Dave in [4] suggests unifying shrink_slab() with >> shrink_slab_memcg(), as the latter doesn't iterate the list but uses IDR. > > Yes, I suggested the IDR route because radix tree lookups under RCU > with reference counted objects are a known safe pattern that we can > easily confirm is correct or not. Hence I suggested the unification > + IDR route because it makes the life of reviewers so, so much > easier... In fact, I originally planned to try the unification + IDR method you suggested at the beginning. But in the case of CONFIG_MEMCG disabled, the struct mem_cgroup is not even defined, and root_mem_cgroup and shrinker_info will not be allocated. This required more code changes, so I ended up keeping the shrinker_list and implementing the above pattern. If the above pattern is not safe, I will go back to the unification + IDR method. Thanks, Qi > > Cheers, > > Dave.