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 64EF5C4332F for ; Thu, 2 Nov 2023 02:17:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E68378D0070; Wed, 1 Nov 2023 22:17:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DF1528D0026; Wed, 1 Nov 2023 22:17:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C91328D0070; Wed, 1 Nov 2023 22:17:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B64838D0026 for ; Wed, 1 Nov 2023 22:17:50 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 855B61A0DED for ; Thu, 2 Nov 2023 02:17:50 +0000 (UTC) X-FDA: 81411403500.12.16A5EF2 Received: from out-188.mta1.migadu.com (out-188.mta1.migadu.com [95.215.58.188]) by imf18.hostedemail.com (Postfix) with ESMTP id B01301C0008 for ; Thu, 2 Nov 2023 02:17:48 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Em1XyI3/"; spf=pass (imf18.hostedemail.com: domain of muchun.song@linux.dev designates 95.215.58.188 as permitted sender) smtp.mailfrom=muchun.song@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698891468; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=B+y5TA+dBJkHS1npJO+WnH+1LCxKJsUTCf46ix28dSM=; b=cl9Q1wKlGKGCHu0Af7bB41DwRUPUOf4VTjHFXsTlJr+t5155cBrEABoJATqz9WZA6TE6Hx wdh1SjqLc5nRNvTg0WnsotVfpU9BciqYEF6h9fTCiEVeY2V1y7G0WT/YbA04V0elY6DaH3 BM2SD2WHsX5BgDeOnIGGoL5jXjfF7Mo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698891468; a=rsa-sha256; cv=none; b=2zSU1otn9sXXzTCJpI4mgXJGTGK7gWoWlBlQN2VEeXQNF5OdPGLtoiX3mS75UwTdL8AEdT HFLIUVqz6nHk/TTqSiGWOAbFwInXVpXy0iIzoS34OTr/Z4SMlB+6TUSgreZhSAfPTVu4vw JGxuZ14h8qvsBsQqBMvRGa1skyTaW7U= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Em1XyI3/"; spf=pass (imf18.hostedemail.com: domain of muchun.song@linux.dev designates 95.215.58.188 as permitted sender) smtp.mailfrom=muchun.song@linux.dev; dmarc=pass (policy=none) header.from=linux.dev Content-Type: text/plain; charset=utf-8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1698891466; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B+y5TA+dBJkHS1npJO+WnH+1LCxKJsUTCf46ix28dSM=; b=Em1XyI3/iCPrthA4ond4YGR3E2WDklG8rIywXuErsHfD8y60SzEoc0FG+91im5EV7jX5G2 IbufFuGf41zfItJPCugj6PWOOpoUt0ajMr3ZMA8p1JsZs3ngpINCSzb/MhgtWta5iBzZaR xbuAunHLd6eEarkKHRkzXpHbTlbmYaA= Mime-Version: 1.0 Subject: Re: [PATCH v4 2/5] zswap: make shrinking memcg-aware X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Muchun Song In-Reply-To: Date: Thu, 2 Nov 2023 10:17:03 +0800 Cc: Andrew Morton , Johannes Weiner , cerasuolodomenico@gmail.com, Yosry Ahmed , sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, Michal Hocko , Roman Gushchin , Shakeel Butt , Chris Li , Linux-MM , kernel-team@meta.com, LKML Content-Transfer-Encoding: quoted-printable Message-Id: References: <20231024203302.1920362-3-nphamcs@gmail.com> <20231101012614.186996-1-nphamcs@gmail.com> To: Nhat Pham X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: B01301C0008 X-Rspam-User: X-Stat-Signature: y6on97pz5ni88tffau1nboup6mzn6t3b X-Rspamd-Server: rspam03 X-HE-Tag: 1698891468-395898 X-HE-Meta: U2FsdGVkX1/MiXClMJKXix1AwJJXTc+xBuAH6OwYUKsCy2ANVoGwm27k4dcMeMjW1zNZvJgMSTb3c+UTRxh42+P13aPsqpUyAmWyWZBlUCA5NAH0Qe6Z3U41xRWEazAlhJJ0u4saNUFI5205Cakoha2qVqDRxRLrboVkb4PPT3W0GvXxMAKx1/Kp8lEvY3x9zzHSRe5ZMU6AU1eduXhwoTArTe/See0C+/YdGEk+PogO/9dbxUBigugcoYwEU6LrTfO1VVN9HJ5Oqy3jy/ogFWouQ6uP7Cd4LRY24CjxqvEM/cUmOODhZbLBZidbitqO+daRwlNMfM0i4Xw3ycEoGSSdpiCYRjyn7ZPRwdPrN/Cgduy0SOwZu6ZceRFQFH6mDWCdSjAWi/tGxJkgrACZdfxBDAHdRDSN8GKH0vCHJ89hILI+IUOI0kip8ZkpEbncaSmV+I1Fv0ogAznIlnyKITIYb6PzUccwJvJbiXxp/AB4u6vQ4OY9yNFuGDSHWg+/5wiKw0ky3BQ9j4d7QAh2JukhpZWPSwrVLYIdor9Mc++IDfCBD+h4wNoIKLwHVQUEIw+N0in8okWzOU7k4FUG16+Hx+ONLbm+SqsPPpJCza46iDNIGTprRG4eZYLwvOS7tdEIQoMzlwBgUh7Ep6wCR/y75VKWTrBhaYUkhiOVUZaQsJr8eXdBbVSvmvT33d2AQblMHzFavCLShdunSe9qBZmpVPalqkgjPPNRkn/+9gXezMp+97NwbHhJmoOv1Zyz2NtX7XnARlgpv4vE3xWDRHGf/sQAYn+RMvonLqSb3YAEzc4mWNQ3lxQfpeoivVZaQPKOQJebeRbygpcjd/BXkD0iP+TlIcQNGNivusB5Qeu5D1a5lgz+I+K1UGg1SzOWRcW3TdAvox/LbxwpMLcCs97TAGuDSOWGHez7UK1qcKsgMXEGPfJ1IGTe4zG8bs877SovXWSUYlPtyPfwvm+ CE4i9LK3 CZpSiotpT2OyZI/Gu7Z1aDB7qNmjlUJhc2MyMWx8qkrmusMWCdiJFCTlBH2dXt28s2Me4Zn3wYeLIJePS0IVUlelRmg== 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: > On Nov 2, 2023, at 01:44, Nhat Pham wrote: >=20 > On Tue, Oct 31, 2023 at 8:07=E2=80=AFPM Muchun Song = wrote: >>=20 >>=20 >>=20 >>> On Nov 1, 2023, at 09:26, Nhat Pham wrote: >>>=20 >>> cc-ing Johannes, Roman, Shakeel, Muchun since you all know much more >>> about memory controller + list_lru reparenting logic than me. >>>=20 >>> There seems to be a race between memcg offlining and zswap=E2=80=99s >>> cgroup-aware LRU implementation: >>>=20 >>> CPU0 CPU1 >>> zswap_lru_add() mem_cgroup_css_offline() >>> get_mem_cgroup_from_objcg() >>> memcg_offline_kmem() >>> memcg_reparent_objcgs() >>> memcg_reparent_list_lrus() >>> memcg_reparent_list_lru() >>> = memcg_reparent_list_lru_node() >>> list_lru_add() >>> memcg_list_lru_free() >>>=20 >>>=20 >>> Essentially: on CPU0, zswap gets the memcg from the entry's objcg >>> (before the objcgs are reparented). Then it performs list_lru_add() >>> after the list_lru entries reparenting = (memcg_reparent_list_lru_node()) >>> step. If the list_lru of the memcg being offlined has not been freed >>> (i.e before the memcg_list_lru_free() call), then the list_lru_add() >>> call would succeed - but the list will be freed soon after. The new >>=20 >> No worries. list_lru_add() will add the object to the lru list of >> the parent of the memcg being offlined, because the ->kmemcg_id of = the >> memcg being offlined will be changed to its parent's ->kmemcg_id = before memcg_reparent_list_lru(). >>=20 >=20 > Ohhh that is subtle. Thanks for pointing this out, Muchun! >=20 > In that case, I think Yosry is right after all! We don't even need to = get > a reference to the memcg: >=20 > rcu_read_lock(); > memcg =3D obj_cgroup_memcg(objcg); > list_lru_add(); > rcu_read_unlock(); >=20 > As long as we're inside this rcu section, we're guaranteed to get > an un-freed memcg. Now it could be offlined etc., but as Muchun has Right. Thanks. > pointed out, the list_lru_add() call will still does the right thing - = it will > either add the new entry to the parent list if this happens after the > kmemcg_id update, or the child list before the list_lru reparenting > action. Both of these scenarios are fine. >=20 >> Muchun, >> Thanks >>=20 >>> zswap entry as a result will not be subjected to future reclaim >>> attempt. IOW, this list_lru_add() call is effectively swallowed. And >>> worse, there might be a crash when we invalidate the zswap_entry in = the >>> future (which will perform a list_lru removal). >>>=20 >>> Within get_mem_cgroup_from_objcg(), none of the following seem >>> sufficient to prevent this race: >>>=20 >>> 1. Perform the objcg-to-memcg lookup inside a rcu_read_lock() >>> section. >>> 2. Checking if the memcg is freed yet (with css_tryget()) (what >>> we're currently doing in this patch series). >>> 3. Checking if the memcg is still online (with = css_tryget_online()) >>> The memcg can still be offlined down the line. >>>=20 >>>=20 >>> I've discussed this privately with Johannes, and it seems like the >>> cleanest solution here is to move the reparenting logic down to = release >>> stage. That way, when get_mem_cgroup_from_objcg() returns, >>> zswap_lru_add() is given an memcg that is reparenting-safe (until we >>> drop the obtained reference).