Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp704500lql; Mon, 11 Mar 2024 15:09:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW5n/o2cwIXR1JEuajmArOsJdxTorBSNjU2bZTAVfG9DiJ2w0urJhyO+LxiXnnTbmjE5ddkziM4WOcMSqkltZkrcX1eSkwwJ0BPiQ9MRQ== X-Google-Smtp-Source: AGHT+IFnwE2CJYkIfh71xlDDk1eQ48ORMBcgsUQ7yEPcMhDC0pe9murOqO9pr7OBVH9xzoodOhjS X-Received: by 2002:a05:6a20:12c6:b0:1a1:87df:c6b9 with SMTP id v6-20020a056a2012c600b001a187dfc6b9mr9657906pzg.1.1710194987081; Mon, 11 Mar 2024 15:09:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710194987; cv=pass; d=google.com; s=arc-20160816; b=itD2rXCesPeN9Ah/f/nqP+UOhYUoS2DouyPvZae9AVYo0lN840TQxpcm987Yp3uC0T 4T3CBlcThopsc15DMCCgs9Izu0HHJHlXLZJidoIljhStVGxAH3DJE2xIIVViPKtz3fkm QlJBCAa6Ims2GJwBdJIEVaNOYB+FpuHuyzNQsIcDxVwAQG9ozoI9GcWhvGwDmI1deegx YfxCH38Ff0XSG32o64z91Tiok5CW4O+olMc1ybpVWYiY4rJlWckJ4komkkbdMDF+E4As +IY2kU7cZ+IuGVInKBdTu8aRbbQOVp9hQ7ejZ/MyzSXgY3AogKQRJXx/obUAvVsbFa1u IjrQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=A5gxKcBG9B2NwGIgck91cK0cCgZ6XAorrAmw0ICvRr8=; fh=uQGlHbdtOAo8iZXC0ev5UK7Rr+uMlmBkH9P2nI54j5s=; b=LHMRtnOBCmROIQEiABrH0dsFqyuAuaHWZcs9dQKVcQEXIJvByanIZSAKBIKWW5J6LW jY7QD9BE2XP0cuWvh2uSORzZFTr4ZgLF2scmTg2Zya6bq20XhikW5zL1vId/+cVknWH2 p1SvI2bySCPttjQAhnVTxfFY2QLV1rER9yZ5r3wBQVxIWcALJXakMGKR4OsC6WrgPH8w B0C42pLXwOlpj6y8E1XTl/KjxTXFeURE3Vc66C4nVaWabQVGYhu6ZBc89+lzjUWImIXi tYZrfionfShSY/tzTzT6Q8jORklt/WEhVYyLJW1U0JORPCLKFFGmrdClO0yakWZRrwWK q0Iw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qhk+FpXU; arc=pass (i=1 spf=pass spfdomain=flex--yosryahmed.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-99594-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99594-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id t20-20020a63eb14000000b005cdf488ba0asi5601560pgh.746.2024.03.11.15.09.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 15:09:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-99594-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qhk+FpXU; arc=pass (i=1 spf=pass spfdomain=flex--yosryahmed.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-99594-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99594-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 900B2281FEC for ; Mon, 11 Mar 2024 22:09:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C28B057329; Mon, 11 Mar 2024 22:09:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qhk+FpXU" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 813A557314 for ; Mon, 11 Mar 2024 22:09:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710194980; cv=none; b=c0LqRGAjEdCEd8FF4ABwUm7TAwggkrSWmXB34y/I0VFj0oKEgy/8ktwSL28JlaBWrskhOBeHy3z+E9Rb3JRYJ8hLrRL75nKtyp8cuBlP9UjBUwnIx+XFgfR/+17iq0NT/JEx9CEk9lhKX+zWDRp8/76SWZDzvYdTsR6i0m0KJJc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710194980; c=relaxed/simple; bh=KfCYvd4kRRmAaHRjwtYVDj7+0KuZFNGtYFFcTCg+INE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UeiOeOdXPzbzPOcjTsY4U2I6hKfnT5ZpOdC53m+FMLg29b3uexFWUrZEmMFggXvLRKtMPY0vm3XjuwxYBO44VNddtsqufuxWzQvyJejEDVRDXCn8fLgwFVdE/N1/E/uxmV7NH1T400wEChn6HYt7rn7xmGq7isNjplVcyYyK9Z4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yosryahmed.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qhk+FpXU; arc=none smtp.client-ip=209.85.128.201 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=flex--yosryahmed.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-609d95434daso70291337b3.1 for ; Mon, 11 Mar 2024 15:09:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710194977; x=1710799777; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=A5gxKcBG9B2NwGIgck91cK0cCgZ6XAorrAmw0ICvRr8=; b=qhk+FpXU72FDx1jUxM2oBAIzqldzJhw2RxmCYiAr5wHCOVJqCzIDSnbU2+LXRV2OhN T4dFv+GX0d29KJrcZd3cItAcCfBDTi6b62PifZL7tkDy98LI4acPebI4Zuzd7rmVvElJ YTAFVrjNZnbDAyyYkX27VZqnZ49cQQgCIk+V6Q4CzL/FhV6BvE8GOS9vgRHaR+aUxMNU BrOP3sh5TDg9yYjXyxD1pkhwnP/QGO8jDodB+xYjn7K+Q3kiMNPViuxwOd0VeFUsh5UD lTjS/ykAKdn6WEYw4864ezMsdzfqLxgiTyhHP/+3jiilZbOBffHmvvwQYOxQvCpcFQss 8a8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710194977; x=1710799777; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=A5gxKcBG9B2NwGIgck91cK0cCgZ6XAorrAmw0ICvRr8=; b=wkP662NcO3EQMOgcW6NiVnEnsCFaTpMliVC8K3svKUE7Cd0LKikLe3eptzJJ7+faYf wVw0nNaf5W5b7mGvLD3prGHldUA7nvGpQvMJ5v7OcQS1BDAsu19g1cWZBQW2/U779gps KVY31y77i+Tc9xp/r9TjRSMGd7Nw0m1dD6rKciwJe6tppGza7vNSuU34KSlsaruE4/JI PzKGRyiOOfZoYFoNKQaxW/VercqbSLXPZTbH/EZR6B66jx6fznF885hLoEoD+m1YufW5 vOPY6D8QqVQQaBq2yWO6nZbudx0CodE4YW1E9UAY8UGytnD6w3TGvAcKtF3FAG+odY9z tXZQ== X-Forwarded-Encrypted: i=1; AJvYcCVf5pucOO4pvkQT68JwJf0DLjdZjwctrN1Rbn+8TP1O8q+MdtyiOAAy9N9KJvKWSA1UAokQGCPIRmPG9r5PJmsbr9LTSyoTpQ8w+uP8 X-Gm-Message-State: AOJu0YxUSOa0y05BKN+2Qqja4fLnXS8cWXpfOzGI1NZNRcFwDmCJRM9P 1+kZS4ZtqZI8N2Rn03N3COsg2/B2ciNAY6bHlDs7TbUwiABLvwXFpHQGrb4xaRcQSrcGiUKetE5 WbsLZ1Myym5wcRdkzJw== X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:110a:b0:dcc:50ca:e153 with SMTP id o10-20020a056902110a00b00dcc50cae153mr2168318ybu.7.1710194977555; Mon, 11 Mar 2024 15:09:37 -0700 (PDT) Date: Mon, 11 Mar 2024 22:09:35 +0000 In-Reply-To: <20240311161214.1145168-1-hannes@cmpxchg.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240311161214.1145168-1-hannes@cmpxchg.org> Message-ID: Subject: Re: [PATCH 1/2] mm: zswap: optimize zswap pool size tracking From: Yosry Ahmed To: Johannes Weiner Cc: Andrew Morton , Nhat Pham , Chengming Zhou , linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" On Mon, Mar 11, 2024 at 12:12:13PM -0400, Johannes Weiner wrote: > Profiling the munmap() of a zswapped memory region shows 50%(!) of the > total cycles currently going into updating the zswap_pool_total_size. Yikes. I have always hated that size update scheme FWIW. I have also wondered whether it makes sense to just maintain the number of pages in zswap as an atomic, like zswap_stored_pages. I guess your proposed scheme is even cheaper for the load/invalidate paths because we do nothing at all. It could be an option if the aggregation in other paths ever becomes a problem, but we would need to make sure it doesn't regress the load/invalidate paths. Just sharing some thoughts. > > There are three consumers of this counter: > - store, to enforce the globally configured pool limit > - meminfo & debugfs, to report the size to the user > - shrink, to determine the batch size for each cycle > > Instead of aggregating everytime an entry enters or exits the zswap > pool, aggregate the value from the zpools on-demand: > > - Stores aggregate the counter anyway upon success. Aggregating to > check the limit instead is the same amount of work. > > - Meminfo & debugfs might benefit somewhat from a pre-aggregated > counter, but aren't exactly hotpaths. > > - Shrinking can aggregate once for every cycle instead of doing it for > every freed entry. As the shrinker might work on tens or hundreds of > objects per scan cycle, this is a large reduction in aggregations. > > The paths that benefit dramatically are swapin, swapoff, and > unmaps. There could be millions of pages being processed until > somebody asks for the pool size again. This eliminates the pool size > updates from those paths entirely. This looks like a big win, thanks! I wonder if you have any numbers of perf profiles to share. That would be nice to have, but I think the benefit is clear regardless. I also like the implicit cleanup when we switch to maintaining the number of pages rather than bytes. The code looks much better with all the shifts and divisions gone :) I have a couple of comments below. With them addressed, feel free to add: Acked-by: Yosry Ahmed [..] > @@ -1385,6 +1365,10 @@ static void shrink_worker(struct work_struct *w) > { > struct mem_cgroup *memcg; > int ret, failures = 0; > + unsigned long thr; > + > + /* Reclaim down to the accept threshold */ > + thr = zswap_max_pages() * zswap_accept_thr_percent / 100; This calculation is repeated twice, so I'd rather keep a helper for it as an alternative to zswap_can_accept(). Perhaps zswap_threshold_page() or zswap_acceptance_pages()? > > /* global reclaim will select cgroup in a round-robin fashion. */ > do { > @@ -1432,10 +1416,9 @@ static void shrink_worker(struct work_struct *w) > break; > if (ret && ++failures == MAX_RECLAIM_RETRIES) > break; > - > resched: > cond_resched(); > - } while (!zswap_can_accept()); > + } while (zswap_total_pages() > thr); > } [..] > @@ -1711,6 +1700,13 @@ void zswap_swapoff(int type) > > static struct dentry *zswap_debugfs_root; > > +static int debugfs_get_total_size(void *data, u64 *val) > +{ > + *val = zswap_total_pages() * PAGE_SIZE; > + return 0; > +} > +DEFINE_DEBUGFS_ATTRIBUTE(total_size_fops, debugfs_get_total_size, NULL, "%llu"); I think we are missing a newline here to maintain the current format (i.e "%llu\n"). > + > static int zswap_debugfs_init(void) > { > if (!debugfs_initialized()) > @@ -1732,8 +1728,8 @@ static int zswap_debugfs_init(void) > zswap_debugfs_root, &zswap_reject_compress_poor); > debugfs_create_u64("written_back_pages", 0444, > zswap_debugfs_root, &zswap_written_back_pages); > - debugfs_create_u64("pool_total_size", 0444, > - zswap_debugfs_root, &zswap_pool_total_size); > + debugfs_create_file("pool_total_size", 0444, > + zswap_debugfs_root, NULL, &total_size_fops); > debugfs_create_atomic_t("stored_pages", 0444, > zswap_debugfs_root, &zswap_stored_pages); > debugfs_create_atomic_t("same_filled_pages", 0444, > -- > 2.44.0 >