From nobody Fri Jun 19 22:36:13 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E08E4C433EF for ; Mon, 28 Mar 2022 00:58:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237179AbiC1A7v (ORCPT ); Sun, 27 Mar 2022 20:59:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233105AbiC1A7t (ORCPT ); Sun, 27 Mar 2022 20:59:49 -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 6554611145 for ; Sun, 27 Mar 2022 17:58:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1648429088; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=t5CzkAYzJdztQErtcdV4oqw1LA76ghZxAVdYYw2i554=; b=JriYPP5TvN0dGsvMjgJcnEqYrDUKQ5eGdC7VT+CFFfL8WkEKusuclYEQFgzz23OErrl8I9 6ZhBWtYCnCa2m3P5Hqm2leiM1KXzikAKAGsdb7c9mITockB7txr3YE0kVdknfkCAsA4Y+i xD2KRF3nqRdS5j/a0Vl2gTyfE5SSX+A= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-248-DPcAUgVXMG2mt9O-XIn8aw-1; Sun, 27 Mar 2022 20:57:57 -0400 X-MC-Unique: DPcAUgVXMG2mt9O-XIn8aw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E5B959219AB; Mon, 28 Mar 2022 00:57:55 +0000 (UTC) Received: from llong.com (unknown [10.22.16.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7443740CF8EA; Mon, 28 Mar 2022 00:57:54 +0000 (UTC) From: Waiman Long To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Roman Gushchin , Waiman Long Subject: [PATCH] mm/list_lru: Fix possible race in memcg_reparent_list_lru_node() Date: Sun, 27 Mar 2022 20:57:36 -0400 Message-Id: <20220328005736.2513727-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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: 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_it= ems)) // Miss reparenting return // Assume 0->1 l->nr_items++ // Assume 0->1 nlru->nr_items++ 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. Adding a spin_is_locked() check will likely be enough for x86, but it is less certain for other arches with a more relaxed memory semantics like arcm64 and ppc. To avoid race, this patch moves the nr_items check to within the lock critical section. Fixes: 405cc51fc104 ("mm/list_lru: optimize memcg_reparent_list_lru_node()") Signed-off-by: Waiman Long Reported-by: Muchun Song --- mm/list_lru.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mm/list_lru.c b/mm/list_lru.c index c669d87001a6..8aec8ebd5995 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -394,18 +394,18 @@ static void memcg_reparent_list_lru_node(struct list_= lru *lru, int nid, int dst_idx =3D dst_memcg->kmemcg_id; struct list_lru_one *src, *dst; =20 - /* - * If there is no lru entry in this nlru, we can skip it immediately. - */ - if (!READ_ONCE(nlru->nr_items)) - return; - /* * Since list_lru_{add,del} may be called under an IRQ-safe lock, * we have to use IRQ-safe primitives here to avoid deadlock. */ spin_lock_irq(&nlru->lock); =20 + /* + * If there is no lru entry in this nlru, we can skip it immediately. + */ + if (!nlru->nr_items) + goto out; + src =3D list_lru_from_memcg_idx(lru, nid, src_idx); if (!src) goto out; --=20 2.27.0