Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp32831pxb; Wed, 30 Mar 2022 22:07:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyNxmBldtWPqSjj34CYh9uhRkpivQQVeH0nxmv3PBJV0kt0pKMOHKFEy6wlMnFGbg8R75DH X-Received: by 2002:aa7:9019:0:b0:4fa:7532:9551 with SMTP id m25-20020aa79019000000b004fa75329551mr3516760pfo.26.1648703268531; Wed, 30 Mar 2022 22:07:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648703268; cv=none; d=google.com; s=arc-20160816; b=sJPAD/J3vlD2fYqYZAJ/2TvRZA30af6JlPZ6QXkiuMiCg0pdOpEEg9S7SAsJg9Eit3 vh8JCzo40v4FOYS7tPTz4Lm0n2p4VyZMOdl4bR0Bfcd4WR2YjeUteHIIBDjI4H6cWzvo b43ig6kVhLLEOHimmX1WXq5XPhJmiCatAQWoB55h1j1qMhDuUmMjBrcFSgiIzxF27bLk jxcKf7ZNC+QTDJeTzGNU0x1k+Ni2fIXwZgGsjrcnU6s96oE2MN4E+q0WwA/A+fxwf1uD OI/fe/y0+r2b2OKjErk3ryZByd9dmKVi4pgTcsUu6IQT82CunI36eSqDv5NJSmh+s1TI H2tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id:cc:date:in-reply-to :from:subject:mime-version:content-transfer-encoding:dkim-signature; bh=GMH0mPyygzee1kjfgYk2myFSoN2Dy3HPVOSEklSnjvE=; b=y/f9CSYg4ZFVFOxk8ITeAJm++uJMZiyijL7Av4te7XxIpl7KPGgdE545M6PY8D1bju o2AVDYws9hKA7UeTiH5HZ8A/93S5gO8BOvPgwHOJJX/2TR+nh3+eNN8lJbi+AGwi6eCT dOct5/Le8PrbTrcreTVFDQog7UiZnJf+Z0W0BlMa2ml47JDTMirnKtzT2iZHGFCdqckA ef0e3LNMNDoyzNnU3YjbqWpV1gfJhsT/h18s1ujAR5sjbD8x3uXbaumRUGSVdzl7Rzvp FL1VY6Augv/26U6O81dYzuzjksOQtvW6zb5GyctiAPOIk4Xi40mIQv/M2L6+eKfBI63F uwSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="wU49G/J7"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id t1-20020a654b81000000b003816043ee19si22905047pgq.14.2022.03.30.22.07.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 22:07:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="wU49G/J7"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A4D49289594; Wed, 30 Mar 2022 20:42:32 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229598AbiCaC6s (ORCPT + 99 others); Wed, 30 Mar 2022 22:58:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229877AbiCaC6p (ORCPT ); Wed, 30 Mar 2022 22:58:45 -0400 Received: from out0.migadu.com (out0.migadu.com [IPv6:2001:41d0:2:267::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A25E31255B4 for ; Wed, 30 Mar 2022 19:56:52 -0700 (PDT) Content-Type: text/plain; charset=utf-8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1648694928; 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=GMH0mPyygzee1kjfgYk2myFSoN2Dy3HPVOSEklSnjvE=; b=wU49G/J70KRunQNmNOTSvdD4e0DvXrXgr1MKUM3Va7O5PWrvx1QxUMswDNikDQyxRpBhH8 Yr9ISTWnKaHFfTqAG2l8sJWb5B9f82wBlqPVdPq6ySt5TSgnDotA9Int2/OGuQGrwc34gP l28o3MkNE3sX2oydsHYVJ56arAYsIjM= Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [PATCH v2] mm/list_lru: Fix possible race in memcg_reparent_list_lru_node() X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin In-Reply-To: <20220330191440.1cc1b2de2b849d1ba93d2ba7@linux-foundation.org> Date: Wed, 30 Mar 2022 19:48:45 -0700 Cc: Waiman Long , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song Message-Id: <89B53D3A-FCC5-4107-8D49-81D5B9AE5172@linux.dev> References: <20220330191440.1cc1b2de2b849d1ba93d2ba7@linux-foundation.org> To: Andrew Morton X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Mar 30, 2022, at 7:14 PM, Andrew Morton wro= te: >=20 > =EF=BB=BFOn Wed, 30 Mar 2022 13:26:46 -0400 Waiman Long wrote: >=20 >> Muchun Song found out there could be a race between list_lru_add() >> and memcg_reparent_list_lru_node() causing the later function to miss >> reparenting of a lru entry as shown below: >>=20 >> CPU0: CPU1: >> list_lru_add() >> spin_lock(&nlru->lock) >> l =3D 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++ >>=20 >> Though it is not likely that a list_lru_node that has 0 item suddenly >> has a newly added lru entry at the end of its life. The race is still >> theoretically possible. >>=20 >> With the lock/unlock pair used within the percpu_ref_kill() which is >> the last function call of memcg_reparent_objcgs(), any read issued >> in memcg_reparent_list_lru_node() will not be reordered before the >> reparenting of objcgs. >>=20 >> Adding a !spin_is_locked()/smp_rmb()/!READ_ONCE(nlru->nr_items) check >> to ensure that either the reading of nr_items is valid or the racing >> list_lru_add() will see the reparented objcg. >>=20 >> ... >>=20 >> --- a/mm/list_lru.c >> +++ b/mm/list_lru.c >> @@ -395,10 +395,33 @@ static void memcg_reparent_list_lru_node(struct lis= t_lru *lru, int nid, >> struct list_lru_one *src, *dst; >>=20 >> /* >> - * If there is no lru entry in this nlru, we can skip it immediately= . >> + * With the lock/unlock pair used within the percpu_ref_kill() >> + * which is the last function call of memcg_reparent_objcgs(), any >> + * read issued here will not be reordered before the reparenting >> + * of objcgs. >> + * >> + * Assuming a racing list_lru_add(): >> + * list_lru_add() >> + * <- memcg_reparent_list_lru_node() >> + * spin_lock(&nlru->lock) >> + * l =3D list_lru_from_kmem(memcg) >> + * nlru->nr_items++ >> + * spin_unlock(&nlru->lock) >> + * <- memcg_reparent_list_lru_node() >> + * >> + * The !spin_is_locked(&nlru->lock) check is true means it is >> + * either before the spin_lock() or after the spin_unlock(). In the >> + * former case, list_lru_add() will see the reparented objcg and so >> + * won't touch the lru to be reparented. In the later case, it will >> + * see the updated nr_items. So we can use the optimization that if >> + * there is no lru entry in this nlru, skip it immediately. >> */ >> - if (!READ_ONCE(nlru->nr_items)) >> - return; >> + if (!spin_is_locked(&nlru->lock)) { >=20 > ick. >=20 >> + /* nr_items read must be ordered after nlru->lock */ >> + smp_rmb(); >> + if (!READ_ONCE(nlru->nr_items)) >> + return; >> + } >=20 > include/linux/spinlock_up.h has >=20 > #define arch_spin_is_locked(lock) ((void)(lock), 0) >=20 > so this `if' will always be true on CONFIG_SMP=3Dn. Will the kernel > still work? I guess yes, because this race is not possible on a !smp machine. >=20 > At the very least let's have changelogging and commenting explaining > that we've actually thought about this. >=20 > Preferably, can we fix this hole properly and avoid this hack? There is > a reason for this: >=20 > hp2:/usr/src/25> grep spin_is_locked mm/*.c > hp2:/usr/src/25>=20 But honestly, I=E2=80=99d drop the original optimization together with the f= ix, if only there is no _real world_ data on the problem and the improvement= . It seems like it has started as a nice simple improvement, but the race ma= kes it complex and probably not worth the added complexity and fragility. Thanks!=