From patchwork Mon Oct 25 12:49:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12581773 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB9CBC433EF for ; Mon, 25 Oct 2021 12:49:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 26AAF60F02 for ; Mon, 25 Oct 2021 12:49:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 26AAF60F02 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id B3500940007; Mon, 25 Oct 2021 08:49:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE51F6B0072; Mon, 25 Oct 2021 08:49:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D383940007; Mon, 25 Oct 2021 08:49:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0053.hostedemail.com [216.40.44.53]) by kanga.kvack.org (Postfix) with ESMTP id 8E4B56B006C for ; Mon, 25 Oct 2021 08:49:26 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 469AB18030C9E for ; Mon, 25 Oct 2021 12:49:26 +0000 (UTC) X-FDA: 78734940732.05.4CEB66E Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf30.hostedemail.com (Postfix) with ESMTP id DCEA8E001985 for ; Mon, 25 Oct 2021 12:49:16 +0000 (UTC) Received: by mail-pf1-f169.google.com with SMTP id v8so10639471pfu.11 for ; Mon, 25 Oct 2021 05:49:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vIr8TomifRdQW16cNm3Q1S6jeoHh7jjuLXp8SgxNC3Y=; b=w5Ov8eXqr5SfV+1QU3TsW2v2ae2rpirSISokBufoFLDgfs9c/z/0gDTtdhMqiTW5VY 7uO7dTx6QKMO1kxEgMRyaMMqt71sw46H/wp3h4SSWw+eWsEvxKMD1hKSIROiQWdxlZqJ 4GbSvqclvaSanWXYug8s+xPpZ3V4Gs+8nIs/oeSt3YWkK/bMEyyqJ2vt7J705UJCVY86 IwTzw57CIKwZ3zjc/lXEN1VTO3/sg2v1wVel2S3NI9LnI5mRtxNRIc1ZmCLvjL2s6pvT hAWmKv6tleunXzlyeSVYW8gqftGdrtF2CqMo8gp/e13wrHqeTMVyko0WzHOlHzoGLY0i lvbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vIr8TomifRdQW16cNm3Q1S6jeoHh7jjuLXp8SgxNC3Y=; b=qL8ElM+Ps9Sfnhlq0/LW3fwM6M9Po+5X6000r1vSdJczGSqos9VxutZv7ureVpwI6O Ljnlvjo2hvuiyzFX4hLIzekpmB1jOYGiUV/ZiMTR8wKayz58GU/7WcrCsHWrJTxJQGcK pjMQDWIOcyrvAZTequXHW5X82pObBs/J+BvH5wly/WU3/qkuTd4mm8jq+1varnooYPO5 eTVXF1jb0BNHCL+bDrFpe4rJ8zxICiXYroa8Zr8t6T0mxz8ywKhmGl9r4kNVhBYZDDYE I1Ro6RP7iTy2F5VPG+OPnjDSSJGIhcG+lYhYI1nRb/3hPXg2zYQhJifT4w1ZoZ7RYAAA yuBA== X-Gm-Message-State: AOAM533xtIx5j6FK5Bp4swyucrX20qvYTa+ocM1o4LLM1KJu+TxkkJ7P UTBVbaySXBUUd9zdIjc9Wu94wGXWAww= X-Google-Smtp-Source: ABdhPJyLSFlTt23w1TIfAXTikh48BynT+U/IuIHjJ1vsDBQHj8P9zBC0euacHFnvryGFEcsSrTvHag== X-Received: by 2002:a05:6a00:15c9:b0:44c:a998:b50d with SMTP id o9-20020a056a0015c900b0044ca998b50dmr18486801pfu.49.1635166164762; Mon, 25 Oct 2021 05:49:24 -0700 (PDT) Received: from localhost.localdomain ([61.120.150.70]) by smtp.gmail.com with ESMTPSA id pg17sm5284577pjb.8.2021.10.25.05.49.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Oct 2021 05:49:24 -0700 (PDT) From: Muchun Song To: akpm@linux-foundation.org, mhocko@kernel.org, shakeelb@google.com, willy@infradead.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH] mm: list_lru: fix the return value of list_lru_count_one() Date: Mon, 25 Oct 2021 20:49:10 +0800 Message-Id: <20211025124910.56433-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: DCEA8E001985 X-Stat-Signature: h7bn618ucs5aibqgtitek766yh8dp91s Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=w5Ov8eXq; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf30.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-HE-Tag: 1635166156-426873 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: Since commit 2788cf0c401c ("memcg: reparent list_lrus and free kmemcg_id on css offline"), ->nr_items can be negative during memory cgroup reparenting. In this case, list_lru_count_one() will return an unusual and huge value, which can surprise users. At least for now it hasn’t affected any users. But it is better to let list_lru_count_ont() returns zero when ->nr_items is negative. Signed-off-by: Muchun Song --- mm/list_lru.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/list_lru.c b/mm/list_lru.c index a6031f1c5bd7..2bba1cd68bb3 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -176,13 +176,16 @@ unsigned long list_lru_count_one(struct list_lru *lru, { struct list_lru_node *nlru = &lru->node[nid]; struct list_lru_one *l; - unsigned long count; + long count; rcu_read_lock(); l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg)); count = READ_ONCE(l->nr_items); rcu_read_unlock(); + if (unlikely(count < 0)) + count = 0; + return count; } EXPORT_SYMBOL_GPL(list_lru_count_one);