From patchwork Mon Jul 26 15:00:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Weiner X-Patchwork-Id: 12399859 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7108CC4338F for ; Mon, 26 Jul 2021 15:00:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D7D6360F02 for ; Mon, 26 Jul 2021 15:00:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org D7D6360F02 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=cmpxchg.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 482506B005D; Mon, 26 Jul 2021 11:00:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4332E8D0002; Mon, 26 Jul 2021 11:00:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 321038D0001; Mon, 26 Jul 2021 11:00:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0103.hostedemail.com [216.40.44.103]) by kanga.kvack.org (Postfix) with ESMTP id 1BAE26B005D for ; Mon, 26 Jul 2021 11:00:23 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id ADF4E22890 for ; Mon, 26 Jul 2021 15:00:22 +0000 (UTC) X-FDA: 78405049884.05.1B2E9CB Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) by imf12.hostedemail.com (Postfix) with ESMTP id 049A31000C87 for ; Mon, 26 Jul 2021 15:00:21 +0000 (UTC) Received: by mail-qt1-f174.google.com with SMTP id b1so7258239qtx.0 for ; Mon, 26 Jul 2021 08:00:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=xTTsptuLITxPc+Owajv0QCvUN6VmzjQA4sTb/kLyZME=; b=ZXLe+pV1dgrpJxW8gR0RgPwPUehS8IrE/2Vbou8UHcmjEUrqODo0mbUUtREDLuboHI uRYwy1cK4J2lucpZS2lkrz+Yw3WhqxYpAkUM404nyr8nhUs+xbXAzDBhgicYHjJ02tcT P7TwXco+KKFQYqfCwlG7Pa+rFK8sjhe3zkhUEIpLvEhU1x/8TfOIdLv3UNg30/xApIS9 n+idNUq/UTyY5HU1LEGAlJPZWfKdyKAYAIaaFhvqhVJnPZULfAhZMEDr+qzP1/Amj40Q BVflhSLUWnZN5aqLPa75Q9QA+mvqr+1FOMZjDpEEW8gOwUNQA5DRllSRQyw3V6m1xd8f cVfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=xTTsptuLITxPc+Owajv0QCvUN6VmzjQA4sTb/kLyZME=; b=LKOA23tFTeOFPFA5dbgxgWF4Z8Yxg4KwSJT5PMhxq7PVuonVp/9JzRwJo8JFJeQH1c akAxCJ9W9KVMT8qfV+giG4X+jKBTP5ngyCVHA7ajFo2h6QTXmqMKUy/gZ6Si63moAwlS Th/0AP5DCh5Qrh7DZ4fdk33pD7q1xEarOzMTK6svdaBjVNhQr+gJmRpnqK8Hc+71pWGA T8ebUVrTIYwz7BOVvR4iNa9sn1A2xkux1gAGx/7rsCihs0OjAuYuKcMMOuzZhKNoZXxG H4Khl8dyH69QcnMqd8HHz7MNwQkB0orro5kb9/j1LLBBqAklWzc8vKcA9Y8o2usHzKoV vNrg== X-Gm-Message-State: AOAM533RKKkUtDB993JKR3PQczEQlVvcDx7hOxjgTbPHSOn978SUBjyr 64xEmozkIincmM7wo6gHf2PR+w== X-Google-Smtp-Source: ABdhPJxUmmX4dY9GgbrCAU62sFsuKrKt/V1WzL+nrW4HxYYqVOvANMZBVBRtyJKBVfb1X3SNBJmi+w== X-Received: by 2002:ac8:66ca:: with SMTP id m10mr14787845qtp.171.1627311621182; Mon, 26 Jul 2021 08:00:21 -0700 (PDT) Received: from localhost ([2620:10d:c091:480::1:e732]) by smtp.gmail.com with ESMTPSA id h10sm99931qka.83.2021.07.26.08.00.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jul 2021 08:00:20 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Dan Carpenter , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH] mm: memcontrol: fix blocking rstat function called from atomic cgroup1 thresholding code Date: Mon, 26 Jul 2021 11:00:19 -0400 Message-Id: <20210726150019.251820-1-hannes@cmpxchg.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=ZXLe+pV1; spf=pass (imf12.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.160.174 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org; dmarc=pass (policy=none) header.from=cmpxchg.org X-Rspamd-Server: rspam02 X-Stat-Signature: wg9fbt6kea46qs9s9gp8n8crhtqwf569 X-Rspamd-Queue-Id: 049A31000C87 X-HE-Tag: 1627311621-401411 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: Dan Carpenter reports: The patch 2d146aa3aa84: "mm: memcontrol: switch to rstat" from Apr 29, 2021, leads to the following static checker warning: kernel/cgroup/rstat.c:200 cgroup_rstat_flush() warn: sleeping in atomic context mm/memcontrol.c 3572 static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) 3573 { 3574 unsigned long val; 3575 3576 if (mem_cgroup_is_root(memcg)) { 3577 cgroup_rstat_flush(memcg->css.cgroup); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This is from static analysis and potentially a false positive. The problem is that mem_cgroup_usage() is called from __mem_cgroup_threshold() which holds an rcu_read_lock(). And the cgroup_rstat_flush() function can sleep. 3578 val = memcg_page_state(memcg, NR_FILE_PAGES) + 3579 memcg_page_state(memcg, NR_ANON_MAPPED); 3580 if (swap) 3581 val += memcg_page_state(memcg, MEMCG_SWAP); 3582 } else { 3583 if (!swap) 3584 val = page_counter_read(&memcg->memory); 3585 else 3586 val = page_counter_read(&memcg->memsw); 3587 } 3588 return val; 3589 } __mem_cgroup_threshold() indeed holds the rcu lock. In addition, the thresholding code is invoked during stat changes, and those contexts have irqs disabled as well. If the lock breaking occurs inside the flush function, it will result in a sleep from an atomic context. Use the irsafe flushing variant in mem_cgroup_usage() to fix this. Fixes: 2d146aa3aa84 ("mm: memcontrol: switch to rstat") Cc: Reported-by: Dan Carpenter Signed-off-by: Johannes Weiner Acked-by: Chris Down Reviewed-by: Rik van Riel Acked-by: Michal Hocko Reviewed-by: Shakeel Butt --- mm/memcontrol.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index ae1f5d0cb581..eb8e87c4833f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3574,7 +3574,8 @@ static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) unsigned long val; if (mem_cgroup_is_root(memcg)) { - cgroup_rstat_flush(memcg->css.cgroup); + /* mem_cgroup_threshold() calls here from irqsafe context */ + cgroup_rstat_flush_irqsafe(memcg->css.cgroup); val = memcg_page_state(memcg, NR_FILE_PAGES) + memcg_page_state(memcg, NR_ANON_MAPPED); if (swap)