Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp3049985rdb; Mon, 4 Dec 2023 15:31:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IHvMQhxfrVVhU8tdJnEPJibycRdnvoYf7gG7BzhL4UT4Ui7u2XX5ZPiVNmdzNnKtK89BLzp X-Received: by 2002:a05:6a00:3390:b0:6c3:4bf2:7486 with SMTP id cm16-20020a056a00339000b006c34bf27486mr362034pfb.7.1701732704541; Mon, 04 Dec 2023 15:31:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701732704; cv=none; d=google.com; s=arc-20160816; b=kgFaNSzsQwilrU0G06Ff78acPnHJ/xeAehtgyRV4MPBA4ISkflTaKRoFtcZror3MAj lCQOT5+6DBVEZkBBAGKmy+aBc7VU9x925QxMxkxfHKudi3gxzZ0fqWxGM8MaQoKUC4aD tJtAUewGnFAlVMca/D+08u4SzEoIORLd9l+ohOFq5og7aI/8Nr88DBk+MN9X7O0mO2vA 9C8z7XwC9zzzwykcncRB3F0Q3STG+n4DHgOdyWTZzzksEJaDmBpfo9cfnYV9j8hunuDv O6rTWzvj1Bw5DdZAapYfx1+/Uv4N8CLmNpYRllcerYCFCsoATHxsvR0b/4mZYPA1z5PR J2/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=0XB2Hd768QRSmXeqfgdQPhQTeGNeuuAaGIf9egd5wTU=; fh=vIFA+LS5j7f8Owjley4jpfM9SmrP+nXyqtjlrxuuMlQ=; b=JBbvt8IMv361LPQ+SHYQQnD/hozFGj+9piN84QJAumJatS+BS3xkAZFmgbPy9qLGTt 5emeYWx4f5PyF0nFGMi14lOvBKZo75FeV8hXONT/6sN+bfHpDLegkSUqOy7ig85fwnWg kyUHwNzo268T656JgQdlUfWQqKv2VAxvuPNVvbsuGu6eXqHLNEo/Zv0cuJbyyqMKCjFH UbNp48y0vEbokrsW6aKkHk2ZAlqjcc6/rD/Wetxfk6M/pLvLCFrgtrYVzHiwroyJHhIr PdWFbD+gM2dFTHioNTUifrkAxqWjvOs1tjFGkTYNSqA2Z57ip3EbawpwJV36w0U/ag5g se2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=BFGkfpiJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id u22-20020a63d356000000b005c5e24c7e8csi8869133pgi.414.2023.12.04.15.31.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 15:31:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=BFGkfpiJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id E581F80A134D; Mon, 4 Dec 2023 15:31:41 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229847AbjLDXbY (ORCPT + 99 others); Mon, 4 Dec 2023 18:31:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229668AbjLDXbX (ORCPT ); Mon, 4 Dec 2023 18:31:23 -0500 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2111CFA for ; Mon, 4 Dec 2023 15:31:30 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1d0b40bb704so64375ad.0 for ; Mon, 04 Dec 2023 15:31:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701732689; x=1702337489; darn=vger.kernel.org; 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=0XB2Hd768QRSmXeqfgdQPhQTeGNeuuAaGIf9egd5wTU=; b=BFGkfpiJ9zJGnHROsRoC74FNdES7hyLgSJvy7KIX8OBPNsAJow9Ugtopo5U8Y5pxep +Bf2TzbWbbd86rMWVRfZeYT9iPT8Vv5sAqhCLMP9YgHAjv66zcO8ibjs+Fn6MWb9oM6Y uLW19e6b6mBmhN9twGZ2KroFL6qHRMAIk0VMKJBIqqkTzzrZEAbPc+EsuaJABSBH81zV Y5yeNSrhDIRaoDiGxpJ5fvxnTpdQKDNQYX6qMzZuBbkFMvkBZaXQN+9OuQkKj7Vzbopr 2NrZo9iX+MWz3fsQNFDmJIUKpsY/mUjBTQZ6lfoyWIY12YenGBpokjrJ+kegc30BfNce ipOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701732689; x=1702337489; 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=0XB2Hd768QRSmXeqfgdQPhQTeGNeuuAaGIf9egd5wTU=; b=bJrYQZDVgYQ3Qq5jzqBnl1N0wxq8NwfnlQJoSn/4la/VhGOycjA06yNo+WXR2vIfHq qVR2DP4ic6PuEFuKZ2gKleF8EK4Z3rDGz4sO0eBjjBt4qwDzmbfIWEAFXQbNm16hBKpV fzI45Fu8/+adruyEBJB9bkp1D7b9dec/V0LrNnoOF3DK7hbj8+1PMtnJYFDU5yYD78dT USIM83oEE2iyk8x5urblin17C0yvlgbJOgg8jcORgbYNgydtYsLhV4B1EQUIKB858IJB 3xFMwzwF4CKjhqjeAURF8uUdaVeMdcs5mIuf8i+GiHhFLAmiY+Sbx1QPgUZdBKSg5tyK 5SFw== X-Gm-Message-State: AOJu0YwZstDSd3ATnXh1KjA4jc1DQ+aLlC/AcO3O35WLkaL9S0YT4yUg 0Z1tSS1uwucTkUaHFVtx8WvswmrJT7NHh4OFqOfnTg== X-Received: by 2002:a17:903:3247:b0:1cf:e100:a99c with SMTP id ji7-20020a170903324700b001cfe100a99cmr723356plb.6.1701732689357; Mon, 04 Dec 2023 15:31:29 -0800 (PST) MIME-Version: 1.0 References: <20231129032154.3710765-1-yosryahmed@google.com> <20231129032154.3710765-6-yosryahmed@google.com> <20231202083129.3pmds2cddy765szr@google.com> In-Reply-To: From: Shakeel Butt Date: Mon, 4 Dec 2023 15:31:17 -0800 Message-ID: Subject: Re: [mm-unstable v4 5/5] mm: memcg: restore subtree stats flushing To: Yosry Ahmed Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Ivan Babrou , Tejun Heo , =?UTF-8?Q?Michal_Koutn=C3=BD?= , Waiman Long , kernel-team@cloudflare.com, Wei Xu , Greg Thelen , Domenico Cerasuolo , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 04 Dec 2023 15:31:42 -0800 (PST) On Mon, Dec 4, 2023 at 1:38=E2=80=AFPM Yosry Ahmed = wrote: > > On Mon, Dec 4, 2023 at 12:12=E2=80=AFPM Yosry Ahmed wrote: > > > > On Sat, Dec 2, 2023 at 12:31=E2=80=AFAM Shakeel Butt wrote: > > > > > > On Wed, Nov 29, 2023 at 03:21:53AM +0000, Yosry Ahmed wrote: > > > [...] > > > > +void mem_cgroup_flush_stats(struct mem_cgroup *memcg) > > > > { > > > > - if (memcg_should_flush_stats(root_mem_cgroup)) > > > > - do_flush_stats(); > > > > + static DEFINE_MUTEX(memcg_stats_flush_mutex); > > > > + > > > > + if (mem_cgroup_disabled()) > > > > + return; > > > > + > > > > + if (!memcg) > > > > + memcg =3D root_mem_cgroup; > > > > + > > > > + if (memcg_should_flush_stats(memcg)) { > > > > + mutex_lock(&memcg_stats_flush_mutex); > > > > > > What's the point of this mutex now? What is it providing? I understan= d > > > we can not try_lock here due to targeted flushing. Why not just let t= he > > > global rstat serialize the flushes? Actually this mutex can cause > > > latency hiccups as the mutex owner can get resched during flush and t= hen > > > no one can flush for a potentially long time. > > > > I was hoping this was clear from the commit message and code comments, > > but apparently I was wrong, sorry. Let me give more context. > > > > In previous versions and/or series, the mutex was only used with > > flushes from userspace to guard in-kernel flushers against high > > contention from userspace. Later on, I kept the mutex for all memcg > > flushers for the following reasons: > > > > (a) Allow waiters to sleep: > > Unlike other flushers, the memcg flushing path can see a lot of > > concurrency. The mutex avoids having a lot of CPUs spinning (e.g. > > concurrent reclaimers) by allowing waiters to sleep. > > > > (b) Check the threshold under lock but before calling cgroup_rstat_flus= h(): > > The calls to cgroup_rstat_flush() are not very cheap even if there's > > nothing to flush, as we still need to iterate all CPUs. If flushers > > contend directly on the rstat lock, overlapping flushes will > > unnecessarily do the percpu iteration once they hold the lock. With > > the mutex, they will check the threshold again once they hold the > > mutex. > > > > (c) Protect non-memcg flushers from contention from memcg flushers. > > This is not as strong of an argument as protecting in-kernel flushers > > from userspace flushers. > > > > There has been discussions before about changing the rstat lock itself > > to be a mutex, which would resolve (a), but there are concerns about > > priority inversions if a low priority task holds the mutex and gets > > preempted, as well as the amount of time the rstat lock holder keeps > > the lock for: > > https://lore.kernel.org/lkml/ZO48h7c9qwQxEPPA@slm.duckdns.org/ > > > > I agree about possible hiccups due to the inner lock being dropped > > while the mutex is held. Running a synthetic test with high > > concurrency between reclaimers (in-kernel flushers) and stats readers > > show no material performance difference with or without the mutex. > > Maybe things cancel out, or don't really matter in practice. > > > > I would prefer to keep the current code as I think (a) and (b) could > > cause problems in the future, and the current form of the code (with > > the mutex) has already seen mileage with production workloads. > > Correction: The priority inversion is possible on the memcg side due > to the mutex in this patch. Also, for point (a), the spinners will > eventually sleep once they hold the lock and hit the first CPU > boundary -- because of the lock dropping and cond_resched(). So > eventually, all spinners should be able to sleep, although it will be > a while until they do. With the mutex, they all sleep from the > beginning. Point (b) still holds though. > > I am slightly inclined to keep the mutex but I can send a small fixlet > to remove it if others think otherwise. > > Shakeel, Wei, any preferences? My preference is to avoid the issue we know we see in production alot i.e. priority inversion. In future if you see issues with spinning then you can come up with the lockless flush mechanism at that time.