From nobody Fri Oct 18 06:13:44 2024 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C48D31EF01 for ; Fri, 26 Jul 2024 09:47:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721987232; cv=none; b=GAGvUbr5QzzfqWF3xUR3p3bY4gUm1BPEJDFzQeDsIdMM5WZTHEzSS0DNd2Ssiy5L5f0iyDR3P2/uUvhrEZ6X4yWUP32zWFt7KzP2sVutQNRjJ2hRwr6w3ggffGZSGESIJ6XzLpvpHaqpbg6xMBx32fqnLcsN3QIlq7FXnRUNCds= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721987232; c=relaxed/simple; bh=PUj5ojexLJ60vx2MwAGXmDubNxRUIGVJzH5fevy9KUk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JSEflDTctjDj0TPnxDK3cuEu40PCZWYaIA51Cl5/5U7St2iGj7ux0y5UhbJM+ekvBN/GgeyqCfR7IuTUZejcBzTIcf9dKvlnytirusg35E6hKPjXSKxjVxSnuXhR/nkaP8FGxM5ql4FFK7sjAo1PyGNbCYsSw+Wak3BUy2WNyY8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jyxP2Eii; arc=none smtp.client-ip=209.85.210.172 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.b="jyxP2Eii" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-70cec4aa1e4so522488b3a.1 for ; Fri, 26 Jul 2024 02:47:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721987230; x=1722592030; 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; bh=QemnTZhx46SI6yTUVPx+f2+oh78G0V4lHylINHFTYuU=; b=jyxP2Eii6yzLpwQfPlNKZora3ha5aS489IEYgHdtL79JUq4vo04a40KPDOKbsZz0yO oOoCvF553T4YNtCRbEQqj5RTudL+Sk5370pvxO3FVGHtyXIwW0Kyc4S6doQZPjkmqWFd RKhfYthjPjCVr7W+nxwXdqeEawNrD29LLFkS6flTwtiAnzL0hRErPd7shR+nhyKP5xx1 Jsjzi3RkdHdHTXA9ZCIqFfQS4NhKwkKmGu5wMGd1iUtMYwTujVRhfKqZTYlyEqvGclg9 XHIWb6dLXPOE1vSLKKHyv396Hiq7F44yxKE0576cLBbGwgy7hryiDlJuHGziExPWV+EH yGtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987230; x=1722592030; 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=QemnTZhx46SI6yTUVPx+f2+oh78G0V4lHylINHFTYuU=; b=GUQ6LbmwGkxJc4CkO4zw83LOKabJbjAXRtGrRFXcuA3TjZW8iWesQSDQuItG4eZ3N5 Qrjg4bNUMzPvxDchTLZqsftgga6GBX8f+ahnGM0UX9AR2G0SBXXS7HkM0DOJX9vNxSpC d4Gs05Mm+iPbtdjOeCD8r+wQlw2xVOWeysmBXTGRGk7jAvmt7oTi9617NkKtHftXWWoq cl6enDzPQMaWsL6pWzgjv9viZeH2GGC5ddW8WoLfZiYtmz4W/ZhrjqxiQ2QLvCQI5XRf sFUyvtNELH7QpFehXETviN1zoovemgyl06cDpji82NoJLaODri8Msnb52Feie/mXalIk WO7w== X-Forwarded-Encrypted: i=1; AJvYcCXmSI28SpvVewxGs1SpBMY1d3z8e0vlMnncnSt0idM9F/T3N/ySeqU9UoD39CB6jPQQrPGrBEJ54oCJV/mQbwskDnbkL6j672gRQ6/R X-Gm-Message-State: AOJu0Ywf2DEpp/77JNevCKHZlMVJCeBFdTsOPYbLLYHZNAtwhzseojaS SLvg0iGBPxI6Gw+3ooZ4Miw4/QWts3Il7lJaDITMyzqO3tM1lQQE1xAVeg== X-Google-Smtp-Source: AGHT+IEuYL/V6q+/Rf3zyV7tTxuHUrsd/jgokfNn9VNFhVU1hmhRuwtOEWzWwCDmdE70qZ7N+f0uTQ== X-Received: by 2002:a05:6a21:390:b0:1c4:23f0:9665 with SMTP id adf61e73a8af0-1c472aaeb54mr6667769637.29.1721987229972; Fri, 26 Jul 2024 02:47:09 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7d15e98sm28127455ad.99.2024.07.26.02.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:47:09 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: ying.huang@intel.com, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hannes@cmpxchg.org, hughd@google.com, kaleshsingh@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, mhocko@suse.com, minchan@kernel.org, nphamcs@gmail.com, ryan.roberts@arm.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, yosryahmed@google.com Subject: [PATCH v5 1/4] mm: swap: introduce swapcache_prepare_nr and swapcache_clear_nr for large folios swap-in Date: Fri, 26 Jul 2024 21:46:15 +1200 Message-Id: <20240726094618.401593-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240726094618.401593-1-21cnbao@gmail.com> References: <20240726094618.401593-1-21cnbao@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Barry Song Commit 13ddaf26be32 ("mm/swap: fix race when skipping swapcache") supports one entry only, to support large folio swap-in, we need to handle multiple swap entries. To optimize stack usage, we iterate twice in __swap_duplicate_nr(): the first time to verify that all entries are valid, and the second time to apply the modifications to the entries. Signed-off-by: Barry Song Reviewed-by: Baolin Wang --- include/linux/swap.h | 9 +++- mm/swap.h | 10 ++++- mm/swapfile.c | 102 ++++++++++++++++++++++++++----------------- 3 files changed, 77 insertions(+), 44 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index ba7ea95d1c57..f1b28fd04533 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -480,7 +480,7 @@ extern int get_swap_pages(int n, swp_entry_t swp_entrie= s[], int order); 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 int swapcache_prepare_nr(swp_entry_t entry, int nr); 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); @@ -554,7 +554,7 @@ static inline int swap_duplicate(swp_entry_t swp) return 0; } =20 -static inline int swapcache_prepare(swp_entry_t swp) +static inline int swapcache_prepare_nr(swp_entry_t swp, int nr) { return 0; } @@ -612,6 +612,11 @@ static inline void swap_free(swp_entry_t entry) swap_free_nr(entry, 1); } =20 +static inline int swapcache_prepare(swp_entry_t entry) +{ + return swapcache_prepare_nr(entry, 1); +} + #ifdef CONFIG_MEMCG static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg) { diff --git a/mm/swap.h b/mm/swap.h index baa1fa946b34..81ff7eb0be9c 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -59,7 +59,7 @@ void __delete_from_swap_cache(struct folio *folio, void delete_from_swap_cache(struct folio *folio); void clear_shadow_from_swap_cache(int type, unsigned long begin, unsigned long end); -void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry); +void swapcache_clear_nr(struct swap_info_struct *si, swp_entry_t entry, in= t nr); struct folio *swap_cache_get_folio(swp_entry_t entry, struct vm_area_struct *vma, unsigned long addr); struct folio *filemap_get_incore_folio(struct address_space *mapping, @@ -120,7 +120,7 @@ static inline int swap_writepage(struct page *p, struct= writeback_control *wbc) return 0; } =20 -static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_= t entry) +static inline void swapcache_clear_nr(struct swap_info_struct *si, swp_ent= ry_t entry, int nr) { } =20 @@ -172,4 +172,10 @@ static inline unsigned int folio_swap_flags(struct fol= io *folio) return 0; } #endif /* CONFIG_SWAP */ + +static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_= t entry) +{ + swapcache_clear_nr(si, entry, 1); +} + #endif /* _MM_SWAP_H */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 5f73a8553371..e688e46f1c62 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3363,7 +3363,7 @@ void si_swapinfo(struct sysinfo *val) } =20 /* - * Verify that a swap entry is valid and increment its swap map count. + * Verify that nr swap entries are valid and increment their swap map coun= ts. * * Returns error code in following case. * - success -> 0 @@ -3373,66 +3373,88 @@ void si_swapinfo(struct sysinfo *val) * - swap-cache reference is requested but the entry is not used. -> ENOENT * - swap-mapped reference requested but needs continued swap count. -> EN= OMEM */ -static int __swap_duplicate(swp_entry_t entry, unsigned char usage) +static int __swap_duplicate_nr(swp_entry_t entry, unsigned char usage, int= nr) { struct swap_info_struct *p; struct swap_cluster_info *ci; unsigned long offset; unsigned char count; unsigned char has_cache; - int err; + int err, i; =20 p =3D swp_swap_info(entry); =20 offset =3D swp_offset(entry); + VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); ci =3D lock_cluster_or_swap_info(p, offset); =20 - count =3D p->swap_map[offset]; + err =3D 0; + for (i =3D 0; i < nr; i++) { + count =3D p->swap_map[offset + i]; =20 - /* - * swapin_readahead() doesn't check if a swap entry is valid, so the - * swap entry could be SWAP_MAP_BAD. Check here with lock held. - */ - if (unlikely(swap_count(count) =3D=3D SWAP_MAP_BAD)) { - err =3D -ENOENT; - goto unlock_out; - } + /* + * swapin_readahead() doesn't check if a swap entry is valid, so the + * swap entry could be SWAP_MAP_BAD. Check here with lock held. + */ + if (unlikely(swap_count(count) =3D=3D SWAP_MAP_BAD)) { + err =3D -ENOENT; + goto unlock_out; + } =20 - has_cache =3D count & SWAP_HAS_CACHE; - count &=3D ~SWAP_HAS_CACHE; - err =3D 0; + has_cache =3D count & SWAP_HAS_CACHE; + count &=3D ~SWAP_HAS_CACHE; =20 - if (usage =3D=3D SWAP_HAS_CACHE) { + if (usage =3D=3D SWAP_HAS_CACHE) { + /* set SWAP_HAS_CACHE if there is no cache and entry is used */ + if (!has_cache && count) + continue; + else if (has_cache) /* someone else added cache */ + err =3D -EEXIST; + else /* no users remaining */ + err =3D -ENOENT; =20 - /* set SWAP_HAS_CACHE if there is no cache and entry is used */ - if (!has_cache && count) - has_cache =3D SWAP_HAS_CACHE; - else if (has_cache) /* someone else added cache */ - err =3D -EEXIST; - else /* no users remaining */ - err =3D -ENOENT; + } else if (count || has_cache) { =20 - } else if (count || has_cache) { + if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) + continue; + else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) + err =3D -EINVAL; + else if (swap_count_continued(p, offset + i, count)) + continue; + else + err =3D -ENOMEM; + } else + err =3D -ENOENT; /* unused swap entry */ =20 - if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) + if (err) + goto unlock_out; + } + + for (i =3D 0; i < nr; i++) { + count =3D p->swap_map[offset + i]; + has_cache =3D count & SWAP_HAS_CACHE; + count &=3D ~SWAP_HAS_CACHE; + + if (usage =3D=3D SWAP_HAS_CACHE) + has_cache =3D SWAP_HAS_CACHE; + else if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) count +=3D usage; - else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) - err =3D -EINVAL; - else if (swap_count_continued(p, offset, count)) - count =3D COUNT_CONTINUED; else - err =3D -ENOMEM; - } else - err =3D -ENOENT; /* unused swap entry */ + count =3D COUNT_CONTINUED; =20 - if (!err) - WRITE_ONCE(p->swap_map[offset], count | has_cache); + WRITE_ONCE(p->swap_map[offset + i], count | has_cache); + } =20 unlock_out: unlock_cluster_or_swap_info(p, ci); return err; } =20 +static int __swap_duplicate(swp_entry_t entry, unsigned char usage) +{ + return __swap_duplicate_nr(entry, usage, 1); +} + /* * Help swapoff by noting that swap entry belongs to shmem/tmpfs * (in which case its reference count is never incremented). @@ -3459,23 +3481,23 @@ int swap_duplicate(swp_entry_t entry) } =20 /* - * @entry: swap entry for which we allocate swap cache. + * @entry: first swap entry from which we allocate nr swap cache. * - * Called when allocating swap cache for existing swap entry, + * Called when allocating swap cache for existing swap entries, * This can return error codes. Returns 0 at success. * -EEXIST means there is a swap cache. * Note: return code is different from swap_duplicate(). */ -int swapcache_prepare(swp_entry_t entry) +int swapcache_prepare_nr(swp_entry_t entry, int nr) { - return __swap_duplicate(entry, SWAP_HAS_CACHE); + return __swap_duplicate_nr(entry, SWAP_HAS_CACHE, nr); } =20 -void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry) +void swapcache_clear_nr(struct swap_info_struct *si, swp_entry_t entry, in= t nr) { unsigned long offset =3D swp_offset(entry); =20 - cluster_swap_free_nr(si, offset, 1, SWAP_HAS_CACHE); + cluster_swap_free_nr(si, offset, nr, SWAP_HAS_CACHE); } =20 struct swap_info_struct *swp_swap_info(swp_entry_t entry) --=20 2.34.1 From nobody Fri Oct 18 06:13:44 2024 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBDC01EF01 for ; Fri, 26 Jul 2024 09:47:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721987240; cv=none; b=kfhBG3UBk0Y8GRwpHQwRwpezHVzOlP+vDq2Dz/SRnA5ohcOh+cMwh78iPLX6Ftmp1/06TtfHKeVTm2t4bLgdwbd3RHiPZ4YOTg9EcFDWB5w3lSE02d3kicWBkbrHSgdIGXpHOgL4d/VRieif/A6WofvdBe5Upnbxwml7zdkdouc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721987240; c=relaxed/simple; bh=hO9I+1EL0ym6IJbhpsqFzCYlpCQcinEzfKCa4f4gmOw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GxY5Vk8CAagRvdu9acCOkp26ivD+eahtMi39J9NK/4hetCBJMHT6fyvfgc1jl0WiAhZeJ8t0TAKYE7L1d9IaZHKQ9ps+LgFf8D6EoQ+a76FI+3bEr5iV8qyw2BDMPeF2idoWI5D1QCwBhWRW3Eb25GwG3upiwkQfB+7lizxzmrA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FSO07mJr; arc=none smtp.client-ip=209.85.214.172 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.b="FSO07mJr" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1fd66cddd07so4125845ad.2 for ; Fri, 26 Jul 2024 02:47:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721987238; x=1722592038; 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; bh=iStQPHQRdxqlUzAFxf7mNsjUptqesHyoCMxe2kJhC00=; b=FSO07mJrT5LRg58chJyyNPNyAL7cRAUO1V+K02pGQN4sUaFUpRXkH4BijT0fwM2h4y 4MchUggMFLBl6u/6XzG6xUcNeimc4esn8NCPWhqmx1L2LFcQJFcXa3K5GkcGGlg4th0B qlRmqAtvOkh7KtWnlaGbbTXcmbcx45cNL5iZ2zJhP0oXKyT+Lv4nGop+luPEbz1L/kFY xQ34GZtXSSBGv2TG26/1cL2Cydi0dt95p0UTKX3HMG04he7jXSpseRF1Kn0aDx40wPZY gnqaNyDLGjvFklrhq2j/8Fo0j55OQjhImy3QOctx/zJs7d05t7m9pMg33ogFOmbwUwmw 6w0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987238; x=1722592038; 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=iStQPHQRdxqlUzAFxf7mNsjUptqesHyoCMxe2kJhC00=; b=qnhBMgou9/IBtUi7u6qjwJF+JuPq55CH0+347Ney8STQvJMbfAexkP56vevzCHRHdz wctwH5fgJ8vTVYut0PSktSt0mcOJcCioh0xAsEyxb1N1Mm9TQF3us/YqGHKv8XNHQNQ9 m919NsS+G2eDY43WzU5vmQ//kO/+6MjP1AZu7Yp8+tVr9LDpjQGeiK2t4uilAjKn7nzb x2wSeQCexfSKpc2UukFnm3Vd54hmZkzWF/mBh2J72RRvpXUpifIRarXd8BFhfPJn2PTv Tg7D5B7XoVw1ln+aV82vSIaiYzfmHrSG6HyNpQlO3q23VomtrEqBRQa6vmRLxo88oFMd 0Buw== X-Forwarded-Encrypted: i=1; AJvYcCWbAfsLxk6XKxn6mXE2CI0vvofulxBYksgcljKsNu2HL8H07tZianrUJzkCVSPJ6jxSY8b0Qycv+nz5I0Evu3XQQsQEmQcIyo72TFwD X-Gm-Message-State: AOJu0YyOuG3NmZ1a1yca5X7vKw27gfxtjiwO6YIfx9snO6EbUoOQJfTM +qaHhsFYUUfdXrvn5JgRYdinjNlxSjBJR2MYYMru7o8WjNwjsM/6 X-Google-Smtp-Source: AGHT+IFV2vyejBZHn0Sbv1/bTqARWkAYD5OnxGQx8g/+AtNmVatjTvFKeAc/Eg61DQwGNl7rxqQGzQ== X-Received: by 2002:a17:903:18a:b0:1fd:6529:7447 with SMTP id d9443c01a7336-1fed389c064mr59497475ad.29.1721987237952; Fri, 26 Jul 2024 02:47:17 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7d15e98sm28127455ad.99.2024.07.26.02.47.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:47:17 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: ying.huang@intel.com, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hannes@cmpxchg.org, hughd@google.com, kaleshsingh@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, mhocko@suse.com, minchan@kernel.org, nphamcs@gmail.com, ryan.roberts@arm.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, yosryahmed@google.com Subject: [PATCH v5 2/4] mm: Introduce mem_cgroup_swapin_uncharge_swap_nr() helper for large folios swap-in Date: Fri, 26 Jul 2024 21:46:16 +1200 Message-Id: <20240726094618.401593-3-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240726094618.401593-1-21cnbao@gmail.com> References: <20240726094618.401593-1-21cnbao@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Barry Song With large folios swap-in, we might need to uncharge multiple entries all together, it is better to introduce a helper for that. Signed-off-by: Barry Song --- include/linux/memcontrol.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 1b79760af685..55958cbce61b 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -684,6 +684,14 @@ int mem_cgroup_swapin_charge_folio(struct folio *folio= , struct mm_struct *mm, gfp_t gfp, swp_entry_t entry); void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry); =20 +static inline void mem_cgroup_swapin_uncharge_swap_nr(swp_entry_t entry, i= nt nr) +{ + int i; + + for (i =3D 0; i < nr; i++, entry.val++) + mem_cgroup_swapin_uncharge_swap(entry); +} + void __mem_cgroup_uncharge(struct folio *folio); =20 /** @@ -1185,6 +1193,10 @@ static inline void mem_cgroup_swapin_uncharge_swap(s= wp_entry_t entry) { } =20 +static inline void mem_cgroup_swapin_uncharge_swap_nr(swp_entry_t entry, i= nt nr) +{ +} + static inline void mem_cgroup_uncharge(struct folio *folio) { } --=20 2.34.1 From nobody Fri Oct 18 06:13:44 2024 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D0521EF01 for ; Fri, 26 Jul 2024 09:47:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721987248; cv=none; b=THSVz11nttlmJBAIwZj6WSwedgwGJJ91cWyrnCVIDJVjxNpCR+ZLVjL0ySQfc2kgoJNetKclBBb81rUlrJ4aopUApWBIYa3G6kvqVJBA7SWjN+wXwmqgvd+TmuODY8GxynmlNYJJdSpoR2LDh3NfFlYHCxQ5fjQ3sL2YOnk9ZLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721987248; c=relaxed/simple; bh=FL0DQLlqOty5LHC8u2KEfUvSaAULygAFyXpxIIXIak0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RyHIs2NWSIikR9UmiX74ybtb2yYkUXeZAzqffgynbcCDEf+/icWdh+KZudCXUcXy7N31nuNE/OCx+sUEQx7zVNhR7qxYKdlT0fUengjfcadWi/C5Eevpl39pcI42eqOL5Wlu154a2rbpD//559nX/JMQMGZJowROqXNAOPdRobs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=E1tqeRqq; arc=none smtp.client-ip=209.85.214.171 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.b="E1tqeRqq" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1fd90c2fc68so4150205ad.1 for ; Fri, 26 Jul 2024 02:47:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721987246; x=1722592046; 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; bh=gT1UL0dJwOaNKYiFjA7EC0yRYS8xQDfFpNmhEqLpJwk=; b=E1tqeRqqmVGSb/Rt2gkAE5VH736ehbcevJVcLyZs3e/Q+W933LLKsU9ZpsFDnG+gD3 uYS5b7cRnMTVwv5PeNqezP1aY/3opbbnHc6f/CXakzTUqdBUPkzJwwqvsvReaqmO+zr8 4Jol3nV6rwjg2erPnAVDGi/JkNVlecWNpSR/TeqLud4G93R+d/rXgrzmjsPTjT2l7kJB CXZ6GzOCJvXUFoWw/mb3jdUr4w2EetzjoBHH+HOjFOPwsMTwYyp1RLgADXqD08i0H3bG RShINeYqXmqlhVSUswOL2/iPdF1TlJfFfzvNH+mFIfx7XAN1plfgTJo3tb8XPHPIoZMj xv3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987246; x=1722592046; 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=gT1UL0dJwOaNKYiFjA7EC0yRYS8xQDfFpNmhEqLpJwk=; b=NURWDFODVcvI+9i+SShDmAcmbU9lQ/1zOu6jjt4WKLuZt4GbWrb2jo/IR9hQZ8WTvf RiuJmgDSo0y919Jat0TnQ0IqZOom9e4r9L46lvTzm1J3JAfr7s85KI2IoQTFLhKOtBuJ yWtwrnCFwbvzH3l1eh9tzotb1UJpnwgtmU2L64ssE97B2VO/fRiuT9t+P9hanh2iHrJu tnjNlovTFeLdqXWdTSudYnZiTGrV11UaELHa+VC1NQvyaxSW+UNz/22nf9FYo5W5D52E zs5+BmQS0ooQqc4bt/Hh7y4XUx8DtimO9iBqKrWzHVDx4z9fPjGkCi+HCmx5bKqk5JO/ KEDA== X-Forwarded-Encrypted: i=1; AJvYcCULU9I9FOVXdDtL+8Iq+1qqPptglG72Bscj0XAgMjnA5s9VIDVkTmUATsdQoasRl2k4CBlGpvKztvEsFqQ3OGOqftIXbTkqkC/FKDU9 X-Gm-Message-State: AOJu0Ywnzyd5QIfjMRQ84vaXulMyNSV9C9SCyIi/HYcP03NndEnLgGD3 DArHNZXcHPDt0HQz/q8+DK1XEC1/U4sbupep2ajxgXCvpxxolMUU X-Google-Smtp-Source: AGHT+IFk/zzhxB/vWOVfPadjTGBbJvMnp6xOZwuxd7f6freLAGGRqyTe1ovwigii5sLP/MuWd1JkEQ== X-Received: by 2002:a17:902:ecd1:b0:1fd:7293:3d70 with SMTP id d9443c01a7336-1fed35301e8mr63985595ad.8.1721987246237; Fri, 26 Jul 2024 02:47:26 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7d15e98sm28127455ad.99.2024.07.26.02.47.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:47:25 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: ying.huang@intel.com, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hannes@cmpxchg.org, hughd@google.com, kaleshsingh@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, mhocko@suse.com, minchan@kernel.org, nphamcs@gmail.com, ryan.roberts@arm.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, yosryahmed@google.com, Chuanhua Han Subject: [PATCH v5 3/4] mm: support large folios swapin as a whole for zRAM-like swapfile Date: Fri, 26 Jul 2024 21:46:17 +1200 Message-Id: <20240726094618.401593-4-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240726094618.401593-1-21cnbao@gmail.com> References: <20240726094618.401593-1-21cnbao@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Chuanhua Han In an embedded system like Android, more than half of anonymous memory is actually stored in swap devices such as zRAM. For instance, when an app is switched to the background, most of its memory might be swapped out. Currently, we have mTHP features, but unfortunately, without support for large folio swap-ins, once those large folios are swapped out, we lose them immediately because mTHP is a one-way ticket. This patch introduces mTHP swap-in support. For now, we limit mTHP swap-ins to contiguous swaps that were likely swapped out from mTHP as a whole. Additionally, the current implementation only covers the SWAP_SYNCHRONOUS case. This is the simplest and most common use case, benefiting millions of Android phones and similar devices with minimal implementation cost. In this straightforward scenario, large folios are always exclusive, eliminating the need to handle complex rmap and swapcache issues. It offers several benefits: 1. Enables bidirectional mTHP swapping, allowing retrieval of mTHP after swap-out and swap-in. 2. Eliminates fragmentation in swap slots and supports successful THP_SWPOUT without fragmentation. 3. Enables zRAM/zsmalloc to compress and decompress mTHP, reducing CPU usage and enhancing compression ratios significantly. Deploying this on millions of actual products, we haven't observed any noticeable increase in memory footprint for 64KiB mTHP based on CONT-PTE on ARM64. Signed-off-by: Chuanhua Han Co-developed-by: Barry Song Signed-off-by: Barry Song --- mm/memory.c | 211 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 188 insertions(+), 23 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 833d2cad6eb2..14048e9285d4 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3986,6 +3986,152 @@ static vm_fault_t handle_pte_marker(struct vm_fault= *vmf) return VM_FAULT_SIGBUS; } =20 +/* + * check a range of PTEs are completely swap entries with + * contiguous swap offsets and the same SWAP_HAS_CACHE. + * ptep must be first one in the range + */ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep, int nr_pages) +{ + struct swap_info_struct *si; + unsigned long addr; + swp_entry_t entry; + pgoff_t offset; + char has_cache; + int idx, i; + pte_t pte; + + addr =3D ALIGN_DOWN(vmf->address, nr_pages * PAGE_SIZE); + idx =3D (vmf->address - addr) / PAGE_SIZE; + pte =3D ptep_get(ptep); + + if (!pte_same(pte, pte_move_swp_offset(vmf->orig_pte, -idx))) + return false; + entry =3D pte_to_swp_entry(pte); + offset =3D swp_offset(entry); + if (swap_pte_batch(ptep, nr_pages, pte) !=3D nr_pages) + return false; + + si =3D swp_swap_info(entry); + has_cache =3D si->swap_map[offset] & SWAP_HAS_CACHE; + for (i =3D 1; i < nr_pages; i++) { + /* + * while allocating a large folio and doing swap_read_folio for the + * SWP_SYNCHRONOUS_IO path, which is the case the being faulted pte + * doesn't have swapcache. We need to ensure all PTEs have no cache + * as well, otherwise, we might go to swap devices while the content + * is in swapcache + */ + if ((si->swap_map[offset + i] & SWAP_HAS_CACHE) !=3D has_cache) + return false; + } + + return true; +} + +static inline unsigned long thp_swap_suitable_orders(pgoff_t swp_offset, + unsigned long addr, unsigned long orders) +{ + int order, nr; + + order =3D highest_order(orders); + + /* + * To swap-in a THP with nr pages, we require its first swap_offset + * is aligned with nr. This can filter out most invalid entries. + */ + while (orders) { + nr =3D 1 << order; + if ((addr >> PAGE_SHIFT) % nr =3D=3D swp_offset % nr) + break; + order =3D next_order(&orders, order); + } + + return orders; +} +#else +static inline bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep, int n= r_pages) +{ + return false; +} +#endif + +static struct folio *alloc_swap_folio(struct vm_fault *vmf) +{ + struct vm_area_struct *vma =3D vmf->vma; +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + unsigned long orders; + struct folio *folio; + unsigned long addr; + swp_entry_t entry; + spinlock_t *ptl; + pte_t *pte; + gfp_t gfp; + int order; + + /* + * If uffd is active for the vma we need per-page fault fidelity to + * maintain the uffd semantics. + */ + if (unlikely(userfaultfd_armed(vma))) + goto fallback; + + /* + * A large swapped out folio could be partially or fully in zswap. We + * lack handling for such cases, so fallback to swapping in order-0 + * folio. + */ + if (!zswap_never_enabled()) + goto fallback; + + entry =3D pte_to_swp_entry(vmf->orig_pte); + /* + * Get a list of all the (large) orders below PMD_ORDER that are enabled + * and suitable for swapping THP. + */ + orders =3D thp_vma_allowable_orders(vma, vma->vm_flags, + TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1); + orders =3D thp_vma_suitable_orders(vma, vmf->address, orders); + orders =3D thp_swap_suitable_orders(swp_offset(entry), vmf->address, orde= rs); + + if (!orders) + goto fallback; + + pte =3D pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address & PMD= _MASK, &ptl); + if (unlikely(!pte)) + goto fallback; + + /* + * For do_swap_page, find the highest order where the aligned range is + * completely swap entries with contiguous swap offsets. + */ + order =3D highest_order(orders); + while (orders) { + addr =3D ALIGN_DOWN(vmf->address, PAGE_SIZE << order); + if (can_swapin_thp(vmf, pte + pte_index(addr), 1 << order)) + break; + order =3D next_order(&orders, order); + } + + pte_unmap_unlock(pte, ptl); + + /* Try allocating the highest of the remaining orders. */ + gfp =3D vma_thp_gfp_mask(vma); + while (orders) { + addr =3D ALIGN_DOWN(vmf->address, PAGE_SIZE << order); + folio =3D vma_alloc_folio(gfp, order, vma, addr, true); + if (folio) + return folio; + order =3D next_order(&orders, order); + } + +fallback: +#endif + return vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vmf->address, false); +} + + /* * We enter with non-exclusive mmap_lock (to exclude vma changes, * but allow concurrent faults), and pte mapped but not yet locked. @@ -4074,35 +4220,37 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (!folio) { if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && __swap_count(entry) =3D=3D 1) { - /* - * Prevent parallel swapin from proceeding with - * the cache flag. Otherwise, another thread may - * finish swapin first, free the entry, and swapout - * reusing the same entry. It's undetectable as - * pte_same() returns true due to entry reuse. - */ - if (swapcache_prepare(entry)) { - /* Relax a bit to prevent rapid repeated page faults */ - schedule_timeout_uninterruptible(1); - goto out; - } - need_clear_cache =3D true; - /* skip swapcache */ - folio =3D vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, - vma, vmf->address, false); + folio =3D alloc_swap_folio(vmf); page =3D &folio->page; if (folio) { __folio_set_locked(folio); __folio_set_swapbacked(folio); =20 + nr_pages =3D folio_nr_pages(folio); + if (folio_test_large(folio)) + entry.val =3D ALIGN_DOWN(entry.val, nr_pages); + /* + * Prevent parallel swapin from proceeding with + * the cache flag. Otherwise, another thread may + * finish swapin first, free the entry, and swapout + * reusing the same entry. It's undetectable as + * pte_same() returns true due to entry reuse. + */ + if (swapcache_prepare_nr(entry, nr_pages)) { + /* Relax a bit to prevent rapid repeated page faults */ + schedule_timeout_uninterruptible(1); + goto out_page; + } + need_clear_cache =3D true; + if (mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, GFP_KERNEL, entry)) { ret =3D VM_FAULT_OOM; goto out_page; } - mem_cgroup_swapin_uncharge_swap(entry); + mem_cgroup_swapin_uncharge_swap_nr(entry, nr_pages); =20 shadow =3D get_shadow_from_swap_cache(entry); if (shadow) @@ -4209,6 +4357,22 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) goto out_nomap; } =20 + /* allocated large folios for SWP_SYNCHRONOUS_IO */ + if (folio_test_large(folio) && !folio_test_swapcache(folio)) { + unsigned long nr =3D folio_nr_pages(folio); + unsigned long folio_start =3D ALIGN_DOWN(vmf->address, nr * PAGE_SIZE); + unsigned long idx =3D (vmf->address - folio_start) / PAGE_SIZE; + pte_t *folio_ptep =3D vmf->pte - idx; + + if (!can_swapin_thp(vmf, folio_ptep, nr)) + goto out_nomap; + + page_idx =3D idx; + address =3D folio_start; + ptep =3D folio_ptep; + goto check_folio; + } + nr_pages =3D 1; page_idx =3D 0; address =3D vmf->address; @@ -4340,11 +4504,12 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio_add_lru_vma(folio, vma); } else if (!folio_test_anon(folio)) { /* - * We currently only expect small !anon folios, which are either - * fully exclusive or fully shared. If we ever get large folios - * here, we have to be careful. + * We currently only expect small !anon folios which are either + * fully exclusive or fully shared, or new allocated large folios + * which are fully exclusive. If we ever get large folios within + * swapcache here, we have to be careful. */ - VM_WARN_ON_ONCE(folio_test_large(folio)); + VM_WARN_ON_ONCE(folio_test_large(folio) && folio_test_swapcache(folio)); VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); folio_add_new_anon_rmap(folio, vma, address, rmap_flags); } else { @@ -4387,7 +4552,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) out: /* Clear the swap cache pin for direct swapin after PTL unlock */ if (need_clear_cache) - swapcache_clear(si, entry); + swapcache_clear_nr(si, entry, nr_pages); if (si) put_swap_device(si); return ret; @@ -4403,7 +4568,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio_put(swapcache); } if (need_clear_cache) - swapcache_clear(si, entry); + swapcache_clear_nr(si, entry, nr_pages); if (si) put_swap_device(si); return ret; --=20 2.34.1 From nobody Fri Oct 18 06:13:44 2024 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 10A1F17836C for ; Fri, 26 Jul 2024 09:47:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721987256; cv=none; b=hJHA79Jl3psqW16EjcTX8MuSY5X2SCDdNlqLMsmByUTJWHBV/FD+EqN5hMHXwghpbyH+bkAbv/Y7vMJXvSRyR0/W/rNBoDyfJYkIcBlSCD03+ZyPppr69x3OpuQEej5CK4qlJI4zrpcJmHqP4ktaOkxeI7VccB9qAs1IaIAf3es= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721987256; c=relaxed/simple; bh=bXOB40B+8P35FLMeevMSsTouLOFbjy0oSgq6ugv5yjY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nocms/58LNNenvMM9m5T+c8CPOODVi/1dFf8Tegf8XcJCHm0IsRG+JtHeCfFU2UWbrws1Nvg+VPz5eDDu6m9y/yqd3C8TQIM8i57QKKZ76t/Np/VkpT3d4CRzDMFvtxJ652wJDSYqOwRolIk7F4kbuLnlno+idS+l3wrM0UydVQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NTRkgLU4; arc=none smtp.client-ip=209.85.214.181 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.b="NTRkgLU4" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1fc4fccdd78so3606985ad.2 for ; Fri, 26 Jul 2024 02:47:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721987254; x=1722592054; 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; bh=hXeSWJ/2XL0C00VB4D2wsSnDGte33aaztEw6N6+DVHo=; b=NTRkgLU4uPMNfQOhmS6daoTqhdwamv1HkJAZgu2DGpyHvsQsD6XKJqv9Tn/+WQEVaS KoDwpHKJWD8tnglIuczgZrgjkYrsxjlK629LrXB/Sa8+fwbdpQjBgvasuYJYMzDkTV5F 08Fwg6CcG3Z/OF2vM62rA00UEqodQ7dkAm56m8G1KIR0TSRP+WKANddGfK9CrQXuii8C RS8J5i0kp8sHltXpxbDjiSnQyMRhJWTZdG72SBfi84J0Bkl1fdprfE1L7BaXQGqnDJyI O4a1tVdtkOQ1rtsTDG64t5ZZX688WBG41t5MzZCSXz6cTZdwNVVsOqiyvuqQrgZtGNQ2 p3PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987254; x=1722592054; 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=hXeSWJ/2XL0C00VB4D2wsSnDGte33aaztEw6N6+DVHo=; b=YhRX64vyBxcvZ0L9cbbuhBYPiTuSxAcoGIJaLEp5oT+YVl71cgoXNC3OLGWnz8Rwr7 GiE1/MTClOiyTe5V+a6rhnM50SZY4uEwvFqw1mhFMuDft85hXBrk50dMCX8zlj9cYeBD hOTIytPunc9L2tCtzwtQZ8G/X5NoTmnO2lm6KlMOyT6tL4uRM5bSP4/CBWaU0ZXG82bM iDoTj9zU86D73tn1lwnDGSO7UyGXxg/K8SIjQOjnvssxfMtrq89PwNjNTL7fEnvxKV69 nqneO/bdDLrf4HDbnwYmRNc3LKCZcaZrxj0x8uHfQTgMP5Dl51k6SduWQBkYRYeYZr0Q LFFg== X-Forwarded-Encrypted: i=1; AJvYcCVJ6mRUv1ajYp/tHHvcHnK2XgDZcYXI8L6SQaxFhT0SRB+GVan5shnvwK1+VUJf6LsXSZ8F++B1PxcvqVJLmGP1Py3IRM1h49AufwBy X-Gm-Message-State: AOJu0YyMk2fi/OMHbZ4eaZP/6LqO7vn8ZIpEpOVRzsvDkQJNwMX33vc7 /RVIgyUEfzSpQ2k5VJNGDJZv+M9FTgmq4lUytBTf8CLu0SIhkjf4 X-Google-Smtp-Source: AGHT+IEsB98ir1v1O+pC1M3K6sUPTrlIGX/N1CqYehZBqQeCofKQvFrGpl3C0i9QXGJhDvMHh4MUqQ== X-Received: by 2002:a17:902:dacc:b0:1fd:95ac:506b with SMTP id d9443c01a7336-1fed3af263fmr68421725ad.64.1721987254203; Fri, 26 Jul 2024 02:47:34 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7d15e98sm28127455ad.99.2024.07.26.02.47.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:47:33 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: ying.huang@intel.com, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hannes@cmpxchg.org, hughd@google.com, kaleshsingh@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, mhocko@suse.com, minchan@kernel.org, nphamcs@gmail.com, ryan.roberts@arm.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, yosryahmed@google.com Subject: [PATCH v5 4/4] mm: Introduce per-thpsize swapin control policy Date: Fri, 26 Jul 2024 21:46:18 +1200 Message-Id: <20240726094618.401593-5-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240726094618.401593-1-21cnbao@gmail.com> References: <20240726094618.401593-1-21cnbao@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Barry Song Quote Ying's comment: A user space interface can be implemented to select different swap-in order policies, similar to the mTHP allocation order policy. We need a distinct policy because the performance characteristics of memory allocation differ significantly from those of swap-in. For example, SSD read speeds can be much slower than memory allocation. With policy selection, I believe we can implement mTHP swap-in for non-SWAP_SYNCHRONOUS scenarios as well. However, users need to understand the implications of their choices. I think that it's better to start with at least always never. I believe that we will add auto in the future to tune automatically, which can be used as default finally. Suggested-by: "Huang, Ying" Signed-off-by: Barry Song --- Documentation/admin-guide/mm/transhuge.rst | 6 +++ include/linux/huge_mm.h | 1 + mm/huge_memory.c | 44 ++++++++++++++++++++++ mm/memory.c | 3 +- 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/adm= in-guide/mm/transhuge.rst index 058485daf186..2e94e956ee12 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -144,6 +144,12 @@ hugepage sizes have enabled=3D"never". If enabling mul= tiple hugepage sizes, the kernel will select the most appropriate enabled size for a given allocation. =20 +Transparent Hugepage Swap-in for anonymous memory can be disabled or enabl= ed +by per-supported-THP-size with one of:: + + echo always >/sys/kernel/mm/transparent_hugepage/hugepages-kB/swapi= n_enabled + echo never >/sys/kernel/mm/transparent_hugepage/hugepages-kB/swapin= _enabled + It's also possible to limit defrag efforts in the VM to generate anonymous hugepages in case they're not immediately free to madvise regions or to never try to defrag memory and simply fallback to regular diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index e25d9ebfdf89..25174305b17f 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -92,6 +92,7 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr; #define TVA_SMAPS (1 << 0) /* Will be used for procfs */ #define TVA_IN_PF (1 << 1) /* Page fault handler */ #define TVA_ENFORCE_SYSFS (1 << 2) /* Obey sysfs configuration */ +#define TVA_IN_SWAPIN (1 << 3) /* Do swap-in */ =20 #define thp_vma_allowable_order(vma, vm_flags, tva_flags, order) \ (!!thp_vma_allowable_orders(vma, vm_flags, tva_flags, BIT(order))) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 0167dc27e365..41460847988c 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -80,6 +80,7 @@ unsigned long huge_zero_pfn __read_mostly =3D ~0UL; unsigned long huge_anon_orders_always __read_mostly; unsigned long huge_anon_orders_madvise __read_mostly; unsigned long huge_anon_orders_inherit __read_mostly; +unsigned long huge_anon_orders_swapin_always __read_mostly; =20 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, unsigned long vm_flags, @@ -88,6 +89,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_s= truct *vma, { bool smaps =3D tva_flags & TVA_SMAPS; bool in_pf =3D tva_flags & TVA_IN_PF; + bool in_swapin =3D tva_flags & TVA_IN_SWAPIN; bool enforce_sysfs =3D tva_flags & TVA_ENFORCE_SYSFS; unsigned long supported_orders; =20 @@ -100,6 +102,8 @@ unsigned long __thp_vma_allowable_orders(struct vm_area= _struct *vma, supported_orders =3D THP_ORDERS_ALL_FILE_DEFAULT; =20 orders &=3D supported_orders; + if (in_swapin) + orders &=3D READ_ONCE(huge_anon_orders_swapin_always); if (!orders) return 0; =20 @@ -523,8 +527,48 @@ static ssize_t thpsize_enabled_store(struct kobject *k= obj, static struct kobj_attribute thpsize_enabled_attr =3D __ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store); =20 +static DEFINE_SPINLOCK(huge_anon_orders_swapin_lock); + +static ssize_t thpsize_swapin_enabled_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + int order =3D to_thpsize(kobj)->order; + const char *output; + + if (test_bit(order, &huge_anon_orders_swapin_always)) + output =3D "[always] never"; + else + output =3D "always [never]"; + + return sysfs_emit(buf, "%s\n", output); +} + +static ssize_t thpsize_swapin_enabled_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int order =3D to_thpsize(kobj)->order; + ssize_t ret =3D count; + + if (sysfs_streq(buf, "always")) { + spin_lock(&huge_anon_orders_swapin_lock); + set_bit(order, &huge_anon_orders_swapin_always); + spin_unlock(&huge_anon_orders_swapin_lock); + } else if (sysfs_streq(buf, "never")) { + spin_lock(&huge_anon_orders_swapin_lock); + clear_bit(order, &huge_anon_orders_swapin_always); + spin_unlock(&huge_anon_orders_swapin_lock); + } else + ret =3D -EINVAL; + + return ret; +} +static struct kobj_attribute thpsize_swapin_enabled_attr =3D + __ATTR(swapin_enabled, 0644, thpsize_swapin_enabled_show, thpsize_swapin_= enabled_store); + static struct attribute *thpsize_attrs[] =3D { &thpsize_enabled_attr.attr, + &thpsize_swapin_enabled_attr.attr, #ifdef CONFIG_SHMEM &thpsize_shmem_enabled_attr.attr, #endif diff --git a/mm/memory.c b/mm/memory.c index 14048e9285d4..27c77f739a2c 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4091,7 +4091,8 @@ static struct folio *alloc_swap_folio(struct vm_fault= *vmf) * and suitable for swapping THP. */ orders =3D thp_vma_allowable_orders(vma, vma->vm_flags, - TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1); + TVA_IN_PF | TVA_IN_SWAPIN | TVA_ENFORCE_SYSFS, + BIT(PMD_ORDER) - 1); orders =3D thp_vma_suitable_orders(vma, vmf->address, orders); orders =3D thp_swap_suitable_orders(swp_offset(entry), vmf->address, orde= rs); =20 --=20 2.34.1