Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp256698lqs; Thu, 13 Jun 2024 09:12:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUNHpmsF4bGoa//GyqIW3wt7yLgAhmgUvKwoICLwyQs4tmhsS59jOmX7CqwEn2e/xBkx9dOF38a/dbQ1iS4ZWpuf6JCM6xuSKNlr1YyFA== X-Google-Smtp-Source: AGHT+IF8X9/jk7gMeke5OeG0JnZhHLyg9MeK5oKrauk5IlrSSd/8w48wVSSf7vMkuyWD59dU6vx0 X-Received: by 2002:a05:6a20:3949:b0:1b4:772d:288a with SMTP id adf61e73a8af0-1bae7eb284dmr386330637.23.1718295146260; Thu, 13 Jun 2024 09:12:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718295146; cv=pass; d=google.com; s=arc-20160816; b=TT7GQcPLhDNXqa7Gyw/it1Egv83GDr0fopiv/Nn/ZuFzjyynrC5g5H1ydZeZjW1Nza J2w5uMoO60qIEiA5WHwLOR3GyAwkoyXhuPTSqJlsG1f09UrWPWQiHAHXzF47Srs03KVt P3iPiK8D9H0QPvdJH2pOYoXmJ+XVwm9FJ2UYexZvGn4ALnQ38fnU+449QU4BAdL6SUmy LXa9EfrzuWeUXrXsE1thvTUs/XZjUPIJQyb0UP+jNKNyWOdirJTqANU1sncgRMETeGPA mK1TJgVxKtVJlJg4aVwL4sDFVKkP05pMUlo8DrSsCXzKrgfUkoulPvm+YyPB//PSgpzx pErQ== 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=yqHB7d/fQOBxu70Lq3/veRtB4jM3A8VBQDLaIwljpU0=; fh=9qrSXnDpz7PbSiURCVnnpBBdWPPTxScup0znZSqb2uw=; b=MuV6Fwi1FbCZo0Wa3wS4pfIXDBEEnO4RU1JiPiC3FBt0VdRCSVdpWI4W334dmB3VDe XOsIhUj64jDozbxhSyUsgA1sK+CC88x9mnbdvTpMyd6l8r3UFDYpA1J7tOfaCfxVIADE JHagPxKJYdPZ94rg76vgLGdYhbnPUUC+cJNa0liq9bVMQI7bhnZ75lnSiT1/MJ74DP/c cT41SKxVm83NTw8mGdmQ1d+8TmXoqA/vqBGA0xR2+1/YhCb56locV8fozd7Cf5Q9nAfU mrG+OA3AvbV+vnfOnVr4iYuNL+G/qTbPwiTkAduug51p+nqX+SOTQwGQKAqnLCy4oovB m4UA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Sal+bMI+; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-213622-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-213622-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d2e1a72fcca58-705cc8f6ca9si1711858b3a.31.2024.06.13.09.12.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 09:12:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-213622-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=@gmail.com header.s=20230601 header.b=Sal+bMI+; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-213622-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-213622-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 BC011285095 for ; Thu, 13 Jun 2024 16:08:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7806612FB3C; Thu, 13 Jun 2024 16:08:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Sal+bMI+" Received: from mail-oo1-f50.google.com (mail-oo1-f50.google.com [209.85.161.50]) (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 46C38A23; Thu, 13 Jun 2024 16:08:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718294899; cv=none; b=hAhgjomdjjvsryakO5LpIz7iVaFhYF1KTzd2XJwaSQsze0hJulvf36zIi9wUGEoPO9Q8lR246M+7OLfRABNMphGBsT/copMxGif0nm4hxigzsTO0d8zJT2PoAWkkqSuX2grXbsDx3N8TPaId9hWtUd3Qr5gHaozGp5b60L9xrU8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718294899; c=relaxed/simple; bh=Gb0Fl6tht05rZTeCNYRhz/ufV7pO3QDjCFBjJjIDfXY=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=aGoVt7hQ0c3HTPeF9YvE0sMN6V0RKGBgogzB2pV/n3v0AYekYaeoa+Kwq8S+CyIIHhV8Rg1EouCU/2zDycl3cfFhIwv2jL/Fq49y+uWVCRrQA1pB3wV97FbK4RW0UgGYpA1jpyjTiO8a6HLo/YyUOFyaHl9HM42pBuJenIHQrEo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Sal+bMI+; arc=none smtp.client-ip=209.85.161.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oo1-f50.google.com with SMTP id 006d021491bc7-5bacd59e562so594600eaf.2; Thu, 13 Jun 2024 09:08:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718294897; x=1718899697; 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=yqHB7d/fQOBxu70Lq3/veRtB4jM3A8VBQDLaIwljpU0=; b=Sal+bMI+oqcbj22cFbgJ27e76P7IOh/ZsfF78ZHBhaTqz9FO8abhu9ywfeJVWqNMSd YGeKCNNZBaQHasyuyEMQH58WRa94wURmaBcQsbn0jIlC0WkxnWAZQWAy33NA8n8dcEph /m4Nd8swPTEb7HhbuYGNIxslHWYtUaMdt5uewKUhrJid+Dq0wyijlavH+vbb4JT0+uZy HAfmeC5KmSXkFrBi228eNWAfqhHJjd4pqIeIBsSFAI3NK/Pmpdt4KKIrqhc11X7CTKXH SkBNBzLLpB4Ur3Ol6I06g1kcyHlCG2J167IfP9Blwc+IvAIkDonc2V4Pivgqk4xt+jD5 wj2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718294897; x=1718899697; 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=yqHB7d/fQOBxu70Lq3/veRtB4jM3A8VBQDLaIwljpU0=; b=qmWrougaimEOK4HT6X8pD9yRse9PuvQkQ1fawOVrHGdCnMSECLfjDiM5/TsTvi85zS biKxUJlOwL9l4ZU8Lcoptpqr++jp8o4f5+osiWuPFUH8n2e8mNHaaID5Kpmchk65GfnO wfSgcCv9jIwqWxuP9LZ5bh7QFUIUgXEgGOvwJ34IFLDn8HOdTSErsGdK1Esbz58ftiaM mrMLMfV2CIeBuEYA4FlsV57jhaTKBgBVeqtPnfqeudDB4FT/jggOGBwJ9qGsV4WZhGbJ X3HKXxh5LgdMW19rBJVq29jLVIw/dHkqS8ACNGAwUZj7BKpca8ZOSiIad7xoAZfWLQqi j38A== X-Forwarded-Encrypted: i=1; AJvYcCXLo4NeU9sEHJCu9QeB5CC88XSZBmNuD1fmbtEPK8O0Cj9waFYb7Cg/g4ytM/UGILjcb1gDq763YR3DBibrf70gZjF6aMmSeOkX6ywUzoU2TO5FvVJ79FRIN9/3JeZkhzhSrQj75H3j X-Gm-Message-State: AOJu0YwfSOcYCdO8est08n0qByW3Qu2gFyDN+rUJZaNNkB9rXG240Dvp WVRt6a25pPWQeNlgh7BNTzJxw8ZU1poIwUsTUWUDCbjtaJwOjw7TraiLGNVETldKdI3ZMi9/ndx DC9P/0m3A66N+3rqeACI5fuk65cLbpRe2 X-Received: by 2002:a05:6358:60ca:b0:183:d2fa:ff5c with SMTP id e5c5f4694b2df-19fa9e3ed22mr29344055d.12.1718294896891; Thu, 13 Jun 2024 09:08:16 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240608155316.451600-1-flintglass@gmail.com> <20240608155316.451600-2-flintglass@gmail.com> In-Reply-To: <20240608155316.451600-2-flintglass@gmail.com> From: Nhat Pham Date: Thu, 13 Jun 2024 09:08:05 -0700 Message-ID: Subject: Re: [PATCH v1 1/3] mm: zswap: fix global shrinker memcg iteration To: Takero Funaki Cc: Johannes Weiner , Yosry Ahmed , Chengming Zhou , Jonathan Corbet , Andrew Morton , Domenico Cerasuolo , linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Jun 8, 2024 at 8:53=E2=80=AFAM Takero Funaki = wrote: > > This patch fixes an issue where the zswap global shrinker stopped > iterating through the memcg tree. > > The problem was that `shrink_worker()` would stop iterating when a memcg > was being offlined and restart from the tree root. Now, it properly > handles the offlining memcg and continues shrinking with the next memcg. > > This patch also modified handing of the lock for offlined memcg cleaner > to adapt the change in the iteration, and avoid negligibly rare skipping > of a memcg from shrink iteration. > Honestly, I think this version is even more complicated than v0 :) Hmmm how about this: do { spin_lock(&zswap_pools_lock); do { /* no offline caller has been called to memcg yet */ if (memcg =3D=3D zswap_next_shrink) { zswap_next_shrink =3D mem_cgroup_iter(NULL, zswap_next_shrink, = NULL); memcg =3D zswap_next_shrink; if (!memcg && ++failure > MAX_FAILURE) { spin_unlock(&zswap_pools_lock); return; } } while (!zswap_next_shrink || !mem_cgroup_tryget_online(zswap_next_shr= ink)) spin_unlock(&zswap_pools_lock); /* proceed with reclaim */ } while (...) This should achieve all the goals right? 1. No restarting from the top, unless we have traversed the entire hierarch= y. 2. No skipping over zswap_next_shrink updated by the memcg offline cleaner. 3. No leaving offlined zswap_next_shrink hanging (and blocking memcg killing indefinitely). 4. No long running loop unnecessarily. If you want to be extra safe, we can put a max number of retries on the offline memcg case too (and restart from the top). 5. At the end of the inner loop, you are guaranteed to hold at least one reference to memcg (and perhaps 2, if zswap_next_shrink is not updated by the cleaner). 5. At the end of the inner loop, the selected memcg is known to not have its cleaner started yet (since it is online with zswap_pools_lock held). Our selection will undo the cleaner and hold the memcg hostage forever. Is there anything that I'm missing? We are not dropping the lock for the entirety of the inner loop, but honestly I'm fine with this trade off, for the sake of simplicity :) If you want it to be even more readable, feel free to refactor the inner loop into a helper function (but it's probably redundant since it's only used once).