Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp466099rdb; Thu, 19 Oct 2023 09:15:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEZ8RnLq+0qw0TEu3F0v9BOP+SsvjntAVmLDm6052jVdCuUvfXLvcm1pAmtuofOGmnh/a9O X-Received: by 2002:a05:6a20:4295:b0:161:346a:e7a1 with SMTP id o21-20020a056a20429500b00161346ae7a1mr3017875pzj.5.1697732147083; Thu, 19 Oct 2023 09:15:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697732147; cv=none; d=google.com; s=arc-20160816; b=HtzUgOlfqgynozNXWu/cvrokQePA5XYAfQRezUB3ebZuYP0kcVxFvP56A6TvgI32Az ZOrLDixYBRhtlCpcebKa/xjsuGOH+VY+pKhoKNHP3sBrbxXBP1/XJTYXfPfSyUZnbw50 Nc4aZTEAIiOenRXHXe/xfvR1fky0OVffQvxmAHeJ5Uq+S9so05qy9s4exkfey3nG6EyL a38pn/jB6+XastiPY+aYOsn+V3Suh2tjfFj4dubzt0FdG0+BV8q8admFDhkBlnGQdvNR DHsj8nezf1GX/WWhgIA7Zn2eWH1yxy3bDIlYHyQQd1CX1KSsbo1MQq71Mu1giIvbERUo uZfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=G/b8NX3LZ08KUi8N/JxWRGCAHy00FjZwd+7ev/CK28g=; fh=1XOB+vtV/A6wrE8cSXedyvkXYzfGs+xE3zLEbSaiZ8E=; b=srSDm9kKV59Q39EMnRPjgIFkXKkntm2ltcPENF62ceKlLEpNtD4ZX+4kykpXZ0EAKR wu5plJnD6czXC0s6fwUTCFXE7u/CI/TGO53cVI+gdqjewXYbiW/YoMQkP1zXV86fRKVs LyLQB57QVkNoJoqGDfPdVuvXFjsBwEY5iqWvF1I2x9x3ypUxTmiNOz2BRazsO4lw88lr CCcWmlLmbHggjYzYSIMnGKG4x9eqDIs6xT7CWyVG0GLZi+3P3ZUCGKS8CdWc66rl4AVd ycScb3AZC/vqz6EATmQ+HjQrtfynynxhmoQVEnF2Ew8S+7nTs145I7AhvFBI2EIpIWym p7XA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="RM/NO2tN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id h11-20020a65480b000000b00573f93787e4si4603538pgs.103.2023.10.19.09.15.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 09:15:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="RM/NO2tN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id E090E80BC53A; Thu, 19 Oct 2023 09:15:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345525AbjJSQP2 (ORCPT + 99 others); Thu, 19 Oct 2023 12:15:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235455AbjJSQPZ (ORCPT ); Thu, 19 Oct 2023 12:15:25 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2B18119 for ; Thu, 19 Oct 2023 09:15:20 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-53e3e7e478bso10897191a12.0 for ; Thu, 19 Oct 2023 09:15:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697732119; x=1698336919; darn=vger.kernel.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=G/b8NX3LZ08KUi8N/JxWRGCAHy00FjZwd+7ev/CK28g=; b=RM/NO2tNF1ngM2+D1mIzwhNmvRS/dHjJK771zbHrAETLv4HSPGjWq4prspNNhx7ZTQ S3/OGJ1XMuOIok0IWhkuz5g56vpXt4xKllBt1J8WFjfvQ9JP+Ypr5zHtOKcdM1nf/wBk Mbft9NK40VRysaxKT7l3VFtN92dTRuQ3oCCY32LFdE9E0Hfj/BbPmVUKZD3RNTNnCZmR 8fC/D5A4dk6PjJvF0sayh9rYFJI6jA2L7/kl+08BsLwyF0AYNk9nFoIHj0TDGKW7DgTr ieW7jo1Xqi409d0H1a2F3FdtMIWpfrkmgdhTi7JauEjt3Lp3s+UltR8xGtodqZSZylVr GX7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697732119; x=1698336919; 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=G/b8NX3LZ08KUi8N/JxWRGCAHy00FjZwd+7ev/CK28g=; b=FeNcvNPix7FzVQ0OYC3YY1B1F50dgCSfCiyy+CiR3XpRLLbfSbPxwhLmnJFbalxrgR P3AICmH66GEgfRnv0G7IIrQ6LOzuIuUL4k/gs4MI0zF4odSHfpQustFZST3zvhoiKrfW ibV0e+g6jNtmCma/UqleiTgqXUaqceAlvtlUFGNFmIDLbELbldsxNz2zIpw6VRh/ggVF dyTygD7/GOiU5dpGaBR2JL4isfm/UaqQs70gwW8LG0vBwKrD+mtEQ8mTytWUzujGa3gQ LDIGeyyq7/d+4zlkGR64bVkR1T8ik1PYx3YAIsfqgR5575ieANiJfsJgUKIMuztjFXSE +oXA== X-Gm-Message-State: AOJu0Yz8KSYHgHYze5IQnEcdBkubBSstH5nToohLDeFLIlTsdw8oWlbn TLxEWfLEaFd2ai6fbsjwVZRdphiTSxhYRIQoIAB9BA== X-Received: by 2002:a17:907:3e20:b0:9ae:594d:d3fc with SMTP id hp32-20020a1709073e2000b009ae594dd3fcmr2175026ejc.17.1697732119001; Thu, 19 Oct 2023 09:15:19 -0700 (PDT) MIME-Version: 1.0 References: <20231017232152.2605440-1-nphamcs@gmail.com> <20231017232152.2605440-3-nphamcs@gmail.com> In-Reply-To: From: Yosry Ahmed Date: Thu, 19 Oct 2023 09:14:40 -0700 Message-ID: Subject: Re: [PATCH v3 2/5] zswap: make shrinking memcg-aware To: Domenico Cerasuolo Cc: Nhat Pham , akpm@linux-foundation.org, hannes@cmpxchg.org, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, 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-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email 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 (agentk.vger.email [0.0.0.0]); Thu, 19 Oct 2023 09:15:44 -0700 (PDT) [..] > > > > > > +/********************************* > > > +* lru functions > > > +**********************************/ > > > +static bool zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry) > > > +{ > > > + struct mem_cgroup *memcg = get_mem_cgroup_from_entry(entry); > > > > Could we avoid the need for get/put with an rcu_read_lock() instead? > > I think we can, I'm not entirely sure of the consequences though. By the > look of it I'd say it's safe but I wouldn't trust my judgement on this. It just seems like we have a pattern of short-lived get/put. If RCU gives enough protection it should be simpler. IIUC taking a reference does not protect against offlining or reparenting, so I am not sure if taking a reference here would provide any more protection than > > > [..] > > > @@ -686,7 +716,36 @@ static int zswap_reclaim_entry(struct zswap_pool *pool) > > > zswap_entry_put(tree, entry); > > > unlock: > > > spin_unlock(&tree->lock); > > > - return ret ? -EAGAIN : 0; > > > + spin_lock(lock); > > > + return ret; > > > +} > > > + > > > +static int shrink_memcg(struct mem_cgroup *memcg) > > > +{ > > > + struct zswap_pool *pool; > > > + int nid, shrunk = 0; > > > + > > > + pool = zswap_pool_current_get(); > > > + if (!pool) > > > + return -EINVAL; > > > + > > > + /* > > > + * Skip zombies because their LRUs are reparented and we would be > > > + * reclaiming from the parent instead of the dead memcgroup. > > > > nit: s/memcgroup/memcg. > > > > > + */ > > > + if (memcg && !mem_cgroup_online(memcg)) > > > + goto out; > > > > If we move this above zswap_pool_current_get(), we can return directly > > and remove the label. I noticed we will return -EAGAIN if memcg is > > offline. IIUC -EAGAIN for the caller will move on to the next memcg, > > but I am wondering if a different errno would be clearer here. > > True, I remember spending some time staring at error codes but couldn't find a > better one. What if we use -EINVAL for retryable errors, and use something else > for the one where there is no pool? -ENODEV? Do you mean -EINVAL for non-retryable errors? Perhaps -ENOENT is more appropriate as a return for offline memcgs? > > > [..] > > > static void shrink_worker(struct work_struct *w) > > > @@ -695,10 +754,13 @@ static void shrink_worker(struct work_struct *w) > > > shrink_work); > > > int ret, failures = 0; > > > > > > + /* global reclaim will select cgroup in a round-robin fashion. */ > > > do { > > > - ret = zswap_reclaim_entry(pool); > > > + pool->next_shrink = mem_cgroup_iter(NULL, pool->next_shrink, NULL); > > > > Perhaps next_shrink_memcg is a better name here? > > Will change if you have a strong preference, I'd keep it shorter because it's > always used in conjunction with a memcg type or function. I'd rather have the more explicit name unless it causes some annoying line breaks or so. > > > > > > + > > > + ret = shrink_memcg(pool->next_shrink); > > > + > > > if (ret) { > > > - zswap_reject_reclaim_fail++; > > > if (ret != -EAGAIN) > > > break; > > > if (++failures == MAX_RECLAIM_RETRIES) > > > @@ -764,8 +826,7 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) > > > */ > > > kref_init(&pool->kref); > > > INIT_LIST_HEAD(&pool->list); > > > - INIT_LIST_HEAD(&pool->lru); > > > - spin_lock_init(&pool->lru_lock); > > > + list_lru_init_memcg(&pool->list_lru, NULL); > > > INIT_WORK(&pool->shrink_work, shrink_worker); > > > > > > zswap_pool_debug("created", pool); > > > @@ -831,6 +892,9 @@ static void zswap_pool_destroy(struct zswap_pool *pool) > > > > > > cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node); > > > free_percpu(pool->acomp_ctx); > > > + list_lru_destroy(&pool->list_lru); > > > + if (pool->next_shrink) > > > + mem_cgroup_put(pool->next_shrink); > > > for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) > > > zpool_destroy_pool(pool->zpools[i]); > > > kfree(pool); > > > @@ -1076,7 +1140,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry, > > > > > > /* try to allocate swap cache page */ > > > page = __read_swap_cache_async(swpentry, GFP_KERNEL, NULL, 0, > > > - &page_was_allocated); > > > + &page_was_allocated, true); > > > if (!page) { > > > ret = -ENOMEM; > > > goto fail; > > > @@ -1142,7 +1206,6 @@ static int zswap_writeback_entry(struct zswap_entry *entry, > > > /* start writeback */ > > > __swap_writepage(page, &wbc); > > > put_page(page); > > > - zswap_written_back_pages++; > > > > > > return ret; > > > > > > @@ -1199,8 +1262,10 @@ bool zswap_store(struct folio *folio) > > > struct scatterlist input, output; > > > struct crypto_acomp_ctx *acomp_ctx; > > > struct obj_cgroup *objcg = NULL; > > > + struct mem_cgroup *memcg = NULL; > > > struct zswap_pool *pool; > > > struct zpool *zpool; > > > + int lru_alloc_ret; > > > unsigned int dlen = PAGE_SIZE; > > > unsigned long handle, value; > > > char *buf; > > > @@ -1230,15 +1295,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); > > > + if (shrink_memcg(memcg)) { > > > + mem_cgroup_put(memcg); > > > + goto reject; > > > + } > > > + mem_cgroup_put(memcg); > > > + } > > > > > > /* reclaim space if needed */ > > > if (zswap_is_full()) { > > > @@ -1254,10 +1319,15 @@ bool zswap_store(struct folio *folio) > > > zswap_pool_reached_full = false; > > > } > > > > > > + pool = zswap_pool_current_get(); > > > + if (!pool) > > > + goto reject; > > > + > > > > Why do we need to move zswap_pool_current_get() up here? > > Ah, thanks. This is a leftover from a previous version where the pool was needed > to allocate the entry. > > > > > > > /* allocate entry */ > > > - entry = zswap_entry_cache_alloc(GFP_KERNEL); > > > + entry = zswap_entry_cache_alloc(GFP_KERNEL, page_to_nid(page)); > > > if (!entry) { > > > zswap_reject_kmemcache_fail++; > > > + zswap_pool_put(pool); > > > goto reject; > > > } > > > > > > @@ -1269,6 +1339,7 @@ bool zswap_store(struct folio *folio) > > > entry->length = 0; > > > entry->value = value; > > > atomic_inc(&zswap_same_filled_pages); > > > + zswap_pool_put(pool); > > > goto insert_entry; > > > } > > > kunmap_atomic(src); > > > @@ -1278,9 +1349,15 @@ bool zswap_store(struct folio *folio) > > > goto freepage; > > > > > > /* if entry is successfully added, it keeps the reference */ > > > - entry->pool = zswap_pool_current_get(); > > > - if (!entry->pool) > > > - goto freepage; > > > + entry->pool = pool; > > > + if (objcg) { > > > + memcg = get_mem_cgroup_from_objcg(objcg); > > > + lru_alloc_ret = memcg_list_lru_alloc(memcg, &pool->list_lru, GFP_KERNEL); > > > + mem_cgroup_put(memcg); > > > + > > > + if (lru_alloc_ret) > > > + goto freepage; > > > + } > > > > > > /* compress */ > > > acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx); > > > @@ -1358,9 +1435,8 @@ bool zswap_store(struct folio *folio) > > > zswap_invalidate_entry(tree, dupentry); > > > } > > > if (entry->length) { > > > - spin_lock(&entry->pool->lru_lock); > > > - list_add(&entry->lru, &entry->pool->lru); > > > - spin_unlock(&entry->pool->lru_lock); > > > + INIT_LIST_HEAD(&entry->lru); > > > + zswap_lru_add(&pool->list_lru, entry); > > > } > > > spin_unlock(&tree->lock); > > > > > > @@ -1373,8 +1449,8 @@ bool zswap_store(struct folio *folio) > > > > > > put_dstmem: > > > mutex_unlock(acomp_ctx->mutex); > > > - zswap_pool_put(entry->pool); > > > freepage: > > > + zswap_pool_put(entry->pool); > > > zswap_entry_cache_free(entry); > > > reject: > > > if (objcg) > > > @@ -1467,9 +1543,8 @@ bool zswap_load(struct folio *folio) > > > zswap_invalidate_entry(tree, entry); > > > folio_mark_dirty(folio); > > > } else if (entry->length) { > > > - spin_lock(&entry->pool->lru_lock); > > > - list_move(&entry->lru, &entry->pool->lru); > > > - spin_unlock(&entry->pool->lru_lock); > > > + zswap_lru_del(&entry->pool->list_lru, entry); > > > + zswap_lru_add(&entry->pool->list_lru, entry); > > > } > > > zswap_entry_put(tree, entry); > > > spin_unlock(&tree->lock); > > > -- > > > 2.34.1