From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335244; b=hemTQpQl47FD+fHRLHKCknKH2999xdPjLdgPBs3kDP9q1Khb0d6rWwBG5rXLo17ToZFH GoYIZ9BIEtqwqlkxfJ5jOl0EgFt3E8vA+EC+SEY4VlmNUw7X2uonwc/ONqB9tErcmidwq dymTcuId35C7zpxzCZnjCrwcQ+wzqMPwW1OLUerhPSqqcewuFWGrcRdAoV3w5XgLlCkIQ YXaerCkwoJ+yq1qAu9u1MANA7pnEgbHp1NgNOOmGhS0Rm0qTmpJRLIAn4u0XI19ircyaX f/VcUxbHVqhY8GMyQYkYkNswHrv0ciaVBnjSwSaWE8GVz4qMLwjARS2y+UgjLZN8bNg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335244; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=4VYi0OpZkb0dZxxFCnx7Tvn/EfSBNHpQtHtOCtmoQ0M=; b=GM4uBNkolO0DYEZaK/90S6YNBWn/dvckSjTFFUMjwhJGo5TCiBffAgqAZESPwtOlH2cw rbfjjxlgppL5nE5ldJmhsNFPt0nE79OA1t30Q5Liy4mCHZ7DOtm9G885ewsjJuuBjfbdH Kepq5CAjHvIctzgTxRmafQffutodfze9uzKevekbdFRs68OrkpGWjUfU1mw16cMVGd0S2 HC14+hT5Ch5w0BshyaPkAXCmRhABhDrdtgCxKYzogf1TqtREmj4GLOTHDRAm9sFV/YvQN 2ZjCmlqNf1aOeKE9+v2EtmdW3MX321Qf8yIBmw6DKuddr8ODOXPAI0X8/qo4SQ0j23Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=I+575cGt; arc=none smtp.remote-ip=209.85.128.179 Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D46360A2610 for ; Thu, 3 Dec 2026 22:00:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7D46360A2610 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=I+575cGt Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-8b30cbfa628so2065767b3.0 for ; Thu, 03 Dec 2026 14:00:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335242; x=1796940042; darn=vger.kernel.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:content-type; bh=4VYi0OpZkb0dZxxFCnx7Tvn/EfSBNHpQtHtOCtmoQ0M=; b=I+575cGtPNMOZBkjDFRlXhwgwogIp3nJlrwICEiYsyyZ4EbQ5/iawoe41gPLDU3h8g /taRfPz0F3SJ3hsrsoXqAGH1bj7MKx1u2V3nzTnX+l1J/lG8zIgJuVCPbwWYZgjN5AJn t9saMmordOAtpYXTNxtG0YsL+2py93747dquDguPjFLGLfPjJSAIFQiOKi/e8ja+9IKb cj3F10LmnELHk+j+/xnOWuGmmLUGUs02TDbmpHqNnLNZTOdAr8r6QVRRip9WzAjRI03/ k+Ua4mNTkGOwv5KTMwSLI14Av7DZbK/BUEEGNSB8FXzyic2HwCsDWdMohjq83CraFytD 6zUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335242; x=1796940042; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=4VYi0OpZkb0dZxxFCnx7Tvn/EfSBNHpQtHtOCtmoQ0M=; b=bQUGq8kMbx4S3wdQI2Lb170dJq4x9z8pZQZNzPcpLAOwskS0PW2J8dK8BBlZaqsV69 w20ZAvD4jqJxL0WZmD1Rhwp017F3nwHouBsiSHjaTIYC85O+V5eRD5D7VMAK2OFp1ShI zsllG5n+xMBZ10mFQtdgcCG74zHrm5eZXGJUQqv7i8ucXT8E4vUOXb9H2StWvmcibDFI Nwe7+EVPNLJ1lcjFiTDRc2xBnBIBLPDZKnGbuVkRE2wT/kt4IBNiPEPxUUyRPyaicjxR Ar+zhlpzZ75G4G/i/nDUGN++RWMKcTuduMFN3SbnDU4h+Ji8kFMQHUKzc//mplztdgzB oMLQ== X-Forwarded-Encrypted: i=1; AL+hErGXz7YkGuNmnUYppGYl9kGsC7WWMm2q3xqOb9yOAKWZgEU78ZtUW60lBR3frMZAcXb2eY9HqLFQ@vger.kernel.org X-Gm-Message-State: APuU4Z87Oze7NtEgmuwbm1BcGkOpru4DKGZGTb556TrGnomQVcUyXofK WAYJ2o9Mtc3LSIM/OaLcyeySAC8kPlYirA2ZfPMfrxsN3LYO7vIVqhiw X-Gm-Gg: AZFdTZepLxE9oD3DHki+i5no4iXSEMukelC7a7fyJDpMyLVnJ1a6/f4mSrMAqMMVAq3 iqME9CRR6aW+BpHr5oN7lETNi07dVWCvuYOzzcG9PbojWGDZi54sgKuznGl0vqECxPUngVSaJnz 4w0kJ4gv+s5frJRWb/RhYxw6VMoUCj12+kTyB8BtyAVwy88uPQs9n5ub3ouNLAZ5BK/qhT9PY7r zHUnhwa4JpkMvIlMv0RISzgL9QhgctcHE9ImLd2ihczZYzqhskyFBUracshnzTdKAuS+ShS++mI ExJZaPWQ/pjwLUi1ZFZp8MH7Az2R2/WaCSTC//NRx0JpZTuRk509H79Kc6Dv5f0r2Bv9p1ucthD vxlmJ2tEVruVko91HV2yAIcq+i3NB8HGKAyPDdOV/pw+Kh3CAwlcpwgiO2M23pPcCNkL2X2jo29 0e8z4ZH8hlIa9bZ5jH6ubd4mUjgc/KiEWpiJTm9AYYJ9gL/RVFyeSTgV2MkYKrmE6po7zHBQAGl XDhae7imFx7FUSwzTi9DgVYxyZL7xMIbylbjt0DYuEvRp5BRc43ZuT3i2HtSKifNoJ0dl7W2iXr SvhknI6w/O727S/NUVtL+pMcyjaUutgH6Ljv7/aMETC0BgVQ/lrVbItmSWAuKwa6VThzkjTKXA4 pRRTn9gB5HagrFgsbFfV1j05IRs6Di9jEyvJMLw== X-Received: by 2002:a05:690c:28a:b0:8b0:f074:75a0 with SMTP id 00721157ae682-8b660659492mr1371007b3.23.1796335242106; Thu, 03 Dec 2026 14:00:42 -0800 (PST) Received: from localhost ([2a03:2880:10ff:71::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660d93133sm1015867b3.28.2026.12.03.14.00.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:41 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Baoquan He , Barry Song , Youngjun Park , Johannes Weiner , Yosry Ahmed , Chengming Zhou , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baolin Wang , Michal Hocko , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Hugh Dickins , Jonathan Corbet , Shuah Khan , Randy Dunlap , Roman Gushchin , Muchun Song , Arnd Bergmann , Karl Mehltretter , Dapeng Mi , Marco Elver , Ethan Nelson-Moore , "Borislav Petkov (AMD)" , Jeff Layton , linux-doc@vger.kernel.org, cgroups@vger.kernel.org, Tejun Heo , =?UTF-8?q?Michal=20Koutn=C3=BD?= Subject: [PATCH v7 16/20] mm/swap: make vswap mandatory Date: Thu, 3 Dec 2026 14:00:06 -0800 Message-ID: <20261203220010.1302207-17-nphamcs@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Optional vswap uses physical entries directly when a folio cannot use zswap or virtual slot allocation fails. Give ordinary swapout a virtual slot even when it needs disk. For folios ineligible for zswap, reserve disk backing before publishing the entry in a PTE or shmem mapping. This lets reclaim split a large folio if a physical run is unavailable. Hibernation continues to reserve physical slots directly. Initialize the virtual device whenever CONFIG_SWAP is enabled and remove the separate vswap boot controls, so ordinary swapping can use it with the default boot settings. The virtual device now limits reclaim admission and usable swap capacity for memory commitments. Check the loss of usable swap capacity when removing a physical device. Use the physical backing's synchronous I/O flag when choosing shmem readahead. Preserve the shmem entry's order on a synchronous read so zram can still read a large folio. Keep base-page reads during physical swapoff and retain the existing order-zero retry. Keep shmem's capacity check on active swap areas. Swapoff can temporarily undercount free slots on other devices. Signed-off-by: Nhat Pham --- .../admin-guide/kernel-parameters.txt | 7 -- include/linux/swap.h | 10 +- mm/Kconfig | 20 ---- mm/memcontrol.c | 13 -- mm/shmem.c | 11 +- mm/swapfile.c | 113 +++++++++++------- mm/util.c | 31 +++-- mm/vmscan.c | 25 +--- mm/vswap.h | 7 ++ mm/zswap.c | 3 - 10 files changed, 108 insertions(+), 132 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 68b2d751439b..dce9b8f3ae98 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -8421,13 +8421,6 @@ Kernel parameters force - force vulnerability detection even on unaffected processors - vswap= [MM,EARLY] - Route swapouts through the virtual swap layer, which - allows zswap and zero-filled pages to be used without - a physical swap device. - Format: { on | off } - Default: on if CONFIG_VSWAP_DEFAULT_ON=y, else off. - vsyscall= [X86-64,EARLY] Controls the behavior of vsyscalls (i.e. calls to fixed addresses of 0xffffffffff600x00 from legacy diff --git a/include/linux/swap.h b/include/linux/swap.h index ae635d5d0bab..4fe3cabe6a39 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -370,7 +370,7 @@ extern void __meminit kswapd_stop(int nid); bool current_is_kswapd(void); #ifdef CONFIG_SWAP -bool mem_cgroup_can_vswap(struct mem_cgroup *memcg); +bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages); unsigned long vswap_usage_in_pages(void); int add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, @@ -451,7 +451,7 @@ static inline void put_swap_device(struct swap_info_struct *si) #else /* CONFIG_SWAP */ static inline void swap_writeback_dropbehind_folio(struct folio *folio) {} -static inline bool mem_cgroup_can_vswap(struct mem_cgroup *memcg) +static inline bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages) { return false; } @@ -591,7 +591,6 @@ static inline void mem_cgroup_swap_put(unsigned short id, long mem_cgroup_get_folio_swap_margin(const struct folio *folio); long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg); -bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages); bool mem_cgroup_swap_full(const struct folio *folio); #else static inline unsigned short mem_cgroup_swap_get(struct folio *folio) @@ -634,11 +633,6 @@ static inline long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg) return get_nr_swap_pages(); } -static inline bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages) -{ - return mem_cgroup_can_vswap(memcg) || get_nr_swap_pages() >= nr_pages; -} - static inline bool mem_cgroup_swap_full(const struct folio *folio) { return vm_swap_full(); diff --git a/mm/Kconfig b/mm/Kconfig index 704d43fcd92b..18925ccb4adc 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -19,26 +19,6 @@ menuconfig SWAP used to provide more virtual memory than the actual RAM present in your computer. If unsure say Y. -config VSWAP_DEFAULT_ON - bool "Route swapouts through virtual swap by default" - depends on SWAP - default n - help - Virtual swap allows zswap and zero-filled pages to be used - without swapping on a physical device first, and lets a page - move between zswap and a swapfile without invalidating the page - table entries that refer to it. - - Swap entries are handed out by a virtual swap device instead of - naming a slot on a real one, so the backing can be chosen and - changed after the entry exists. - - Say Y to make "vswap=on" the default, routing swapouts through - the virtual swap layer from boot. - - Say N (default) to leave vswap off unless "vswap=on" is passed - on the kernel command line. - config ZSWAP bool "Compressed cache for swap pages" depends on SWAP diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 5811ad3889df..2a87f33bc56d 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6326,19 +6326,6 @@ long mem_cgroup_get_folio_swap_margin(const struct folio *folio) return margin; } -/** - * mem_cgroup_can_swap - can @memcg swap out at least @nr_pages more pages? - * @memcg: the memcg to query - * @nr_pages: the number of pages the caller wants to swap out - * - * Return: true if @memcg can swap out at least @nr_pages more pages. - */ -bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages) -{ - return mem_cgroup_can_vswap(memcg) || - mem_cgroup_get_nr_swap_pages(memcg) >= nr_pages; -} - bool mem_cgroup_swap_full(const struct folio *folio) { struct mem_cgroup *memcg; diff --git a/mm/shmem.c b/mm/shmem.c index 5d3695886ecd..a9000a443674 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1914,8 +1914,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio, if ((info->flags & SHMEM_F_LOCKED) || sbinfo->noswap) goto redirty; - /* vswap doesn't contribute to total_swap_pages */ - if (!total_swap_pages && !(vswap_is_enabled() && zswap_is_enabled())) + if (!vswap_is_enabled() || (!total_swap_pages && !zswap_is_enabled())) goto redirty; /* @@ -2609,10 +2608,12 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, /* Look it up and read it in.. */ folio = swap_cache_get_folio(swap); if (!folio) { - if (vswap_unuse || data_race(si->flags & SWP_SYNCHRONOUS_IO)) { - /* Direct swapin skipping swap cache & readahead */ + if (vswap_unuse || + swap_entry_backend_has_flag(si, swap, SWP_SYNCHRONOUS_IO)) { folio = shmem_swap_alloc_folio(inode, vmf, index, - swap, vswap_unuse ? 0 : order, gfp); + swap, + vswap_unuse ? 0 : order, + gfp); } else { /* Cached swapin only supports order 0 folio */ folio = shmem_swapin_cluster(swap, gfp, info, index); diff --git a/mm/swapfile.c b/mm/swapfile.c index b0865d617512..72652a2099e9 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2062,25 +2062,37 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si, return err; } -bool mem_cgroup_can_vswap(struct mem_cgroup *memcg) +/** + * mem_cgroup_can_swap - check backing headroom for ordinary swapout + * @memcg: the cgroup to query, or NULL for global reclaim + * @nr_pages: the number of pages the caller wants to swap out + * + * Return: true if compression or physical capacity and quota permit swapout. + */ +bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages) { - return vswap_is_enabled() && zswap_is_enabled() && - (!memcg || mem_cgroup_may_zswap(memcg, false)); + long nr_swap_pages; + + if (!vswap_is_enabled()) + return false; + + if (zswap_is_enabled() && + (!memcg || mem_cgroup_may_zswap(memcg, false))) + return true; + + nr_swap_pages = memcg ? mem_cgroup_get_nr_swap_pages(memcg) : + get_nr_swap_pages(); + return nr_swap_pages >= nr_pages; } -static bool folio_may_vswap(struct folio *folio) +static bool folio_may_zswap(struct folio *folio) { struct obj_cgroup *objcg; bool may_zswap; - if (!vswap_is_enabled() || !zswap_is_enabled()) + if (!zswap_is_enabled()) return false; - /* - * If zswap will not take the folio, writeout has to find a physical - * slot anyway. We are just incurring indirection overhead - * unnecessarily. - */ objcg = get_obj_cgroup_from_folio(folio); may_zswap = !objcg || obj_cgroup_may_zswap(objcg); if (objcg) @@ -2193,12 +2205,14 @@ int folio_alloc_swap(struct folio *folio) unsigned int order = folio_order(folio); unsigned short memcg_id; unsigned int size = 1 << order; - bool may_vswap; + bool may_zswap; VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio); - may_vswap = folio_may_vswap(folio); + if (!vswap_is_enabled()) + return -ENOSPC; + may_zswap = folio_may_zswap(folio); if (order) { /* @@ -2218,8 +2232,7 @@ int folio_alloc_swap(struct folio *folio) } } - if (!may_vswap || !vswap_alloc(folio)) - folio_alloc_phys_swap(folio); + vswap_alloc(folio); /* * Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. @@ -2227,27 +2240,25 @@ int folio_alloc_swap(struct folio *folio) * folio_realloc_swap() charges it once backing is allocated. */ memcg_id = mem_cgroup_swap_get(folio); - if (memcg_id) { - if (!is_vswap_entry(folio->swap) && - unlikely(mem_cgroup_swap_charge(memcg_id, size))) { - mem_cgroup_swap_put(memcg_id, size, false); - swap_cache_del_folio(folio); - } else { - mem_cgroup_swap_record(folio, memcg_id); - } - } + if (memcg_id) + mem_cgroup_swap_record(folio, memcg_id); if (unlikely(!folio_test_swapcache(folio))) goto failed; + /* Reserve disk-only backing here so large allocation failures can split. */ + if (!may_zswap && !folio_realloc_swap(folio).val) { + swap_cache_del_folio(folio); + goto failed; + } + return 0; failed: - if (get_nr_swap_pages() <= 0 && !may_vswap) + if (!may_zswap && get_nr_swap_pages() <= 0) return -ENOSPC; - if (!may_vswap && mem_cgroup_get_folio_swap_margin(folio) <= 0) + if (!may_zswap && mem_cgroup_get_folio_swap_margin(folio) <= 0) return -ENOMEM; - return order ? -E2BIG : -ENOMEM; } @@ -4054,6 +4065,24 @@ static void flush_percpu_swap_cluster(struct swap_info_struct *si) } +static int swapoff_check_commit(struct swap_info_struct *si) +{ + long capacity = vswap_capacity_in_pages(); + long pages; + int ret; + + if (!capacity) + return 0; + pages = min(total_swap_pages, capacity) - + min(total_swap_pages - (long)si->pages, capacity); + if (!pages) + return 0; + ret = security_vm_enough_memory_mm(current->mm, pages); + if (!ret) + vm_unacct_memory(pages); + return ret; +} + SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) { struct swap_info_struct *p = NULL; @@ -4097,10 +4126,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) goto out_dput; } - if (!security_vm_enough_memory_mm(current->mm, p->pages)) - vm_unacct_memory(p->pages); - else { - err = -ENOMEM; + err = swapoff_check_commit(p); + if (err) { spin_unlock(&swap_lock); goto out_dput; } @@ -4978,16 +5005,19 @@ subsys_initcall(swapfile_init); struct swap_info_struct *vswap_si; DEFINE_STATIC_KEY_FALSE(vswap_key); -static bool vswap_enabled_early __initdata = IS_ENABLED(CONFIG_VSWAP_DEFAULT_ON); +/* vswap does no IO on its own. */ +static const struct swap_ops vswap_ops = { }; -static int __init early_vswap(char *buf) +long vswap_capacity_in_pages(void) { - return kstrtobool(buf, &vswap_enabled_early); -} -early_param("vswap", early_vswap); + struct swap_info_struct *si; -/* vswap does no IO on its own. */ -static const struct swap_ops vswap_ops = { }; + if (!vswap_is_enabled()) + return 0; + /* Pairs with publication in vswap_init(), before allocation is enabled. */ + si = smp_load_acquire(&vswap_si); + return si->pages; +} unsigned long vswap_usage_in_pages(void) { @@ -5016,12 +5046,9 @@ static int __init vswap_init(void) unsigned long maxpages; int err; - if (!vswap_enabled_early) - return 0; - si = alloc_swap_info(); if (IS_ERR(si)) { - pr_warn("vswap: alloc_swap_info failed (%ld); vswap disabled, swapout falls back to direct physical swap\n", + pr_warn("vswap: alloc_swap_info failed (%ld); ordinary swapping is disabled\n", PTR_ERR(si)); return 0; } @@ -5055,7 +5082,7 @@ static int __init vswap_init(void) enable_swap_info(si); mutex_unlock(&swapon_mutex); - vswap_si = si; + smp_store_release(&vswap_si, si); root = debugfs_create_dir("vswap", NULL); debugfs_create_file("used", 0444, root, NULL, &vswap_used_fops); @@ -5069,7 +5096,7 @@ static int __init vswap_init(void) return 0; fail: - pr_warn("vswap: setup_swap_clusters_info failed (%d); vswap disabled, swapout falls back to direct physical swap\n", + pr_warn("vswap: setup_swap_clusters_info failed (%d); ordinary swapping is disabled\n", err); spin_lock(&swap_lock); si->flags = 0; diff --git a/mm/util.c b/mm/util.c index 47c4cf7400cf..d0dbe4de593f 100644 --- a/mm/util.c +++ b/mm/util.c @@ -924,7 +924,7 @@ unsigned long vm_commit_limit(void) else allowed = ((totalram_pages() - hugetlb_total_pages()) * sysctl_overcommit_ratio / 100); - allowed += total_swap_pages; + allowed += min(total_swap_pages, vswap_capacity_in_pages()); return allowed; } @@ -954,6 +954,23 @@ unsigned long vm_memory_committed(void) } EXPORT_SYMBOL_GPL(vm_memory_committed); +static long vm_overcommit_guess_limit(void) +{ + long capacity = vswap_capacity_in_pages(); + long swap_pages = total_swap_pages; + + /* + * With no physical swap the bound + * would collapse to RAM alone even though zswap can absorb + * more. Compression swap is backed by memory, so budget a 3x + * compression ratio, typical of most workloads. Anything that + * compresses better can use OVERCOMMIT_ALWAYS. + */ + if (capacity && zswap_is_enabled()) + swap_pages += 2 * totalram_pages(); + return totalram_pages() + min(swap_pages, capacity); +} + /* * Check that a process has enough memory to allocate a new virtual * mapping. 0 means there is enough memory for the allocation to @@ -984,17 +1001,7 @@ int __vm_enough_memory(const struct mm_struct *mm, long pages, int cap_sys_admin return 0; if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { - allowed = totalram_pages() + total_swap_pages; - /* - * Vswap is in neither term, so with no swapfile the bound - * would collapse to RAM alone even though zswap can absorb - * more. Compression swap is backed by memory, so budget a 3x - * compression ratio, typical of most workloads. Anything that - * compresses better can use OVERCOMMIT_ALWAYS. - */ - if (vswap_is_enabled() && zswap_is_enabled()) - allowed += 2 * totalram_pages(); - if (pages > allowed) + if (pages > vm_overcommit_guess_limit()) goto error; return 0; } diff --git a/mm/vmscan.c b/mm/vmscan.c index 77aefadc24f9..42ab70acdf5c 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -299,26 +299,9 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, int nid, struct scan_control *sc) { - if (memcg == NULL) { - /* - * For non-memcg reclaim, is there space in any swap device? - * vswap does not contribute to nr_swap_pages. And under - * GFP_NOIO, is there enough swapcached anon to make scanning - * anon worthwhile? - */ - if ((get_nr_swap_pages() > 0 || - (vswap_is_enabled() && zswap_is_enabled())) && - !reclaimable_anon_is_low(memcg, nid, sc)) - return true; - } else { - /* - * Is the memcg below its swap limit, and under GFP_NOIO does - * it have enough swapcached anon to make scanning worthwhile? - */ - if (mem_cgroup_can_swap(memcg, 1) && - !reclaimable_anon_is_low(memcg, nid, sc)) - return true; - } + if (mem_cgroup_can_swap(memcg, 1) && + !reclaimable_anon_is_low(memcg, nid, sc)) + return true; /* * The page can not be swapped. @@ -2754,7 +2737,7 @@ static bool can_age_anon_pages(struct lruvec *lruvec, struct scan_control *sc) { /* Aging the anon LRU is valuable if swap is present: */ - if (total_swap_pages > 0 || (vswap_is_enabled() && zswap_is_enabled())) + if (vswap_is_enabled() && (total_swap_pages > 0 || zswap_is_enabled())) return true; /* Also valuable if anon pages can be demoted: */ diff --git a/mm/vswap.h b/mm/vswap.h index 72b43897faae..c02aa8f6da57 100644 --- a/mm/vswap.h +++ b/mm/vswap.h @@ -29,6 +29,8 @@ enum vswap_backing_type { #include "swap_table.h" DECLARE_STATIC_KEY_FALSE(vswap_key); +long vswap_capacity_in_pages(void); + /* * Only true once vswap_init() has published vswap_si, so callers never * see the device half built. @@ -372,6 +374,11 @@ static inline void vswap_cluster_free_vtable(struct swap_cluster_info *ci) #else /* !CONFIG_SWAP */ +static inline long vswap_capacity_in_pages(void) +{ + return 0; +} + static inline bool vswap_is_enabled(void) { return false; diff --git a/mm/zswap.c b/mm/zswap.c index f1f0fdf3945d..bab1d52aa24c 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1217,9 +1217,6 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, * shrinker functions **********************************/ /* - * With vswap disabled, all zswap entries use directly mapped physical swap - * entries. With vswap enabled, such entries are rare. - * * A zswap-backed vswap entry needs physical swap capacity for writeback * and cgroup swap margin where applicable. @memcg may be NULL to check * device capacity alone. -- 2.53.1-Meta