Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1292925rdb; Wed, 24 Jan 2024 10:23:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IEwFa+aQ5uMFqKlD2Qdy8iArXh8x94WGzRBruMTbHAQp8Tf4JLetdU5/mVBpANdY+XXTIiV X-Received: by 2002:a92:d78c:0:b0:361:acb4:3f85 with SMTP id d12-20020a92d78c000000b00361acb43f85mr1920264iln.63.1706120599701; Wed, 24 Jan 2024 10:23:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706120599; cv=pass; d=google.com; s=arc-20160816; b=q/IlBlwLkLmsrmJv3eKTdX0O44J3pr5vBIHSt4D6ZkS9olq5OQ6POrV6bm7GKcTNpw 3glJcbYrcaalMEm3mxdF6RPEWVQPHtGPSbVBIwxqCb3dBZa1H2suBDUryITqwY4g+CSA 3kBeUlaT2YLbi/AdgugPdnCtXfluKBjfn4kiqoD5mMMlQN7iRvxnDJ/QegcsWvxPWicD mc4LATjd1ijTId6TXyLj7jecfz3CK1CtBMPGQgRHy/kvZ9va68moycUzgJInc/EYVcTS yMF/JpGAFpV0TSPu96s0/oROcFE4ZbYa3tttL8wWUI/4TksF1erCu+dlxdHVNT3LuOe9 3J3g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=yJ/BKkfpXJzgeyNiHaKMu/CgCnQKf/dgognXWBGDJRs=; fh=epZo8bRdGtWPf6/PnWqb4X6/OYqK+3w7Tnhho4gBvaw=; b=Jju0S+ZbcuzIgYnTdc2fTtcNohxMtLAgnVcIjFpO6iRCTnXXqslr6UPDGkhIdEL/W3 4r/Gfmf/N4708VoPZ1RIBTq3gx1NxORUuB8hywrWh4Wt/Wc7KMVQzqfOIGv1fa5Wk7W0 4dvEAt4ftsogsFxXElcRY6IhzO99XUEjq8MrC+nttGkNvDQyt1KZ062yuaFqvyQvzWJx XpfB7ptA/eyvdlr1nAXoB5N7+/RHEb86DlfoON2oQuSto6EolPf2WiBOTpvvmZjNxkWg 0vwBt2FFCXTmShqTBeuktli171Q2AHPujP4CUAR35GFIoR5HBBOiMITfFb0qifgHFN1O nosA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lpF5jWzx; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-37447-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37447-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id f27-20020a631f1b000000b005cdc7c487f1si11992032pgf.427.2024.01.24.10.23.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 10:23:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-37447-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lpF5jWzx; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-37447-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37447-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 85AA9B27FFB for ; Wed, 24 Jan 2024 17:38:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D62781208; Wed, 24 Jan 2024 17:38:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lpF5jWzx" Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DB1D7F7E5 for ; Wed, 24 Jan 2024 17:38:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706117912; cv=none; b=IynD5kJ38SX/jP82RXzupSKy0436RUrWiB6y5bx7x/y45OKPRfDCkwzY8OfnMW8QzB1Ad2bqG4MwiJ4m2tCuubJRbwWV0E1x+Z7+j7hYJxw+LFA1Ms7oV9o1arM0VVQYRGU5ko71fooLEoExcLLDN/cNZtiDq5BEJkPxg+gQqvE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706117912; c=relaxed/simple; bh=yJ/BKkfpXJzgeyNiHaKMu/CgCnQKf/dgognXWBGDJRs=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=G6uCMsUaRr2xG5y5zs0VSTR0ONmv5Abt3H/R40J9aSf2r4UJJvgi8rXzXNrN8U0+E6UgnswOVMBUnkV64IisW33zk3I1iRSo342n5wT0nfsGffIUzzqywUveIvL6SYL12MjnJhrKG7QXQYB9D0BI8usw+44AqD0hvzItbHq7pJg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=lpF5jWzx; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1d72d240c69so168965ad.1 for ; Wed, 24 Jan 2024 09:38:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706117910; x=1706722710; 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=yJ/BKkfpXJzgeyNiHaKMu/CgCnQKf/dgognXWBGDJRs=; b=lpF5jWzxTcpFc2COD2Q4RapamyszvZxdwd0bsZg/HrnlfOdVIDIffTbWB1o/BIWmvr MN5A6AvmJbCDvWMWV8TOtNI2rCvv7Sw3NbyhBQ5AxTS8UrdTpn1SJvuLth28j8tHDSRI G2fwQg2QoYI5Z7rICBx6JLCP/Oflqet6iVbSsuoNG/fram0WXqnyXOd50B4XBlHeFv/8 Al0APVU4jlXMtGMAM+9KbFA9ajOQi1Y9e10C/gfsyhFF0PwOZAnJ4NWRNQ4otvcdgHj6 ypI22zdRz7nDlGm2v6R+0ntIkCqwtYHwVql6M8MHQYOypPoBmJoowJw/gKycs/FjI7B4 HvrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706117910; x=1706722710; 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=yJ/BKkfpXJzgeyNiHaKMu/CgCnQKf/dgognXWBGDJRs=; b=IAIMNsz48ea65mmWL4KZIIjurxYp51cTQcJtgygV6Z48RM/j2UKZt634AWhEv5u7UY MwenV0aJCvgf/N8gcDWNLHeJsi1phUBJu5Z6338rZtBwMWNxt5QXY+mw9kPD9XFl16GL rWiSFYmlW+usv3dw9UoaOrNZrMXjpaSbiC6QGSa19aK//MEvWjwS5WyFXcbtGHEELe9M V4N/WS/ISxsGYL6xTQ+I5RIhzR+HfuviYDHZTdlccZ/K7j9JwCOVtNncOMF8qfocRFXA Am2OSpGxL/C4Swco3YEDNoUTCOufBFN0tojO/tzXBYtYtqaACul/f9UYGXjm0JpOBvIH GSSQ== X-Gm-Message-State: AOJu0Yy249N7eC0Qf+43ScYF7tcOgcfX3GpXwEqmknQtsVRgsmsN2LmP IdFkOcQlXC3k2aqIUI6yCfV+pVZ0mo3mxUPMPtDl1yLd7wXJcsmKqDc/wH8nNIiuY0Mm9RJngXz hcNBUXza6dBZ+HJAet/aFJuWgAbCen8bh4hA3 X-Received: by 2002:a17:902:db04:b0:1d7:246c:2fc1 with SMTP id m4-20020a170902db0400b001d7246c2fc1mr216102plx.26.1706117910167; Wed, 24 Jan 2024 09:38:30 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240124100023.660032-1-yosryahmed@google.com> In-Reply-To: <20240124100023.660032-1-yosryahmed@google.com> From: Shakeel Butt Date: Wed, 24 Jan 2024 09:38:18 -0800 Message-ID: Subject: Re: [PATCH] mm: memcg: optimize parent iteration in memcg_rstat_updated() To: Yosry Ahmed Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel test robot Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jan 24, 2024 at 2:00=E2=80=AFAM Yosry Ahmed = wrote: > > In memcg_rstat_updated(), we iterate the memcg being updated and its > parents to update memcg->vmstats_percpu->stats_updates in the fast path > (i.e. no atomic updates). According to my math, this is 3 memory loads > (and potentially 3 cache misses) per memcg: > - Load the address of memcg->vmstats_percpu. > - Load vmstats_percpu->stats_updates (based on some percpu calculation). > - Load the address of the parent memcg. > > Avoid most of the cache misses by caching a pointer from each struct > memcg_vmstats_percpu to its parent on the corresponding CPU. In this > case, for the first memcg we have 2 memory loads (same as above): > - Load the address of memcg->vmstats_percpu. > - Load vmstats_percpu->stats_updates (based on some percpu calculation). > > Then for each additional memcg, we need a single load to get the > parent's stats_updates directly. This reduces the number of loads from > O(3N) to O(2+N) -- where N is the number of memcgs we need to iterate. > > Additionally, stash a pointer to memcg->vmstats in each struct > memcg_vmstats_percpu such that we can access the atomic counter that all > CPUs fold into, memcg->vmstats->stats_updates. > memcg_should_flush_stats() is changed to memcg_vmstats_needs_flush() to > accept a struct memcg_vmstats pointer accordingly. > > In struct memcg_vmstats_percpu, make sure both pointers together with > stats_updates live on the same cacheline. Finally, update > mem_cgroup_alloc() to take in a parent pointer and initialize the new > cache pointers on each CPU. The percpu loop in mem_cgroup_alloc() may > look concerning, but there are multiple similar loops in the cgroup > creation path (e.g. cgroup_rstat_init()), most of which are hidden > within alloc_percpu(). > > According to Oliver's testing [1], this fixes multiple 30-38% > regressions in vm-scalability, will-it-scale-tlb_flush2, and > will-it-scale-fallocate1. This comes at a cost of 2 more pointers per > CPU (<2KB on a machine with 128 CPUs). > > [1] https://lore.kernel.org/lkml/ZbDJsfsZt2ITyo61@xsang-OptiPlex-9020/ > > Fixes: 8d59d2214c23 ("mm: memcg: make stats flushing threshold per-memcg"= ) > Tested-by: kernel test robot > Reported-by: kernel test robot > Closes: https://lore.kernel.org/oe-lkp/202401221624.cb53a8ca-oliver.sang@= intel.com > Signed-off-by: Yosry Ahmed > --- Nice work. Acked-by: Shakeel Butt