From patchwork Mon Nov 27 19:36:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13470244 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 7AFFBC0755A for ; Mon, 27 Nov 2023 19:37:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6E946B02B4; Mon, 27 Nov 2023 14:37:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BA9BF6B02B6; Mon, 27 Nov 2023 14:37:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 938E46B02BD; Mon, 27 Nov 2023 14:37:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 822A46B02B4 for ; Mon, 27 Nov 2023 14:37:09 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 514B2B5C7C for ; Mon, 27 Nov 2023 19:37:09 +0000 (UTC) X-FDA: 81504742578.13.1D0DF64 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf20.hostedemail.com (Postfix) with ESMTP id 69A011C0010 for ; Mon, 27 Nov 2023 19:37:06 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=m3Yeeb44; spf=pass (imf20.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701113826; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=w2o+b0lga7F1iFKAolLKeFbumONlFF0UNEMGkNVQOs0=; b=FISO9AbN6lrVuHmD6zM6tfYhDteydLumB3f8c/k09IXuOiw2tVKZ0lp0vuA6xEpdQlDKyE 419l284m3EqF54wQNTTVPHWFyOxFu2vPdK2SRAnkFGIv/h9aVhV1av2Rvjali4PhuKul+1 PapQrLBWTblCY2rpBs6mW2MyZDeRdCw= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=m3Yeeb44; spf=pass (imf20.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701113826; a=rsa-sha256; cv=none; b=oB+Rh4/zTFrnkMo2ybWGn16NLy5MxC8CZKmQMMofArH9CfPirVtNbzNed5S0p+lAW8lI2m pOWq/5gjsyn2g8oKP9s9TpuNNnz4ahkAkZOaqeplrWPIq97pK3AYlCcEAz/04a/A6YEZS7 vE0aU2TtYXl+6NcS5uEJ+3ODhTCxpPs= Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6c3363a2b93so4359512b3a.3 for ; Mon, 27 Nov 2023 11:37:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701113825; x=1701718625; darn=kvack.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=w2o+b0lga7F1iFKAolLKeFbumONlFF0UNEMGkNVQOs0=; b=m3Yeeb44yBBOu2X7FDybh1lq7ZkTEokKc9byubcg+ZtKI6V2zQThNNkBg2q2kq71Rg QdTssvS0vO1n98i31J08hDVrGynFAjS4Zg8NM5l8Md8RZ0gxKhlBRnLR+cLU2ZAxwd3z zV+phLMkzC0nTWKGMGgNW8cy7HPFbtZvZiBwpDmvCD4gqHkSu5a+98a5aKGR5ZKv3m8R 6C1WMBwulLsNrEtgHiitGpwczZmPFl7W4qz+q34bVr98cO7AFXt8PhBA70id8U4JfOC/ 0G9NoSgu2JxOyGeK8Rn25WvTojZJ4hKdcgX995eLMpn5ccIY3AMi+tG5pCYmNvfevJRZ F2EA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701113825; x=1701718625; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=w2o+b0lga7F1iFKAolLKeFbumONlFF0UNEMGkNVQOs0=; b=KDzcipbbJcdHdDWUEeBz0BdJskRif51ewijPViDhY/pOiXEEFqRxn9kLwAgibm9sPu Z0qJQFzALjjTB0Jb6wFpHgW4ibBWU6JtEycEBBRZUxaZfPb9qzqgkOIIZmHH045P+tdL e4rONoi/f/ozBcvVX92x/BC8Vef7S3yL608O4O1Pk7aqeBwiKhYtUA5sSHUb9Ldkx9VB QQtRhGp/pvXHRPflvChNkdXzfoVyrUEsae8FYsdqbVUvh/LbeIuVNeaob9o8QRP3v3wi SnaL9c9eM+REN6flc8LMDlAbMSb6SKsxcjQdi8NNeerVyv6IKiWHBvRU3TA3GpoRqqQP cH8Q== X-Gm-Message-State: AOJu0YyCjgwoLN6sEYk6c13jauA+g42WGh6oLiFx6XITQuAdxkNEjvAI 8dnurr4cvrPWGWDtU/F9RRI= X-Google-Smtp-Source: AGHT+IG/uwQaoZ4e4LvCxtlUeF1P7ML/roZ5Q6yMqYjZkWur02QHJTY2f52zz1gH5lw2ENGG13bhSQ== X-Received: by 2002:a05:6a00:1990:b0:6cb:911b:8000 with SMTP id d16-20020a056a00199000b006cb911b8000mr15049485pfl.28.1701113825034; Mon, 27 Nov 2023 11:37:05 -0800 (PST) Received: from localhost (fwdproxy-prn-005.fbsv.net. [2a03:2880:ff:5::face:b00c]) by smtp.gmail.com with ESMTPSA id r27-20020aa78b9b000000b006cb6fee548esm7768292pfd.91.2023.11.27.11.37.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 11:37:04 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.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, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v6 1/6] list_lru: allows explicit memcg and NUMA node selection Date: Mon, 27 Nov 2023 11:36:58 -0800 Message-Id: <20231127193703.1980089-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127193703.1980089-1-nphamcs@gmail.com> References: <20231127193703.1980089-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 69A011C0010 X-Rspam-User: X-Stat-Signature: u6h19y5hcn9yyghe414pwicaqkxesq9e X-Rspamd-Server: rspam01 X-HE-Tag: 1701113826-151762 X-HE-Meta: U2FsdGVkX19HIRW55XsLjKel9flYDJGLNUAatw2pmp5SqShVu9ba6lVxwEnk+ZI9EzErpo1/Y0BXGkShMU88rA+G6d2QW+g9M3FSYKJ8bc5JuTVWF+TfryTjOrBJgkVtbv63JZ+N1ptey9YID+7BI06viir3CDzVAvm49h3MjLQwai4WUds3ObK6iAxqgIbAqupS56pMcvxmHN9mu4uc4127blR6ufyPWd9Tfu0gRwWMg+xViWqtirClUJLGxxDFoUZYJT5GOPuiy+hKDlvxrSK+LTb4/qIgw3birXi07p75QF6zxdSFfvg2OIFlC/zojKTAwf6JXs7wMl2YTCFlQ4ZlmYBAbGXXS26DuNudZSORYXN66A1+XlzHSFXWC4F4KbYByZSwSM/b2d2KYORBl4tO0VvKjBYHkGplMgM6k5w3PV/R7dEEIOdtR97YCHGwITSl1OKi5VW4WMAGOqTuB8XihRowJva29no6E4foOj/5HIrbto9Qr92vlUI1Dcm1gOwGlT400BGls/vLqkBQLnuWPvjQfPOs+dsP02y9J21ad35gYHKGSzWHQcYc9rWH4/KWgurcWLZUuWxAq7iwFR7fJ59QOxISaGAGxMCc9GUL0TDvzOWB281BSUpiNP/sAOd1/ptG1ui524fGcqPQhTZsno4uuY0C628I1SYv+9DL8QA8Ils78Va5EDC7W6g2jAzGzAIAfHjwoRbwaaeIFVP60zT89jQehucxeNgVrzeKVkp9JomhMJJTm8XGx6WFnYVz+oE7lA1ewKkMr9TLkotsGeDqNpfA9mxc8GZkAqWkje6Fnp/TiJGMSGug+4lmGNhUWl4xS3uYl3GlP5sWdAoPyLQAK/XEIIi7atBS2NoMiSniyDMFS9r8nF/ftYBrngIlPRZaKCu5D5Nw+aL5HvKMPZdSxOM3ocDJQASVmqJTPobeXMwwwvbXalmblULYXgZuTibJpheTIy3Voa+ F+vLeyYi w0Tmehv2m47drFWSWsXhZq2gWa3sdAapiloxoe5sKVHzVcQDxcCIQBOe39TLc0H9WEyDYOWwKxOkphelRr1JN+a5gsc53QpTi3r587661C3HDyAKYsYZIvqTBUd8K3PjsmY5JgNMtmXZ4tk3ZLhEhW1YT4zqshng/DfWOb81jcw68lEHluccp6dB+NX1XzBacyqqghv2e9/CbgCGtsgflZNg2/hBFtGSoSLCCtwSxnKj9BcX5Rh+WHcrJ39qQX6pZojV9Hk1DobC38iCevQoncLYR/S6P71F4qJYuv0EubkqMqPwXUhZZwKkcYtxSQVdCGeLAp/QO7NosDvSHEUwXudvFkLWW+haY3LapUzoRNOnQi5Vb9aUZVdKjL2HUtYo8Su1sc44RQQcN/f0/0nHqe+vo9R8/zylQYcjTgSkL+jNGNyYnzKjKw+Q0F2UMFjDfwElqae6DZIeh4YE+5ujyX0PtHz0UWHpGgKY+D6fT+59hXl4zrKIR1VVCWDfvxk6KaJheaXZhApkieTjai8eoW167fKKU468O0WccxbZrNyBNYl0= 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: The interface of list_lru is based on the assumption that the list node and the data it represents belong to the same allocated on the correct node/memcg. While this assumption is valid for existing slab objects LRU such as dentries and inodes, it is undocumented, and rather inflexible for certain potential list_lru users (such as the upcoming zswap shrinker and the THP shrinker). It has caused us a lot of issues during our development. This patch changes list_lru interface so that the caller must explicitly specify numa node and memcg when adding and removing objects. The old list_lru_add() and list_lru_del() are renamed to list_lru_add_obj() and list_lru_del_obj(), respectively. It also extends the list_lru API with a new function, list_lru_putback, which undoes a previous list_lru_isolate call. Unlike list_lru_add, it does not increment the LRU node count (as list_lru_isolate does not decrement the node count). list_lru_putback also allows for explicit memcg and NUMA node selection. Suggested-by: Johannes Weiner Signed-off-by: Nhat Pham --- drivers/android/binder_alloc.c | 5 ++-- fs/dcache.c | 8 +++-- fs/gfs2/quota.c | 6 ++-- fs/inode.c | 4 +-- fs/nfs/nfs42xattr.c | 8 ++--- fs/nfsd/filecache.c | 4 +-- fs/xfs/xfs_buf.c | 6 ++-- fs/xfs/xfs_dquot.c | 2 +- fs/xfs/xfs_qm.c | 2 +- include/linux/list_lru.h | 54 ++++++++++++++++++++++++++++++++-- mm/list_lru.c | 48 +++++++++++++++++++++++++----- mm/workingset.c | 4 +-- 12 files changed, 116 insertions(+), 35 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 138f6d43d13b..e80669d4e037 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -285,7 +285,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate, trace_binder_free_lru_start(alloc, index); - ret = list_lru_add(&binder_alloc_lru, &page->lru); + ret = list_lru_add_obj(&binder_alloc_lru, &page->lru); WARN_ON(!ret); trace_binder_free_lru_end(alloc, index); @@ -848,7 +848,7 @@ void binder_alloc_deferred_release(struct binder_alloc *alloc) if (!alloc->pages[i].page_ptr) continue; - on_lru = list_lru_del(&binder_alloc_lru, + on_lru = list_lru_del_obj(&binder_alloc_lru, &alloc->pages[i].lru); page_addr = alloc->buffer + i * PAGE_SIZE; binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, @@ -1287,4 +1287,3 @@ int binder_alloc_copy_from_buffer(struct binder_alloc *alloc, return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset, dest, bytes); } - diff --git a/fs/dcache.c b/fs/dcache.c index c82ae731df9a..2ba37643b9c5 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -428,7 +428,8 @@ static void d_lru_add(struct dentry *dentry) this_cpu_inc(nr_dentry_unused); if (d_is_negative(dentry)) this_cpu_inc(nr_dentry_negative); - WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru)); + WARN_ON_ONCE(!list_lru_add_obj( + &dentry->d_sb->s_dentry_lru, &dentry->d_lru)); } static void d_lru_del(struct dentry *dentry) @@ -438,7 +439,8 @@ static void d_lru_del(struct dentry *dentry) this_cpu_dec(nr_dentry_unused); if (d_is_negative(dentry)) this_cpu_dec(nr_dentry_negative); - WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru)); + WARN_ON_ONCE(!list_lru_del_obj( + &dentry->d_sb->s_dentry_lru, &dentry->d_lru)); } static void d_shrink_del(struct dentry *dentry) @@ -1240,7 +1242,7 @@ static enum lru_status dentry_lru_isolate(struct list_head *item, * * This is guaranteed by the fact that all LRU management * functions are intermediated by the LRU API calls like - * list_lru_add and list_lru_del. List movement in this file + * list_lru_add_obj and list_lru_del_obj. List movement in this file * only ever occur through this functions or through callbacks * like this one, that are called from the LRU API. * diff --git a/fs/gfs2/quota.c b/fs/gfs2/quota.c index 95dae7838b4e..b57f8c7b35be 100644 --- a/fs/gfs2/quota.c +++ b/fs/gfs2/quota.c @@ -271,7 +271,7 @@ static struct gfs2_quota_data *gfs2_qd_search_bucket(unsigned int hash, if (qd->qd_sbd != sdp) continue; if (lockref_get_not_dead(&qd->qd_lockref)) { - list_lru_del(&gfs2_qd_lru, &qd->qd_lru); + list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru); return qd; } } @@ -344,7 +344,7 @@ static void qd_put(struct gfs2_quota_data *qd) } qd->qd_lockref.count = 0; - list_lru_add(&gfs2_qd_lru, &qd->qd_lru); + list_lru_add_obj(&gfs2_qd_lru, &qd->qd_lru); spin_unlock(&qd->qd_lockref.lock); } @@ -1517,7 +1517,7 @@ void gfs2_quota_cleanup(struct gfs2_sbd *sdp) lockref_mark_dead(&qd->qd_lockref); spin_unlock(&qd->qd_lockref.lock); - list_lru_del(&gfs2_qd_lru, &qd->qd_lru); + list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru); list_add(&qd->qd_lru, &dispose); } spin_unlock(&qd_lock); diff --git a/fs/inode.c b/fs/inode.c index f238d987dec9..ef2034a985e0 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -464,7 +464,7 @@ static void __inode_add_lru(struct inode *inode, bool rotate) if (!mapping_shrinkable(&inode->i_data)) return; - if (list_lru_add(&inode->i_sb->s_inode_lru, &inode->i_lru)) + if (list_lru_add_obj(&inode->i_sb->s_inode_lru, &inode->i_lru)) this_cpu_inc(nr_unused); else if (rotate) inode->i_state |= I_REFERENCED; @@ -482,7 +482,7 @@ void inode_add_lru(struct inode *inode) static void inode_lru_list_del(struct inode *inode) { - if (list_lru_del(&inode->i_sb->s_inode_lru, &inode->i_lru)) + if (list_lru_del_obj(&inode->i_sb->s_inode_lru, &inode->i_lru)) this_cpu_dec(nr_unused); } diff --git a/fs/nfs/nfs42xattr.c b/fs/nfs/nfs42xattr.c index 2ad66a8922f4..49aaf28a6950 100644 --- a/fs/nfs/nfs42xattr.c +++ b/fs/nfs/nfs42xattr.c @@ -132,7 +132,7 @@ nfs4_xattr_entry_lru_add(struct nfs4_xattr_entry *entry) lru = (entry->flags & NFS4_XATTR_ENTRY_EXTVAL) ? &nfs4_xattr_large_entry_lru : &nfs4_xattr_entry_lru; - return list_lru_add(lru, &entry->lru); + return list_lru_add_obj(lru, &entry->lru); } static bool @@ -143,7 +143,7 @@ nfs4_xattr_entry_lru_del(struct nfs4_xattr_entry *entry) lru = (entry->flags & NFS4_XATTR_ENTRY_EXTVAL) ? &nfs4_xattr_large_entry_lru : &nfs4_xattr_entry_lru; - return list_lru_del(lru, &entry->lru); + return list_lru_del_obj(lru, &entry->lru); } /* @@ -349,7 +349,7 @@ nfs4_xattr_cache_unlink(struct inode *inode) oldcache = nfsi->xattr_cache; if (oldcache != NULL) { - list_lru_del(&nfs4_xattr_cache_lru, &oldcache->lru); + list_lru_del_obj(&nfs4_xattr_cache_lru, &oldcache->lru); oldcache->inode = NULL; } nfsi->xattr_cache = NULL; @@ -474,7 +474,7 @@ nfs4_xattr_get_cache(struct inode *inode, int add) kref_get(&cache->ref); nfsi->xattr_cache = cache; cache->inode = inode; - list_lru_add(&nfs4_xattr_cache_lru, &cache->lru); + list_lru_add_obj(&nfs4_xattr_cache_lru, &cache->lru); } spin_unlock(&inode->i_lock); diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c index ef063f93fde9..6c2decfdeb4b 100644 --- a/fs/nfsd/filecache.c +++ b/fs/nfsd/filecache.c @@ -322,7 +322,7 @@ nfsd_file_check_writeback(struct nfsd_file *nf) static bool nfsd_file_lru_add(struct nfsd_file *nf) { set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags); - if (list_lru_add(&nfsd_file_lru, &nf->nf_lru)) { + if (list_lru_add_obj(&nfsd_file_lru, &nf->nf_lru)) { trace_nfsd_file_lru_add(nf); return true; } @@ -331,7 +331,7 @@ static bool nfsd_file_lru_add(struct nfsd_file *nf) static bool nfsd_file_lru_remove(struct nfsd_file *nf) { - if (list_lru_del(&nfsd_file_lru, &nf->nf_lru)) { + if (list_lru_del_obj(&nfsd_file_lru, &nf->nf_lru)) { trace_nfsd_file_lru_del(nf); return true; } diff --git a/fs/xfs/xfs_buf.c b/fs/xfs/xfs_buf.c index 545c7991b9b5..669332849680 100644 --- a/fs/xfs/xfs_buf.c +++ b/fs/xfs/xfs_buf.c @@ -169,7 +169,7 @@ xfs_buf_stale( atomic_set(&bp->b_lru_ref, 0); if (!(bp->b_state & XFS_BSTATE_DISPOSE) && - (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru))) + (list_lru_del_obj(&bp->b_target->bt_lru, &bp->b_lru))) atomic_dec(&bp->b_hold); ASSERT(atomic_read(&bp->b_hold) >= 1); @@ -1047,7 +1047,7 @@ xfs_buf_rele( * buffer for the LRU and clear the (now stale) dispose list * state flag */ - if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) { + if (list_lru_add_obj(&bp->b_target->bt_lru, &bp->b_lru)) { bp->b_state &= ~XFS_BSTATE_DISPOSE; atomic_inc(&bp->b_hold); } @@ -1060,7 +1060,7 @@ xfs_buf_rele( * was on was the disposal list */ if (!(bp->b_state & XFS_BSTATE_DISPOSE)) { - list_lru_del(&bp->b_target->bt_lru, &bp->b_lru); + list_lru_del_obj(&bp->b_target->bt_lru, &bp->b_lru); } else { ASSERT(list_empty(&bp->b_lru)); } diff --git a/fs/xfs/xfs_dquot.c b/fs/xfs/xfs_dquot.c index ac6ba646624d..49f619f5aa96 100644 --- a/fs/xfs/xfs_dquot.c +++ b/fs/xfs/xfs_dquot.c @@ -1064,7 +1064,7 @@ xfs_qm_dqput( struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo; trace_xfs_dqput_free(dqp); - if (list_lru_add(&qi->qi_lru, &dqp->q_lru)) + if (list_lru_add_obj(&qi->qi_lru, &dqp->q_lru)) XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused); } xfs_dqunlock(dqp); diff --git a/fs/xfs/xfs_qm.c b/fs/xfs/xfs_qm.c index 94a7932ac570..67d0a8564ff3 100644 --- a/fs/xfs/xfs_qm.c +++ b/fs/xfs/xfs_qm.c @@ -171,7 +171,7 @@ xfs_qm_dqpurge( * hits zero, so it really should be on the freelist here. */ ASSERT(!list_empty(&dqp->q_lru)); - list_lru_del(&qi->qi_lru, &dqp->q_lru); + list_lru_del_obj(&qi->qi_lru, &dqp->q_lru); XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused); xfs_qm_dqdestroy(dqp); diff --git a/include/linux/list_lru.h b/include/linux/list_lru.h index db86ad78d428..7675a48a0701 100644 --- a/include/linux/list_lru.h +++ b/include/linux/list_lru.h @@ -75,6 +75,8 @@ void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *paren * list_lru_add: add an element to the lru list's tail * @lru: the lru pointer * @item: the item to be added. + * @nid: the node id of the sublist to add the item to. + * @memcg: the cgroup of the sublist to add the item to. * * If the element is already part of a list, this function returns doing * nothing. Therefore the caller does not need to keep state about whether or @@ -87,12 +89,28 @@ void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *paren * * Return: true if the list was updated, false otherwise */ -bool list_lru_add(struct list_lru *lru, struct list_head *item); +bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg); /** - * list_lru_del: delete an element to the lru list + * list_lru_add_obj: add an element to the lru list's tail + * @lru: the lru pointer + * @item: the item to be added. + * + * This function is similar to list_lru_add(), but the NUMA node and the + * memcg of the sublist is determined by @item list_head. This assumption is + * valid for slab objects LRU such as dentries, inodes, etc. + * + * Return value: true if the list was updated, false otherwise + */ +bool list_lru_add_obj(struct list_lru *lru, struct list_head *item); + +/** + * list_lru_del: delete an element from the lru list * @lru: the lru pointer * @item: the item to be deleted. + * @nid: the node id of the sublist to delete the item from. + * @memcg: the cgroup of the sublist to delete the item from. * * This function works analogously as list_lru_add() in terms of list * manipulation. The comments about an element already pertaining to @@ -100,7 +118,21 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item); * * Return: true if the list was updated, false otherwise */ -bool list_lru_del(struct list_lru *lru, struct list_head *item); +bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg); + +/** + * list_lru_del_obj: delete an element from the lru list + * @lru: the lru pointer + * @item: the item to be deleted. + * + * This function is similar to list_lru_del(), but the NUMA node and the + * memcg of the sublist is determined by @item list_head. This assumption is + * valid for slab objects LRU such as dentries, inodes, etc. + * + * Return value: true if the list was updated, false otherwise. + */ +bool list_lru_del_obj(struct list_lru *lru, struct list_head *item); /** * list_lru_count_one: return the number of objects currently held by @lru @@ -138,6 +170,22 @@ static inline unsigned long list_lru_count(struct list_lru *lru) void list_lru_isolate(struct list_lru_one *list, struct list_head *item); void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item, struct list_head *head); +/** + * list_lru_putback: undo list_lru_isolate + * @lru: the lru pointer. + * @item: the item to put back. + * @nid: the node id of the sublist to put the item back to. + * @memcg: the cgroup of the sublist to put the item back to. + * + * Put back an isolated item into its original LRU. Note that unlike + * list_lru_add, this does not increment the node LRU count (as + * list_lru_isolate does not originally decrement this count). + * + * Since we might have dropped the LRU lock in between, recompute list_lru_one + * from the node's id and memcg. + */ +void list_lru_putback(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg); typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item, struct list_lru_one *list, spinlock_t *lock, void *cb_arg); diff --git a/mm/list_lru.c b/mm/list_lru.c index a05e5bef3b40..fcca67ac26ec 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -116,21 +116,19 @@ list_lru_from_kmem(struct list_lru *lru, int nid, void *ptr, } #endif /* CONFIG_MEMCG_KMEM */ -bool list_lru_add(struct list_lru *lru, struct list_head *item) +bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg) { - int nid = page_to_nid(virt_to_page(item)); struct list_lru_node *nlru = &lru->node[nid]; - struct mem_cgroup *memcg; struct list_lru_one *l; spin_lock(&nlru->lock); if (list_empty(item)) { - l = list_lru_from_kmem(lru, nid, item, &memcg); + l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg)); list_add_tail(item, &l->list); /* Set shrinker bit if the first element was added */ if (!l->nr_items++) - set_shrinker_bit(memcg, nid, - lru_shrinker_id(lru)); + set_shrinker_bit(memcg, nid, lru_shrinker_id(lru)); nlru->nr_items++; spin_unlock(&nlru->lock); return true; @@ -140,15 +138,25 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item) } EXPORT_SYMBOL_GPL(list_lru_add); -bool list_lru_del(struct list_lru *lru, struct list_head *item) +bool list_lru_add_obj(struct list_lru *lru, struct list_head *item) { int nid = page_to_nid(virt_to_page(item)); + struct mem_cgroup *memcg = list_lru_memcg_aware(lru) ? + mem_cgroup_from_slab_obj(item) : NULL; + + return list_lru_add(lru, item, nid, memcg); +} +EXPORT_SYMBOL_GPL(list_lru_add_obj); + +bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg) +{ struct list_lru_node *nlru = &lru->node[nid]; struct list_lru_one *l; spin_lock(&nlru->lock); if (!list_empty(item)) { - l = list_lru_from_kmem(lru, nid, item, NULL); + l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg)); list_del_init(item); l->nr_items--; nlru->nr_items--; @@ -160,6 +168,16 @@ bool list_lru_del(struct list_lru *lru, struct list_head *item) } EXPORT_SYMBOL_GPL(list_lru_del); +bool list_lru_del_obj(struct list_lru *lru, struct list_head *item) +{ + int nid = page_to_nid(virt_to_page(item)); + struct mem_cgroup *memcg = list_lru_memcg_aware(lru) ? + mem_cgroup_from_slab_obj(item) : NULL; + + return list_lru_del(lru, item, nid, memcg); +} +EXPORT_SYMBOL_GPL(list_lru_del_obj); + void list_lru_isolate(struct list_lru_one *list, struct list_head *item) { list_del_init(item); @@ -175,6 +193,20 @@ void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item, } EXPORT_SYMBOL_GPL(list_lru_isolate_move); +void list_lru_putback(struct list_lru *lru, struct list_head *item, int nid, + struct mem_cgroup *memcg) +{ + struct list_lru_one *list = + list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg)); + + if (list_empty(item)) { + list_add_tail(item, &list->list); + if (!list->nr_items++) + set_shrinker_bit(memcg, nid, lru_shrinker_id(lru)); + } +} +EXPORT_SYMBOL_GPL(list_lru_putback); + unsigned long list_lru_count_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg) { diff --git a/mm/workingset.c b/mm/workingset.c index b192e44a0e7c..c17d45c6f29b 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -615,12 +615,12 @@ void workingset_update_node(struct xa_node *node) if (node->count && node->count == node->nr_values) { if (list_empty(&node->private_list)) { - list_lru_add(&shadow_nodes, &node->private_list); + list_lru_add_obj(&shadow_nodes, &node->private_list); __inc_lruvec_kmem_state(node, WORKINGSET_NODES); } } else { if (!list_empty(&node->private_list)) { - list_lru_del(&shadow_nodes, &node->private_list); + list_lru_del_obj(&shadow_nodes, &node->private_list); __dec_lruvec_kmem_state(node, WORKINGSET_NODES); } } From patchwork Mon Nov 27 19:36:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13470245 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 B412DC07D5B for ; Mon, 27 Nov 2023 19:37:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E8DCA6B02B6; Mon, 27 Nov 2023 14:37:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D313C6B02BE; Mon, 27 Nov 2023 14:37:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FC7B6B02B8; Mon, 27 Nov 2023 14:37:09 -0500 (EST) 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 8967E6B02B6 for ; Mon, 27 Nov 2023 14:37:09 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 58986406E8 for ; Mon, 27 Nov 2023 19:37:09 +0000 (UTC) X-FDA: 81504742578.13.A91BACB Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf11.hostedemail.com (Postfix) with ESMTP id 7EFB44000B for ; Mon, 27 Nov 2023 19:37:07 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mPNI9lsR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701113827; a=rsa-sha256; cv=none; b=YrhdN8lYKaA9VadzOEBOOH5ef0pW1vmU+w+jC0vuDdBnhkM46jKkJ2C21fAAPS15Yz4w1i 0QERC7KaUQ3tmq2m70z+J8zBtfJ82fMhh0ci1r6yAGU7ynYUClopNW6bfiE3EXJ8cf6q/v DOePiYwqIoGs31Kwe/pRLraAGxaSvyA= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mPNI9lsR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701113827; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Yk3Ra7LxPSTcjlQasqmaE6aQE/pvNfDE/tLSZ7yZw8U=; b=MoFoM0j71Mu7pCQgTVpwARHCZ0hrtXqNIbG/PVBZ0dtV0SGnyU+otdbTxmNYfULaLUPA5+ 3xSeCVoJsEWjh3vqCLXvjuYM9dFd6cQylYiRR6m6K57gAPr2vyTQPA67Zu9sws4fa0wOOu mOKbnMdGNE63+Ki3ScT+bF6uvCE42NI= Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2839c113cbcso3693850a91.3 for ; Mon, 27 Nov 2023 11:37:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701113826; x=1701718626; darn=kvack.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=Yk3Ra7LxPSTcjlQasqmaE6aQE/pvNfDE/tLSZ7yZw8U=; b=mPNI9lsRwjtFqCfUnrk+pW7RMev7kItGPEj0eh9ELLSzfQvAjXIW+jS5PjC5FARM66 eJycHqV5ey1ET2UK+a3oKsu4TJm3PzuXGWmVpylV/6WQasRqJtNlZy97fTGQvd5KQzC+ pQuLwmEN7HfZLraNDZW9ztOgQqQjwZOqpJMZ+h4H6ay/piPsRe85EmPpKpktTEJn9peE szphypO7vbtPlmJRTeufxyw9iyiuuE8qZeF13v6Hrn1hCl4Ik4KOO2lrkrBrHB5cOf7i 5c37SAWeejM1UcMgRycyao+e3PfdqZ4V7rdPc7i6JTkTC9Rea/qkn9AMXXW4+Z0pSI/Y yVXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701113826; x=1701718626; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Yk3Ra7LxPSTcjlQasqmaE6aQE/pvNfDE/tLSZ7yZw8U=; b=ouxUwJVPJp13bvFrHVD+5KpUmkZr/Hyy9ze8MsFW91Nd42VtrPgWXAf6H++Lp6xTph yDXgR6fE3Hpwf7RbxIrYEPAfVyIEOwe6B2ljM2GAmuxMgaNM6yjBTUTgFLXlDvTZblVu nPyV3bD1R710NKOXFHV7pYwRSoYONUK9UeEEpJs8koiQRRUWXSe0YQJxvEbzIcZrRqGg ufYJI68GbS+4Bv0XG7lQxV2COpQcDhoBDwf/0pXBd+wauBwP8jLOM8ea++8v/5gNYsQ3 Pg08biyuiaMzQFuYXMXNJQh02WG6ARtrcMFQa8leVLlN4elBz8l4Zh9565R0cQrxfGUk cN6g== X-Gm-Message-State: AOJu0YwUE/z9BZXnol2C9adOjeJ6GaaP7AeUFz3Q/HYbVMHiEi5k1K8P T90O2SOpdi7a6R2HqpniOz4= X-Google-Smtp-Source: AGHT+IESo6RUTEUw2quoScohVLKJzp+po5d5yitAfhgPXdhC6sxqbbGd5iWNr4rtvNf8otQ+V25FDw== X-Received: by 2002:a17:90b:3b92:b0:285:a72f:966f with SMTP id pc18-20020a17090b3b9200b00285a72f966fmr8454941pjb.46.1701113826185; Mon, 27 Nov 2023 11:37:06 -0800 (PST) Received: from localhost (fwdproxy-prn-026.fbsv.net. [2a03:2880:ff:1a::face:b00c]) by smtp.gmail.com with ESMTPSA id a21-20020a17090ad81500b002851e283c21sm7822831pjv.12.2023.11.27.11.37.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 11:37:05 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.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, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v6 2/6] memcontrol: allows mem_cgroup_iter() to check for onlineness Date: Mon, 27 Nov 2023 11:36:59 -0800 Message-Id: <20231127193703.1980089-3-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127193703.1980089-1-nphamcs@gmail.com> References: <20231127193703.1980089-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 7EFB44000B X-Stat-Signature: kgx6wqzct9ryoahqdzni7pfsfsqzdqqy X-HE-Tag: 1701113827-205626 X-HE-Meta: U2FsdGVkX1/BBcdSzokmxD2H/TefWLgdSe9ZFKHC4YEwDH7qRBEq23O+Ot0ZaNa5JoyV8+06UtHIaHqhP1drovjvQ7IHevs4NBrR6U45+TklJLFK9PPcwyhCmz4USCeRKAKi8zCm6RoIP7Fxks1a65SnMab9jbo6CXugowzyyEvPPqCmEvDX8D4vafMpZ6StcN+fG8j/mRBsfT3gfMUfqQs45yVV81E1oukANLAdyB4DY0VVo5eEGG+YyGrCCzmZroBvAztGm9g/QAnOW+tL8ksUQbsOCOL97C9IOW9AlsYFJ3fTykWw3o3ULsCI4F6uLCs+XeOKgNl7Jf+HWAT/Ibn9aHA7EIR5wGEEha0dRYBRk4AfqT9JXaXJDx104tZr6hk/LOV3ZXin6rIZ2gJCxEXmcM+KeTUB4l8bUusauqSNR9AKoGfh+1k4glvOoXPzrFoqm9zmiW8uk2JpWFjKP+1z4jde/ilQbQ4akqik9ttVnccRd854/IPFfT+NECdCwLCi4E0y5wwjpIBgXL3j4SpKWsRcSK12H+c6hS3G95OdZ7sBFC5GSfymWrnbPWKfm1SYoeK2t51EH9AtYbuXJSY5Y5D7MGwbkcSyzZIegF3kyo3BbRR13i1IepZgs3Qmis7nx+3dAeC82qSHbfaivp2pvciio28+nd60TUfhKuU8fYD/qO4kqJM2wX9HjrfIpq1pyTAxVAIIG9Y7N7RfQX6OQUocHQYgP3UWO15PwTSPZGFejVTM5bypEpONKoDrK+9ILpcbyerrkAYHmG0zkEmE+OUISYJ6lljFXjcwtvK7bPG8l3d9EVAV3PvzN3jVEpEr7/WGOncqG8AtNlZ6ShQOP3nfIRuhc+IwJ+Wg62P8hrpPRNwGPZXgLD5aO1jn6vxEtI0dzimDmDzgN+fFJtg6MbypypBpwVenJdnDSj7U9BgYhGg+TP65b5tBHnLpn+qpN+Dz75kqRpfFUhW 7+W/1gyb sMd2EC5O/pBUpRcMSxoR0TODmCh7NKFMCWHkwsmre8/BUE1stwBBWaCKg0ip1CmMAPTgDyqPVBnSLmCRKx/gOAoF4ZM+S116hY/0RYl2hMsB7ttXq2uuH187SJfIyCBFzVsM+Ks3NFl8FhH9ZhFwJkLS/PAq3EIRzcp0HEvUaf4GXpTJYPTl+GIu4BsLWAHJeceSY1BdYZzOLmlOavvnfUU9QDX8N1wE+WrhMFCO4JYcnQyjFFdzOT0+AepBglWk5eptFYpjts/EVMWpVgZX7ECnZtlMz1Lu0muZV7Gh9IGHVy0CvBZhas2yyYuQvuCbM3WzyosNXDjPugu5iX+dmRJSUb4pYGtTmIN+KiI+78Q9Bs3JGd3V9cOv6+4bW1apekreSh/9cfASVoeVWc1+wbYrTkSMRkTUyMH06gp46BlrbJEzBvv8G3nIrlBA1/NVIvjST 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: The new zswap writeback scheme requires an online-only memcg hierarchy traversal. Add a new parameter to mem_cgroup_iter() to check for onlineness before returning. Signed-off-by: Nhat Pham --- include/linux/memcontrol.h | 4 ++-- mm/memcontrol.c | 17 ++++++++++------- mm/shrinker.c | 4 ++-- mm/vmscan.c | 26 +++++++++++++------------- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 7bdcf3020d7a..86adce081a08 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -832,7 +832,7 @@ static inline void mem_cgroup_put(struct mem_cgroup *memcg) struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *, struct mem_cgroup *, - struct mem_cgroup_reclaim_cookie *); + struct mem_cgroup_reclaim_cookie *, bool online); void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *); void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, int (*)(struct task_struct *, void *), void *arg); @@ -1381,7 +1381,7 @@ static inline struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, static inline struct mem_cgroup * mem_cgroup_iter(struct mem_cgroup *root, struct mem_cgroup *prev, - struct mem_cgroup_reclaim_cookie *reclaim) + struct mem_cgroup_reclaim_cookie *reclaim, bool online) { return NULL; } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 564aa8f25b71..a1f051adaa15 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -221,14 +221,14 @@ enum res_type { * be used for reference counting. */ #define for_each_mem_cgroup_tree(iter, root) \ - for (iter = mem_cgroup_iter(root, NULL, NULL); \ + for (iter = mem_cgroup_iter(root, NULL, NULL, false); \ iter != NULL; \ - iter = mem_cgroup_iter(root, iter, NULL)) + iter = mem_cgroup_iter(root, iter, NULL, false)) #define for_each_mem_cgroup(iter) \ - for (iter = mem_cgroup_iter(NULL, NULL, NULL); \ + for (iter = mem_cgroup_iter(NULL, NULL, NULL, false); \ iter != NULL; \ - iter = mem_cgroup_iter(NULL, iter, NULL)) + iter = mem_cgroup_iter(NULL, iter, NULL, false)) static inline bool task_is_dying(void) { @@ -1115,6 +1115,7 @@ struct mem_cgroup *get_mem_cgroup_from_current(void) * @root: hierarchy root * @prev: previously returned memcg, NULL on first invocation * @reclaim: cookie for shared reclaim walks, NULL for full walks + * @online: skip offline memcgs * * Returns references to children of the hierarchy below @root, or * @root itself, or %NULL after a full round-trip. @@ -1129,7 +1130,8 @@ struct mem_cgroup *get_mem_cgroup_from_current(void) */ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, struct mem_cgroup *prev, - struct mem_cgroup_reclaim_cookie *reclaim) + struct mem_cgroup_reclaim_cookie *reclaim, + bool online) { struct mem_cgroup_reclaim_iter *iter; struct cgroup_subsys_state *css = NULL; @@ -1199,7 +1201,8 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, * is provided by the caller, so we know it's alive * and kicking, and don't take an extra reference. */ - if (css == &root->css || css_tryget(css)) { + if (css == &root->css || (!online && css_tryget(css)) || + css_tryget_online(css)) { memcg = mem_cgroup_from_css(css); break; } @@ -1812,7 +1815,7 @@ static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg, excess = soft_limit_excess(root_memcg); while (1) { - victim = mem_cgroup_iter(root_memcg, victim, &reclaim); + victim = mem_cgroup_iter(root_memcg, victim, &reclaim, false); if (!victim) { loop++; if (loop >= 2) { diff --git a/mm/shrinker.c b/mm/shrinker.c index dd91eab43ed3..54f5d3aa4f27 100644 --- a/mm/shrinker.c +++ b/mm/shrinker.c @@ -160,7 +160,7 @@ static int expand_shrinker_info(int new_id) new_size = shrinker_unit_size(new_nr_max); old_size = shrinker_unit_size(shrinker_nr_max); - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { ret = expand_one_shrinker_info(memcg, new_size, old_size, new_nr_max); @@ -168,7 +168,7 @@ static int expand_shrinker_info(int new_id) mem_cgroup_iter_break(NULL, memcg); goto out; } - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL, false)) != NULL); out: if (!ret) shrinker_nr_max = new_nr_max; diff --git a/mm/vmscan.c b/mm/vmscan.c index d8c3338fee0f..9a65ee3a1bb7 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -397,10 +397,10 @@ static unsigned long drop_slab_node(int nid) unsigned long freed = 0; struct mem_cgroup *memcg = NULL; - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL, false)) != NULL); return freed; } @@ -3935,7 +3935,7 @@ static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) if (!min_ttl || sc->order || sc->priority == DEF_PRIORITY) return; - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); @@ -3945,7 +3945,7 @@ static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) } cond_resched(); - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL, false))); /* * The main goal is to OOM kill if every generation from all memcgs is @@ -5037,7 +5037,7 @@ static void lru_gen_change_state(bool enabled) else static_branch_disable_cpuslocked(&lru_gen_caps[LRU_GEN_CORE]); - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { int nid; @@ -5061,7 +5061,7 @@ static void lru_gen_change_state(bool enabled) } cond_resched(); - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL, false))); unlock: mutex_unlock(&state_mutex); put_online_mems(); @@ -5164,7 +5164,7 @@ static void *lru_gen_seq_start(struct seq_file *m, loff_t *pos) if (!m->private) return ERR_PTR(-ENOMEM); - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { int nid; @@ -5172,7 +5172,7 @@ static void *lru_gen_seq_start(struct seq_file *m, loff_t *pos) if (!nr_to_skip--) return get_lruvec(memcg, nid); } - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL, false))); return NULL; } @@ -5195,7 +5195,7 @@ static void *lru_gen_seq_next(struct seq_file *m, void *v, loff_t *pos) nid = next_memory_node(nid); if (nid == MAX_NUMNODES) { - memcg = mem_cgroup_iter(NULL, memcg, NULL); + memcg = mem_cgroup_iter(NULL, memcg, NULL, false); if (!memcg) return NULL; @@ -5798,7 +5798,7 @@ static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) struct mem_cgroup *target_memcg = sc->target_mem_cgroup; struct mem_cgroup *memcg; - memcg = mem_cgroup_iter(target_memcg, NULL, NULL); + memcg = mem_cgroup_iter(target_memcg, NULL, NULL, false); do { struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); unsigned long reclaimed; @@ -5855,7 +5855,7 @@ static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) sc->nr_scanned - scanned, sc->nr_reclaimed - reclaimed); - } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); + } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL, false))); } static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) @@ -6522,12 +6522,12 @@ static void kswapd_age_node(struct pglist_data *pgdat, struct scan_control *sc) if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) return; - memcg = mem_cgroup_iter(NULL, NULL, NULL); + memcg = mem_cgroup_iter(NULL, NULL, NULL, false); do { lruvec = mem_cgroup_lruvec(memcg, pgdat); shrink_active_list(SWAP_CLUSTER_MAX, lruvec, sc, LRU_ACTIVE_ANON); - memcg = mem_cgroup_iter(NULL, memcg, NULL); + memcg = mem_cgroup_iter(NULL, memcg, NULL, false); } while (memcg); } From patchwork Mon Nov 27 19:37:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13470246 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 654BAC4167B for ; Mon, 27 Nov 2023 19:37:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE01B6B02BD; Mon, 27 Nov 2023 14:37:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E91826B02BE; Mon, 27 Nov 2023 14:37:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C94A06B02BF; Mon, 27 Nov 2023 14:37:10 -0500 (EST) 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 AEE936B02BD for ; Mon, 27 Nov 2023 14:37:10 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 63A7240707 for ; Mon, 27 Nov 2023 19:37:10 +0000 (UTC) X-FDA: 81504742620.02.EAA929A Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf10.hostedemail.com (Postfix) with ESMTP id 77DDEC002A for ; Mon, 27 Nov 2023 19:37:08 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=koqSoxrh; spf=pass (imf10.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701113828; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5JsvpM2sicZ1ksZpV1CZVXoYdic5VxaRuqwpzIK01Mo=; b=Af07jztX9vaRld7zlDVBZo1G0jVAY+hfNsHnf2WeE3WqE4E1aUY54X8LyJM3YKWDiU+53h 6T9HsmICewiUzoDdIeR9Ug6Opc+3CJ40W4Sz26fxIJOcdVWELXT4+jmNIBNPCgWNr+PusD 7Q5Jq7EIAeIy6yFJ3yOWK+adNqWnKOg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701113828; a=rsa-sha256; cv=none; b=lsyq7nPzT/REmkocH2EXwVkRHE6aCWIs0BW/zkTaQyKAO2R9f4P+ZjfEY8TjQl9+V74DMP A7+CngBFLl1zVzdzyHeD07ONW5xbCZwcJoh4XNqDekgH79D3eWBiYAiUxTkQU3YDlAXrPf 4lRJ9yaTjuGAe93wqQV0BFI4m1SoPGo= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=koqSoxrh; spf=pass (imf10.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1cf856663a4so31682975ad.3 for ; Mon, 27 Nov 2023 11:37:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701113827; x=1701718627; darn=kvack.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=5JsvpM2sicZ1ksZpV1CZVXoYdic5VxaRuqwpzIK01Mo=; b=koqSoxrhXWKYAo9QxuOvu8douzsgPD2EhPCSJ38XGqB2DDYuD+xPsMKsA6BIruUEkp qZmjSKmAej6m0sX85eUEyTE30YuwxdSs4nEyLXl6cfQU4F1Vj/Io18OZ1cJoJj0VxaQc gno0YpQS+UpqCQ24grFJqe8TCAx3P50QEf6Y0fW2m1YKbFFq15l3rXxhfVog2StaNuwD yUMDuQ+fmlohqna6peCCxNQyyVaAUTm87brMZCzz42L5+nhaiWTq0NVyYqqiv6QaAHwr ragXUfvp030rOCdgRiIUbJXWI0X3naEIC/CQAHa1887ysv1k17EfzrXkWpkJCq79Tgay bLHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701113827; x=1701718627; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5JsvpM2sicZ1ksZpV1CZVXoYdic5VxaRuqwpzIK01Mo=; b=B+HF+OGLFMIvPvKSd5jIZmE3Sl8HCkJslFkhenSfB3lzT3jZD8Oy4YOgxpgDiRm43J eQ+bj7ArpH0ORWQExmpADL7B5r17YT+ncqY+N6VkkO+8jPDIsu8FQjZqgWauvrub/CMs R8fwblirWxpLbqlzeoJl2AeLoA0Tcg1h9L4NjXQ3Tzh/PPzQUFG1DRcrustUN/Qc0b2P 5TeP7hEFwzQctjhANE4/gRDK9upXg077gBnHKymUWK81FoNpR8/8ivb+rZnEaLvYw3GW IpwP38r+mn0jB6R1rbcd6ABcSDC260ABaUBrpvwCMDfOq5QkBEeYNczAiN5TOY7dI9nw h2yA== X-Gm-Message-State: AOJu0YwWXbrZTUrfnDkYQy4KR7AYUiXQy01fgiak5ja7HRrj3RqAcDH9 HKDdSic/pOQMrJE9Ga1n3h8= X-Google-Smtp-Source: AGHT+IEYrVmJL31hI65NJSE2/9kkrEVs5x+/KAxcv3YqU4dynNSKT67FqWq3hsRVWHyOjR+/j8P3Ig== X-Received: by 2002:a17:903:25d1:b0:1cf:5782:7c74 with SMTP id jc17-20020a17090325d100b001cf57827c74mr10365049plb.7.1701113827164; Mon, 27 Nov 2023 11:37:07 -0800 (PST) Received: from localhost (fwdproxy-prn-027.fbsv.net. [2a03:2880:ff:1b::face:b00c]) by smtp.gmail.com with ESMTPSA id y5-20020a17090322c500b001cf5d0e7e05sm8620148plg.109.2023.11.27.11.37.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 11:37:06 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.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, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v6 3/6] zswap: make shrinking memcg-aware Date: Mon, 27 Nov 2023 11:37:00 -0800 Message-Id: <20231127193703.1980089-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127193703.1980089-1-nphamcs@gmail.com> References: <20231127193703.1980089-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 77DDEC002A X-Rspam-User: X-Stat-Signature: sqfyjcq3ddf7zhqikigoxptoxdpxhzay X-Rspamd-Server: rspam03 X-HE-Tag: 1701113828-66537 X-HE-Meta: U2FsdGVkX1//SUqlCebprKCDqfOZI8BcehftRJTDt+7kMqDKcNVflXlQ08AKSfEHWstR4QqGmZ5VENN1FqQVhOeNjI8kcN4cqr+8CuYnIsUrare5hqvtaunqOfoenraPJmX4mYpaBtp6LL+tD/XonhWgt1FXmSaHDFyVS7I5uyhYrsc6ez4bhcGhT0ECSKxmlEaLZzRppOQfhHlVxvex+PJwO4VjYnMBMNFWCrNCUzsPk5sMiDCbpfz3tZqQlTrsGbEAdhuWdMQJMI8KV/rgtmiD6AUN07Uc3f3AAjEk4w8z3skmrHEaM9c1L6bAMDXYAASY4MJkqkRCldr8mtZnItF6I5k4dAExgAHSeD/vhLdhrf+g0QBnTntLL8usGFKBY9eOh0NwULt/t0lvAfLqR9OH9kt3+bxAw8xGKmdTyyFRLunhU1Zz41dthJrO8PMGLdJladVFyVugBfXydCN/+VceEEIcAY7QjNX4eIV5h6SWlZ3slTuKa3P/SwAKHFfQqHVGScaIktFwu7M+IN36GftObLyoLo5lQ9HLcMTmoyfbRm12Ih/MozCLcKlHEOR6XRhIxdahkaanFrW+MWTly6rCYsefsfAimdNB25BxEYpeoO/LYrfJH4GC803gRO0rrbMON7ls7xdNaYH3/lo+Aw/wja1MlpsVmJCwTM4rmduXOw/oH8uFWFzSE49Jmvue6Gvr8cHoYZ+ahhtJ702ZQaa2nIOwFqpnQ7fLmV1AmPDdm8ElfiKwYs8SawuQHJwHMhbbESusQFt98CShUgVPo8/JJPm+r9nDtcpYu9pIj39oTwEeqEOlko2xMc+evyUQ6EtDShf3gYpoxKcVXWIyHfj2b6ATWCFIs2a7De3on7H9iV0wD8IiDQ2oUXDVUXcQ8MIYOtxrEVaeMA/6LwatPOEqnW1vUljGmsDoCu0kbuw/phx2yST5jNsdZKvXZdJqepR7F2UMhyFrtFSinp0 MD5ssX+Z gOJiVzFGVknWD7NaZWGuBalQnchHEYa200EDszAlvHVrQB/QXTk5xIH6AtOQpe7pOP9HFgPLFjRw7TTbAtIA7OdzSo7SLwee7xgw8PS44K786ujUDxXw6q1dEbNDQU3k3h51gvTGL7u6Rrb1/McBQ3SGPuCk2R9xERgqnQL5Y25Cn0RAPF6vF85QQCqdruaAOTfdD3uYI4ZgZcbQ+SmDJjQeO5N2wo+XWtK8pc2E1wRxqhqE+eJaPOh+EgDwHTMvaSViDUInR/F/ydtmCZW2KhVWU5CjdXN6/kPTJ1/N1lIuuea/SK+zfJMgRu1P2sANtxXaopLKGjdJrEgaHkRTXo8UB6nPpwPtXGbFzx5fDxG+NCOB9loVOX/OlzwPGGyASB2VGcK6vVkB6aQGpbqEccAYHgM0QNFAh7w6TGeAoI4BBb80i3Elo1LPF5NG1dTbuMaYcmhKK09WnnlVyUw4dnOzsF0urSvevwtJHjVzVt9EpIgY= 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: From: Domenico Cerasuolo Currently, we only have a single global LRU for zswap. This makes it impossible to perform worload-specific shrinking - an memcg cannot determine which pages in the pool it owns, and often ends up writing pages from other memcgs. This issue has been previously observed in practice and mitigated by simply disabling memcg-initiated shrinking: https://lore.kernel.org/all/20230530232435.3097106-1-nphamcs@gmail.com/T/#u This patch fully resolves the issue by replacing the global zswap LRU with memcg- and NUMA-specific LRUs, and modify the reclaim logic: a) When a store attempt hits an memcg limit, it now triggers a synchronous reclaim attempt that, if successful, allows the new hotter page to be accepted by zswap. b) If the store attempt instead hits the global zswap limit, it will trigger an asynchronous reclaim attempt, in which an memcg is selected for reclaim in a round-robin-like fashion. Signed-off-by: Domenico Cerasuolo Co-developed-by: Nhat Pham Signed-off-by: Nhat Pham --- include/linux/memcontrol.h | 5 + include/linux/zswap.h | 2 + mm/memcontrol.c | 2 + mm/swap.h | 3 +- mm/swap_state.c | 24 +++- mm/zswap.c | 248 +++++++++++++++++++++++++++++-------- 6 files changed, 223 insertions(+), 61 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 86adce081a08..83590fd0d6d1 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1187,6 +1187,11 @@ static inline struct mem_cgroup *page_memcg_check(struct page *page) return NULL; } +static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg) +{ + return NULL; +} + static inline bool folio_memcg_kmem(struct folio *folio) { return false; diff --git a/include/linux/zswap.h b/include/linux/zswap.h index 2a60ce39cfde..e571e393669b 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -15,6 +15,7 @@ bool zswap_load(struct folio *folio); void zswap_invalidate(int type, pgoff_t offset); void zswap_swapon(int type); void zswap_swapoff(int type); +void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); #else @@ -31,6 +32,7 @@ static inline bool zswap_load(struct folio *folio) static inline void zswap_invalidate(int type, pgoff_t offset) {} static inline void zswap_swapon(int type) {} static inline void zswap_swapoff(int type) {} +static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) {} #endif diff --git a/mm/memcontrol.c b/mm/memcontrol.c index a1f051adaa15..0399aec8c0e3 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5617,6 +5617,8 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) page_counter_set_min(&memcg->memory, 0); page_counter_set_low(&memcg->memory, 0); + zswap_memcg_offline_cleanup(memcg); + memcg_offline_kmem(memcg); reparent_shrinker_deferred(memcg); wb_memcg_offline(memcg); diff --git a/mm/swap.h b/mm/swap.h index 73c332ee4d91..c0dc73e10e91 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -51,7 +51,8 @@ struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct swap_iocb **plug); struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx, - bool *new_page_allocated); + bool *new_page_allocated, + bool skip_if_exists); struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t flag, struct mempolicy *mpol, pgoff_t ilx); struct page *swapin_readahead(swp_entry_t entry, gfp_t flag, diff --git a/mm/swap_state.c b/mm/swap_state.c index 85d9e5806a6a..6c84236382f3 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -412,7 +412,8 @@ struct folio *filemap_get_incore_folio(struct address_space *mapping, struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx, - bool *new_page_allocated) + bool *new_page_allocated, + bool skip_if_exists) { struct swap_info_struct *si; struct folio *folio; @@ -470,6 +471,17 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, if (err != -EEXIST) goto fail_put_swap; + /* + * Protect against a recursive call to __read_swap_cache_async() + * on the same entry waiting forever here because SWAP_HAS_CACHE + * is set but the folio is not the swap cache yet. This can + * happen today if mem_cgroup_swapin_charge_folio() below + * triggers reclaim through zswap, which may call + * __read_swap_cache_async() in the writeback path. + */ + if (skip_if_exists) + goto fail_put_swap; + /* * We might race against __delete_from_swap_cache(), and * stumble across a swap_map entry whose SWAP_HAS_CACHE @@ -537,7 +549,7 @@ struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, mpol = get_vma_policy(vma, addr, 0, &ilx); page = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, - &page_allocated); + &page_allocated, false); mpol_cond_put(mpol); if (page_allocated) @@ -654,7 +666,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, /* Ok, do the async read-ahead now */ page = __read_swap_cache_async( swp_entry(swp_type(entry), offset), - gfp_mask, mpol, ilx, &page_allocated); + gfp_mask, mpol, ilx, &page_allocated, false); if (!page) continue; if (page_allocated) { @@ -672,7 +684,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, skip: /* The page was likely read above, so no need for plugging here */ page = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, - &page_allocated); + &page_allocated, false); if (unlikely(page_allocated)) swap_readpage(page, false, NULL); return page; @@ -827,7 +839,7 @@ static struct page *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, pte_unmap(pte); pte = NULL; page = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, - &page_allocated); + &page_allocated, false); if (!page) continue; if (page_allocated) { @@ -847,7 +859,7 @@ static struct page *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, skip: /* The page was likely read above, so no need for plugging here */ page = __read_swap_cache_async(targ_entry, gfp_mask, mpol, targ_ilx, - &page_allocated); + &page_allocated, false); if (unlikely(page_allocated)) swap_readpage(page, false, NULL); return page; diff --git a/mm/zswap.c b/mm/zswap.c index 030cc137138f..e441cbcab9a9 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "swap.h" #include "internal.h" @@ -174,8 +175,8 @@ struct zswap_pool { struct work_struct shrink_work; struct hlist_node node; char tfm_name[CRYPTO_MAX_ALG_NAME]; - struct list_head lru; - spinlock_t lru_lock; + struct list_lru list_lru; + struct mem_cgroup *next_shrink; }; /* @@ -291,15 +292,40 @@ static void zswap_update_total_size(void) zswap_pool_total_size = total; } +/* should be called under RCU */ +static inline struct mem_cgroup *mem_cgroup_from_entry(struct zswap_entry *entry) +{ + return entry->objcg ? obj_cgroup_memcg(entry->objcg) : NULL; +} + +static inline int entry_to_nid(struct zswap_entry *entry) +{ + return page_to_nid(virt_to_page(entry)); +} + +void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) +{ + struct zswap_pool *pool; + + /* lock out zswap pools list modification */ + spin_lock(&zswap_pools_lock); + list_for_each_entry(pool, &zswap_pools, list) { + if (pool->next_shrink == memcg) + pool->next_shrink = + mem_cgroup_iter(NULL, pool->next_shrink, NULL, true); + } + spin_unlock(&zswap_pools_lock); +} + /********************************* * zswap entry functions **********************************/ static struct kmem_cache *zswap_entry_cache; -static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp) +static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp, int nid) { struct zswap_entry *entry; - entry = kmem_cache_alloc(zswap_entry_cache, gfp); + entry = kmem_cache_alloc_node(zswap_entry_cache, gfp, nid); if (!entry) return NULL; entry->refcount = 1; @@ -312,6 +338,61 @@ static void zswap_entry_cache_free(struct zswap_entry *entry) kmem_cache_free(zswap_entry_cache, entry); } +/********************************* +* lru functions +**********************************/ +static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry) +{ + int nid = entry_to_nid(entry); + struct mem_cgroup *memcg; + + /* + * Note that it is safe to use rcu_read_lock() here, even in the face of + * concurrent memcg offlining. Thanks to the memcg->kmemcg_id indirection + * used in list_lru lookup, only two scenarios are possible: + * + * 1. list_lru_add() is called before memcg->kmemcg_id is updated. The + * new entry will be reparented to memcg's parent's list_lru. + * 2. list_lru_add() is called after memcg->kmemcg_id is updated. The + * new entry will be added directly to memcg's parent's list_lru. + * + * Similar reasoning holds for list_lru_del() and list_lru_putback(). + */ + rcu_read_lock(); + memcg = mem_cgroup_from_entry(entry); + /* will always succeed */ + list_lru_add(list_lru, &entry->lru, nid, memcg); + rcu_read_unlock(); +} + +static void zswap_lru_del(struct list_lru *list_lru, struct zswap_entry *entry) +{ + int nid = entry_to_nid(entry); + struct mem_cgroup *memcg; + + rcu_read_lock(); + memcg = mem_cgroup_from_entry(entry); + /* will always succeed */ + list_lru_del(list_lru, &entry->lru, nid, memcg); + rcu_read_unlock(); +} + +static void zswap_lru_putback(struct list_lru *list_lru, + struct zswap_entry *entry) +{ + int nid = entry_to_nid(entry); + spinlock_t *lock = &list_lru->node[nid].lock; + struct mem_cgroup *memcg; + + rcu_read_lock(); + memcg = mem_cgroup_from_entry(entry); + spin_lock(lock); + /* we cannot use list_lru_add here, because it increments node's lru count */ + list_lru_putback(list_lru, &entry->lru, nid, memcg); + spin_unlock(lock); + rcu_read_unlock(); +} + /********************************* * rbtree functions **********************************/ @@ -396,9 +477,7 @@ static void zswap_free_entry(struct zswap_entry *entry) if (!entry->length) atomic_dec(&zswap_same_filled_pages); else { - spin_lock(&entry->pool->lru_lock); - list_del(&entry->lru); - spin_unlock(&entry->pool->lru_lock); + zswap_lru_del(&entry->pool->list_lru, entry); zpool_free(zswap_find_zpool(entry), entry->handle); zswap_pool_put(entry->pool); } @@ -632,21 +711,15 @@ static void zswap_invalidate_entry(struct zswap_tree *tree, zswap_entry_put(tree, entry); } -static int zswap_reclaim_entry(struct zswap_pool *pool) +static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_one *l, + spinlock_t *lock, void *arg) { - struct zswap_entry *entry; + struct zswap_entry *entry = container_of(item, struct zswap_entry, lru); struct zswap_tree *tree; pgoff_t swpoffset; - int ret; + enum lru_status ret = LRU_REMOVED_RETRY; + int writeback_result; - /* Get an entry off the LRU */ - spin_lock(&pool->lru_lock); - if (list_empty(&pool->lru)) { - spin_unlock(&pool->lru_lock); - return -EINVAL; - } - entry = list_last_entry(&pool->lru, struct zswap_entry, lru); - list_del_init(&entry->lru); /* * Once the lru lock is dropped, the entry might get freed. The * swpoffset is copied to the stack, and entry isn't deref'd again @@ -654,28 +727,32 @@ static int zswap_reclaim_entry(struct zswap_pool *pool) */ swpoffset = swp_offset(entry->swpentry); tree = zswap_trees[swp_type(entry->swpentry)]; - spin_unlock(&pool->lru_lock); + list_lru_isolate(l, item); + /* + * It's safe to drop the lock here because we return either + * LRU_REMOVED_RETRY or LRU_RETRY. + */ + spin_unlock(lock); /* Check for invalidate() race */ spin_lock(&tree->lock); - if (entry != zswap_rb_search(&tree->rbroot, swpoffset)) { - ret = -EAGAIN; + if (entry != zswap_rb_search(&tree->rbroot, swpoffset)) goto unlock; - } + /* Hold a reference to prevent a free during writeback */ zswap_entry_get(entry); spin_unlock(&tree->lock); - ret = zswap_writeback_entry(entry, tree); + writeback_result = zswap_writeback_entry(entry, tree); spin_lock(&tree->lock); - if (ret) { - /* Writeback failed, put entry back on LRU */ - spin_lock(&pool->lru_lock); - list_move(&entry->lru, &pool->lru); - spin_unlock(&pool->lru_lock); + if (writeback_result) { + zswap_reject_reclaim_fail++; + zswap_lru_putback(&entry->pool->list_lru, entry); + ret = LRU_RETRY; goto put_unlock; } + zswap_written_back_pages++; /* * Writeback started successfully, the page now belongs to the @@ -689,27 +766,76 @@ 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; + + /* + * Skip zombies because their LRUs are reparented and we would be + * reclaiming from the parent instead of the dead memcg. + */ + if (memcg && !mem_cgroup_online(memcg)) + return -ENOENT; + + pool = zswap_pool_current_get(); + if (!pool) + return -EINVAL; + + for_each_node_state(nid, N_NORMAL_MEMORY) { + unsigned long nr_to_walk = 1; + + shrunk += list_lru_walk_one(&pool->list_lru, nid, memcg, + &shrink_memcg_cb, NULL, &nr_to_walk); + } + zswap_pool_put(pool); + return shrunk ? 0 : -EAGAIN; } static void shrink_worker(struct work_struct *w) { struct zswap_pool *pool = container_of(w, typeof(*pool), shrink_work); + struct mem_cgroup *memcg; int ret, failures = 0; + /* global reclaim will select cgroup in a round-robin fashion. */ do { - ret = zswap_reclaim_entry(pool); - if (ret) { - zswap_reject_reclaim_fail++; - if (ret != -EAGAIN) - break; + spin_lock(&zswap_pools_lock); + memcg = pool->next_shrink = + mem_cgroup_iter(NULL, pool->next_shrink, NULL, true); + + /* full round trip */ + if (!memcg) { + spin_unlock(&zswap_pools_lock); if (++failures == MAX_RECLAIM_RETRIES) break; + + goto resched; } + + /* + * Acquire an extra reference to the iterated memcg in case the + * original reference is dropped by the zswap offlining callback. + */ + css_get(&memcg->css); + spin_unlock(&zswap_pools_lock); + + ret = shrink_memcg(memcg); + mem_cgroup_put(memcg); + + if (ret == -EINVAL) + break; + if (ret && ++failures == MAX_RECLAIM_RETRIES) + break; + +resched: cond_resched(); } while (!zswap_can_accept()); - zswap_pool_put(pool); } static struct zswap_pool *zswap_pool_create(char *type, char *compressor) @@ -767,8 +893,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); @@ -834,6 +959,13 @@ 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); + + spin_lock(&zswap_pools_lock); + mem_cgroup_put(pool->next_shrink); + pool->next_shrink = NULL; + spin_unlock(&zswap_pools_lock); + for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) zpool_destroy_pool(pool->zpools[i]); kfree(pool); @@ -1081,7 +1213,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry, /* try to allocate swap cache page */ mpol = get_task_policy(current); page = __read_swap_cache_async(swpentry, GFP_KERNEL, mpol, - NO_INTERLEAVE_INDEX, &page_was_allocated); + NO_INTERLEAVE_INDEX, &page_was_allocated, true); if (!page) { ret = -ENOMEM; goto fail; @@ -1152,7 +1284,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; @@ -1209,6 +1340,7 @@ 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; unsigned int dlen = PAGE_SIZE; @@ -1240,15 +1372,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()) { @@ -1265,7 +1397,7 @@ bool zswap_store(struct folio *folio) } /* 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++; goto reject; @@ -1292,6 +1424,15 @@ bool zswap_store(struct folio *folio) if (!entry->pool) goto freepage; + if (objcg) { + memcg = get_mem_cgroup_from_objcg(objcg); + if (memcg_list_lru_alloc(memcg, &entry->pool->list_lru, GFP_KERNEL)) { + mem_cgroup_put(memcg); + goto put_pool; + } + mem_cgroup_put(memcg); + } + /* compress */ acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx); @@ -1370,9 +1511,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(&entry->pool->list_lru, entry); } spin_unlock(&tree->lock); @@ -1385,6 +1525,7 @@ bool zswap_store(struct folio *folio) put_dstmem: mutex_unlock(acomp_ctx->mutex); +put_pool: zswap_pool_put(entry->pool); freepage: zswap_entry_cache_free(entry); @@ -1479,9 +1620,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); From patchwork Mon Nov 27 19:37:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13470247 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 95573C07D5B for ; Mon, 27 Nov 2023 19:37:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BD5BB6B02BE; Mon, 27 Nov 2023 14:37:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B81F56B02BF; Mon, 27 Nov 2023 14:37:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A23016B02C4; Mon, 27 Nov 2023 14:37:11 -0500 (EST) 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 8DD0F6B02BE for ; Mon, 27 Nov 2023 14:37:11 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4F5C61406D7 for ; Mon, 27 Nov 2023 19:37:11 +0000 (UTC) X-FDA: 81504742662.24.C06A9F7 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf16.hostedemail.com (Postfix) with ESMTP id 7685118002A for ; Mon, 27 Nov 2023 19:37:09 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dhd2pIPZ; spf=pass (imf16.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701113829; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5O5wu9Cq/51IfGIxfcOGm2UEiIvcNVg0blWRA633/+k=; b=8MmY7PYELTWBG3ePiS9wjLO5LrcnRwGhXrejhqODRT9U9+q49rB9+HsUwEcQYQW3TDgHKJ WLXZ7DWHMbZF6VBm464bPHxXfFRzNPslNK20Zy7OKrdO3XZ9xQRc88/spGUfrjVfJDyz+a aZq/tEf2M1yDQSu558JCXw00K4uxp0o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701113829; a=rsa-sha256; cv=none; b=290SDZ/uDDZwRQCVW5qhJUjkwLJdEc+VgxozGcCiyrJ8HvSHEoxwZ975ORgrUevmC9wHnr ZEdMw6uIHC+2JWEZOwDw4sBOTmY/2K0ZXkqZ5Qt3WR0w6VBbP7F/hkPnhkUw3xofbCZ4j9 o09rqIHh0B/nDz/BmJo2uvAqzWBfCN4= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dhd2pIPZ; spf=pass (imf16.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-6cbe5b6ec62so3599035b3a.1 for ; Mon, 27 Nov 2023 11:37:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701113828; x=1701718628; darn=kvack.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=5O5wu9Cq/51IfGIxfcOGm2UEiIvcNVg0blWRA633/+k=; b=dhd2pIPZXEzxw5GXyaD0Yp8oSNyddp9AvB/jIr2Oq/GLyZXY3IaBs2JHvIpKerid/D DcsqZgtn2W1N/Ip0S553BHDS9T5VpvhsfkxU4kXZDh+XAcrzcwU2ZACBWx/zZT5BfHVA XmuBNiU7jthHrALg3AMYPTL5wKj6DfG84DR8vKinU3WbAqvajrJogn+srXFXJ0TT6GkV 5s/zezmj0nlb020+vLS4htcCdRWIe9tqMsZVCCUuTqtv1RQt6Mofg4WV6J/7AEsjpHBn jQmGaMUUmXl4n2AKCGmbd4HpvBlM03FNUQqmRsOB7oPcLR4R+B+8OWp/hRiukM4ynG2G 2gCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701113828; x=1701718628; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5O5wu9Cq/51IfGIxfcOGm2UEiIvcNVg0blWRA633/+k=; b=NRp+1ROCTQ2pSCoLCf/hGEM2yHf3+zAUyGDnu4rrl7fZ1lVn8LRyHfx58+hSPyuCPa C2HP7IFJg/F5CL1cjD12XIkuCLqDHyVZB54rSxfM05Up1yOiDqlqIH7lU8P3LFh803LO G9SacPT5AslzsfbH0Ah5oP50X9oPffjk8v856yu2xI46Uyg/DfYD/woXHO3TAVKRYA1d yaWyREcF8frlHDf49aGEM1sIxIbMpxe8r6TyqpS2aYeIXqIjvQ06baqGn7k70VaTasym bKQDx7YHKvMiiXczYKc4hgvAA8l2P7ssPeHNVjPOyAV59/WW23x5djwtER173t5lE1y2 1TVQ== X-Gm-Message-State: AOJu0Yz193Cm6BoMVORTOINrteHDeo8GuD7RJ/Zl716enLnkszXxt2X5 Y+OCkWKZ9WF09sQt8OFv9Yc= X-Google-Smtp-Source: AGHT+IEwXPRn/KjUwT82GUAwUvlbaVFt6ZyMPdi94ntaKv5CHgpItTfrPBYXTZevCB0RopFyqvfLqw== X-Received: by 2002:a05:6a21:19a:b0:18c:ab4:f6c9 with SMTP id le26-20020a056a21019a00b0018c0ab4f6c9mr12929291pzb.56.1701113828278; Mon, 27 Nov 2023 11:37:08 -0800 (PST) Received: from localhost (fwdproxy-prn-023.fbsv.net. [2a03:2880:ff:17::face:b00c]) by smtp.gmail.com with ESMTPSA id x18-20020aa793b2000000b00688435a9915sm7569191pff.189.2023.11.27.11.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 11:37:07 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.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, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v6 4/6] mm: memcg: add per-memcg zswap writeback stat Date: Mon, 27 Nov 2023 11:37:01 -0800 Message-Id: <20231127193703.1980089-5-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127193703.1980089-1-nphamcs@gmail.com> References: <20231127193703.1980089-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7685118002A X-Rspam-User: X-Stat-Signature: rk3j41sz7sayxk44x14kpxkmc3raprna X-Rspamd-Server: rspam03 X-HE-Tag: 1701113829-461684 X-HE-Meta: U2FsdGVkX1+RV7Fw0sHwhd0W0+peNNMjVXxX42Ie5a0/JRwG4nbNFFkntjY07mduYMUhhdoKOI0X8vs+MzzSuMff9zg2dEYabzwt+ivH3L6S++cV71cJFF4G1EY9I8tpCuOJWydoT7uk+PisfugsiWHyER4X1o/h2rSUqOvbHvGTCCfz4a3pJVCefQpXOwpCRRGdki/QiJXeq/D5YHHEz4evzFUfEeKE5K8NavxzgrSoMqcQcN2NsymbbRaLn+0kTwo3TthACqcm84eNLVhzmmg5YZNOZ7T9dnpU/g7OY0jVpEdYir0snUGOIMNG0KwA5Ncy1I7dyU3tP2eXN62sCja8z4yCJBoc8sMp1UsVQFaaDmRDDqXJxVHEEUljtnOS/2TsQluNE5uSQQyWdqpeegunzzoSDDLyMcyf4FReI5WZvqkhWtf8/mI2MJQUgLM91jOc8ytrDFDk2Riq+D6gu9FJaaS2ydspuH28aHvxclSk0ztU9GEYuleT4J5g72zZN/BbvciupCb+IjR368NhEEePRO7AjXCWCUJn7R97ORp/u1qGdj89xD0Ea8ypF7+XAnc2A80/D7/2VSc8a+To0R7NJX0UUSIITmGhAkAeFicgr9UYWgrurdjwUNXWlc9xyG/NB2Rwh7/3K99otyDQZhGf0Hmy7KGF5+2Ge/ddtpy886wsVQLzXbgmc38G4oXK+CKCGsq/LnYzSQASemJMsJqnayt5NLoFmzoUPUxIs34Y7E83x9ywkIf7Z7UvT3Zna554lPxvX2QSVCXzu8+ZYoOIJY3d4XoltUSZFZr/tste4hD92IGxvKJL1QMpZkq78uagylHH4cJt7r1sY9hBITHKbtYxZZdNnTjGUxIQayAZ7k00aC8P6PXitaBwcFSXlMxzI1bsJVZ0RQrBCZlnnN2YgrZx4hV5YEW44YVIPKn1H1c1fHKDE0Zn/fsy/w5C+fPAiQyYXREX2++reL5 lNs1o3Jk 9Fti13S/B/ir08CsfkOticGiedc//dM9C84V+KUFTMlk50+EklZu/+HxjCqdK4R2NwATxjc4FTcgymGN78DYSc/ebAGHnXmZHL7s72IZN2mnfGLpKKunaa6KHQHniPKYWoa1kgFaEffvHS03hRN3J2sNMhrejSmX01VsW3Dr69TNc1IDnNA37GspLbHisWnxVsI07wRWb/hA9A5v/xqoSN4BxdNqMu3U+kqWjMYOZm9J4/4qyR6f7e0J17SBETiyS5NOq4Mg5lDF43/etFd9RMHmjAgwQe6CrGWyR8oxgDFLWvL7vDUm3h0+Pn8xZiBM7NPtF4DQQyB6ynxEXCQ1opDuifPvx0YIQdZuapv6ebV1ENviCYjH4pioHgT/AqW9eejO3PfORmS9pb9ecmslq6+QguuCt8QU1TD20LWJo7P/Zjsa3gls38nLHVEXv+ql3TPR8SRgu62miyqWyqYFo2W+FWQ== 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: From: Domenico Cerasuolo Since zswap now writes back pages from memcg-specific LRUs, we now need a new stat to show writebacks count for each memcg. Suggested-by: Nhat Pham Signed-off-by: Domenico Cerasuolo Signed-off-by: Nhat Pham --- include/linux/vm_event_item.h | 1 + mm/memcontrol.c | 1 + mm/vmstat.c | 1 + mm/zswap.c | 3 +++ 4 files changed, 6 insertions(+) diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h index d1b847502f09..f4569ad98edf 100644 --- a/include/linux/vm_event_item.h +++ b/include/linux/vm_event_item.h @@ -142,6 +142,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, #ifdef CONFIG_ZSWAP ZSWPIN, ZSWPOUT, + ZSWP_WB, #endif #ifdef CONFIG_X86 DIRECT_MAP_LEVEL2_SPLIT, diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 0399aec8c0e3..a82f9c695c6c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -703,6 +703,7 @@ static const unsigned int memcg_vm_event_stat[] = { #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) ZSWPIN, ZSWPOUT, + ZSWP_WB, #endif #ifdef CONFIG_TRANSPARENT_HUGEPAGE THP_FAULT_ALLOC, diff --git a/mm/vmstat.c b/mm/vmstat.c index afa5a38fcc9c..2249f85e4a87 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1401,6 +1401,7 @@ const char * const vmstat_text[] = { #ifdef CONFIG_ZSWAP "zswpin", "zswpout", + "zswp_wb", #endif #ifdef CONFIG_X86 "direct_map_level2_splits", diff --git a/mm/zswap.c b/mm/zswap.c index e441cbcab9a9..9f5142524d48 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -754,6 +754,9 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o } zswap_written_back_pages++; + if (entry->objcg) + count_objcg_event(entry->objcg, ZSWP_WB); + /* * Writeback started successfully, the page now belongs to the * swapcache. Drop the entry from zswap - unless invalidate already From patchwork Mon Nov 27 19:37:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13470248 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 9CBDDC07D5A for ; Mon, 27 Nov 2023 19:37:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFA5B6B02BF; Mon, 27 Nov 2023 14:37:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BA9D86B02C4; Mon, 27 Nov 2023 14:37:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FD506B02C5; Mon, 27 Nov 2023 14:37:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 8CBA96B02BF for ; Mon, 27 Nov 2023 14:37:12 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 6A7491A06FA for ; Mon, 27 Nov 2023 19:37:12 +0000 (UTC) X-FDA: 81504742704.13.7F15735 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf24.hostedemail.com (Postfix) with ESMTP id 80676180011 for ; Mon, 27 Nov 2023 19:37:10 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bpn4189H; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701113830; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qpgmz+lE50DZaA/oBKT5EnJWyAwPJO9gmArvQZM8Xwo=; b=MoOnqmUlSAhK0RJxiXgIF5m39m50PoNEevJ9oidx3Dglv8/hQPCtCQlhjKlq5ZSq87goZI W6aFXE3ngzRo+PQqtWB8eBYQh+tyvH8Y6pofwfD1qPSoFAUiN2PR+vEzgCd3VmmZvi3zod gvuQ3ExSpt3b3GmHdMSxT3LLIAe7izs= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bpn4189H; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701113830; a=rsa-sha256; cv=none; b=h/xY9mifjyYVoK1+irBd3XH1pncqBjIQFQgPDQymZukyl8NjCaLGVTz/rzCw2YJ+6wbCrF 2g4hdMM6PkHC+hiUVvBtfy1HFClZ9egSWvPi2wrY1p953m1GLraSCGVFuEW/giGlVx6XuX pdZnDDCmFwuAmmgCBkk/2qmFyPu/KPM= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1ce627400f6so36781115ad.2 for ; Mon, 27 Nov 2023 11:37:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701113829; x=1701718629; darn=kvack.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=qpgmz+lE50DZaA/oBKT5EnJWyAwPJO9gmArvQZM8Xwo=; b=bpn4189HhNBTayIkmorKh+Tnn71C593ul/x0y5RK4x9MxLpC1Dzk/ovMAVEA5tzoS7 S/vVKGrXyBatRhl/F1ogJoZYt4q0MDWxaPbGw4FWhuGXvBSOivWyMRUxKpdKQpbDP83z iB0pJePNv8L8uIIi194gPfj2/0rIHo2XwmH1yLaHqrcrWeVI67SnRxuxnl7rE3nGEVNS cLF5fU/9+vkdKOlN9XsV5HBcQNdZSO5kmMYVp0Uzd/R8NTrZlI8HScBuQTnTPzwL4MGX LMP5uhnAlBftEuUM7VRFJ1zVtGH9eoReU0/TRwcomx11q+f+YauzbkW11K2wo3sF0OTT bN0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701113829; x=1701718629; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qpgmz+lE50DZaA/oBKT5EnJWyAwPJO9gmArvQZM8Xwo=; b=A7fcYMv2nCuYEMLg0lXTbZuEjXIALKvyjLOkyOr5cqPisqQ5+1QhL6qytF8/BRo+4+ 4LdngRfS4SGMwTHHHf8S6vmEBVcWcFyG7Y8gkjDvZOuvhBQmW0kqlsdIhkNnEMJFQxl0 bnJ2MZ0C9CzSlGc0pwUbEavdxXvJt7OO5sEwvhQhzJgYFdXJ9W2zjd0t0VLKPWHaHnL+ /Qw2on7/+kj8lO7+c08w+HAAMuo+LvUinoq+M0GEKAeBHpT+RtWqUGHILlOjGysJmk6x NSsYKDleZKCD9eWgQnQZy5s8za+mBH4E+7zujvCPFStnQg5RGXlmIfw9RR9sSK5srlXU qPFw== X-Gm-Message-State: AOJu0YyCrdnru1V3nLKRrZLAQYka/6rfmNpI38O4lbZM4oMj93HUIgja HgM3wPY68+48BSDZ96TSC+s= X-Google-Smtp-Source: AGHT+IGGnMiCHJ1sPxrNpRk0g5/gHoSEWXLvzV7mXBDrZMEhN4XMuBH1qpquSCpMs9EvfZ7SF0hM6Q== X-Received: by 2002:a17:903:2281:b0:1cf:ea64:f508 with SMTP id b1-20020a170903228100b001cfea64f508mr1418367plh.2.1701113829283; Mon, 27 Nov 2023 11:37:09 -0800 (PST) Received: from localhost (fwdproxy-prn-015.fbsv.net. [2a03:2880:ff:f::face:b00c]) by smtp.gmail.com with ESMTPSA id o11-20020a170902d4cb00b001cc0e3a29a8sm7207131plg.89.2023.11.27.11.37.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 11:37:08 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.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, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v6 5/6] selftests: cgroup: update per-memcg zswap writeback selftest Date: Mon, 27 Nov 2023 11:37:02 -0800 Message-Id: <20231127193703.1980089-6-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127193703.1980089-1-nphamcs@gmail.com> References: <20231127193703.1980089-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 80676180011 X-Stat-Signature: fosrmpmwtr1mnked9j9g1wph38d6dw1b X-HE-Tag: 1701113830-464198 X-HE-Meta: U2FsdGVkX18tpOksWObBP/SfxkJjG49GGJVv3PqPLnyiGM0UkPwP1qd/1dtzDO5iCIgqQT+jDEgrFAkoR8h4Xoxcpmp8EylJhBuaSPLubQtcjywLPn+TzoqCehkQhnZu032xFwbsbmw+Vn7UPmJXFSyqaBiBRPourHKD3+Obt8gFtlq5amCP0UzLtp5rupYwLD79+cWZxTsGBpNn0VKX+RA9IkcvmNSWEku7awyQlasrHEqrJWacNlq+iaj6xysp/WAT//jkE/0IFJpQaCybcC15hK9x0qPKeDwINvDsGFpahrDDBhD7Jx2dyqp76kUoFRUhVwJQxDKj95P+ZzD0XLTLfg4S/OgbLiqJEDPD46Eu5AUKIqN2zi5JsNpzjYd4WSYUG7soEu+0owHj/me09H707Jqw+c7uY+PNIiPmy6g8DsVB3ZP//86YPwq3pDKV2BnV+H82378fYplSnRajSRdNTH6fG0lZxcsBe6PwlFtt2U4j5PPW7/4DhjVVAK2YGS09ErqByaGzJTroWGUryJoDYAupsbVyiScQU2VmwAKDSvPjcLw58IAmeYZdh4+cWTD4dfXucO3D4tEOIcSZjE+os7YKY4WYPlXrcjrDYmObAD5ToCAzSgTeEg/lp/LAcbbw0ppkP26rZPobs5z9nrGsKTrNw+zr0k2SrGP5c3e3f7rOjK8yvTsI0lOb8FOgH5PVjI0c72RgM/fgTtQ/HE7BoqS1FWXgBDi2Nj2M9nLvKtzxfxPpGTeqiKm9uhUMcF51Wz5Zj26rWrvkcraKfPNW5HwuhZTYqq5kTUA5DmiPKqYRXLG2RydZoW+E4/F2YtZzXN7MLAC5Ocqi3m2pCzWMGgIOc8mnJNrS/5HocjAXBpcDQ4fX3+MQSHEn/I3GiI/xfJOJ8bwCxOz+v14wW07n41/C4YRr2Uu2isJFDy0NDnlGSlnaD9c/TjhWrEDy961ps7vhC2OIkapYL8W N2cpJhzo Q7Q3of2BMPXulCNKgoM7qIZpUu/9DasUxNVR/5PBHlpoLcLDXD1znyJC2eUO4QtwU7n9GTx+KbSWpgw1UuWqXSrCVHU76+PCT3/cW8Ye5BKNsUQashpC00NAn9HFNn9ttdN9OKifdQz3/HKYZtQrpO3xehMwFqK2btgCxHkbEH64qOr2JvAZQMD/8OLP+17rZ48Hw9whk5s3XjTuud7NrvIgGpmIRZpZF+jF0ol8QvCTDlqygO4G4RltgUO3APS1BBpe7cxaB7bISyeGpTFDg11c6MrvRX8lYloetZKuinwZJcrI1JtxpewiStSUMORJVdjO0yW+QWxUC2GEDC3o3ufM+BTKhqld7XjsD9bgY0/+XeG/KmcjZSAQboInL4oWjJW0z0VBA+EyQEb1uZNFZmNiFCvuv3yHOwy3Gl+e7Lh1sZdA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.201130, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Domenico Cerasuolo The memcg-zswap self test is updated to adjust to the behavior change implemented by commit 87730b165089 ("zswap: make shrinking memcg-aware"), where zswap performs writeback for specific memcg. Signed-off-by: Domenico Cerasuolo Signed-off-by: Nhat Pham --- tools/testing/selftests/cgroup/test_zswap.c | 74 ++++++++++++++------- 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/cgroup/test_zswap.c b/tools/testing/selftests/cgroup/test_zswap.c index c99d2adaca3f..47fdaa146443 100644 --- a/tools/testing/selftests/cgroup/test_zswap.c +++ b/tools/testing/selftests/cgroup/test_zswap.c @@ -50,9 +50,9 @@ static int get_zswap_stored_pages(size_t *value) return read_int("/sys/kernel/debug/zswap/stored_pages", value); } -static int get_zswap_written_back_pages(size_t *value) +static int get_cg_wb_count(const char *cg) { - return read_int("/sys/kernel/debug/zswap/written_back_pages", value); + return cg_read_key_long(cg, "memory.stat", "zswp_wb"); } static long get_zswpout(const char *cgroup) @@ -73,6 +73,24 @@ static int allocate_bytes(const char *cgroup, void *arg) return 0; } +static char *setup_test_group_1M(const char *root, const char *name) +{ + char *group_name = cg_name(root, name); + + if (!group_name) + return NULL; + if (cg_create(group_name)) + goto fail; + if (cg_write(group_name, "memory.max", "1M")) { + cg_destroy(group_name); + goto fail; + } + return group_name; +fail: + free(group_name); + return NULL; +} + /* * Sanity test to check that pages are written into zswap. */ @@ -117,43 +135,51 @@ static int test_zswap_usage(const char *root) /* * When trying to store a memcg page in zswap, if the memcg hits its memory - * limit in zswap, writeback should not be triggered. - * - * This was fixed with commit 0bdf0efa180a("zswap: do not shrink if cgroup may - * not zswap"). Needs to be revised when a per memcg writeback mechanism is - * implemented. + * limit in zswap, writeback should affect only the zswapped pages of that + * memcg. */ static int test_no_invasive_cgroup_shrink(const char *root) { - size_t written_back_before, written_back_after; int ret = KSFT_FAIL; - char *test_group; + size_t control_allocation_size = MB(10); + char *control_allocation, *wb_group = NULL, *control_group = NULL; /* Set up */ - test_group = cg_name(root, "no_shrink_test"); - if (!test_group) - goto out; - if (cg_create(test_group)) + wb_group = setup_test_group_1M(root, "per_memcg_wb_test1"); + if (!wb_group) + return KSFT_FAIL; + if (cg_write(wb_group, "memory.zswap.max", "10K")) goto out; - if (cg_write(test_group, "memory.max", "1M")) + control_group = setup_test_group_1M(root, "per_memcg_wb_test2"); + if (!control_group) goto out; - if (cg_write(test_group, "memory.zswap.max", "10K")) + + /* Push some test_group2 memory into zswap */ + if (cg_enter_current(control_group)) goto out; - if (get_zswap_written_back_pages(&written_back_before)) + control_allocation = malloc(control_allocation_size); + for (int i = 0; i < control_allocation_size; i += 4095) + control_allocation[i] = 'a'; + if (cg_read_key_long(control_group, "memory.stat", "zswapped") < 1) goto out; - /* Allocate 10x memory.max to push memory into zswap */ - if (cg_run(test_group, allocate_bytes, (void *)MB(10))) + /* Allocate 10x memory.max to push wb_group memory into zswap and trigger wb */ + if (cg_run(wb_group, allocate_bytes, (void *)MB(10))) goto out; - /* Verify that no writeback happened because of the memcg allocation */ - if (get_zswap_written_back_pages(&written_back_after)) - goto out; - if (written_back_after == written_back_before) + /* Verify that only zswapped memory from gwb_group has been written back */ + if (get_cg_wb_count(wb_group) > 0 && get_cg_wb_count(control_group) == 0) ret = KSFT_PASS; out: - cg_destroy(test_group); - free(test_group); + cg_enter_current(root); + if (control_group) { + cg_destroy(control_group); + free(control_group); + } + cg_destroy(wb_group); + free(wb_group); + if (control_allocation) + free(control_allocation); return ret; } From patchwork Mon Nov 27 19:37:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13470249 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 1A798C07D5B for ; Mon, 27 Nov 2023 19:37:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F8976B02C4; Mon, 27 Nov 2023 14:37:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A4776B02C5; Mon, 27 Nov 2023 14:37:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F90A6B02C6; Mon, 27 Nov 2023 14:37:14 -0500 (EST) 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 1148E6B02C4 for ; Mon, 27 Nov 2023 14:37:14 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C4E85806DA for ; Mon, 27 Nov 2023 19:37:13 +0000 (UTC) X-FDA: 81504742746.29.0710DE3 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf09.hostedemail.com (Postfix) with ESMTP id AE272140013 for ; Mon, 27 Nov 2023 19:37:11 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="antlq4W/"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701113831; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8CFCgQneqAF2pYra6qWTbPztDlTzrhUgQ5xOfrabi7w=; b=dylMbK5MnR7QrfsDKZKQ0GGxCjw5u9WUTnHdXIacz6FkRohFJu5UMSoiscIXwgngWkIim0 1he362hixTAPSzZL4b7tscnpQkFDFT45C+clHvzubfdYMBSND5phpytHW/uxUlBVFXopo5 n+98SWeFukLnL/zp8swLUG5DpEjniAE= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="antlq4W/"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701113831; a=rsa-sha256; cv=none; b=0HTvyJ7CjR8OvOBaFilFgjby8HmrHMJ1HIxBsZLAmaIHpDtF60LlcEe2w8eh2q6oayKGhz 9l+ANMe78dq/Pf1ubsYJJMOxxmiBWoZe7RHD8yiE+iBw6ikMrBfgAugXLx6lPRKbif2Gsn NNAYxLY80UhRcuL/MPrlCgNoq5oC1FA= Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6cb66fbc63dso3319656b3a.0 for ; Mon, 27 Nov 2023 11:37:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701113830; x=1701718630; darn=kvack.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=8CFCgQneqAF2pYra6qWTbPztDlTzrhUgQ5xOfrabi7w=; b=antlq4W/aKmMvL/PIF4lcC3B8jkWvms7T9j9TRQVOl/gYsz5R1iOgAAH/Wes2pkVIp oJ9TyN1KdiqTqzK8KMxFIvt8HDARde5wy/ldUFyMVGLUFdX75VFi68F83ccvcQ6ABF1E /c6nXLRtmYQ02u6XJ6kJpeA0StOtjAJRPs6DkQ9JGqxTJmRqTmwqixL4YcRR3WYBbjp2 LX+wXln4+EZ6Y8BiaPepCQcvPg79X3Fx1IwkfsHWriAPEJk8+r4nrJ9lL5hQ+7m3hTef GmcZK7woeahNCKzvHdpEHywrNlm/7x8Q0FusOLixxGFkLNV4Y6j1x75wX84ZgTBI8Vpt X+qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701113830; x=1701718630; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8CFCgQneqAF2pYra6qWTbPztDlTzrhUgQ5xOfrabi7w=; b=X3I2Cn2w0huTR9e0dIy17YpjxyGOqpzIYs9Tyi9MK8NCS+vM5loAocbU1cfwIpqaoO Bt4Jsci8jQ5n0H2PM60CFEOmN4WPIZaVm3fB7r+2N2YRpnvfExiXI37T+/trdqkvBcnr olhuSJQnUAoTkm8d5B1cgAQ6qoHH8/YoTM5+F0wKFfRMI//P/NF4aaFOr9phdstxps9G hxmu54Dv9hhPOMsIX2ZXz+X7oeeJaSAmdEWgdFGB6ck9vbt7n3RmcRhm/jPz4Tnn7w34 UjyK+UrThlTWf4XGlhkILj6rxREuF8Rk6I30CgY5WWCiFX3j76OInTamrTkag2acznwi EmPg== X-Gm-Message-State: AOJu0Yy2bM1PrbUkJ4M0e4yyRBQ5L+R6tpjaLY0RychFEhtR5m3iBNdv 7g4vvfFV4TSaEamjwY7jcoo= X-Google-Smtp-Source: AGHT+IGlIp4LlP5/NgFKZ0vrR7vDICSEBaQhxXXvac1Z5gfjCxeZfr+2/WUSp8O4EXfnaNqkLcAFTw== X-Received: by 2002:a05:6a00:2e8b:b0:6cb:4361:773c with SMTP id fd11-20020a056a002e8b00b006cb4361773cmr16698823pfb.5.1701113830376; Mon, 27 Nov 2023 11:37:10 -0800 (PST) Received: from localhost (fwdproxy-prn-024.fbsv.net. [2a03:2880:ff:18::face:b00c]) by smtp.gmail.com with ESMTPSA id f23-20020a63f117000000b005b3cc663c8csm8045652pgi.21.2023.11.27.11.37.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 11:37:10 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.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, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v6 6/6] zswap: shrinks zswap pool based on memory pressure Date: Mon, 27 Nov 2023 11:37:03 -0800 Message-Id: <20231127193703.1980089-7-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127193703.1980089-1-nphamcs@gmail.com> References: <20231127193703.1980089-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: AE272140013 X-Stat-Signature: xpyrktn44ptu8zuo3ancm69uw9yabtkj X-HE-Tag: 1701113831-177883 X-HE-Meta: U2FsdGVkX18CSvFENcXmnH53PgL2839r1XmOSNVLa0bU3zwLAg8HWFjtz8St768+akQb7t/Yse/FrB6Pm96ht/Krwe4Jb8AB8HLYMGuUThQe76FUTeHpsy2LxapjxOY7VHstqTy2U23MH2Fv5yEzyL4hHC/UpOXsJR2FNGO9yssNw/Xibcwf1H/ZLG3NA0VPpO6HHk5100RAjvY8zyjmHTlcFAt9a3gOYUkvaPw9M+NA1A0X22yBDaded1OBTs4LaKqhdZlDm/MxS4W/gecf6lBeQZivzmlvlaXFwl3iPpl6tsviN3nfHoUtrSwN9GkGWzhTSNQtvmK3o0LrDAJ5TuvIPbYEZ1C2tzgeYgMPuR7HInnb8VVR05VeMguquxw+esEJ9gQeUrBVmHGFIpX2MlvsviKY0sKeQsrcjPRrhpk0lZHvaEE0FYRqOzbShzOKTxhRKa0qYP6RbPPga6AqcvOFLZziUmjjj9PMTOLwzhD43gcSPDyKqi0YG02XPMKhe1TnKm8uVelNYL5sUKRdvcPZKKwdH8fA2lLoagkFFK9cPFib15Ha5zbY483eapFXoiMAx63tK74S2cZ7mBgNcXedSVU8M9Zz3DTMpIfb6rUdZw6X5Qb36Qn+rbN0EQgFkTs897DO0pHsG87EjsVqGCCJ9gu+0JlFMW3k3zf1IXPXBmtwYGvnrWJSMrBIrYMyZRzZ7MOSY+st6nAltkgOhfm0nJ0lJp1o3YRG2aZ/avuscy8zx4OWDNiuaNWW9uWJ5UytuL+nN/L6vUBevFMyMBqIKfuxGUWjdZg+PzFNWjYzZLbfItphgO70Xo01cnOfpkwq5mytcdHeZMcoOe3CZNgN7Ai1TyFPW3YQI+1Sz5GxZUE7nbsUB5FJ39HAVgPG03IWOMrA2wCbWOEwuFxIlsvRo1f4pnw455DsLluhMq0AMyVScnknr7WBxuGID8dKpHiDGryxJgQRpu0hwHz HKqNHboW 1yS+BPV5943h/gzIg2ci3OuIe4nhA33XeTtl4xhmaKeJhvtrn75Hx4KStmb247r3ZtUXL4k6KqqI9OZUlpdwVeaJQ8L4hioBHZlLQyt7qSLpwZDKu1U/yQva54F3iDPfD8E9PgD2cxqhJ0isBau1oZXuuS+4rJvV47hN6uYWq9UGNcOl3JLcd19NZRHpuqPjM9PPSJjfoIJePNv/tZaEj64HdNsCbMOIdFRWMvOZExmafuxN2GKFG+t3SE0xVww4qq6h9hY4VZaV6te3tZEZA/6iiOY8sI1LtiQeR0Ns1LvtQ+bN2Wids64jW1B+H1iiVLXGdsfjmV1n4qqJiKzEdq0a70gpb3eDhK65BVylQvdXC80fjYTH/ugEShQ5stgPzmyAx5wQPLQ888BeEu2LzdzdE8sw4eBNDR1jig6fpZ1qDHUJSOGYc1saBtQ== 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: Currently, we only shrink the zswap pool when the user-defined limit is hit. This means that if we set the limit too high, cold data that are unlikely to be used again will reside in the pool, wasting precious memory. It is hard to predict how much zswap space will be needed ahead of time, as this depends on the workload (specifically, on factors such as memory access patterns and compressibility of the memory pages). This patch implements a memcg- and NUMA-aware shrinker for zswap, that is initiated when there is memory pressure. The shrinker does not have any parameter that must be tuned by the user, and can be opted in or out on a per-memcg basis. Furthermore, to make it more robust for many workloads and prevent overshrinking (i.e evicting warm pages that might be refaulted into memory), we build in the following heuristics: * Estimate the number of warm pages residing in zswap, and attempt to protect this region of the zswap LRU. * Scale the number of freeable objects by an estimate of the memory saving factor. The better zswap compresses the data, the fewer pages we will evict to swap (as we will otherwise incur IO for relatively small memory saving). * During reclaim, if the shrinker encounters a page that is also being brought into memory, the shrinker will cautiously terminate its shrinking action, as this is a sign that it is touching the warmer region of the zswap LRU. As a proof of concept, we ran the following synthetic benchmark: build the linux kernel in a memory-limited cgroup, and allocate some cold data in tmpfs to see if the shrinker could write them out and improved the overall performance. Depending on the amount of cold data generated, we observe from 14% to 35% reduction in kernel CPU time used in the kernel builds. Signed-off-by: Nhat Pham --- Documentation/admin-guide/mm/zswap.rst | 7 + include/linux/mmzone.h | 2 + include/linux/zswap.h | 25 +++- mm/mmzone.c | 1 + mm/swap_state.c | 2 + mm/zswap.c | 177 ++++++++++++++++++++++++- 6 files changed, 208 insertions(+), 6 deletions(-) diff --git a/Documentation/admin-guide/mm/zswap.rst b/Documentation/admin-guide/mm/zswap.rst index 45b98390e938..522ae22ccb84 100644 --- a/Documentation/admin-guide/mm/zswap.rst +++ b/Documentation/admin-guide/mm/zswap.rst @@ -153,6 +153,13 @@ attribute, e. g.:: Setting this parameter to 100 will disable the hysteresis. +When there is a sizable amount of cold memory residing in the zswap pool, it +can be advantageous to proactively write these cold pages to swap and reclaim +the memory for other use cases. By default, the zswap shrinker is disabled. +User can enable it as follows: + + echo Y > /sys/module/zswap/parameters/shrinker_enabled + A debugfs interface is provided for various statistic about pool size, number of pages stored, same-value filled pages and various counters for the reasons pages are rejected. diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 7b1816450bfc..b23bc5390240 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -22,6 +22,7 @@ #include #include #include +#include #include /* Free memory management - zoned buddy allocator. */ @@ -641,6 +642,7 @@ struct lruvec { #ifdef CONFIG_MEMCG struct pglist_data *pgdat; #endif + struct zswap_lruvec_state zswap_lruvec_state; }; /* Isolate for asynchronous migration */ diff --git a/include/linux/zswap.h b/include/linux/zswap.h index e571e393669b..cbd373ba88d2 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -5,20 +5,40 @@ #include #include +struct lruvec; + extern u64 zswap_pool_total_size; extern atomic_t zswap_stored_pages; #ifdef CONFIG_ZSWAP +struct zswap_lruvec_state { + /* + * Number of pages in zswap that should be protected from the shrinker. + * This number is an estimate of the following counts: + * + * a) Recent page faults. + * b) Recent insertion to the zswap LRU. This includes new zswap stores, + * as well as recent zswap LRU rotations. + * + * These pages are likely to be warm, and might incur IO if the are written + * to swap. + */ + atomic_long_t nr_zswap_protected; +}; + bool zswap_store(struct folio *folio); bool zswap_load(struct folio *folio); void zswap_invalidate(int type, pgoff_t offset); void zswap_swapon(int type); void zswap_swapoff(int type); void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); - +void zswap_lruvec_state_init(struct lruvec *lruvec); +void zswap_lruvec_swapin(struct page *page); #else +struct zswap_lruvec_state {}; + static inline bool zswap_store(struct folio *folio) { return false; @@ -33,7 +53,8 @@ static inline void zswap_invalidate(int type, pgoff_t offset) {} static inline void zswap_swapon(int type) {} static inline void zswap_swapoff(int type) {} static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) {} - +static inline void zswap_lruvec_init(struct lruvec *lruvec) {} +static inline void zswap_lruvec_swapin(struct page *page) {} #endif #endif /* _LINUX_ZSWAP_H */ diff --git a/mm/mmzone.c b/mm/mmzone.c index b594d3f268fe..c01896eca736 100644 --- a/mm/mmzone.c +++ b/mm/mmzone.c @@ -78,6 +78,7 @@ void lruvec_init(struct lruvec *lruvec) memset(lruvec, 0, sizeof(struct lruvec)); spin_lock_init(&lruvec->lru_lock); + zswap_lruvec_state_init(lruvec); for_each_lru(lru) INIT_LIST_HEAD(&lruvec->lists[lru]); diff --git a/mm/swap_state.c b/mm/swap_state.c index 6c84236382f3..94ed2d508db0 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -687,6 +687,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, &page_allocated, false); if (unlikely(page_allocated)) swap_readpage(page, false, NULL); + zswap_lruvec_swapin(page); return page; } @@ -862,6 +863,7 @@ static struct page *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, &page_allocated, false); if (unlikely(page_allocated)) swap_readpage(page, false, NULL); + zswap_lruvec_swapin(page); return page; } diff --git a/mm/zswap.c b/mm/zswap.c index 9f5142524d48..943090dfe793 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -148,6 +148,10 @@ module_param_named(exclusive_loads, zswap_exclusive_loads_enabled, bool, 0644); /* Number of zpools in zswap_pool (empirically determined for scalability) */ #define ZSWAP_NR_ZPOOLS 32 +/* Enable/disable memory pressure-based shrinker. */ +static bool zswap_shrinker_enabled; +module_param_named(shrinker_enabled, zswap_shrinker_enabled, bool, 0644); + /********************************* * data structures **********************************/ @@ -177,6 +181,8 @@ struct zswap_pool { char tfm_name[CRYPTO_MAX_ALG_NAME]; struct list_lru list_lru; struct mem_cgroup *next_shrink; + struct shrinker *shrinker; + atomic_t nr_stored; }; /* @@ -275,17 +281,26 @@ static bool zswap_can_accept(void) DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE); } +static u64 get_zswap_pool_size(struct zswap_pool *pool) +{ + u64 pool_size = 0; + int i; + + for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) + pool_size += zpool_get_total_size(pool->zpools[i]); + + return pool_size; +} + static void zswap_update_total_size(void) { struct zswap_pool *pool; u64 total = 0; - int i; rcu_read_lock(); list_for_each_entry_rcu(pool, &zswap_pools, list) - for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) - total += zpool_get_total_size(pool->zpools[i]); + total += get_zswap_pool_size(pool); rcu_read_unlock(); @@ -338,13 +353,34 @@ static void zswap_entry_cache_free(struct zswap_entry *entry) kmem_cache_free(zswap_entry_cache, entry); } +/********************************* +* zswap lruvec functions +**********************************/ +void zswap_lruvec_state_init(struct lruvec *lruvec) +{ + atomic_long_set(&lruvec->zswap_lruvec_state.nr_zswap_protected, 0); +} + +void zswap_lruvec_swapin(struct page *page) +{ + struct lruvec *lruvec; + + if (page) { + lruvec = folio_lruvec(page_folio(page)); + atomic_long_inc(&lruvec->zswap_lruvec_state.nr_zswap_protected); + } +} + /********************************* * lru functions **********************************/ static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry) { + atomic_long_t *nr_zswap_protected; + unsigned long lru_size, old, new; int nid = entry_to_nid(entry); struct mem_cgroup *memcg; + struct lruvec *lruvec; /* * Note that it is safe to use rcu_read_lock() here, even in the face of @@ -362,6 +398,19 @@ static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry) memcg = mem_cgroup_from_entry(entry); /* will always succeed */ list_lru_add(list_lru, &entry->lru, nid, memcg); + + /* Update the protection area */ + lru_size = list_lru_count_one(list_lru, nid, memcg); + lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); + nr_zswap_protected = &lruvec->zswap_lruvec_state.nr_zswap_protected; + old = atomic_long_inc_return(nr_zswap_protected); + /* + * Decay to avoid overflow and adapt to changing workloads. + * This is based on LRU reclaim cost decaying heuristics. + */ + do { + new = old > lru_size / 4 ? old / 2 : old; + } while (!atomic_long_try_cmpxchg(nr_zswap_protected, &old, new)); rcu_read_unlock(); } @@ -383,6 +432,7 @@ static void zswap_lru_putback(struct list_lru *list_lru, int nid = entry_to_nid(entry); spinlock_t *lock = &list_lru->node[nid].lock; struct mem_cgroup *memcg; + struct lruvec *lruvec; rcu_read_lock(); memcg = mem_cgroup_from_entry(entry); @@ -390,6 +440,10 @@ static void zswap_lru_putback(struct list_lru *list_lru, /* we cannot use list_lru_add here, because it increments node's lru count */ list_lru_putback(list_lru, &entry->lru, nid, memcg); spin_unlock(lock); + + lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(entry_to_nid(entry))); + /* increment the protection area to account for the LRU rotation. */ + atomic_long_inc(&lruvec->zswap_lruvec_state.nr_zswap_protected); rcu_read_unlock(); } @@ -479,6 +533,7 @@ static void zswap_free_entry(struct zswap_entry *entry) else { zswap_lru_del(&entry->pool->list_lru, entry); zpool_free(zswap_find_zpool(entry), entry->handle); + atomic_dec(&entry->pool->nr_stored); zswap_pool_put(entry->pool); } zswap_entry_cache_free(entry); @@ -520,6 +575,95 @@ static struct zswap_entry *zswap_entry_find_get(struct rb_root *root, return entry; } +/********************************* +* shrinker functions +**********************************/ +static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_one *l, + spinlock_t *lock, void *arg); + +static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, + struct shrink_control *sc) +{ + struct lruvec *lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid)); + unsigned long shrink_ret, nr_protected, lru_size; + struct zswap_pool *pool = shrinker->private_data; + bool encountered_page_in_swapcache = false; + + nr_protected = + atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); + lru_size = list_lru_shrink_count(&pool->list_lru, sc); + + /* + * Abort if the shrinker is disabled or if we are shrinking into the + * protected region. + */ + if (!zswap_shrinker_enabled || nr_protected >= lru_size - sc->nr_to_scan) { + sc->nr_scanned = 0; + return SHRINK_STOP; + } + + shrink_ret = list_lru_shrink_walk(&pool->list_lru, sc, &shrink_memcg_cb, + &encountered_page_in_swapcache); + + if (encountered_page_in_swapcache) + return SHRINK_STOP; + + return shrink_ret ? shrink_ret : SHRINK_STOP; +} + +static unsigned long zswap_shrinker_count(struct shrinker *shrinker, + struct shrink_control *sc) +{ + struct zswap_pool *pool = shrinker->private_data; + struct mem_cgroup *memcg = sc->memcg; + struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid)); + unsigned long nr_backing, nr_stored, nr_freeable, nr_protected; + +#ifdef CONFIG_MEMCG_KMEM + cgroup_rstat_flush(memcg->css.cgroup); + nr_backing = memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_SHIFT; + nr_stored = memcg_page_state(memcg, MEMCG_ZSWAPPED); +#else + /* use pool stats instead of memcg stats */ + nr_backing = get_zswap_pool_size(pool) >> PAGE_SHIFT; + nr_stored = atomic_read(&pool->nr_stored); +#endif + + if (!zswap_shrinker_enabled || !nr_stored) + return 0; + + nr_protected = + atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); + nr_freeable = list_lru_shrink_count(&pool->list_lru, sc); + /* + * Subtract the lru size by an estimate of the number of pages + * that should be protected. + */ + nr_freeable = nr_freeable > nr_protected ? nr_freeable - nr_protected : 0; + + /* + * Scale the number of freeable pages by the memory saving factor. + * This ensures that the better zswap compresses memory, the fewer + * pages we will evict to swap (as it will otherwise incur IO for + * relatively small memory saving). + */ + return mult_frac(nr_freeable, nr_backing, nr_stored); +} + +static void zswap_alloc_shrinker(struct zswap_pool *pool) +{ + pool->shrinker = + shrinker_alloc(SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE, "mm-zswap"); + if (!pool->shrinker) + return; + + pool->shrinker->private_data = pool; + pool->shrinker->scan_objects = zswap_shrinker_scan; + pool->shrinker->count_objects = zswap_shrinker_count; + pool->shrinker->batch = 0; + pool->shrinker->seeks = DEFAULT_SEEKS; +} + /********************************* * per-cpu code **********************************/ @@ -715,6 +859,7 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o spinlock_t *lock, void *arg) { struct zswap_entry *entry = container_of(item, struct zswap_entry, lru); + bool *encountered_page_in_swapcache = (bool *)arg; struct zswap_tree *tree; pgoff_t swpoffset; enum lru_status ret = LRU_REMOVED_RETRY; @@ -750,6 +895,17 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o zswap_reject_reclaim_fail++; zswap_lru_putback(&entry->pool->list_lru, entry); ret = LRU_RETRY; + + /* + * Encountering a page already in swap cache is a sign that we are shrinking + * into the warmer region. We should terminate shrinking (if we're in the dynamic + * shrinker context). + */ + if (writeback_result == -EEXIST && encountered_page_in_swapcache) { + ret = LRU_SKIP; + *encountered_page_in_swapcache = true; + } + goto put_unlock; } zswap_written_back_pages++; @@ -889,6 +1045,11 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) &pool->node); if (ret) goto error; + + zswap_alloc_shrinker(pool); + if (!pool->shrinker) + goto error; + pr_debug("using %s compressor\n", pool->tfm_name); /* being the current pool takes 1 ref; this func expects the @@ -896,13 +1057,19 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) */ kref_init(&pool->kref); INIT_LIST_HEAD(&pool->list); - list_lru_init_memcg(&pool->list_lru, NULL); + if (list_lru_init_memcg(&pool->list_lru, pool->shrinker)) + goto lru_fail; + shrinker_register(pool->shrinker); INIT_WORK(&pool->shrink_work, shrink_worker); + atomic_set(&pool->nr_stored, 0); zswap_pool_debug("created", pool); return pool; +lru_fail: + list_lru_destroy(&pool->list_lru); + shrinker_free(pool->shrinker); error: if (pool->acomp_ctx) free_percpu(pool->acomp_ctx); @@ -960,6 +1127,7 @@ static void zswap_pool_destroy(struct zswap_pool *pool) zswap_pool_debug("destroying", pool); + shrinker_free(pool->shrinker); cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node); free_percpu(pool->acomp_ctx); list_lru_destroy(&pool->list_lru); @@ -1516,6 +1684,7 @@ bool zswap_store(struct folio *folio) if (entry->length) { INIT_LIST_HEAD(&entry->lru); zswap_lru_add(&entry->pool->list_lru, entry); + atomic_inc(&entry->pool->nr_stored); } spin_unlock(&tree->lock);