From patchwork Fri May 3 00:50:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13652247 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 B0110C4345F for ; Fri, 3 May 2024 00:51:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 44F466B008C; Thu, 2 May 2024 20:51:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3D81B6B0092; Thu, 2 May 2024 20:51:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 252706B0093; Thu, 2 May 2024 20:51:02 -0400 (EDT) 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 03BBA6B008C for ; Thu, 2 May 2024 20:51:01 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7ACA8A0947 for ; Fri, 3 May 2024 00:51:01 +0000 (UTC) X-FDA: 82075255122.15.15C5C44 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf15.hostedemail.com (Postfix) with ESMTP id 9A983A0008 for ; Fri, 3 May 2024 00:50:59 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gE6XFe7q; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714697459; 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=wFl5l9cUG6TUpWzvnR4XiCKTQilGE1cDMcBQh7vvAdE=; b=5lHtZXp/dFN8pRgw5o7RdfD1QC7+s3z/hy79AT/iEqyrjd4YvNwxy7xJXTddGfEH+Im3gm R4Do9qtcqno2FjGJgHE3FknbAeo6Pv06yNzRDv9JApY5ywyZdtw4yGMOVq2FLoZKg39jhw WANk9Pev3m1ough9PNEQPY/uAQYs6DI= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gE6XFe7q; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714697459; a=rsa-sha256; cv=none; b=kutd+Q3KgmG8Y1OypWpleWxo31KPebE7Diu2bq9dpwz0I53edUTInRoHteE8EwDuMAVk1R tIBiKIksAv3D44pSuuildR4OXO8GrGgWX8aTvuJ+Q1I8FEG6xDzmfyhLGnQ4i7bgD7rlet qLI3ryZbGXcZDWY54M0RK7gN7sYZnys= Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1ec5387aed9so31516245ad.3 for ; Thu, 02 May 2024 17:50:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714697458; x=1715302258; 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=wFl5l9cUG6TUpWzvnR4XiCKTQilGE1cDMcBQh7vvAdE=; b=gE6XFe7qiit3jn5vV2Qrisl3SaTgx7OnShOlg6hdfDM6nAujZ5yLpGOigYNDHLxf30 KzltE3HtgJevgqETlekd8T08Ej1VdpUDFRKNJDI9xb3Sx+fX8VyC4wabPtrh8aPnIxkN 7YzsqccxHC+tfGc7Y60gw/joNhcbT4vitDeMoeTGRurUagcP0euD1xHoU61f2/02cLM9 nNnhfc2mdBZb7k+PwpirguohwncleuK/kkSV4urdD+0aUQYS6VYOGx5/Y4c9OvVf5IZf eK38wm5DX16ySx5yODed6Tm//FWjGwVFwqy4Fe2p9KXm8ET0a5V6o8IDxaL3GyPBueDy gFrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714697458; x=1715302258; 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=wFl5l9cUG6TUpWzvnR4XiCKTQilGE1cDMcBQh7vvAdE=; b=R+JUPchGEu3i+ZGIuTPO+S5Eeh6x5M8G4WCnlNedsJT5y/h2wRLTuE0AB0jlCZqJ/T 27Sbg0gcNk2FR1K7y78Wk0wtwT0tdYIIY6TvKgKSp5L8xYzHKXDu6SYqLDw7pE7FoFmC b66B3sjrwu9qZ/yHwxFu/C+sFYaa4tZBMmLIOtd7929aUCH/FAeIEu1bT8lTkej8WA9k zwTnjHy8sBmdbAdDby8wjwffDJFboP98WEx4oyMmXBYA2wKQk+ydHA/qttn+mLi3S/EL nvAO0I4T/2K27NJcQ/idWp3tPDXL7ji/QvtjXQmVklB2CdYxs7YQ6OL7ViytrhOGuPsk LXGw== X-Forwarded-Encrypted: i=1; AJvYcCXlk8TKQPrt3t+TgfIYCG/LWy3fkXVgHQ2PV3PvCEJU9JY06UlPTkXFU5Ua5P/3pwMs8jhJSXnlU4JXgYUOXFOlQ6k= X-Gm-Message-State: AOJu0YwET/IR6rC8B5nGlOSHIoTjM13BWSUes446Z0CU1Oh66Qg6PH98 IxhD0Tix8B1feNQARu1SF3x/px20QqQi+ByWFFpYL3cvo5g5k//K X-Google-Smtp-Source: AGHT+IHgGPs6Tx/aHvD+V7N1MdRzTr0fR1RogpKIZp6wEsXbZrG/1C29hAfRf2MxmZtp2ra0jMkL5g== X-Received: by 2002:a17:902:b495:b0:1ea:f7d4:cf2b with SMTP id y21-20020a170902b49500b001eaf7d4cf2bmr1203978plr.17.1714697458583; Thu, 02 May 2024 17:50:58 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d14-20020a170903230e00b001ec48e41d2esm1969175plh.212.2024.05.02.17.50.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 17:50:58 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hanchuanhua@oppo.com, hannes@cmpxchg.org, hughd@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, ryan.roberts@arm.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yosryahmed@google.com, yuzhao@google.com, ziy@nvidia.com, "Rafael J. Wysocki" , Pavel Machek , Len Brown Subject: [PATCH v3 2/6] mm: remove swap_free() and always use swap_free_nr() Date: Fri, 3 May 2024 12:50:19 +1200 Message-Id: <20240503005023.174597-3-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240503005023.174597-1-21cnbao@gmail.com> References: <20240503005023.174597-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 9A983A0008 X-Stat-Signature: izszgn8oew8z3us5bopqken61w1edymd X-HE-Tag: 1714697459-867255 X-HE-Meta: U2FsdGVkX18Gx1NVaqwYKKo1lq3W/mQtVVNPxQDzgPtiY6OcXz6tm8XlFihLoutX7nON4CLFCnKJArCwns4ySndot0qBYYgFvb8v5xy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Hby679Ry798x39+VqADAHBjxp5xgFCeMJI+VX4L8c5RIhg6PZuDd+/p9LOAgpS+zaxrMlMmTNFeDj3dWfzEnPDj77ApqJdvv4+uP3DoNiSZe+MHRDmIvAzypzyOzBMlf1kQN/4LnilfQyM+oldHG7GJnyKfs+Q+VFxYUdgMx1ydIlZNxMC4SJdrsP6iAj2gxa9pxWW9eOyXf/nfCw7uvB2KxD+7l2mnP6Jrsn9NdxwDxxkgsY2cz91ma4n+uUSMzNMvUlu6A0FVRW/V0TkqFZNwK9U5IvABOP7GeeRxls3qHJp3i0oSXvCDf6Ki6qwivA+ur2/hoG0cWdts/0MfAPYC0pPsQn+wC+w1WJIPVh398QeDdJeUcrA6txM3oSrLk2PHJekaxYV4ut0YjAKL9Ss9xbMkqyVHNOXkc/lnMMgSpdCSjb9uTmwVSJLJTjU5pp9yLXgPSOv/bS4FJSgYKtxG5TwEH4OR/Lk3px0UmpxJwOPfFMhyMBKRPA= 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: Barry Song To streamline maintenance efforts, we propose discontinuing the use of swap_free(). Instead, we can simply invoke swap_free_nr() with nr set to 1. This adjustment offers the advantage of enabling batch processing within kernel/power/swap.c. Furthermore, swap_free_nr() is designed with a bitmap consisting of only one long, resulting in overhead that can be ignored for cases where nr equals 1. Suggested-by: "Huang, Ying" Signed-off-by: Barry Song Cc: "Rafael J. Wysocki" Cc: Pavel Machek Cc: Len Brown Cc: Hugh Dickins Acked-by: Chris Li Reviewed-by: "Huang, Ying" --- include/linux/swap.h | 5 ----- kernel/power/swap.c | 7 +++---- mm/memory.c | 2 +- mm/rmap.c | 4 ++-- mm/shmem.c | 4 ++-- mm/swapfile.c | 19 +++++-------------- 6 files changed, 13 insertions(+), 28 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index d1d35e92d7e9..f03cb446124e 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -482,7 +482,6 @@ extern int add_swap_count_continuation(swp_entry_t, gfp_t); extern void swap_shmem_alloc(swp_entry_t); extern int swap_duplicate(swp_entry_t); extern int swapcache_prepare(swp_entry_t); -extern void swap_free(swp_entry_t); extern void swap_free_nr(swp_entry_t entry, int nr_pages); extern void swapcache_free_entries(swp_entry_t *entries, int n); extern void free_swap_and_cache_nr(swp_entry_t entry, int nr); @@ -561,10 +560,6 @@ static inline int swapcache_prepare(swp_entry_t swp) return 0; } -static inline void swap_free(swp_entry_t swp) -{ -} - static inline void swap_free_nr(swp_entry_t entry, int nr_pages) { } diff --git a/kernel/power/swap.c b/kernel/power/swap.c index 5bc04bfe2db1..6befaa88a342 100644 --- a/kernel/power/swap.c +++ b/kernel/power/swap.c @@ -181,7 +181,7 @@ sector_t alloc_swapdev_block(int swap) offset = swp_offset(get_swap_page_of_type(swap)); if (offset) { if (swsusp_extents_insert(offset)) - swap_free(swp_entry(swap, offset)); + swap_free_nr(swp_entry(swap, offset), 1); else return swapdev_block(swap, offset); } @@ -200,12 +200,11 @@ void free_all_swap_pages(int swap) while ((node = swsusp_extents.rb_node)) { struct swsusp_extent *ext; - unsigned long offset; ext = rb_entry(node, struct swsusp_extent, node); rb_erase(node, &swsusp_extents); - for (offset = ext->start; offset <= ext->end; offset++) - swap_free(swp_entry(swap, offset)); + swap_free_nr(swp_entry(swap, ext->start), + ext->end - ext->start + 1); kfree(ext); } diff --git a/mm/memory.c b/mm/memory.c index eea6e4984eae..f033eb3528ba 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4225,7 +4225,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) * We're already holding a reference on the page but haven't mapped it * yet. */ - swap_free(entry); + swap_free_nr(entry, 1); if (should_try_to_free_swap(folio, vma, vmf->flags)) folio_free_swap(folio); diff --git a/mm/rmap.c b/mm/rmap.c index 087a79f1f611..39ec7742acec 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1865,7 +1865,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, goto walk_done_err; } if (arch_unmap_one(mm, vma, address, pteval) < 0) { - swap_free(entry); + swap_free_nr(entry, 1); set_pte_at(mm, address, pvmw.pte, pteval); goto walk_done_err; } @@ -1873,7 +1873,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, /* See folio_try_share_anon_rmap(): clear PTE first. */ if (anon_exclusive && folio_try_share_anon_rmap_pte(folio, subpage)) { - swap_free(entry); + swap_free_nr(entry, 1); set_pte_at(mm, address, pvmw.pte, pteval); goto walk_done_err; } diff --git a/mm/shmem.c b/mm/shmem.c index fa2a0ed97507..bfc8a2beb24f 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1836,7 +1836,7 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, * in shmem_evict_inode(). */ shmem_recalc_inode(inode, -1, -1); - swap_free(swap); + swap_free_nr(swap, 1); } /* @@ -1927,7 +1927,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, delete_from_swap_cache(folio); folio_mark_dirty(folio); - swap_free(swap); + swap_free_nr(swap, 1); put_swap_device(si); *foliop = folio; diff --git a/mm/swapfile.c b/mm/swapfile.c index ec12f2b9d229..ddcd0f24b9a1 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1343,19 +1343,6 @@ static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry) swap_range_free(p, offset, 1); } -/* - * Caller has made sure that the swap device corresponding to entry - * is still around or has not been recycled. - */ -void swap_free(swp_entry_t entry) -{ - struct swap_info_struct *p; - - p = _swap_info_get(entry); - if (p) - __swap_entry_free(p, entry); -} - static void cluster_swap_free_nr(struct swap_info_struct *sis, unsigned long offset, int nr_pages) { @@ -1385,6 +1372,10 @@ static void cluster_swap_free_nr(struct swap_info_struct *sis, unlock_cluster_or_swap_info(sis, ci); } +/* + * Caller has made sure that the swap device corresponding to entry + * is still around or has not been recycled. + */ void swap_free_nr(swp_entry_t entry, int nr_pages) { int nr; @@ -1930,7 +1921,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, new_pte = pte_mkuffd_wp(new_pte); setpte: set_pte_at(vma->vm_mm, addr, pte, new_pte); - swap_free(entry); + swap_free_nr(entry, 1); out: if (pte) pte_unmap_unlock(pte, ptl);