Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3990448pxp; Wed, 23 Mar 2022 09:19:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxtvJm1/4wHW5qyU/SXOXk2awO1R7iuKjKaq31xk4jILD1gM85I02i0hF5qxnlHNkSj1w2k X-Received: by 2002:a17:90a:4882:b0:1c5:f4e2:989a with SMTP id b2-20020a17090a488200b001c5f4e2989amr487062pjh.160.1648052367361; Wed, 23 Mar 2022 09:19:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648052367; cv=none; d=google.com; s=arc-20160816; b=IBPlhg91Q6ogVU2uyZ+YK98Pd3cK3SdqTMeHI2bz8Svov3+IiZH7PrZ8qiAb/UtvOJ hwLV4XhoIn9+Ohz3hRsFAbgVb+PcfFBbEm9OFEp7X1sz18rG2v6jUDDBRKP6FtajC0SW IQ7U2te+rq+3h23j1MWpu4Tiaf8ZcOYMXKp3LH5A6llOSonlab6mSeDFKpRMFx+vMI+S jz5zA4uAmAYrHYFDRi9EZcN+j+oEKhHwW0QF6cyzgJOC4OI6HpVRDx9Qd2KyCP4ewrpM M1kyfOTWcaZ+Z7AIatJnDAwqr2cFuwVe/R7Hy1hkdleU8wn/eYQRcY+jbBjBXUWkJmUt Rd1g== 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=GQneE/rU6uYhLlhfDWprcrRBY9/cQlczmjL+8igVejo=; b=YbH//ygtW80Do1mxpiWCtEyrutHdWmERG+dan9tSqofompIQ8zMNf/UzjR3BtmkxGi FM6haliZfpFJjpf1w4pHO2VoBfab2ez8KG86PK+wZfZiMgZXO1NX3KUZtpTJ3IErLXcn ZGIup9BUh71M+aeOqYWkGDRKmikPgRQ+ibZoKWjj+uealYhoJIzZVYo5FjMHkKIubzvj KAMS72zSD8YHfP2GpZ4txqQRiQ6j3R1zbe7ll+cP1K+REce56z+PSgOIt6V3YR+Xbt1H yVQkmUkJ9t81pCUr1O4Z19GGFPE80/tiOF+I6wac1lXSEh8BhSojPZ/p8GmJUSiovKS6 PVzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="gT7R/uCe"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y16-20020aa78050000000b004fa3a8e0089si12769584pfm.320.2022.03.23.09.18.58; Wed, 23 Mar 2022 09:19:27 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="gT7R/uCe"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240797AbiCWB4l (ORCPT + 99 others); Tue, 22 Mar 2022 21:56:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240157AbiCWB4k (ORCPT ); Tue, 22 Mar 2022 21:56:40 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 839A456202 for ; Tue, 22 Mar 2022 18:55:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1648000510; h=from:from: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; bh=GQneE/rU6uYhLlhfDWprcrRBY9/cQlczmjL+8igVejo=; b=gT7R/uCegvH/p5aLMmbZgYko1Tu3Ewg1dz2OOxuXoltL+IgqlR4x89ml9aDa2HRNgYlGx8 1o/JOcrGsPkbQjKHCM4JonHzRVQP6O4DyYtkXQrFObCcOd3QraudDwmugwV6pj7crmPC+L uNFM8CF6+XlUt/szX6aRfysj85uAxjU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-588-37AbWpS6Mxu4ESKL-r-VHw-1; Tue, 22 Mar 2022 21:55:07 -0400 X-MC-Unique: 37AbWpS6Mxu4ESKL-r-VHw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B87BE3C11A0B; Wed, 23 Mar 2022 01:55:06 +0000 (UTC) Received: from [10.22.16.140] (unknown [10.22.16.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6AF3043E90C; Wed, 23 Mar 2022 01:55:06 +0000 (UTC) Message-ID: <2263666d-5eef-b1fe-d5e3-b166a3185263@redhat.com> Date: Tue, 22 Mar 2022 21:55:05 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH-mm v3] mm/list_lru: Optimize memcg_reparent_list_lru_node() Content-Language: en-US To: Muchun Song Cc: Andrew Morton , Linux Memory Management List , LKML , Roman Gushchin References: <20220309144000.1470138-1-longman@redhat.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, 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 3/22/22 21:06, Muchun Song wrote: > On Wed, Mar 9, 2022 at 10:40 PM Waiman Long wrote: >> Since commit 2c80cd57c743 ("mm/list_lru.c: fix list_lru_count_node() >> to be race free"), we are tracking the total number of lru >> entries in a list_lru_node in its nr_items field. In the case of >> memcg_reparent_list_lru_node(), there is nothing to be done if nr_items >> is 0. We don't even need to take the nlru->lock as no new lru entry >> could be added by a racing list_lru_add() to the draining src_idx memcg >> at this point. > Hi Waiman, > > Sorry for the late reply. Quick question: what if there is an inflight > list_lru_add()? How about the following race? > > CPU0: CPU1: > list_lru_add() > spin_lock(&nlru->lock) > l = list_lru_from_kmem(memcg) > memcg_reparent_objcgs(memcg) > memcg_reparent_list_lrus(memcg) > memcg_reparent_list_lru() > memcg_reparent_list_lru_node() > if (!READ_ONCE(nlru->nr_items)) > // Miss reparenting > return > // Assume 0->1 > l->nr_items++ > // Assume 0->1 > nlru->nr_items++ > > IIUC, we use nlru->lock to serialise this scenario. I guess this race is theoretically possible but very unlikely since it means a very long pause between list_lru_from_kmem() and the increment of nr_items. How about the following changes to make sure that this race can't happen? diff --git a/mm/list_lru.c b/mm/list_lru.c index c669d87001a6..c31a0a8ad4e7 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -395,9 +395,10 @@ static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid,         struct list_lru_one *src, *dst;         /* -        * If there is no lru entry in this nlru, we can skip it immediately. +        * If there is no lru entry in this nlru and the nlru->lock is free, +        * we can skip it immediately.          */ -       if (!READ_ONCE(nlru->nr_items)) +       if (!READ_ONCE(nlru->nr_items) && !spin_is_locked(&nlru->lock))                 return; Cheers, Longman