From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 109DEC4167B for ; Tue, 5 Dec 2023 19:00:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C68C6B0088; Tue, 5 Dec 2023 14:00:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 976256B0089; Tue, 5 Dec 2023 14:00:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8163D6B008A; Tue, 5 Dec 2023 14:00:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6FE6E6B0088 for ; Tue, 5 Dec 2023 14:00:21 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 35496160261 for ; Tue, 5 Dec 2023 19:00:21 +0000 (UTC) X-FDA: 81533680242.16.D530E4C Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) by imf05.hostedemail.com (Postfix) with ESMTP id 51AA0100012 for ; Tue, 5 Dec 2023 19:00:19 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vl2jBXFu; spf=pass (imf05.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.54 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701802819; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pjh7Ubx9BbyItPB9Xg+Qe9tw2FWSOXg11Jr74Wx0OFw=; b=WNHf4bnKEUBJiCOEYmFBztC4JFuiacvHXwvMDA2N7pll76/bfYMp6EyLu+BiBwo+vsV/hj Oj8csRDAlk2LY9ovSD28iv2wC7FILKrrDgbNlD9qT2B4Tbsr73KqeynvUb3Y6OD63lPRFp TJGka2R6EVRHtYkGTHLWkyDQSF1J0HU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701802819; a=rsa-sha256; cv=none; b=CKqRuVQ9TKgbzZhktvA3NIBGw4n1kKpHihWDiBf1PerieBg10JziRz35ikgLYxyyNWxceT zhn/esOhiWX+WoF7ZaqLvCeHVGtjoRlEeY/gLsFrTzNtchRHbLevHjofCqhAsgRPajj8GI 1U2Uj+A6tGKmZNMOo/GNhRnGESmESa0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vl2jBXFu; spf=pass (imf05.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.54 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-a1a0bc1e415so567896566b.0 for ; Tue, 05 Dec 2023 11:00:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701802818; x=1702407618; darn=kvack.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=pjh7Ubx9BbyItPB9Xg+Qe9tw2FWSOXg11Jr74Wx0OFw=; b=vl2jBXFuo6Qxaj+V7jRTIXa859CdP5wFSiKJq83t9df55h1jrHXbgDgfXeZV1d4tp+ hu3YCrjLH3tz8bRij6ZVGhbIaldqA/FasJcvZzafaUyLGXBeRmIfHQW9Bl14TzUbt2J/ RLv28INPfLVsBoNTlSoP678P6lWfJak4ri5PgA+yg8HuU8koemvvw5X9GYwl+ClzvQZc 9MMR6Pcqm6SGQLPyVb2C5d9TxE9orhr6La7aH0m7F4nGOxcAfPyz2mJYobOg2moTfEDQ ZB9cve9fr3Mg4jjRAV4nUgiCeRCBoCyA3QsgGdBQx26L6W1q10h9wCzpH1D0gKBAw/9v IJyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701802818; x=1702407618; h=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=pjh7Ubx9BbyItPB9Xg+Qe9tw2FWSOXg11Jr74Wx0OFw=; b=GK2+Al12tFGyisnoCHsnJ6Vbzu/A1RM1D1rq2QHIiFPMxO5vLGghwe4F0xO76sXi71 vhLrFQQeWAcqAf0FUow6o5wm+2plrNAd25lPkbrPP+WsrS+PSawhSjn1p+P6hgO2AvgL 8rAJRKguqu84l7H4g3nG9ZivBh9HDUbKpMGaU2cxZhiVoWI8VYTJHWO91TuzXXAfA4wO 7nP6cGhTr//9Lk/GbYjBo2JJvRUBYm/pwDyhxxkU/NEM1IxSf0VGesMKvnFeBuf73ACG DyFXpKfgZ6k5XZ6fXAzbOiTG5YnMDfRox4fELHhiLFA0xc1Q+KcK28NvagupKmrnq9Hm vsig== X-Gm-Message-State: AOJu0YxWg1HvwsFWvnputDEccsVlop7sYKLS/DjVr994tfKK1t1JtBxb pvLFlpIsXHca4FByA9e/xk71SmmtNkgLQzROUjifrA== X-Google-Smtp-Source: AGHT+IHgdcBdFf1UHwXVmyqpTmKQlO+VWkt+HaI2W2eYEZy5VFr0THrQhpyhuf+1LfRUz2o5Ld0BTwIYRpdFNaxq6yE= X-Received: by 2002:a17:906:7487:b0:a1c:d0f7:3f7a with SMTP id e7-20020a170906748700b00a1cd0f73f7amr516860ejl.43.1701802817693; Tue, 05 Dec 2023 11:00:17 -0800 (PST) MIME-Version: 1.0 References: <20231130194023.4102148-1-nphamcs@gmail.com> <20231130194023.4102148-4-nphamcs@gmail.com> In-Reply-To: From: Yosry Ahmed Date: Tue, 5 Dec 2023 10:59:41 -0800 Message-ID: Subject: Re: [PATCH v8 3/6] zswap: make shrinking memcg-aware To: Nhat Pham Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, cerasuolodomenico@gmail.com, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, chrisl@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: otn7rqihkpqa5wqzd4day3eock9jt69k X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 51AA0100012 X-Rspam-User: X-HE-Tag: 1701802819-321228 X-HE-Meta: U2FsdGVkX1+9nT5sDYbwnmIiHZ9in72KhZPLH0YAzfLgyW1vLLb1ftAvmDyy73noEwXtt/xm8QONJr/BXOv6RZfPXYfEn4xtEsnQtZUVdrepHeibwRPGIRmFSMmwziR+lFJWDgopT14YCxd6K7UeI54Z+YjiuaKjL07A/6na9h9qOQnxSvL5x71aLfYBwYP8i0veq15oc0AN+TilyKPYtBRnp9V3nutRMsdTwUUsXU/5DubkMlxqGcY9xj34L3HLLQQT2HlLnS9jUSEkzG8z5Qi3L6PB2AvefYWmrgW/NFXRyeGwGwtWOabCw4+cLYbC9qfLoSeqj271E5qsJB1lalA7l1X37FGAbJPBTI5XCNoX5qGIsFNe+4Hvis1taNVDDE4a18SMqz0AsAmm9trWIo20JbDoZdN2reCnH5G75cdmy6JwrN+7bN/SdMP4qb2WSHAcvdT6OyuN4sHH4BYjnOBYYMbCLFos7/2nLv2ePEpD2NnjSMv9C/gx0enrtplVsU2y3SexX9v/8JB9TD7WsxSr7BbY7Z8sLWXbMwdSBQFVOLXX8sGfPzDi/3OT2cUUc34rT+pROx5tzPgcJT1Ngj5OLatxxWOskpI8mA+/TCQk308DlbuKHlRmzY2HhwF2Sh3ga9dXrqmq+F4kxPLUe0bQeejrR66CgKnJjFHhKm8r9w7dc1sIwVTXJ35zgoy91h7c7jGpqO4ONxwTrozjyc8zVABGF/XK4/MwtiAyIH2TtOH/VvYARwCXEPy4YG415//g8oQbHmd9POYwC87BAgrtjTZFtuN+A741gr+m19opGyLI6mFPc3hKuoKNYAO+yIhbYn7rzy1zwT7R3P1vqn2k7xshYc1qzc7hXML5JfuNhFealwMaKeqI/jQrOB3Ug+mIE60G92cfkhx5pmpgfJTrgQIl+LZb5GmLYHdBF3820+b9r3tditrxsJjbovQBhZAwH/UmMVgP/30rAX6 Ahartr0s F8bF2lCY98Was+UlJP+nJNU3c7/A0hCrqVGxOUV/VwN72n94qJ6mU6TyxdMKwlvnoGRCKUiyhb3shKpVGbhxjZ7p3nCgWzE3QCHGyF8BIqjUyDNU3pITTRjjZRcj4Fk0t89YAA4sgAPHDsrt8qp4wSotpXVQRAvaxJXoXNzPIBIxvYtT3tjkwxFSSp1VDpRUeCjhXQG4QuMwmAwphrgvikm5DT+xTervIXa2HJTKcO0iICIecT3WEmyy57wqNjiTiy0M9zXOx/v8/W6q2zIutv1Cs6Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [..] > > > static void shrink_worker(struct work_struct *w) > > > { > > > struct zswap_pool *pool = container_of(w, typeof(*pool), > > > shrink_work); > > > + struct mem_cgroup *memcg; > > > int ret, failures = 0; > > > > > > + /* global reclaim will select cgroup in a round-robin fashion. */ > > > do { > > > - ret = zswap_reclaim_entry(pool); > > > - if (ret) { > > > - zswap_reject_reclaim_fail++; > > > - if (ret != -EAGAIN) > > > + spin_lock(&zswap_pools_lock); > > > + pool->next_shrink = mem_cgroup_iter(NULL, pool->next_shrink, NULL); > > > + memcg = pool->next_shrink; > > > + > > > + /* > > > + * We need to retry if we have gone through a full round trip, or if we > > > + * got an offline memcg (or else we risk undoing the effect of the > > > + * zswap memcg offlining cleanup callback). This is not catastrophic > > > + * per se, but it will keep the now offlined memcg hostage for a while. > > > + * > > > + * Note that if we got an online memcg, we will keep the extra > > > + * reference in case the original reference obtained by mem_cgroup_iter > > > + * is dropped by the zswap memcg offlining callback, ensuring that the > > > + * memcg is not killed when we are reclaiming. > > > + */ > > > + if (!memcg) { > > > + spin_unlock(&zswap_pools_lock); > > > + if (++failures == MAX_RECLAIM_RETRIES) > > > break; > > > + > > > + goto resched; > > > + } > > > + > > > + if (!mem_cgroup_online(memcg)) { > > > + /* drop the reference from mem_cgroup_iter() */ > > > + mem_cgroup_put(memcg); > > > > Probably better to use mem_cgroup_iter_break() here? > > mem_cgroup_iter_break(NULL, memcg) seems to perform the same thing, right? Yes, but it's better to break the iteration with the documented API (e.g. if mem_cgroup_iter_break() changes to do extra work). > > > > > Also, I don't see mem_cgroup_tryget_online() being used here (where I > > expected it to be used), did I miss it? > > Oh shoot yeah that was a typo - it should be > mem_cgroup_tryget_online(). Let me send a fix to that. > > > > > > + pool->next_shrink = NULL; > > > + spin_unlock(&zswap_pools_lock); > > > + > > > if (++failures == MAX_RECLAIM_RETRIES) > > > break; > > > + > > > + goto resched; > > > } > > > + spin_unlock(&zswap_pools_lock); > > > + > > > + ret = shrink_memcg(memcg); > > > > We just checked for online-ness above, and then shrink_memcg() checks > > it again. Is this intentional? > > Hmm these two checks are for two different purposes. The check above > is mainly to prevent accidentally undoing the offline cleanup callback > during memcg selection step. Inside shrink_memcg(), we check > onlineness again to prevent reclaiming from offlined memcgs - which in > effect will trigger the reclaim of the parent's memcg. Right, but two checks in close proximity are not doing a lot. Especially that the memcg online-ness can change right after the check inside shrink_memcg() anyway, so it's a best effort thing. Anyway, it shouldn't matter much. We can leave it. > > > > > > + /* drop the extra reference */ > > > > Where does the extra reference come from? > > The extra reference is from mem_cgroup_tryget_online(). We get two > references in the dance above - one from mem_cgroup_iter() (which can > be dropped) and one extra from mem_cgroup_tryget_online(). I kept the > second one in case the first one was dropped by the zswap memcg > offlining callback, but after reclaiming it is safe to just drop it. Right. I was confused by the missing mem_cgroup_tryget_online(). > > > > > > + mem_cgroup_put(memcg); > > > + > > > + if (ret == -EINVAL) > > > + break; > > > + if (ret && ++failures == MAX_RECLAIM_RETRIES) > > > + break; > > > + > > > +resched: > > > cond_resched(); > > > } while (!zswap_can_accept()); > > > - zswap_pool_put(pool); > > > } > > > > > > static struct zswap_pool *zswap_pool_create(char *type, char *compressor) [..] > > > @@ -1240,15 +1395,15 @@ bool zswap_store(struct folio *folio) > > > zswap_invalidate_entry(tree, dupentry); > > > } > > > spin_unlock(&tree->lock); > > > - > > > - /* > > > - * XXX: zswap reclaim does not work with cgroups yet. Without a > > > - * cgroup-aware entry LRU, we will push out entries system-wide based on > > > - * local cgroup limits. > > > - */ > > > objcg = get_obj_cgroup_from_folio(folio); > > > - if (objcg && !obj_cgroup_may_zswap(objcg)) > > > - goto reject; > > > + if (objcg && !obj_cgroup_may_zswap(objcg)) { > > > + memcg = get_mem_cgroup_from_objcg(objcg); > > > > Do we need a reference here? IIUC, this is folio_memcg() and the folio > > is locked, so folio_memcg() should remain stable, no? > > Hmmm obj_cgroup_may_zswap() also holds a reference to the objcg's > memcg, so I just followed the patterns to be safe. Perhaps it's less clear inside obj_cgroup_may_zswap(). We can actually pass the folio to obj_cgroup_may_zswap(), add a debug check that the folio is locked, and avoid getting the ref there as well. That can be done separately. Perhaps Johannes can shed some light on this, if there's a different reason why getting a ref there is needed. For this change, I think the refcount manipulation is unnecessary. > > > > > > Same for the call below. > > > > > + if (shrink_memcg(memcg)) { > > > + mem_cgroup_put(memcg); > > > + goto reject; > > > + } > > > + mem_cgroup_put(memcg); > > > + } > > > > > > /* reclaim space if needed */ > > > if (zswap_is_full()) { [..]