From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) (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 BD14536607E for ; Wed, 4 Mar 2026 16:26:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772641618; cv=none; b=sz8FBTtIVtwx85+7Sbwls1fE7vtZOsU8E1AdGwp5XXeUBlvwzJSiFPKLKY72M8kA6Ovom3dsrT1SY0XItWg3DecuIchi1LzVicOLr4Kec2S30gVPnSiwRU2jLGunpJ5NnNpejXJxFGSj3T1ajVNzeuJKJlD6V8avxUuW96uwcm0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772641618; c=relaxed/simple; bh=eA7E7eZTn6WWCSz2VDvWTjTbPH+bebgGwzaVQ9OPJa8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=SCyZnONeDbX4QLU+viOVA6ABJIrVpXhLcSoCQXmJMk0WZ0M6CqZFnsevf3zDHhwkFLZwVCCfk70+r9Zz9O6LYUXkTVkRVfUgpAJa0Xk35qAdQM/42i9lRQNJ7hg4oxG2mN8Zc2F6FS+zbllXTv6lJbcn9dFvvABvCu/bW3+mBZI= 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=hrltKoY9; arc=none smtp.client-ip=209.85.167.174 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="hrltKoY9" Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-45f015a3259so2884627b6e.2 for ; Wed, 04 Mar 2026 08:26:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772641614; x=1773246414; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2Hg+2cx4ctUEYaUblGwODgDafYhXfvGplcz0+hFdRQQ=; b=hrltKoY9WbyA+csyMCGezwa/+q0zLsbNf32HJnykOG2ayehe0BhB4hp9mExNfBHXHt DtBE16KpRAjiG/77oXx2scMJF9NbkHJ3XxHy9l3TZP3834+5CkMv0TYXMRZgnmA49QOx cmK0BVPl+y6I+mSlDaH3DgplWTr77Shqdlmzu6kUmxOoMJo5/W+2sT/IZ/ygIKIrKfb/ MsEubCQVsjRAZ/LIj8B9wJjc422Q5wQHZbuDtSwCrZLOR7YV488LgVyQ/1LgSdZFEKE0 O3ekfXfzbs+uccwxjAZMZcoOgR+nP6fiXgrgIVgIGMKsPEpqUqdzOckpgUjqFk3cLYBk MY3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772641614; x=1773246414; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=2Hg+2cx4ctUEYaUblGwODgDafYhXfvGplcz0+hFdRQQ=; b=Wd93Pobtd99Du9TnpkPql3sZgByyKr8SOT/dqo2VLGRywk6PbfgcxrkgW6rNfRrq1+ fmmmuEHDPZKE+UCHASuUtyRrEB+RInzHwuOv73Y+YGvmoRTwNVFfIrR4v6JK4BO3HCiV FbnAKexL/FeocHQVfOtbxclsFabsJThOQSArVKgz8x0ZHqVK+6ghuOc97pkPZ6ZBGSva bjYw6bvRu89wi8hM9LquzIyHBIIwXtVWUYs/5z8tc+gtyySH6N4GXDb8nYo2gR2bKUUL aNMvurXiDDQmORFmFHFPy9giC5FhSEmabjBxR88VmXs7qlUJRd7ZjDhPYNbDAsrTptFt H0aw== X-Forwarded-Encrypted: i=1; AJvYcCU9mzUMir86/qR2f+dQ2jekteg8Wp9cBqcOxFbXQojxXJIa094YBa08WXqG89iUFCknvnZYWmzVOuoedFo=@vger.kernel.org X-Gm-Message-State: AOJu0YzjsDO/oydEXDAJ/MR9yeRgc/wSC/ay7RsR9NLJexU3gnjIj0lS VhnJ4xAibmLLfhRkOxGLGfZsnau/Su9lXsqp8BpwdG+CzEEj+IyuT7SC X-Gm-Gg: ATEYQzzM+yxjsnG35StF8vw6KyL6gILYQNdPv9epP4rzEbHpVUn5Nyhvu1pmoxGqtQz GNNYXTxSZ8MMiVEwDYCj/rztAmqMKxUuzmCL2SSQa2e0WEojGTYwYbP4YRyqtBF7nYu6/xAAuhW XGEqiVdK/T7IKiD1CTCEWRVjejdPhQBL975RgYg1N8k/sPYYg+kpqACbp3LAmik8T1Rri2COkPF kRYBSqkgXXohhPJDgPyT7//CC7r9BtiRZ55yIzyQeHhdXSGi9i7dwqUM6MLVrrPkDcFC176NpPe CoOvzrM1kFqRPK3X6wwwsaTO56YdR/EpABjiuAEmM4hyrrDUm39Zd1GTL64NNtgFwbpmZBN3IMc Zal9qnHJOwuGgTHZtamHHAtk7GhbaHibGrf7YkjDYP1ezsyGXyJhOoML9XxicSa3ThIY1wLuyWz Q02fbp8qj9IgKPKDjABBqw X-Received: by 2002:a05:6808:22ac:b0:450:3e21:f567 with SMTP id 5614622812f47-4651ad0d5aamr1135266b6e.56.1772641614328; Wed, 04 Mar 2026 08:26:54 -0800 (PST) Received: from localhost ([2a03:2880:10ff:3::]) by smtp.gmail.com with ESMTPSA id 5614622812f47-464bb4028cdsm11886351b6e.9.2026.03.04.08.26.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2026 08:26:53 -0800 (PST) From: Joshua Hahn To: Yosry Ahmed Cc: Minchan Kim , Sergey Senozhatsky , Johannes Weiner , Yosry Ahmed , Nhat Pham , Nhat Pham , Chengming Zhou , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: Re: [PATCH 6/8] mm/zsmalloc, zswap: Handle objcg charging and lifetime in zsmalloc Date: Wed, 4 Mar 2026 08:26:50 -0800 Message-ID: <20260304162652.335793-1-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Wed, 4 Mar 2026 07:46:48 -0800 Yosry Ahmed wrote: > On Wed, Mar 4, 2026 at 7:11 AM Joshua Hahn wrote: > > > > On Tue, 3 Mar 2026 15:53:31 -0800 Yosry Ahmed wrote: > > > > > > diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c > > > > index 067215a6ddcc..88c7cd399261 100644 > > > > --- a/mm/zsmalloc.c > > > > +++ b/mm/zsmalloc.c > > > > @@ -963,6 +963,44 @@ static bool alloc_zspage_objcgs(struct size_class *class, gfp_t gfp, > > > > return true; > > > > } > > > > > > > > +static void zs_charge_objcg(struct zpdesc *zpdesc, struct obj_cgroup *objcg, > > > > + int size, unsigned long offset) > > > > +{ > > > > + struct mem_cgroup *memcg; > > > > + > > > > + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) > > > > + return; > > > > + > > > > + VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC)); > > > > + > > > > + /* PF_MEMALLOC context, charging must succeed */ > > > > + if (obj_cgroup_charge(objcg, GFP_KERNEL, size)) > > > > + VM_WARN_ON_ONCE(1); > > > > + > > > > + rcu_read_lock(); > > > > + memcg = obj_cgroup_memcg(objcg); > > > > + mod_memcg_state(memcg, MEMCG_ZSWAP_B, size); > > > > + mod_memcg_state(memcg, MEMCG_ZSWAPPED, 1); > > > > Hello Yosry, I hope you are doing well! > > Thank you for your feedback : -) > > > > > Zsmalloc should not be updating zswap stats (e.g. in case zram starts > > > supporting memcg charging). How about moving the stat updates to > > > zswap? > > > > Yeah... I think this was also a big point of concern for me. While reading > > the code, I was really amazed by how clean the logical divide between > > zsmalloc and zswap / zram were, and I wanted to preserve it as much as > > possible. > > > > There are a few problems, though. Probably the biggest is that migration > > of zpdescs and compressed objects within them are invisible to zswap. > > Of course, this is by design, but this leads to two problems. > > > > zswap's ignorance of compressed objects' movements across physical nodes > > makes it impossible to accurately charge and uncharge from the correct > > memcg-lruvec. > > > > Conversely, zsmalloc's ignorance of memcg association makes it impossible > > to correctly restrict cpusets.mems during migration. > > > > So the clean logical divide makes a lot of sense for separating the > > high-level cgroup association, compression, etc. from the physical > > location of the memory and migration / zpdesc compaction, but it would > > appear that this comes at a cost of oversimplifying the logic and missing > > out on accurate memory charging and a unified source of truth for the > > counters. > > > > The last thing I wanted to note was that I agree that zsmalloc doing > > explicit zswap stat updates feels a bit awkward. The reason I chose to do > > this right now is because when enlightening zsmalloc about the compressed > > objs' objcgs, zswap is the only one that does this memory accounting. > > So having an objcg is a bit of a proxy to understand that the consumer > > is zswap (as opposed to zram). Of course, if zram starts to do memcg > > accounting as well, we'll have to start doing some other checks to > > see if the compresed object should be accounted as zram or zswap. > > > > OK. That's all the defense I have for my design : -) Now for thinking > > about other designs: > > > > I also explored whether it makes sense to make zsmalloc call a hook into > > zswap code during and after migrations. The problem is that there isn't > > a good way to do the compressed object --> zswap entry lookup, and this > > still doesn't solve the issue of zsmalloc migrating compressed objects > > without checking whether that object can live on another node. > > > > Maybe one possible approach is to turn the array of objcgs into an array > > of backpointers from compressed objects to their corresponding zswap_entries? > > One concern is that this does add 8 bytes of additional overhead per > > zswap entry, and I'm not sure that this is acceptable. I'll keep thinking > > on whether there's a creative way to save some memory here, though... > > > > Of course the other concern is what this will look like for zram users. > > I guess it can be done similarly to what is done here, and only allocate > > the array of pointers when called in from zswap. > > > > Anyways, thank you for bringing this up. What do you think about the > > options we have here? I hope that I've motivated why we want > > per-memcg-lruvec accounting as well. Please let me know if there is anything > > I can provide additional context for : -) > > Thanks for the detailed elaboration. > > AFAICT the only zswap-specific part is the actual stat indexes, what > if these are parameterized at the zsmalloc pool level? AFAICT zswap > and zram will never share a pool. That's a great idea, we can abstract the ZSWAP and ZSWAPPED idxs as "compressed" and "uncompressed" and leave the flexibility for zram to do similar accounting in the future if they wish to. Thanks for the suggestion, Yosry. I'll include this in the v2 and send it out! I hope you have a great day!! Joshua