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 0B750C4332F for ; Wed, 1 Nov 2023 03:07:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F9BC8D0030; Tue, 31 Oct 2023 23:07:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 683358D0001; Tue, 31 Oct 2023 23:07:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 525998D0030; Tue, 31 Oct 2023 23:07:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 445B48D0001 for ; Tue, 31 Oct 2023 23:07:20 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 1397680C24 for ; Wed, 1 Nov 2023 03:07:20 +0000 (UTC) X-FDA: 81407899440.08.8971BCD Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) by imf03.hostedemail.com (Postfix) with ESMTP id 0DC8520005 for ; Wed, 1 Nov 2023 03:07:17 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tdHJYSBR; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of muchun.song@linux.dev designates 95.215.58.175 as permitted sender) smtp.mailfrom=muchun.song@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698808038; 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=uWoT6krnJovWyrEsMbfgyVFj2a0jkXtJTHAUCAcBSHk=; b=QPzAhyqOv5Cg5iv48DXIUi6Yhv9ixYWhS18xJ1yeWzQc6h6WolPdkV/snTWm3uM+4Wbvvt 6+WwV+nAKoPgDiYNi9TdSxZo+XWBH/u5pmIVZcAXP04knCmU/Eaiaagb9OZwMoA415A3BS 20nBAb+7Y+L31SXJdyBcUQzuJhzLWZM= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tdHJYSBR; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of muchun.song@linux.dev designates 95.215.58.175 as permitted sender) smtp.mailfrom=muchun.song@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698808038; a=rsa-sha256; cv=none; b=bpFhMrJWPh+d8+vIUpKxmIgCeYpvLowxh2AAAJlCs6toKChT1NXdCPtdDJONoFCUnt7wBd svH3M/ppBA1Iydl+D0Yi04RyyDTPp8IF0RWaWmspWBh7Lg8ExFxYSIGrYG1+uB7qpxK4fv Mk9yPXGBEElC/dgp8LTsBudpGwRKcJY= Content-Type: text/plain; charset=utf-8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1698808035; 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=uWoT6krnJovWyrEsMbfgyVFj2a0jkXtJTHAUCAcBSHk=; b=tdHJYSBR9O6TjCmWCn2ljaxzGettdzXpL+yVuzXAIkcUPgnCN7ueVaIJI4+NTgBqk01xm1 bxU5yWujuCAG4cVbxT23sYtyr4ehEISpdILe17tsUdbHjohsz7Ni1wf5DMUsAGOy0N1vfe 69csr/e6AnbTYIRoWHcrPVcA8jdj/vY= 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: <20231101012614.186996-1-nphamcs@gmail.com> Date: Wed, 1 Nov 2023 11:06:26 +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: 0DC8520005 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: gdmfyexjxbiqq6bpdfy6a477bstiiq6t X-HE-Tag: 1698808037-621978 X-HE-Meta: U2FsdGVkX19MkUQjcYKN3yT7Ia7emklIxE3GrY94ltgUX3JxEXhQz59PEPdsFBBJ3dKSzTcj7SqcWm/j+wkWvIgxq141/xt9O9MRW8p8twi2voUpxWPtAfvTJ+ccmd6OJPUxOG79nlzCEXjaa+qReNg5/KyKucveBQ7moZiRGBIUpj2SRPMHBh5T6fSue+fpk2tnchbDcDelq/9xSb/SumKs44/W3PAH75m2IULrzTtx8ALeJU9xSHrKcpVNwcmSOj4q2bQuF1TtA0j+5OjgIl8KVcZ9DhjkvDNxFO1VNIx/2fdgHhyAWrI/+gXgbkUPECvbF3bL+rYErMETDD0J+1JjQHmyQe+EYe1TOLmForNJf6ph5wKnC2aIBKoSC+QCvwbhIJRiVVhHrF7QUphJbk5/AOzXByD0wbqv0cQzDDuMTwl3H1dxGqoAE82QpoxfpdjydovUY6dJGjHaI8m/voubbWD7HHe+vYrK3tbcib9yyxUxkl5uTuf16jQl0pg8Z1vUwSpsHPoLlEY37U/4796sfyZZ9nWSOtnRJJqdjguoxSdb88ePxF5Bzz21v6IIg5MdnTEhN23jOQofoR++4zTmqsZ9EZJbFV9oNVkOHHrW/vm1g+XX9AeWdvissYTwoRr8xuhQLUXPwpqVF80b6AkLkaWZJsTbz8DlsM4QF+YVTP+UPgql7K2xlqbvNWs9oHIPAUjjDJKd/M/FCt/9959CXhaCrx0a090miDVk8gA1qqprFrfpHwIHnWvEL05lSnhVPXw/CfKMyH9fM/LDX5WUkPtb/1ZVeq189+m6LJouUkNI0MJCZP6KEH8p3/qEFNbh0XGenUrMQJ+x4sRo/2WQtc7zV/3qykE1dYBMT5elfUbZ+4XOqqCsL/3igUPNyWaouXYBk8aeddyYCJGY/Van3WCUcC52ZnEkXlR+rgZD4VyhXxHQl+UrPaodgKhNZIKmGBVtE6mPMVMhfg0 oILV6iNx jEVIxJWjI3mTV2n+MHVWPmB82thQkv31C8H0/lzBEs1PJR1CofXw6VFoounD7TpLz+B8rwnOyyoSDAAQDIS71geQk6d6Fh9mUcPBkCsDLIpZn0LHPMqOVZ0IAWp1njKuVq1tz 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 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 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(). Muchun, Thanks > 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).