Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp7869457rwl; Thu, 23 Mar 2023 09:35:24 -0700 (PDT) X-Google-Smtp-Source: AK7set8GXyEM4CRSIgCbzRArdMQ+zGDVVEy5XnEYbx1qDJvibWRZRWOpBxp/ZDRuR3p9qJS6Eew4 X-Received: by 2002:a17:906:9bd8:b0:879:ab3:93d1 with SMTP id de24-20020a1709069bd800b008790ab393d1mr10308084ejc.4.1679589323887; Thu, 23 Mar 2023 09:35:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679589323; cv=none; d=google.com; s=arc-20160816; b=TfXLV3srd2pl3P5d6tdGlcBuG7F25yoQAYg8LkJgAwRg1mMwvUWaZOJ6gHusx27Ebj b99V2LETBbMCerZLZdjAkH7nJUtXV8CQntMRA6H7hA19gKO4OGHx6+TW+V9NFOjfQEkD 2lJrWQRlWGrIdo4z7pzEaQLgT4r+xDQzPWSlk2yq9ryKGMOsQa2ruODQqVFDUUO6zFhd kvCzXNC6eOc5DgyHM+190D+vQU80rsbbiqvVGk0ZEBJ4LHEk1gUtsnaS+PDal+Lr6wyF 3S3hlhW4WFl2b9APtSSGySTzqfrjyGQriI+oGLGBHDx43QekOaPR1hvX9PmF4YtYnx/m Jm+w== 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=Re7Ul0ocDttfAu2MIei1cNhSsXPUW8eyd1Dmg4NOWNY=; b=RrnkWJCHImTCS+WGWL7F+SLDKT/dtHl5KyvolrwNv6gOy2vdKs6I7GM5iwhkoHZ+rz X5K+6e2nmrxXCoazAT4WAPcuU8y/oHEclzdFZrV4Uu+lRXCFhmfLkfqc49VfB6j/GiQF CLcHyhfPvu61Pvt9LgBDWLY+2SjFy0Y31BrAccPzs/3g1jEr5NMs+evjN3ORPPJ7/A/k pdRTBQtu33yHXsc9W7KjQVkK/hnd2wFzXrFZTW8IzkXvelztmmKUPkV3oJ/dTcPCUQ+e tBuDLAPC5b1S6ppBtIQqbf+MTCpXVNfMnP2NZAu+UU+M/NxQTFc6dPMwN6CsOZTClUJ7 OjYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ta8VBHiR; 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 io4-20020a17090780c400b0093ba70535e8si4545270ejc.767.2023.03.23.09.34.59; Thu, 23 Mar 2023 09:35:23 -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=ta8VBHiR; 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 S232191AbjCWQaT (ORCPT + 99 others); Thu, 23 Mar 2023 12:30:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232316AbjCWQ3y (ORCPT ); Thu, 23 Mar 2023 12:29:54 -0400 Received: from mail-yb1-xb2d.google.com (mail-yb1-xb2d.google.com [IPv6:2607:f8b0:4864:20::b2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDD6426CCE for ; Thu, 23 Mar 2023 09:29:48 -0700 (PDT) Received: by mail-yb1-xb2d.google.com with SMTP id e71so25581317ybc.0 for ; Thu, 23 Mar 2023 09:29:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679588988; 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=Re7Ul0ocDttfAu2MIei1cNhSsXPUW8eyd1Dmg4NOWNY=; b=ta8VBHiRyoygVRQCyQdLSUWlxj/b6S8E/YOXEjvQvBe+KnMbrmIzc8Q5T/2Db4c1fC POV8HV4H5T1K63dxkgXi76qwKMFVbmhC3hacNk/nBSEksUMJvjMclFKx/lyBuATImAeN t0jIOpB7aVfUPX9uuC+89PXHxfqs9Rlh6eGrUGdRduuyJM2hunicNCOFkMsNdRNsIeBX kUBCxLSaYyc/lkmVfJQ2KnM/BZn4rW5AYN+5TWngULmqNlS57bA2WWMwgy45bh9ZMLDJ bs/OzYmIvgTWjQbeZrl2GwvvIHs4BJaPX3DoANTZtPPHl/zJZxVOARVxA1wMMpj6hcm0 EzaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679588988; 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=Re7Ul0ocDttfAu2MIei1cNhSsXPUW8eyd1Dmg4NOWNY=; b=GUC5syhO3AhDDrQDGdHhpiGqpTTJpnxzISNPcnZBNSqQheau2Y/5H8ptFmoyzanASR /vT/ozzNCMeFwXlwuULEpEjiNt9plCyX8n7Rh6OSlU1gv88C3GT5FcwyNBWd4COcrEe1 zFetyVbqQ9dXtc2g7l9YGCcr6EqZqaOllqDpvcSyAL+qdTGttLMiSH6F9Dvc86mM5HK1 5kA/r2Q1lO/5gTuMFWmJKCJyej/h3h8vEZF4WszsqdRihf2bPIzluWkj/HJBzCVwsNKB cWJ6TPKIRFnN/XW8DXpk4RHzpon8ZAOcXaHzKdv00D+bkPWTZd9WsmDzyPzBBFvx94un qSow== X-Gm-Message-State: AAQBX9eXWrwD5t0j+ZuK0a0Eh6V7ZcrudAj5bTrsCmUtumu4J9FEbcqC rJCKUUbbSis8h3Y4AJPyem4/PvQ5hn0b2nDqJXGBrQ== X-Received: by 2002:a05:6902:100d:b0:b6d:fc53:c5c0 with SMTP id w13-20020a056902100d00b00b6dfc53c5c0mr2573188ybt.1.1679588988014; Thu, 23 Mar 2023 09:29:48 -0700 (PDT) MIME-Version: 1.0 References: <20230323040037.2389095-1-yosryahmed@google.com> <20230323040037.2389095-2-yosryahmed@google.com> In-Reply-To: From: Shakeel Butt Date: Thu, 23 Mar 2023 09:29:36 -0700 Message-ID: Subject: Re: [RFC PATCH 1/7] cgroup: rstat: only disable interrupts for the percpu lock To: Yosry Ahmed 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: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 path: > > > > > > > > memcg_check_events()->mem_cgroup_threshold()->mem_cgroup_us= age(). I > > > > > > > > added the protection against flushing in an interrupt conte= xt for > > > > > > > > future callers as well, as it may cause a deadlock if we do= n'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() intere= sted in root > > > > > > > > > memcg usage? Why not ignore root memcg in mem_cgroup_thre= shold() ? > > > > > > > > > > > > > > > > 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 i= s ill defined. > > > > > > > > > > > > Right, but I think this would be orthogonal to this patch serie= s. > > > > > > > > > > > > > > > > I don't think we can make cgroup_rstat_lock a non-irq-disabling l= ock > > > > > without either breaking a link between mem_cgroup_threshold and > > > > > cgroup_rstat_lock or make mem_cgroup_threshold work without disab= ling > > > > > irqs. > > > > > > > > > > So, this patch can not be applied before either of those two task= s 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 on= ly > > > > 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 take= n > > > 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. > > 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_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. > > 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 userspace. > This should be an easy workaround though. This is a cgroup v1 behavior and to me it is totally reasonable to get 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.