Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp7892156rwl; Thu, 23 Mar 2023 09:53:52 -0700 (PDT) X-Google-Smtp-Source: AK7set/dCpYh/p6IQVtwJ70X8LXXWl2REfO4rtC+zTvbbRQM9uCJheQeuUTqXnlzYPF9YC0psJiR X-Received: by 2002:a17:906:b28b:b0:926:fce:c080 with SMTP id q11-20020a170906b28b00b009260fcec080mr12014160ejz.17.1679590432482; Thu, 23 Mar 2023 09:53:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679590432; cv=none; d=google.com; s=arc-20160816; b=G+wWUafyRN1KTrwsLDGPIsfDUWlMsdqu+0tpWV54L8DG8JUaCsoWDgWw9jC5/d0L3a mEjAGz8g6TzsBByUUXWPCJEZ9HvLwd6bHEDPrF8GFHPbux9QbzM/M4GRBQmK7Q9m6KKA gOMI2WUJKQuKDIY1Hfu/UK3QDzS7JWJyNVbVUFZHyj8u0IxAEG0gIgsAWZnx5IyfNW2/ LjfBMJoJawyiVgsE/OumFh56T5/FAJdyEvfq0NElTbI5EmGD+5/YlI4Aw9HaSlISvxuY PUnTmOL0FLpB0G0wPGCGeOSDWnzZm+jLn0L8k/VnWqXx2CAYZWRDeKs8sFAuDgME9n16 K3Eg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=VOHurpH2QBVwThGFlZ8crYEQpG4K5Nj3qYdrgtUjem4=; b=PYavWjiAuJ0J6SiQj2Q/Jv2YGwHUD22959ZKxs1PaagFmfZq9AVYaDllk1/2V7TC/x ZTc32GIwj6dauomf7rHFGn7k9Zv48jFrI+iGtth12bodTfR2Cb6oQRyL2lNxy4WXuDum Rpx9jYSon2OL7LsTuOt1AWF7Kj+uXwiPb/Y7fM6Mbpp/sSDmht+wd1Ah5/Q1Yh/+6ZIf XdlgIFPg2YxURIv+RxZIx0EsSMpN8a8hf9mYnkTNJ+MiBipe8fuWT/RFrNDQBa0ChHTK IS3ehHeHt53Km4IIWuM62nCwt5/+XhYEqhYJiHe3aQaPBMU3RIrBzwVexJntaJlKx/HC ZfzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kMaWqgzR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id tj10-20020a170907c24a00b0093308b4f91fsi15038209ejc.269.2023.03.23.09.53.28; Thu, 23 Mar 2023 09:53:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kMaWqgzR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232469AbjCWQwm (ORCPT + 99 others); Thu, 23 Mar 2023 12:52:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232417AbjCWQwj (ORCPT ); Thu, 23 Mar 2023 12:52:39 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD49D9EFE for ; Thu, 23 Mar 2023 09:52:35 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id eh3so89596273edb.11 for ; Thu, 23 Mar 2023 09:52:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679590354; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=VOHurpH2QBVwThGFlZ8crYEQpG4K5Nj3qYdrgtUjem4=; b=kMaWqgzR2vvNtaGPfonHpApcQDQuX605YFrobGccymTC1kzhDvZj3uzUwY/OGu1ojC JSsawrHWC2d9REN3PawI0wk2z84JKtSMJGNAcW3qeVKuZ/FcofM5K0fzLaNcGJMRKo// bYSp9T2JFuLa93mdHlpQEHWZgonpp5svFktD/LhmLFo8bG7LR64XMrKzIJG5Xjnzppx2 EoThWl3HouEyGSMwNEv79uwgbweYttltY124sshwnzdRlWfX0zQ9veQCVY4S1ap4ew8P jVbECrk69bG41wCMNi+NeC0pFUfs7PpUdqglwIj85HadF5SaiBJvLfxWVBRe05eYplnK dqjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679590354; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VOHurpH2QBVwThGFlZ8crYEQpG4K5Nj3qYdrgtUjem4=; b=xuyNxuZrauxJ0amJcbChFH49VL2ZFX7Jsvbbf7mxZrr03OqSgrYJu2tvk3MmLzObp6 Zt92ytEdXd2IOIdGCsrw50LbM3GW6VSTxxVo6VhaOhJ2Qj76ESMZcNbdFIjkBt9RwbBp b5mmtFb7Qo+iVtJ6Gq+2WJhDC2Ld/lXYxDWL3eVo0dOeRw6mxZBwY6ISUfJnIHrCncV4 4gT81Zp/C6IjYiikzZy1jg0pZlGuXZ9m5kb6g4Z9epdoU7V+kpQIPrlKhdD4wWg5/5GJ YwfdRMu7VKh0ig+223fxaXhalJ9yBu2EW3HoBh22Q3YW5qzFjCocyDNJc4VdB1LGns2H oeUg== X-Gm-Message-State: AO0yUKWe447jaNzPwTsO4M9287Zwt9VU5jhQBS3vbiFqcwmg2HDkwzB+ oIBFlVRt3s5jSdXHVNHXqtS9OwjqrX4yR5dkLyTEvQ== X-Received: by 2002:a17:906:344d:b0:933:7658:8b44 with SMTP id d13-20020a170906344d00b0093376588b44mr5407827ejb.15.1679590354122; Thu, 23 Mar 2023 09:52:34 -0700 (PDT) MIME-Version: 1.0 References: <20230323040037.2389095-1-yosryahmed@google.com> <20230323040037.2389095-2-yosryahmed@google.com> In-Reply-To: From: Yosry Ahmed Date: Thu, 23 Mar 2023 09:51:57 -0700 Message-ID: Subject: Re: [RFC PATCH 1/7] cgroup: rstat: only disable interrupts for the percpu lock To: Shakeel Butt Cc: Tejun Heo , Josef Bacik , Jens Axboe , Zefan Li , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Andrew Morton , Vasily Averin , cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-15.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,ENV_AND_HDR_SPF_MATCH, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=unavailable 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 Thu, Mar 23, 2023 at 9:45=E2=80=AFAM Shakeel Butt = wrote: > > On Thu, Mar 23, 2023 at 9:37=E2=80=AFAM Yosry Ahmed wrote: > > > > On Thu, Mar 23, 2023 at 9:29=E2=80=AFAM Shakeel Butt wrote: > > > > > > On Thu, Mar 23, 2023 at 9:18=E2=80=AFAM Yosry Ahmed wrote: > > > > > > > > On Thu, Mar 23, 2023 at 9:10=E2=80=AFAM Shakeel Butt wrote: > > > > > > > > > > On Thu, Mar 23, 2023 at 8:46=E2=80=AFAM Shakeel Butt wrote: > > > > > > > > > > > > On Thu, Mar 23, 2023 at 8:43=E2=80=AFAM Yosry Ahmed wrote: > > > > > > > > > > > > > > On Thu, Mar 23, 2023 at 8:40=E2=80=AFAM Shakeel Butt wrote: > > > > > > > > > > > > > > > > On Thu, Mar 23, 2023 at 6:36=E2=80=AFAM Yosry Ahmed wrote: > > > > > > > > > > > > > > > > > [...] > > > > > > > > > > > > > > > > > > > > > > > 2. Are we really calling rstat flush in irq context= ? > > > > > > > > > > > > > > > > > > > > > > I think it is possible through the charge/uncharge pa= th: > > > > > > > > > > > memcg_check_events()->mem_cgroup_threshold()->mem_cgr= oup_usage(). I > > > > > > > > > > > added the protection against flushing in an interrupt= context for > > > > > > > > > > > future callers as well, as it may cause a deadlock if= we don't disable > > > > > > > > > > > interrupts when acquiring cgroup_rstat_lock. > > > > > > > > > > > > > > > > > > > > > > > 3. The mem_cgroup_flush_stats() call in mem_cgroup_= usage() is only > > > > > > > > > > > > done for root memcg. Why is mem_cgroup_threshold() = interested in root > > > > > > > > > > > > memcg usage? Why not ignore root memcg in mem_cgrou= p_threshold() ? > > > > > > > > > > > > > > > > > > > > > > I am not sure, but the code looks like event notifica= tions may be set > > > > > > > > > > > up on root memcg, which is why we need to check thres= holds. > > > > > > > > > > > > > > > > > > > > This is something we should deprecate as root memcg's u= sage is ill defined. > > > > > > > > > > > > > > > > > > Right, but I think this would be orthogonal to this patch= series. > > > > > > > > > > > > > > > > > > > > > > > > > I don't think we can make cgroup_rstat_lock a non-irq-disab= ling lock > > > > > > > > without either breaking a link between mem_cgroup_threshold= and > > > > > > > > cgroup_rstat_lock or make mem_cgroup_threshold work without= disabling > > > > > > > > irqs. > > > > > > > > > > > > > > > > So, this patch can not be applied before either of those tw= o tasks are > > > > > > > > done (and we may find more such scenarios). > > > > > > > > > > > > > > > > > > > > > Could you elaborate why? > > > > > > > > > > > > > > My understanding is that with an in_task() check to make sure= we only > > > > > > > acquire cgroup_rstat_lock from non-irq context it should be f= ine to > > > > > > > acquire cgroup_rstat_lock without disabling interrupts. > > > > > > > > > > > > From mem_cgroup_threshold() code path, cgroup_rstat_lock will b= e taken > > > > > > with irq disabled while other code paths will take cgroup_rstat= _lock > > > > > > with irq enabled. This is a potential deadlock hazard unless > > > > > > cgroup_rstat_lock is always taken with irq disabled. > > > > > > > > > > Oh you are making sure it is not taken in the irq context through > > > > > should_skip_flush(). Hmm seems like a hack. Normally it is recomm= ended > > > > > to actually remove all such users instead of silently > > > > > ignoring/bypassing the functionality. > > > > > > > > It is a workaround, we simply accept to read stale stats in irq > > > > context instead of the expensive flush operation. > > > > > > > > > > > > > > So, how about removing mem_cgroup_flush_stats() from > > > > > mem_cgroup_usage(). It will break the known chain which is taking > > > > > cgroup_rstat_lock with irq disabled and you can add > > > > > WARN_ON_ONCE(!in_task()). > > > > > > > > This changes the behavior in a more obvious way because: > > > > 1. The memcg_check_events()->mem_cgroup_threshold()->mem_cgroup_usa= ge() > > > > path is also exercised in a lot of paths outside irq context, this > > > > will change the behavior for any event thresholds on the root memcg= . > > > > With proposed skipped flushing in irq context we only change the > > > > behavior in a small subset of cases. > > > > > > > > I think we can skip flushing in irq context for now, and separately > > > > deprecate threshold events for the root memcg. When that is done we > > > > can come back and remove should_skip_flush() and add a VM_BUG_ON or > > > > WARN_ON_ONCE instead. WDYT? > > > > > > > > 2. mem_cgroup_usage() is also used when reading usage from userspac= e. > > > > This should be an easy workaround though. > > > > > > This is a cgroup v1 behavior and to me it is totally reasonable to ge= t > > > the 2 second stale root's usage. Even if you want to skip flushing in > > > irq, do that in the memcg code and keep VM_BUG_ON/WARN_ON_ONCE in the > > > rstat core code. This way we will know if other subsystems are doing > > > the same or not. > > > > We can do that. Basically in mem_cgroup_usage() have: > > > > /* Some useful comment */ > > if (in_task()) > > mem_cgroup_flush_stats(); > > > > and in cgroup_rstat_flush() have: > > WARN_ON_ONCE(!in_task()); > > > > I am assuming VM_BUG_ON is not used outside mm code. > > > > The only thing that worries me is that if there is another unlikely > > path somewhere that flushes stats in irq context we may run into a > > deadlock. I am a little bit nervous about not skipping flushing if > > !in_task() in cgroup_rstat_flush(). > > I think it is a good thing. We will find such scenarios and fix those > instead of hiding them forever or keeping the door open for new such > scenarios. Sure, I can do that in the next version. I will include a patch that adds an in_task() check to mem_cgroup_usage() before this one. Since BUG_ON() is discouraged and VM_BUG_ON() is mm specific, I guess we are left with WARN_ON_ONCE() for the rstat core code, right? Thanks Shakeel. Any other thoughts I should address for the next version?