Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp8025570rwl; Thu, 23 Mar 2023 11:32:47 -0700 (PDT) X-Google-Smtp-Source: AKy350b8QWG0KjBOu4HQ7VUwHO+C0ImRnpCscD3YM7ac+xLP8ssIl/7poYetdDYJLCO3cVoaQdZK X-Received: by 2002:aa7:c948:0:b0:4fe:94a2:81be with SMTP id h8-20020aa7c948000000b004fe94a281bemr377184edt.7.1679596366860; Thu, 23 Mar 2023 11:32:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679596366; cv=none; d=google.com; s=arc-20160816; b=Wi40d5fP294V0CByrc9t1c3M92c72yXJy4Oc8rgvOg27lsnH6hpQslNk1UckSTL7E1 yxJS5+iiGUjMDqztfrbmFpuZD94AbtxitrGYvIBO5TF8LEfmgrD2v2/v7esh+QADzwDt jeHDjmJRHVdgqwHmhxOd4BbAhX50TCpjCDAX3zZJnsH0VcnykVOWwfbvYXu7VEerWO3A t3p1bu2ZOE07g/NjQO4h6YF9FrKblba6uTE6w4/JsXICb8odAb6O6zTgpZSgAHddKup3 jnalECVfgs8qUkulzkXZfUH1AB9kAMSkDVCQCr4H7HNcEL+g47f59iw3XPlOBGM0XgT8 2W7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=4UdIOBBpU5M34Q+dnxHktyTShJiu1SyYtXU+zhX+XRQ=; b=YeifEBprxkZj+gO7gb/wOYOmdDgCzWzTEx3B5K+3KXDrbu1BfxZyLNad0oaqxDGi1N cCYreCcjbMNUjzqxrsJRK0GepsOIpyQcU42UnposT8FxvbaVJTt0s0VxOMvYnQ3Tigj3 PbdUAzBTxOanrKMEV5A+O9Pgg3W07j3FfsF01i3Yr0JjqMM7aimBPJ8vwkXy7/zkCa+a JkJ7ZzLklwBacslGz4/n8bpxHWeExXzIizRz/qVfZH/RIrHA9nTIUTLV0PWbq/VDBhCR K2mKMvkKHnMzNBvzyLWLflydNbmFX5NVrowtLN41RxtSddqxnnmN3H89BN7Dpc/AHCWp yQaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=iSci0p8J; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e9-20020a056402104900b004ad06322427si4293898edu.9.2023.03.23.11.32.22; Thu, 23 Mar 2023 11:32:46 -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=@cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=iSci0p8J; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231357AbjCWSTe (ORCPT + 99 others); Thu, 23 Mar 2023 14:19:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229672AbjCWSTb (ORCPT ); Thu, 23 Mar 2023 14:19:31 -0400 Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D19EA30EA for ; Thu, 23 Mar 2023 11:19:27 -0700 (PDT) Received: by mail-qt1-x834.google.com with SMTP id w25so5038079qtc.5 for ; Thu, 23 Mar 2023 11:19:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20210112.gappssmtp.com; s=20210112; t=1679595566; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=4UdIOBBpU5M34Q+dnxHktyTShJiu1SyYtXU+zhX+XRQ=; b=iSci0p8JFRPXqHPEil3M1UZ/AptTFLyiH4nOONlx8jlszjb2lxcuRkMBw7UZXMYuIb 9VHiSPLwsHCGWKxBshnY3dxP8I+VjQfUKrOS8QxSItXdSt2WijBjz2KEim5klCxbUPhR gr9geE8sgGsa6YxvHsaCLYHtCOhtZ6Cj5uxcAZqUHM/Oo9TpSV6LTpCjwajd0gTTy2/o iGV78mzHEJs46abGoP8FjHVkdxrPvpdUU0X6sI4iRzWXH+1PxvpLLlNb42LXVJdJuGZu 4FeSqA110YAy7/7r+UTZpX1+gN+1sKQK2Ig4sSlYBcZ1rci0KNN0Hu5Nnn+hpheYoXSX jdGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679595566; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4UdIOBBpU5M34Q+dnxHktyTShJiu1SyYtXU+zhX+XRQ=; b=MizkvGMYFfi8fcaQ783GKElCRJxrdE3lEFu/TSrfpvFrj/UG6+k/h2bu+QOUO/6uk5 xkfm0E8OeN8lRrprMeepRkFyiQPxTDuMJ3aZz+nvFR1Io3m5r9KrF9OMfeyuj5CRcLYd CcI6b2ap0AUILsr3YJXOVa6xa1Gvo1ZmRfxOIrjGF0UwY+3zKINmKZuksqsdeagGREWO hYIN0K4ReuoOC4U8bHKZSiRYkRr7FFU3q6ivWbQpu0k9Ems9p/P2BMuna2HYryky5+SW wsc4CpEKEJd67yUxZDfC3s33qpqSY6nfyp19LE2PzEI1emQXV2zLNfnXlMZQGhr1ysz1 zw0g== X-Gm-Message-State: AO0yUKWorqApK0JBkGCOuS3rhNMtuBnGPuN1EIueX7pBfys20LAOSUDo 6asxJXrFTUNThdVIVvs1PhevzQ== X-Received: by 2002:a05:622a:15d2:b0:3e3:8502:fbcb with SMTP id d18-20020a05622a15d200b003e38502fbcbmr394669qty.40.1679595566584; Thu, 23 Mar 2023 11:19:26 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:62db]) by smtp.gmail.com with ESMTPSA id f16-20020ac86ed0000000b003e390b48958sm1837405qtv.55.2023.03.23.11.19.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 11:19:26 -0700 (PDT) Date: Thu, 23 Mar 2023 14:19:25 -0400 From: Johannes Weiner To: Yosry Ahmed Cc: Shakeel Butt , Tejun Heo , Josef Bacik , Jens Axboe , Zefan Li , 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 Subject: Re: [RFC PATCH 1/7] cgroup: rstat: only disable interrupts for the percpu lock Message-ID: <20230323181925.GA742010@cmpxchg.org> References: <20230323173343.GF739026@cmpxchg.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 11:09:30AM -0700, Yosry Ahmed wrote: > On Thu, Mar 23, 2023 at 10:33 AM Johannes Weiner wrote: > > > > On Thu, Mar 23, 2023 at 09:17:33AM -0700, Yosry Ahmed wrote: > > > On Thu, Mar 23, 2023 at 9:10 AM Shakeel Butt wrote: > > > > > > > > On Thu, Mar 23, 2023 at 8:46 AM Shakeel Butt wrote: > > > > > > > > > > On Thu, Mar 23, 2023 at 8:43 AM Yosry Ahmed wrote: > > > > > > > > > > > > On Thu, Mar 23, 2023 at 8:40 AM Shakeel Butt wrote: > > > > > > > > > > > > > > On Thu, Mar 23, 2023 at 6:36 AM Yosry Ahmed wrote: > > > > > > > > > > > > > > > [...] > > > > > > > > > > > > > > > > > > > > > 2. Are we really calling rstat flush in irq context? > > > > > > > > > > > > > > > > > > > > I think it is possible through the charge/uncharge path: > > > > > > > > > > memcg_check_events()->mem_cgroup_threshold()->mem_cgroup_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_cgroup_threshold() ? > > > > > > > > > > > > > > > > > > > > I am not sure, but the code looks like event notifications may be set > > > > > > > > > > up on root memcg, which is why we need to check thresholds. > > > > > > > > > > > > > > > > > > This is something we should deprecate as root memcg's usage 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-disabling 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 two 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 fine to > > > > > > acquire cgroup_rstat_lock without disabling interrupts. > > > > > > > > > > From mem_cgroup_threshold() code path, cgroup_rstat_lock will be 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 recommended > > > > to actually remove all such users instead of silently > > > > ignoring/bypassing the functionality. > > > > +1 > > > > It shouldn't silently skip the requested operation, rather it > > shouldn't be requested from an incompatible context. > > > > > > 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_usage() > > > 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. > > > > Can you do > > > > /* Note: stale usage data when called from irq context!! */ > > if (in_task()) > > mem_cgroup_flush_stats() > > > > directly in the callsite? Maybe even include the whole callchain in > > the comment that's currently broken and needs fixing/removing. > > Yeah, we can do that in mem_cgroup_usage(), which is the only context > that I am aware of that may flush from irq context. We can also add > WARN_ON_ONCE(!in_task()) in the rstat core flushing code to catch any > other code paths that we are not aware of -- which may result in a > deadlock, but hopefully if there is a violation it will be caught soon > enough. That sounds good to me, thanks!