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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 095C7EB64D7 for ; Fri, 23 Jun 2023 13:13:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DF498D0002; Fri, 23 Jun 2023 09:13:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 58F228D0001; Fri, 23 Jun 2023 09:13:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 457FC8D0002; Fri, 23 Jun 2023 09:13:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 376DA8D0001 for ; Fri, 23 Jun 2023 09:13:01 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 03667C01D8 for ; Fri, 23 Jun 2023 13:13:00 +0000 (UTC) X-FDA: 80934052962.23.446BC60 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by imf07.hostedemail.com (Postfix) with ESMTP id 8AD1F400B6 for ; Fri, 23 Jun 2023 13:11:09 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=c4Lc90e3; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf07.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687525870; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/7x6DiXSOkypZgSRa+BISNq5WosZP8Hu0i35XNPtJQQ=; b=mOsZGW9uJVXN707Ajy7rs27mZ7iGEuu6L191Hkpub6nPyP3xLFdkNs7/tb+Kep48UHlYjp YjpWndZ9GLjuS3vu0lFHOtXUXb1r/8yySlMLY6LZaMb8ZXXafm3sQGU30ICqH781QQ3n05 c1miRVGs4SqncVleKOq9MbXwk9XgO6E= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=c4Lc90e3; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf07.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687525870; a=rsa-sha256; cv=none; b=WUh5DZqFstkXzZdaf03/o4OXAqaqdHb45zYXAjNm4BNQig+cbA2BeB+A0/9UxYwdSGqJPz 4QXaZoikE/ZyTh0ljD4Xtnz74WsW+eQ2xaYInG1tDrJdDxFj3VowVZuJU/0u4Syog9OCjL hg5Gh8GV75VE1NoL8LKluopzvyA28xk= Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-25eb58f4e70so126188a91.0 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=cnPLKrHaknQLfXaenCl6FTuCqLbsbfkJurkKP4hqC9Rr4rLCzlQ5S258LDN7L44rbp nKR7JLTXcQWHTo9eU3qV1hVtmg7xSoGUqYDOfSM32a115XhT9TEauzVGvkvfpJTo+sJ5 uC75sLsdF4s0F57lH5Ht+YX/pgAb0ot4yUBc6M1M+N3kLgkMQDPcQm0Dat3eCrONXfHw vl+K7H7295aXYI/STx76L1X0fpRPGtG53ohKjXHtUSZCdWcjpJpjzn6N7JQyCJpAq8/g QFgbAdoC0PTBNesccCQA029eFpFV4GOKx6bJ+da9Sh+jyZeF7GFv2mlCcQu6JkOJ2HAT BHMA== X-Gm-Message-State: AC+VfDxIQqVYODKIiKkQAPMbgjtOepmo5trsQooalkDNZUTOPlQeoFbQ VUWfJuLibxt99hOCQnJwsaZApg== X-Google-Smtp-Source: ACHHUZ64Jh43TfSAOTNgwrqo87qYAZfwOnz5bgoh5xCGMYf1cG7+iQJqF84ndnWAmMn6TwTiYqZEBw== 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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 8AD1F400B6 X-Stat-Signature: eeqmxztgtr6i77yxxer34zazrchjg4ku X-HE-Tag: 1687525869-479321 X-HE-Meta: U2FsdGVkX18qTNeYwEe035wxPqWSNdSstOvVCUSaPf/MJB/huU9ZBUog2ZY3DFotQ3yE09MVT6w8n9CLWlx+0z3VjUUM4jagfsEGcfWYRFjG3ybrY5oCALgAy72/nyShpoSB9UHOq2hxwIg8YJrWbm2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Hd8PNqL/uC9Selh8Wt/81xX+1G91xy6bYPtaXjHpzfGGJD71QzzBLe0JxzLvFswtMWbGzXAF3gpbJeSNzOS8kVacwChmjbeGjNhhanKAxWL97vqqVXtFaPxOW72zfwiG8mS3aCrXA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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.