Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp626055rdb; Tue, 31 Oct 2023 18:42:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGNQP1ZokPfN76Ru+Eh89MADAW659YuX83PZZThNRTdXZDLU+6tXsFXohm3Rrt2pEA1IDSQ X-Received: by 2002:a05:6870:cd93:b0:1e9:97cc:59ff with SMTP id xb19-20020a056870cd9300b001e997cc59ffmr17913231oab.9.1698802931681; Tue, 31 Oct 2023 18:42:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698802931; cv=none; d=google.com; s=arc-20160816; b=ad8zEsZ6cVuHC9b4K2bDjCfTZablnHlGY4q9bWGr7+amecwAbMTjodYXSCV8IFa/nU l+gD7hF7/mTZfRGcx02nmyscli3ORFELoh7ui8vnP0HnrFlpu7ZmrZCEtXIDvbWrEvR+ +9SFRlM0MfM0NqZZ2x9lqszGCJH9Rh3sw8kOXrJVpnrbeijrfxqm0l8uVu66Efk+LIbN Am8RR1OfQw8ToycpXGUvL6fftl3xKoALhn9ypwzcVXQ8zyNjwIfcvGKCqncA5edu3p47 yGI4PPU8i4cvr7OZyabhqRJrBA1HQWNP3SDkcrQivqBUAJ8r6lx1cHk/Bk1lM/W1FAWb nA8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=WBro9oe1zZ628ykQDCAf+JGt6uQlYsotXXea9Ml/Ecg=; fh=00tMhqfnZV2dv2MhkJ3hzROBD7xBQLOlRCHgqBe20Mw=; b=TWRklJAziFofM7XiDxo6nMGGS7Uq7Vbe+c3HWVuWWMTuNgZCi+UIacMLIyhRXP2lH7 oI7dqhvVu/mba3lXjn5YdxoQnNyf4ZEvaL9NE6NUwMb+h51fjNma7kx/gWKWGbkiIX9h t3yb3XhWZH+4cUc5BoKApV+hy//aq3kLgdoMxy0evswnoUKUlgIYDxd/tvvVW9h9FBO2 q8RKwdT7edvvwjYhuTeM4OOX1O6QLWEAzpNnSATbtq/aJrDgLXdoDf/LNzIgnjLj/4NL r1EYfUmt7hXlWFQyz/j8IfWXEcrl1+K9VHPH0i2/12LCsKDz6uvnrgtcejnuG7dWkhpP VMsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JPjvenVD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id k69-20020a638448000000b005b11e5a69f8si1887873pgd.531.2023.10.31.18.42.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 18:42:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JPjvenVD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id EEDF480984B5; Tue, 31 Oct 2023 18:41:53 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377010AbjKABlt (ORCPT + 99 others); Tue, 31 Oct 2023 21:41:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345060AbjKABlr (ORCPT ); Tue, 31 Oct 2023 21:41:47 -0400 Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADE0BED for ; Tue, 31 Oct 2023 18:41:45 -0700 (PDT) Received: by mail-io1-xd2d.google.com with SMTP id ca18e2360f4ac-7a69a71cc1dso226882739f.0 for ; Tue, 31 Oct 2023 18:41:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698802905; x=1699407705; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=WBro9oe1zZ628ykQDCAf+JGt6uQlYsotXXea9Ml/Ecg=; b=JPjvenVDE9wb8glY2N3q8K6d3R4WZHOS4QHRVfJQIwJc2vuCpQZ3j+B0gtWwVufNOI 2oSf63XGpInEEp40/YR1yEJVByJvCQpdIbQLGAREzFE8U2EMOuw/GHeZp1tFf3n5vHO3 lSOSnmOkBmnp4uT4/AgyqlVCaQ52kU3U5shX3ByCdj//27pFnN762XF54VSnixLNqvWo 4a4CEH18EpYV1/QI1cQAWNtkfk3UKL3UIPy5MMfDnRz0T9N/NA8B6Gir2uTn5GQU6aMn f64nzyls0vvf8fE6LSfxgHemgds7wXArJGEx7kzOdoecpMAlPLHs9uMkGot+hPXNc3iN Qxtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698802905; x=1699407705; h=content-transfer-encoding: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=WBro9oe1zZ628ykQDCAf+JGt6uQlYsotXXea9Ml/Ecg=; b=S1ZRcA3HP1z3DprTkZpQQabCT2V+K8z5ak4GPZCwnqz2CqH6EY/Fh+qCiVJq7E3YfZ Xav1QmZMdXVQ1hgmi53pcYLZ9lJ+0ryqCYvv2mpREL3bBTM2jouVANTjywzfWIlOT7LV GFtwfgxasWTs36SoumO5kZ04jXBrXb3uwNdPLglCMTqv5hkSvqzrCERWGqBzLg8ix4Q3 nYgMRYB98H8FQ74mRaDx1Z8Q2u0pm6sWTpFhhlL0oCtN6wLEPQ8Nan3U//U5NAuSfxNo CqiM0Cghp+Zgx/hkFxk1C7k0W9ezrETz9/iBoys1EPIwkT4G7whgANS1gXp8tj77G03d bq5w== X-Gm-Message-State: AOJu0YwBxJ4teIfDqBx4Zc3Lq0jrrQWXEKp283Te/WiZIuRRatzChGWK AQqZIxBaIavv08japBIgSOafCSJOWRPZIEl301JFI1ZYsgQ= X-Received: by 2002:a05:6602:3c8:b0:7a9:b1c9:4380 with SMTP id g8-20020a05660203c800b007a9b1c94380mr18950530iov.1.1698802904942; Tue, 31 Oct 2023 18:41:44 -0700 (PDT) MIME-Version: 1.0 References: <20231024203302.1920362-3-nphamcs@gmail.com> <20231101012614.186996-1-nphamcs@gmail.com> In-Reply-To: From: Nhat Pham Date: Tue, 31 Oct 2023 18:41:33 -0700 Message-ID: Subject: Re: [PATCH v4 2/5] zswap: make shrinking memcg-aware To: Yosry Ahmed 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 31 Oct 2023 18:41:54 -0700 (PDT) On Tue, Oct 31, 2023 at 6:33=E2=80=AFPM Yosry Ahmed = wrote: > > On Tue, Oct 31, 2023 at 6:26=E2=80=AFPM Nhat Pham wro= te: > > > > cc-ing Johannes, Roman, Shakeel, Muchun since you all know much more > > about memory controller + list_lru reparenting logic than me. > > > > There seems to be a race between memcg offlining and zswap=E2=80=99s > > cgroup-aware LRU implementation: > > > > 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() > > > > > > 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 > > 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). > > > > Within get_mem_cgroup_from_objcg(), none of the following seem > > sufficient to prevent this race: > > > > 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. > > > > > > 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). > > The objcgs hold refs to the memcg, which are dropped during > reparenting. How can we do reparenting in the release stage, which > IIUC happens after all refs are dropped? Oh I meant just the list_lru reparenting. The list_lru themselves don't hold any ref I believe, right? Then it's safe to perform this at the release stage. (also, I think I might have messed up the encoding for the email above. Let me know if people cannot view it, and I'll resend it :( )