From mboxrd@z Thu Jan 1 00:00:00 1970 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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL 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 7F936C4338F for ; Tue, 27 Jul 2021 17:00:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6111061039 for ; Tue, 27 Jul 2021 17:00:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230292AbhG0RAU (ORCPT ); Tue, 27 Jul 2021 13:00:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230198AbhG0RAO (ORCPT ); Tue, 27 Jul 2021 13:00:14 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2F64C061760 for ; Tue, 27 Jul 2021 10:00:10 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id h14so22856975lfv.7 for ; Tue, 27 Jul 2021 10:00:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kvve0PvD+BG/Fg4YS9gRmkOVtrJzBbhe3AaQP2JrJi8=; b=BamPGKxkayrhztUhyIbCPdfJ8zOowXYiQvF4Ibi4hR0BXtMIs0NGaC5lPTvZHBn0Bj mzTVS882/CQo5XFnXL+kJORZmjlCxYKwnGheT0Mr4xI5I3Yz7pWttn2GW7k6ri8BGFj4 /tuUI5A98uxjuAqFl1H6Cg6ALu6Gctc66YS0ZJ2uTIrJXzhfOvU5KfgqpqJhu7maJu/a NVk17jZ4yzeksCWzllgFbNe9pAgPx0k9n0jdpRhENWB/kHvVMJT0VhWqYjXiQR6GW3yz Y5xlnUlSP02eTGvm9lLHUxGDrXWl29SGfFf709MaMb2bdgYobYPeSAp+9ixsNYkmIs2a 3dOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kvve0PvD+BG/Fg4YS9gRmkOVtrJzBbhe3AaQP2JrJi8=; b=LwF3Ue+qciDXXzstj5a5XekB9FRcCrMjKcMHxYGf70BOMrTYocbcTwYrv6mQrSunF3 QpUuSr5Vx9xMqIaVB7TATy8aX+HO+5O7IbmLHHAR/zRS594QPYYZnqG79q6sWbhPAfo/ uqxVLdffBgpLNFRm85QaBv2SVOBz8Bpsu6QvMfxbHuOdwr8bg6XBaI1qZRagFmHX1/lk mxoQEwo0VqXx5/zzs7o6aBMB4gBs+lHSaSWedr9gRvHiDCG3Y/tgfuyRofE/5AUWKNVy /Xxb/oce03lVqAYSvvPVICCY46xGqhDk9x3i7Ujc6DLGAj6Y2OwJIHndbkZKV0ekdh/j 4Ifg== X-Gm-Message-State: AOAM532QeZxhmAcuNkfqbb0gzF5Ace3bq8vwS9e6pUIzai9qr4JKmTfG grRDdUrZ825SfSA7dRX/XSml6Fl9kUHzyuWGgaRDPg== X-Google-Smtp-Source: ABdhPJw3HT5zZziCLyixwFiw0R8p8dngZ62UL76GxgmYW/TEr3P9n8CI/cqFloMXIxXkKLzFLg/dz8VKxewa+txr/zw= X-Received: by 2002:a19:dc50:: with SMTP id f16mr17009143lfj.347.1627405208894; Tue, 27 Jul 2021 10:00:08 -0700 (PDT) MIME-Version: 1.0 References: <20210726150019.251820-1-hannes@cmpxchg.org> In-Reply-To: <20210726150019.251820-1-hannes@cmpxchg.org> From: Shakeel Butt Date: Tue, 27 Jul 2021 09:59:57 -0700 Message-ID: Subject: Re: [PATCH] mm: memcontrol: fix blocking rstat function called from atomic cgroup1 thresholding code To: Johannes Weiner Cc: Andrew Morton , Dan Carpenter , Linux MM , Cgroups , LKML , Kernel Team Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 26, 2021 at 8:01 AM Johannes Weiner wrote: > > 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 Reviewed-by: Shakeel Butt BTW what do you think of removing stat flushes from the read side (kernel and userspace) completely after periodic flushing and async flushing from update side? Basically with "memcg: infrastructure to flush memcg stats". From mboxrd@z Thu Jan 1 00:00:00 1970 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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=unavailable 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 97AADC4338F for ; Tue, 27 Jul 2021 17:01:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4608160240 for ; Tue, 27 Jul 2021 17:01:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 4608160240 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id B15E28D0002; Tue, 27 Jul 2021 13:01:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC6256B006C; Tue, 27 Jul 2021 13:01:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B4988D0002; Tue, 27 Jul 2021 13:01:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0182.hostedemail.com [216.40.44.182]) by kanga.kvack.org (Postfix) with ESMTP id 7EB346B005D for ; Tue, 27 Jul 2021 13:01:11 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 22E048249980 for ; Tue, 27 Jul 2021 17:01:11 +0000 (UTC) X-FDA: 78408983142.27.6D51B70 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by imf19.hostedemail.com (Postfix) with ESMTP id AD466B000C28 for ; Tue, 27 Jul 2021 17:00:10 +0000 (UTC) Received: by mail-lf1-f44.google.com with SMTP id f18so22833575lfu.10 for ; Tue, 27 Jul 2021 10:00:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kvve0PvD+BG/Fg4YS9gRmkOVtrJzBbhe3AaQP2JrJi8=; b=BamPGKxkayrhztUhyIbCPdfJ8zOowXYiQvF4Ibi4hR0BXtMIs0NGaC5lPTvZHBn0Bj mzTVS882/CQo5XFnXL+kJORZmjlCxYKwnGheT0Mr4xI5I3Yz7pWttn2GW7k6ri8BGFj4 /tuUI5A98uxjuAqFl1H6Cg6ALu6Gctc66YS0ZJ2uTIrJXzhfOvU5KfgqpqJhu7maJu/a NVk17jZ4yzeksCWzllgFbNe9pAgPx0k9n0jdpRhENWB/kHvVMJT0VhWqYjXiQR6GW3yz Y5xlnUlSP02eTGvm9lLHUxGDrXWl29SGfFf709MaMb2bdgYobYPeSAp+9ixsNYkmIs2a 3dOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kvve0PvD+BG/Fg4YS9gRmkOVtrJzBbhe3AaQP2JrJi8=; b=eF/W2lvAeF5vEmVn6Q2jQWZb4lVWJyZAHBl6yN9CZYz0e9mRCeqy3G5h+MZYD8rlvd 6JXtd8Ucw76ghkf4FWpGHWgWQCfsSySZPadSPmk601PfLpafForGuzUb8IB8CeYL0A21 a8LXs38fJnq27hPEwIYVeKQQLJwgJgcr2hkS22AowlJApFHTT3nS3T2qXllq4AvZfAf1 lwR39BKIRvXKk7jdlGbJow2w2C1wbOWbWfDtKwgU940JZJm18P73GKe96k2fCbw3vZYc JW0C01SeeXNy79per6FPa/as9lO5hH7O+L/y4r3+lkpzDj5CiwynrDwTze4f8HeQR/fR lO4g== X-Gm-Message-State: AOAM53244wQLHMo4Z7RHg5QdNA92pBXc2INteWaYgxcVAScPi5BGf/l8 NdrUXUFPNOXVN5yvNPnkcf9Xik6wdBAuA/9SXtxVjQ== X-Google-Smtp-Source: ABdhPJw3HT5zZziCLyixwFiw0R8p8dngZ62UL76GxgmYW/TEr3P9n8CI/cqFloMXIxXkKLzFLg/dz8VKxewa+txr/zw= X-Received: by 2002:a19:dc50:: with SMTP id f16mr17009143lfj.347.1627405208894; Tue, 27 Jul 2021 10:00:08 -0700 (PDT) MIME-Version: 1.0 References: <20210726150019.251820-1-hannes@cmpxchg.org> In-Reply-To: <20210726150019.251820-1-hannes@cmpxchg.org> From: Shakeel Butt Date: Tue, 27 Jul 2021 09:59:57 -0700 Message-ID: Subject: Re: [PATCH] mm: memcontrol: fix blocking rstat function called from atomic cgroup1 thresholding code To: Johannes Weiner Cc: Andrew Morton , Dan Carpenter , Linux MM , Cgroups , LKML , Kernel Team Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: AD466B000C28 Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=BamPGKxk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of shakeelb@google.com designates 209.85.167.44 as permitted sender) smtp.mailfrom=shakeelb@google.com X-Stat-Signature: igppgx1ey8x195zsf4skhbsz11zq3sjk X-HE-Tag: 1627405210-723362 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: On Mon, Jul 26, 2021 at 8:01 AM Johannes Weiner wrote: > > 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 Reviewed-by: Shakeel Butt BTW what do you think of removing stat flushes from the read side (kernel and userspace) completely after periodic flushing and async flushing from update side? Basically with "memcg: infrastructure to flush memcg stats". From mboxrd@z Thu Jan 1 00:00:00 1970 From: Shakeel Butt Subject: Re: [PATCH] mm: memcontrol: fix blocking rstat function called from atomic cgroup1 thresholding code Date: Tue, 27 Jul 2021 09:59:57 -0700 Message-ID: References: <20210726150019.251820-1-hannes@cmpxchg.org> Mime-Version: 1.0 Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kvve0PvD+BG/Fg4YS9gRmkOVtrJzBbhe3AaQP2JrJi8=; b=BamPGKxkayrhztUhyIbCPdfJ8zOowXYiQvF4Ibi4hR0BXtMIs0NGaC5lPTvZHBn0Bj mzTVS882/CQo5XFnXL+kJORZmjlCxYKwnGheT0Mr4xI5I3Yz7pWttn2GW7k6ri8BGFj4 /tuUI5A98uxjuAqFl1H6Cg6ALu6Gctc66YS0ZJ2uTIrJXzhfOvU5KfgqpqJhu7maJu/a NVk17jZ4yzeksCWzllgFbNe9pAgPx0k9n0jdpRhENWB/kHvVMJT0VhWqYjXiQR6GW3yz Y5xlnUlSP02eTGvm9lLHUxGDrXWl29SGfFf709MaMb2bdgYobYPeSAp+9ixsNYkmIs2a 3dOQ== In-Reply-To: <20210726150019.251820-1-hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org> List-ID: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Johannes Weiner Cc: Andrew Morton , Dan Carpenter , Linux MM , Cgroups , LKML , Kernel Team On Mon, Jul 26, 2021 at 8:01 AM Johannes Weiner wrote: > > 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 Reviewed-by: Shakeel Butt BTW what do you think of removing stat flushes from the read side (kernel and userspace) completely after periodic flushing and async flushing from update side? Basically with "memcg: infrastructure to flush memcg stats".