From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 A380A2EC09F for ; Wed, 24 Jun 2026 11:55:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782302126; cv=none; b=oXs6evQ0zaQfzs6LWy52AdSq4LyPc73xmrdQHoPOegyIq8WrBB30+83k1gpRHeih4xb/I1UkO79oK82VtHKngbal7OHNStyOXTeTvxJGgWaEkPl6SfLH5vWtklbHwTG1Mu5ZpB33Lt6RCDpBoheTM5v3v0NntzpzSNCiCJKIc+8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782302126; c=relaxed/simple; bh=+th0CrBhc9xLunvVRMGkRAEj3ZZ8/wqm7in4jBwAio4=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ISmZeVH7eHrvNvpztXGWCxTrLS6Qy1G9LGhN3X+6uPyLceF90jzkg3nArDhAJ63qYTAd6cVgpkCO23uoWA98ig1XhxoITNFDjk4q9iM7EwwleUsBACOv9FDFiE3Wf3kv6E7hSrLvwbYjNTd/PVigqtw8FJs2BrtKxg9+XpGYBso= 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=dImxTR0q; arc=none smtp.client-ip=209.85.210.179 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dImxTR0q" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-845385adf4cso639411b3a.0 for ; Wed, 24 Jun 2026 04:55:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782302125; x=1782906925; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=lFU8MyOIrfhmG7QxZLchLSQCDUz4MOf5up+HUhjl+OU=; b=dImxTR0qovpIMR0kDHETYWH15hxVNHw/hc8qoKrD+Ujye4Ef5lfuIajm/KW2ZZ5YZm YgzpAoSlaDqBsNYSobUB3vqnxiyZyGKws8Ms0ywL14XbNpNE8JXCZo8fKIlbU9sCGW9b IDPC54vUlaB+OjBu/RLKF65q+HvpsZwv4qUnQ+EfxmW/4+1YktabbtkzcT5WOhF0RPlZ iHx7MFMHAMTr+XaxIH2eo+1vEkkXMy8S5sWTATwdnbHcCWcBlMPOC0DLp1bBrXd3s1kc +Dw8W/0qbGvYJhf7eCdr988P28/Il4URkZRLhS22z0vxICDeJeuVWLz6+uClilQk0G6F dB3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782302125; x=1782906925; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=lFU8MyOIrfhmG7QxZLchLSQCDUz4MOf5up+HUhjl+OU=; b=LQRoOoUTFujn0OjzkhaE2HPTHRRyfSwEhLZQvC8d2ZCY03SuTR/JrkI/KcC1qxAwwx 6sy2eFWL6Fd4NkLv8dW12x1m/ltDHWGtZIBi/wjWTbteJBz+C98sSuIlhemB6P1+wMB9 c7JDmg0kGFxTs7Bstp3hZmecjRyh5BvJmYVYngnrtqdtS2aIcVulaWDKBqC2F8pdaT6C gpazy0AlZjyfiQ4b/1bDfolXlQSjl07fFoJ60ujruGxMPBdM4VCboTgK6LsBkPnaPRXX +bDiC6WnxNSCJMBnEIn1eDg/AoAf5+6IkEEjaOyE7xiI5EOXWotj81v5XasOeQv+MKUm R1Tg== X-Forwarded-Encrypted: i=1; AFNElJ8xhF1erYFY/xlVrxe0rFqRoaPCmPfw9E//08lkW4LeN6dcLVU0++8kBI26pMcUKrjM6qz/OgKtBZFRU6A=@vger.kernel.org X-Gm-Message-State: AOJu0YxHe2m1UdvgFYm8xU8Os1B9h/xa8iacGi5mYDZv8ThegdvuZAPw ugPX/f2R7IibSgOtL2EfIaqmCSuCvwGiECyBqzn+hj2DoQk1njiG1iKS X-Gm-Gg: AfdE7clVI7d+1x6FIXl21ZEwXJKZDQceEIhF8gBk6bciyBGYyDqGW5BLgy0YpWj2Xg0 PBuH//M1mJTFjIzC+cbWoea0hOZk+EaeY0VKoYj63ZQiSr3eaYzx0Pe6ZrsAhmyfXv62WdYCqlR sMrTS9yo7OUsQveemV7/ucnvTSww3Odk627jphEsn7Sl1TnQPh6mQGbhVDEhrWq2bXtc8crV9kP VV3BsT+a8+ZLzMimvZqzzgb/HfR4iKKC4cWcULxFPcs5Gp7OEr1ynOFIbjpth8h22AwfRs6tQF6 3WeJNkvz0dfOvNzRsUfN11hwE6G0t/48KA5WzomV0ZvpTeUlkF1qzCM1Vk5fQSNFa1vTna5Y2ME OdzKGaM5V/TH96YWSlr0ZVvcHCVG4mFXnPwBfI3gxt+ziJ3uPpuB8fckZ2IfNZIa/9CSgFykFvL 1Faak01Xu08qhXe4PICzfjbubAjE1lp9lj X-Received: by 2002:a05:6a00:179c:b0:845:344f:eee9 with SMTP id d2e1a72fcca58-845a26dbf63mr4128286b3a.1.1782302124802; Wed, 24 Jun 2026 04:55:24 -0700 (PDT) Received: from [10.125.192.89] ([210.184.73.204]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-845a40d23ccsm2563857b3a.36.2026.06.24.04.55.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 Jun 2026 04:55:24 -0700 (PDT) Message-ID: <0916e673-861f-b472-7417-afbffbcc98ad@gmail.com> Date: Wed, 24 Jun 2026 19:55:14 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Subject: Re: [PATCH v4 2/5] mm/zswap: Factor writeback loop out of shrink_worker() To: Yosry Ahmed Cc: akpm@linux-foundation.org, tj@kernel.org, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@kernel.org, mkoutny@suse.com, nphamcs@gmail.com, chengming.zhou@linux.dev, muchun.song@linux.dev, roman.gushchin@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Hao Jia References: <20260618044857.69439-1-jiahao.kernel@gmail.com> <20260618044857.69439-3-jiahao.kernel@gmail.com> From: Hao Jia In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2026/6/23 07:36, Yosry Ahmed wrote: >> +/* >> + * Walk the memcg tree and write back zswap pages until the >> + * (lower_pages, upper_pages) window closes, or abort encounter >> + * MAX_RECLAIM_RETRIES times of the following conditions: >> + * - No writeback-candidate memcgs found in a memcg tree walk. >> + * - Shrinking a writeback-candidate memcg failed. >> + * >> + * For shrink_worker(), it passes lower=thr and upper=zswap_total_pages(). >> + * The @upper limit is refreshed in each iteration by re-evaluating >> + * zswap_total_pages(), and the window closes once the total falls >> + * below the threshold. > > This is the wrong abstraction level, and it's obvious by the fact that > the function calls zswap_total_pages() again to recalcualte > 'upper_pages'. It gets much worse in the next patch as well. > > The lower_pages and upper_pages thing is also unnecessarily hard to > follow. > > The core of the reuse here is the retry logic. So maybe keep the memcg > iteration in the callers, and define a function that takes in one memcg > and reclaims one batch from it? failures and attempts can be passed into > the function to maintain the state across scans of different memcgs, > like zswap_shrink_walk_arg? > > WDYT? Perhaps something like this? struct zswap_shrink_state { int attempts; int failures; bool stop; }; static bool zswap_shrink_no_candidate(struct zswap_shrink_state *s) { if (!s->attempts && ++s->failures == MAX_RECLAIM_RETRIES) return true; s->attempts = 0; return false; } static long zswap_shrink_one(struct mem_cgroup *memcg, struct zswap_shrink_state *s) { long shrunk; shrunk = shrink_memcg(memcg, NR_ZSWAP_WB_BATCH); if (shrunk == -ENOENT) return 0; s->attempts++; if (shrunk <= 0 && ++s->failures == MAX_RECLAIM_RETRIES) s->stop = true; return shrunk; } static void shrink_worker(struct work_struct *w) { struct zswap_shrink_state s = {}; unsigned long thr; /* Reclaim down to the accept threshold */ thr = zswap_accept_thr_pages(); while (zswap_total_pages() > thr) { struct mem_cgroup *memcg; cond_resched(); memcg = zswap_iter_global(); if (!memcg) { if (zswap_shrink_no_candidate(&s)) break; continue; } zswap_shrink_one(memcg, &s); /* Drop the extra reference taken by the iterator. */ mem_cgroup_put(memcg); if (s.stop) break; } } We could also fold the logic of zswap_shrink_no_candidate() into zswap_shrink_one(), but adding a !memcg check inside zswap_shrink_one() feels a bit awkward. WDYT? Thanks, Hao