From patchwork Mon Jan 29 17:54:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13536156 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 9366EC47DB3 for ; Mon, 29 Jan 2024 17:55:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0DAAC6B007B; Mon, 29 Jan 2024 12:55:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 08AC66B0081; Mon, 29 Jan 2024 12:55:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E45D16B0080; Mon, 29 Jan 2024 12:55:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D39916B007B for ; Mon, 29 Jan 2024 12:55:17 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 91515160B11 for ; Mon, 29 Jan 2024 17:55:17 +0000 (UTC) X-FDA: 81733100274.05.A980379 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf08.hostedemail.com (Postfix) with ESMTP id 9FD12160016 for ; Mon, 29 Jan 2024 17:55:15 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=b0jDCtrn; spf=pass (imf08.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=ryncsn@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=1706550915; h=from:from:sender:reply-to: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=4HCCIzc96WSKi6JLRF97+BAewUvCrXyaCTHCA/v+eQk=; b=WFevbxWRCZ496HRO4IMkgnlHKBgPituBjZHmlJ5ZgDr+BAFK5D7RqfOSOSOjbEYms+LGL3 27RNC/qlseJCJjIBkrjld6gHdvi1krU/UXwrxFkpK0YdvNewZpM5ZSi6f7u6FSrBz5Qx83 MeravZxEU+AWOe/ILtdmv+j0uAJeIYg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706550915; a=rsa-sha256; cv=none; b=6EgbeAU6Z65HcsCL0fumgubLCKUHaTYDck2xqHwSPkCeNtXDQz2/TMdT7LAULeeUgHS8AA /+/L9ymFpPFYaPDWsiCbhqpXAVmT5xswqh60Qyt19bsgFFcssXV4hbqyZL3emJxiQnzYKC ZMfrSSldTMCGBptAl5QdUIVZKXg8JKk= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=b0jDCtrn; spf=pass (imf08.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-5d8bc1caf00so1591296a12.1 for ; Mon, 29 Jan 2024 09:55:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706550913; x=1707155713; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=4HCCIzc96WSKi6JLRF97+BAewUvCrXyaCTHCA/v+eQk=; b=b0jDCtrnzpZKwYsb/vCXuOI/RAECPrpZawMaRUXIqQEm3eBss98uWbO+hXXcYcgddn XISmlQtByHv3aheKArPQkViL5DxEP+jpxQAF0DKSgigMUJ99WSv7OqFGpW80jAc+vyvJ FQk3tE7aHbeV96kWlqSGapxuk4KE9AyN66MgBPWRIbrZd+5sJKTnADJWGPkjeESysM9M WtXS4PiXeZaJBT0SWi+aZHlK8hvGsCTu3I/FxZNMB1QRafg1JZ0h58Bfc5QoWVmOK2GJ cdXoFqjqT2D0BC7OOe98g+UOt4PtEE+g7kQLQC2pjO+AaV39AaPNm+Y2MgAW0y/BztAR PlgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706550913; x=1707155713; h=content-transfer-encoding:mime-version:reply-to: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=4HCCIzc96WSKi6JLRF97+BAewUvCrXyaCTHCA/v+eQk=; b=KnUvVTaje19+QWNLPkZV4+WVTwRx9QWuic7guIZOyYRb1DF/+Dg1dK6IvX57mFfhJ3 CVOM33Okf/QWysAKszag+E5P3mJ+jMtCUb1iPuoiJrFidzGVvyDzK8ZK4kFOibitn/t3 uzMPEhuvgFEYNZlJiSW84uvQY0xLJrP/HHWZopdx/MpflagcjvWiHswk1ZohysYAxehg pXDoLy3zZPGPQ36p9VTYZ0GJ+cufDiG72z3lAvMDZcFr/fCjRJJI7lHoozrjudtGuN9E CFs0k5EyHOxU3EGwpj/1MvkW2pJuwaDD+o3ypp8TXA9ytaPAH3RgOGhNEuBCVkbv2Eah P7qQ== X-Gm-Message-State: AOJu0YyVBCcG0Co+d47gfjIBX7BEvTXfk0BGLliqCBgiwEMlDs+w4kKE YMfSdiYt44iSoJy8/KBmAuDuYnL5Cm1yGpiBxC8kYtTjlGV2VG3B0viAbrBHzHTSeA== X-Google-Smtp-Source: AGHT+IFgfEMwLjjlfjYQs4zDPrkINQJQhzD4VfcEfeCzLRYP1ozN1jTtfQN+mykLWdBBe4DQTBUsWg== X-Received: by 2002:a05:6a20:438d:b0:19c:a398:4a67 with SMTP id i13-20020a056a20438d00b0019ca3984a67mr4049800pzl.55.1706550913483; Mon, 29 Jan 2024 09:55:13 -0800 (PST) Received: from KASONG-MB2.tencent.com ([1.203.117.98]) by smtp.gmail.com with ESMTPSA id h8-20020aa79f48000000b006ddcadb1e2csm6116676pfr.29.2024.01.29.09.55.10 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 29 Jan 2024 09:55:12 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , "Huang, Ying" , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Yosry Ahmed , David Hildenbrand , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v3 3/7] mm/swap: always account swapped in page into current memcg Date: Tue, 30 Jan 2024 01:54:18 +0800 Message-ID: <20240129175423.1987-4-ryncsn@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240129175423.1987-1-ryncsn@gmail.com> References: <20240129175423.1987-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Queue-Id: 9FD12160016 X-Rspam-User: X-Stat-Signature: 9qmikjwfq1js6adexxd3rwoci888okmw X-Rspamd-Server: rspam03 X-HE-Tag: 1706550915-261970 X-HE-Meta: U2FsdGVkX1/Xo6Un5rveoP/cWWhTL8fwrrOeNo+nWN3al3cjXoXX187uSDizZr3RFiwjSrd+0zHHamJdsmtn7TFLpyUPC0IJnq8oQ/PmrMng0PE2+JsbNrW5gWdp0KKiTX+l3UaV4v98n1bgGKCjxGteg26LkHeuYYwHVRCabAIkBmmYNLi/EUx1vIXCju87pqvF5Wfb910qgS6h7z82TUX8n6boAsbQ1Ea3P73+wORMm8wSy2ImgsdJHvtEt20nP78R1mC/KCvxW3vQtOX5nfkP4bVCjzTD0rcuZPCnX3/qne3klHefrYrgC381xv9RhhOZ+dYFiUcAEHQaD955evNOQ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leASif4ZuQuPLEyzv2NQFRoanK1zCzHfvzaZN1iZRxylvXl/dw98VjskwZ7A1Y4pb/Gi8tikL+D1DxjuLSo809f3Rvs0SGPp4aaq9dZaLQ/gHIeX1rP+mIInS3VVBiq5iFt2G865jza721ARn/bhl5affTp3W94S/Mng6KRr8QrO/Tt3P+8ZGlQtBpDhJ2c7S8PU5PBsrDWkpAfhdUrf 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: Kairui Song Currently, mem_cgroup_swapin_charge_folio is always called with mm == NULL, except in swapin_direct. swapin_direct is only used when swapin should skip readahead and swapcache (SWP_SYNCHRONOUS_IO). All other callers of mem_cgroup_swapin_charge_folio are for swapin that should not skip readahead and cache. This could cause swapin charging to behave differently depending on swap device, which is unexpected. This is currently not happening because the only caller of swapin_direct is the direct anon page fault path, where mm always equals to current->mm, but will no longer be true if swapin_direct is shared and have other callers (eg, swapoff) to share the readahead skipping logic. So make swapin_direct also pass NULL for mm, so swpain charge will behave consistently and not effected by type of swapin device or readahead policy. After this, the second param of mem_cgroup_swapin_charge_folio is never used now, so it can be safely dropped. Signed-off-by: Kairui Song --- include/linux/memcontrol.h | 4 ++-- mm/memcontrol.c | 5 ++--- mm/swap_state.c | 7 +++---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 20ff87f8e001..540590d80958 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -693,7 +693,7 @@ static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp, long nr_pages); -int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, +int mem_cgroup_swapin_charge_folio(struct folio *folio, gfp_t gfp, swp_entry_t entry); void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry); @@ -1281,7 +1281,7 @@ static inline int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, } static inline int mem_cgroup_swapin_charge_folio(struct folio *folio, - struct mm_struct *mm, gfp_t gfp, swp_entry_t entry) + gfp_t gfp, swp_entry_t entry) { return 0; } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e4c8735e7c85..5852742df958 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -7306,8 +7306,7 @@ int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp, * * Returns 0 on success. Otherwise, an error code is returned. */ -int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, - gfp_t gfp, swp_entry_t entry) +int mem_cgroup_swapin_charge_folio(struct folio *folio, gfp_t gfp, swp_entry_t entry) { struct mem_cgroup *memcg; unsigned short id; @@ -7320,7 +7319,7 @@ int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, rcu_read_lock(); memcg = mem_cgroup_from_id(id); if (!memcg || !css_tryget_online(&memcg->css)) - memcg = get_mem_cgroup_from_mm(mm); + memcg = get_mem_cgroup_from_current(); rcu_read_unlock(); ret = charge_memcg(folio, memcg, gfp); diff --git a/mm/swap_state.c b/mm/swap_state.c index 645f5bcad123..a450d09fc0db 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -495,7 +495,7 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, __folio_set_locked(folio); __folio_set_swapbacked(folio); - if (mem_cgroup_swapin_charge_folio(folio, NULL, gfp_mask, entry)) + if (mem_cgroup_swapin_charge_folio(folio, gfp_mask, entry)) goto fail_unlock; /* May fail (-ENOMEM) if XArray node allocation failed. */ @@ -884,9 +884,8 @@ struct folio *swapin_direct(swp_entry_t entry, gfp_t gfp_mask, __folio_set_locked(folio); __folio_set_swapbacked(folio); - if (mem_cgroup_swapin_charge_folio(folio, - vma->vm_mm, GFP_KERNEL, - entry)) { + if (mem_cgroup_swapin_charge_folio(folio, GFP_KERNEL, + entry)) { folio_unlock(folio); folio_put(folio); return NULL;