From nobody Sat Nov 30 16:42:56 2024 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 BE554136663 for ; Sun, 8 Sep 2024 23:21:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725837705; cv=none; b=l7SuAyRgSZ0QTQVJvgCmTHgY3/rW3X1Y9ZDOoNwCg08SKXOhxE8yiqrgnxA9SUVwt/jpyuwyULm3YU6/TO4JLWkN9vjFdGvlJ622cZhkyQZzjNrDlphb/6t9wX4pGoW4R2CCUMopqhHMtvT/HXmxAiSEaa2NdXs4T+HqFMHgAIk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725837705; c=relaxed/simple; bh=wdNlq0LZHcppiaaSB0gvnyZ/a3Roq3Xi+TBV9VIz/98=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QEtvfSsNTF5srhkEXos25X7QeqLK2V07uCux1bBR4QtRBxCxuhfGYPC2MmuJwKnNnxN2Z92+t3QEZXa1NIx44zk4Dfpusbdnjo440b7jE5gOq8Ds1OdX86y6Mqmgsy4gR20G+/hPdd1UgFlTVCbBNpXo2bGpeaHZWHzz3SyBIxs= 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=ALkz4Arl; arc=none smtp.client-ip=209.85.214.173 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="ALkz4Arl" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2054feabfc3so31917725ad.1 for ; Sun, 08 Sep 2024 16:21:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725837703; x=1726442503; 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=R2xC+tIjHn2wWvMR2T1OflcDy+oEzjuKFqq/trb+O1U=; b=ALkz4ArlhG8xp4wV8o8tuvplyF3DnevefyuNWha2Dw5iW+5RMv8qzCc+d2ih6uZG8M sRi8Y7S5OzaCbWZlIt0bzMkMxP7gF0GJaEwJU11t4n6MQLtQRnIPRDyHOnu0qpxo6LEi 7LueX0G63m3qVzeXTVQa81YYraFqXmjFw8bnexgl/s2Bxc+EuibjWa725fi2HaQkBLp6 chSFgp6ej68uz/9A/em/WfBCN8mkyIQtk7GLzwBsffDyRmvXjNR+LS0zk0aHm59rmL+n gWcbDXbnAge2fbPAQOVOHcStkS5J+nZDyjiSN0wLc9Nj9/SZjPmkyfQIvV5BXrHjrf+b AqVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725837703; x=1726442503; 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=R2xC+tIjHn2wWvMR2T1OflcDy+oEzjuKFqq/trb+O1U=; b=iFvI9sfcDwgk6gLTr/5MdSqC+KXWU+YWbNqCg92oyjSIsW5Nf+fYfLiJJwSeSPMekw O3BOt+CDetV0QKD4IgWT1/Qawzz09HVUgF6JxiG8hq8e72b37xuIjSBVqpn/Sdy9jpmi 4+LA7WLhlBOGhOHLzHonV1dQTF6Rjzz1I64/f1Lir1RhSV5cf27FKoBanvN+qoOIusFs it1+FquYyNNKHgCPPbs/25ZkVbDTwSHHBG7FWEpRrbWpN2BH+Vp6dPfbVY0TsUjLWZ43 xOT1EguCp0eoIzcpcqzDFDRKdK6gfCzF2441GdkksL442tGOFho6I4+TV1RIAbFdSIb1 ufXg== X-Forwarded-Encrypted: i=1; AJvYcCVSaSeSZgc/4KZaHvK1Mv0XR/8AsnL6LuOG0CpVKJZgkmx32aAlfXR+7bQXaMOV1rRuuroTQ4H+e1bL7Mo=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9Xi2fBTtqeQ5xNVLoc6m4QeEUXjiCT6EKITTL0M3YIhx19vTD uw4unbun1B3KqonjxSVUa0ZiqojJVdXp8JoR5VXzcgtyT/zxD/ef X-Google-Smtp-Source: AGHT+IFgSDJb5dN2/2g631ifqwqWtDRQhZpgCd9etl+bEZOuXVcAb2DQHY5prsg3HDa8T4Pw/lnH/g== X-Received: by 2002:a05:6a21:4581:b0:1cf:3838:1ec5 with SMTP id adf61e73a8af0-1cf38381efdmr4465870637.5.1725837702701; Sun, 08 Sep 2024 16:21:42 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:ecbd:b95f:f7b2:8158]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-718e58c703asm2447643b3a.82.2024.09.08.16.21.33 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 08 Sep 2024 16:21:42 -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, hch@infradead.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, ryncsn@gmail.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yosryahmed@google.com, Usama Arif Subject: [PATCH v9 1/3] mm: Fix swap_read_folio_zeromap() for large folios with partial zeromap Date: Mon, 9 Sep 2024 11:21:17 +1200 Message-Id: <20240908232119.2157-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240908232119.2157-1-21cnbao@gmail.com> References: <20240908232119.2157-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 There could be a corner case where the first entry is non-zeromap, but a subsequent entry is zeromap. In this case, we should not let swap_read_folio_zeromap() return false since we will still read corrupted data. Additionally, the iteration of test_bit() is unnecessary and can be replaced with bitmap operations, which are more efficient. We can adopt the style of swap_pte_batch() and folio_pte_batch() to introduce swap_zeromap_batch() which seems to provide the greatest flexibility for the caller. This approach allows the caller to either check if the zeromap status of all entries is consistent or determine the number of contiguous entries with the same status. Since swap_read_folio() can't handle reading a large folio that's partially zeromap and partially non-zeromap, we've moved the code to mm/swap.h so that others, like those working on swap-in, can access it. Fixes: 0ca0c24e3211 ("mm: store zero pages to be swapped out in a bitmap") Cc: Usama Arif Reviewed-by: Yosry Ahmed Signed-off-by: Barry Song Reviewed-by: Usama Arif --- mm/page_io.c | 32 +++++++------------------------- mm/swap.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/mm/page_io.c b/mm/page_io.c index 4bc77d1c6bfa..bc1183299a7d 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -226,26 +226,6 @@ static void swap_zeromap_folio_clear(struct folio *fol= io) } } =20 -/* - * Return the index of the first subpage which is not zero-filled - * according to swap_info_struct->zeromap. - * If all pages are zero-filled according to zeromap, it will return - * folio_nr_pages(folio). - */ -static unsigned int swap_zeromap_folio_test(struct folio *folio) -{ - struct swap_info_struct *sis =3D swp_swap_info(folio->swap); - swp_entry_t entry; - unsigned int i; - - for (i =3D 0; i < folio_nr_pages(folio); i++) { - entry =3D page_swap_entry(folio_page(folio, i)); - if (!test_bit(swp_offset(entry), sis->zeromap)) - return i; - } - return i; -} - /* * We may have stale swap cache pages in memory: notice * them here and get rid of the unnecessary final write. @@ -524,19 +504,21 @@ static void sio_read_complete(struct kiocb *iocb, lon= g ret) =20 static bool swap_read_folio_zeromap(struct folio *folio) { - unsigned int idx =3D swap_zeromap_folio_test(folio); - - if (idx =3D=3D 0) - return false; + int nr_pages =3D folio_nr_pages(folio); + bool is_zeromap; =20 /* * Swapping in a large folio that is partially in the zeromap is not * currently handled. Return true without marking the folio uptodate so * that an IO error is emitted (e.g. do_swap_page() will sigbus). */ - if (WARN_ON_ONCE(idx < folio_nr_pages(folio))) + if (WARN_ON_ONCE(swap_zeromap_batch(folio->swap, nr_pages, + &is_zeromap) !=3D nr_pages)) return true; =20 + if (!is_zeromap) + return false; + folio_zero_range(folio, 0, folio_size(folio)); folio_mark_uptodate(folio); return true; diff --git a/mm/swap.h b/mm/swap.h index f8711ff82f84..ad2f121de970 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -80,6 +80,32 @@ static inline unsigned int folio_swap_flags(struct folio= *folio) { return swp_swap_info(folio->swap)->flags; } + +/* + * Return the count of contiguous swap entries that share the same + * zeromap status as the starting entry. If is_zeromap is not NULL, + * it will return the zeromap status of the starting entry. + */ +static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr, + bool *is_zeromap) +{ + struct swap_info_struct *sis =3D swp_swap_info(entry); + unsigned long start =3D swp_offset(entry); + unsigned long end =3D start + max_nr; + bool first_bit; + + first_bit =3D test_bit(start, sis->zeromap); + if (is_zeromap) + *is_zeromap =3D first_bit; + + if (max_nr <=3D 1) + return max_nr; + if (first_bit) + return find_next_zero_bit(sis->zeromap, end, start) - start; + else + return find_next_bit(sis->zeromap, end, start) - start; +} + #else /* CONFIG_SWAP */ struct swap_iocb; static inline void swap_read_folio(struct folio *folio, struct swap_iocb *= *plug) @@ -171,6 +197,13 @@ static inline unsigned int folio_swap_flags(struct fol= io *folio) { return 0; } + +static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr, + bool *has_zeromap) +{ + return 0; +} + #endif /* CONFIG_SWAP */ =20 #endif /* _MM_SWAP_H */ --=20 2.34.1 From nobody Sat Nov 30 16:42:56 2024 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 7620217A59B for ; Sun, 8 Sep 2024 23:21:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725837714; cv=none; b=liEQ/TcdTCUKdyDDHz9Cc106xmDzavT9WsXYvFP0XJJpLNHxzPiKisyBCoafQh5Th09NQo2/PzjELgEJt1mqa0JSTsyjLsDsf6ap3Uy1g3r4QnWPvuQbPVOSdVOsmWaVudwgTn+PRh/y5yzSRFjEy/f8TEMt4HpC2/QwJFkcqwQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725837714; c=relaxed/simple; bh=s5cN/CR30bvQrW0O19dF2kIQlH/i6E72paHAZrCWM3k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XkUognxSPsTalEKzQcVq4Os979Tn/rareWEgQ7RrLWApktY5sbZcfp/tsCfekmgM3VrMpxwq8Ed65Rzc8N7Xjtz5GBEA3NnKf01Toki/Y+wbBdX7M3sxydNW/Nhdxn9JedcySQvIM59FeegdDZHNMrouy0dHtyG3awhGk83Sd1U= 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=TF4mhsFg; arc=none smtp.client-ip=209.85.210.174 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="TF4mhsFg" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-718e9c8bd83so538872b3a.1 for ; Sun, 08 Sep 2024 16:21:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725837712; x=1726442512; 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=OlYXV3Fv3CqOaRAKUuC/QzTZ9pL0veLk2T3qfKeSJ+c=; b=TF4mhsFgG9w2umPnQfVFVeOF5/g+1ZcMPYWtmvhk4hN3SC+JmTpfHLmP8nhYf8fTBV FGfqjG/m+u3eO1jTSeYMQlFx50I8sCcz5JRqBbzSGeLcLbYWWO5n/W+dB80BD2m/YkaM 8xEt02VlFaijiafC2eKCiCHA6wcbQasAb6gYEah3kTqOzSfCIwfufDieqZ6kttsH8DP6 e5tgcEnUJ7HueTM1O4+pARVZ+l9gC5J57/roW2Kr7ZzjwzJbii+o4rWvuENB5sKw8cZn YpCToFgYzNeJMAIUqAx6paLYL2XPp/DJngwHCskFMXbwT8CQUIREOSiaPe7f3y/5Hsy2 WqKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725837712; x=1726442512; 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=OlYXV3Fv3CqOaRAKUuC/QzTZ9pL0veLk2T3qfKeSJ+c=; b=Q0NIXfVT6IlxG0uuwCpi9hnpOJYpfEdMpeT6zWfr98BNz24efpCfn+CyVJ/SSiWLng PlE+Oz7VyF04gGe9jTby+hilM34ZSTcjrNHxFzXpJHdhGzEJq+0MnnMwFeBj2j+XFXVf 6zuGXFS7rH3+fKzrJhNMObmBbbZy70uUhMFuOWxwJUi4mJxclZA8lMD9aIa4i8kzoUyg DTcGsEYo5MehWJFHy4rwHI37raW5PZ2qsxVnKmIpIAUoJWqzPOOZV/E3NLTSRaF8mTt2 UBzS08m3YCA4xxtgmYjwGEmwNhXdN9GOcTC/LurcCnBdlfdzsOIQu/Z0OZCiq02//MuB uQXw== X-Forwarded-Encrypted: i=1; AJvYcCWtUnlgCZxHwNJqrSzfNBj4xXoXOQ6/PhHLKvQCYYrgq4g+JF06WmCr6eAJzTTs3qOgjgD9OhJTfgauCvQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yxt/rNCrgJHzdQ0cT2osIngAPNABgqV7IqMeOzo7j/hceAw+lc5 MRD2IKq+9VSIqByQk0AXu32tAZPyOqibeEzmO7wd8PWmIgxoHbWN X-Google-Smtp-Source: AGHT+IGL5o84rj87mdfLAxsPpLUvBk6PXR77RV9OsfYtgvZ14Unfm2nHOrZAzsQDfp012KISjhaCqw== X-Received: by 2002:a62:a503:0:b0:717:92d8:ca5c with SMTP id d2e1a72fcca58-71792d8cadamr17178636b3a.3.1725837711653; Sun, 08 Sep 2024 16:21:51 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:ecbd:b95f:f7b2:8158]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-718e58c703asm2447643b3a.82.2024.09.08.16.21.43 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 08 Sep 2024 16:21:51 -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, hch@infradead.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, ryncsn@gmail.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yosryahmed@google.com Subject: [PATCH v9 2/3] mm: add nr argument in mem_cgroup_swapin_uncharge_swap() helper to support large folios Date: Mon, 9 Sep 2024 11:21:18 +1200 Message-Id: <20240908232119.2157-3-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240908232119.2157-1-21cnbao@gmail.com> References: <20240908232119.2157-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, add nr argument in mem_cgroup_swapin_uncharge_swap(). For the existing two users, just pass nr=3D1. Signed-off-by: Barry Song Acked-by: Chris Li Reviewed-by: Yosry Ahmed Cc: Shakeel Butt Cc: Baolin Wang Cc: Christoph Hellwig Cc: David Hildenbrand Cc: Gao Xiang Cc: "Huang, Ying" Cc: Hugh Dickins Cc: Johannes Weiner Cc: Kairui Song Cc: Kairui Song Cc: Kalesh Singh Cc: Matthew Wilcox (Oracle) Cc: Michal Hocko Cc: Minchan Kim Cc: Nhat Pham Cc: Ryan Roberts Cc: Sergey Senozhatsky Cc: Suren Baghdasaryan Cc: Yang Shi --- include/linux/memcontrol.h | 5 +++-- mm/memcontrol.c | 7 ++++--- mm/memory.c | 2 +- mm/swap_state.c | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 2ef94c74847d..34d2da05f2f1 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -699,7 +699,8 @@ int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *me= mcg, gfp_t gfp, =20 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); + +void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry, unsigned int nr_pa= ges); =20 void __mem_cgroup_uncharge(struct folio *folio); =20 @@ -1206,7 +1207,7 @@ static inline int mem_cgroup_swapin_charge_folio(stru= ct folio *folio, return 0; } =20 -static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry) +static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry, unsi= gned int nr) { } =20 diff --git a/mm/memcontrol.c b/mm/memcontrol.c index bda6f75d22ff..c0d36ca20332 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4559,14 +4559,15 @@ int mem_cgroup_swapin_charge_folio(struct folio *fo= lio, struct mm_struct *mm, =20 /* * mem_cgroup_swapin_uncharge_swap - uncharge swap slot - * @entry: swap entry for which the page is charged + * @entry: the first swap entry for which the pages are charged + * @nr_pages: number of pages which will be uncharged * * Call this function after successfully adding the charged page to swapca= che. * * Note: This function assumes the page for which swap slot is being uncha= rged * is order 0 page. */ -void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry) +void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry, unsigned int nr_pa= ges) { /* * Cgroup1's unified memory+swap counter has been charged with the @@ -4586,7 +4587,7 @@ void mem_cgroup_swapin_uncharge_swap(swp_entry_t entr= y) * let's not wait for it. The page already received a * memory+swap charge, drop the swap entry duplicate. */ - mem_cgroup_uncharge_swap(entry, 1); + mem_cgroup_uncharge_swap(entry, nr_pages); } } =20 diff --git a/mm/memory.c b/mm/memory.c index 42674c0748cb..cdf03b39a92c 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4100,7 +4100,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) ret =3D VM_FAULT_OOM; goto out_page; } - mem_cgroup_swapin_uncharge_swap(entry); + mem_cgroup_swapin_uncharge_swap(entry, 1); =20 shadow =3D get_shadow_from_swap_cache(entry); if (shadow) diff --git a/mm/swap_state.c b/mm/swap_state.c index a042720554a7..4669f29cf555 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -522,7 +522,7 @@ struct folio *__read_swap_cache_async(swp_entry_t entry= , gfp_t gfp_mask, if (add_to_swap_cache(new_folio, entry, gfp_mask & GFP_RECLAIM_MASK, &sha= dow)) goto fail_unlock; =20 - mem_cgroup_swapin_uncharge_swap(entry); + mem_cgroup_swapin_uncharge_swap(entry, 1); =20 if (shadow) workingset_refault(new_folio, shadow); --=20 2.34.1 From nobody Sat Nov 30 16:42:56 2024 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (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 283F417085C for ; Sun, 8 Sep 2024 23:22:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725837724; cv=none; b=LXYUGKqfWvGFG4t9qjE/cXfnpkA8MHfduIPrBwuw7rx6Hcqc8Mlk2qj5bspYpMBTrzfD1MhpInSMzZtAWi3kCSEE1RtGq6ZAqI6LZlxe9IvKCtjqzbQNPOLEvkfbmpkIWpYjFoMQP8MzK5KkC0AzqJmuwsE8lUViOLCjTQyCGDM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725837724; c=relaxed/simple; bh=2IEesW/JBIfF/kzi7UC9vArzc7cARt7TQnQPOk18wqc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uqT+22NuFi4Hr42RmdJHnSZSflP2VhsSNpYD5a91qsTzLG6E5eX8rP435Qs8UvmRjdNqeZxfvjEbndXm1LZd5yqQ5MtRt8wKfYvIiBZMhvtyjrQXvAxoAirUlB+EA93Ung8gqsBH5K3Qljw0PTkhexsjxnC3WL2PrMhX5H/lCmk= 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=LoCmHQfd; arc=none smtp.client-ip=209.85.210.177 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="LoCmHQfd" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-717929b671eso2885035b3a.0 for ; Sun, 08 Sep 2024 16:22:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725837721; x=1726442521; 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=vV2tR4Goy2aJJyrtdQU+NgrOzgVlBcOxf/RbnCByfrk=; b=LoCmHQfd0sI6nbtIkkhdYJjL9TtSYaKx2XmHc4GNRjK3q3V403RmW21m1UO767FzFO sIFhLDq1p2OsYcgkSsD+NUcCKrFV1T0aB89Q+jCOD76b+bNIXD+y+kYMJCcmV+sdfetE A8dcMIf7kY+05ROth2nctoZHel0EECqERxqnaPD21ENSCNhoWSNCm/T1jj+aF/Yy2Dbp DerEZepsD729SN6+DTBfpRvIZ2jWQ9fZkgzzv5r0qPo/VaXFTuw59womVcT+wAcD994u ABIZVeOQaANIjtMyDFvg3cIuXBVzywebmvWiN4/U7SpaOL9nA8XTlLQIzO1wLiAsSl16 AczQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725837721; x=1726442521; 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=vV2tR4Goy2aJJyrtdQU+NgrOzgVlBcOxf/RbnCByfrk=; b=WEdZ24o8iky2KtXoaVSI8V06k0chYBymTGOabG1+yTp9wl1nUOVnXM/zL3JaVRdMou zfhqYKVjY7mQ6Rc9MUoGvNc4vK4WmpRoCMmQ5R4BhGu8by5tKmJ2Y3qzmB/h/oridKh6 w/iCocZNns+LLx4RHBCxOqP0D+QPKsc6dkMB9uuVa4RviWiNuAJ4Ka5mp906Y+nbO8C1 UdDuyE79coOfW7DbB8T5TMMi+xChgB3DzPlToaHxFGyAg874ndutGq82FpuRnuXXJOwS ksaDGmjtew4LCw4xJ4Nzs2LVATcblF56uah9/zy29lj/0lPvfqpphJ9B4joNc+PuFuGc YEqw== X-Forwarded-Encrypted: i=1; AJvYcCUORnBDVBf/0ZQb+0l16tSsFhERTQJTAAhkipO0dvf5GkxNbkc4BS35bWYsoIAB6dnkQIV8zYyJ2PuuDcE=@vger.kernel.org X-Gm-Message-State: AOJu0Yxh7VqBrBZoYpsp3gxN34IQKg0t6V6E2zNIDI+JjdyftT7E8StI G4dGe8gHWQhblQfWXPLQmiHx2MxufxTk5+DbPibv7k3r9tRK34va X-Google-Smtp-Source: AGHT+IHRJq5JYuctycrfpKYXeZj++jFBYwPbiiUe8eU/NW1tJMIzK2yvOhZETJo0+EJD3FXhIFsKrw== X-Received: by 2002:a05:6a00:1993:b0:714:3de8:a616 with SMTP id d2e1a72fcca58-718d5ef9415mr9444174b3a.19.1725837721272; Sun, 08 Sep 2024 16:22:01 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:ecbd:b95f:f7b2:8158]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-718e58c703asm2447643b3a.82.2024.09.08.16.21.52 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 08 Sep 2024 16:22:00 -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, hch@infradead.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, ryncsn@gmail.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yosryahmed@google.com, Usama Arif , Kanchana P Sridhar Subject: [PATCH v9 3/3] mm: support large folios swap-in for sync io devices Date: Mon, 9 Sep 2024 11:21:19 +1200 Message-Id: <20240908232119.2157-4-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240908232119.2157-1-21cnbao@gmail.com> References: <20240908232119.2157-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 Currently, we have mTHP features, but unfortunately, without support for large folio swap-ins, once these large folios are swapped out, they are lost because mTHP swap is a one-way process. The lack of mTHP swap-in functionality prevents mTHP from being used on devices like Android that heavily rely on swap. This patch introduces mTHP swap-in support. It starts from sync devices such as zRAM. This is probably the simplest and most common use case, benefiting billions 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. Large folios in the buddy system are also preserved as much as possible, rather than being fragmented due to swap-in. 2. Eliminates fragmentation in swap slots and supports successful THP_SWPOUT. w/o this patch (Refer to the data from Chris's and Kairui's latest swap allocator optimization while running ./thp_swap_allocator_test w/o "-a" option [1]): ./thp_swap_allocator_test Iteration 1: swpout inc: 233, swpout fallback inc: 0, Fallback percentag= e: 0.00% Iteration 2: swpout inc: 131, swpout fallback inc: 101, Fallback percent= age: 43.53% Iteration 3: swpout inc: 71, swpout fallback inc: 155, Fallback percenta= ge: 68.58% Iteration 4: swpout inc: 55, swpout fallback inc: 168, Fallback percenta= ge: 75.34% Iteration 5: swpout inc: 35, swpout fallback inc: 191, Fallback percenta= ge: 84.51% Iteration 6: swpout inc: 25, swpout fallback inc: 199, Fallback percenta= ge: 88.84% Iteration 7: swpout inc: 23, swpout fallback inc: 205, Fallback percenta= ge: 89.91% Iteration 8: swpout inc: 9, swpout fallback inc: 219, Fallback percentag= e: 96.05% Iteration 9: swpout inc: 13, swpout fallback inc: 213, Fallback percenta= ge: 94.25% Iteration 10: swpout inc: 12, swpout fallback inc: 216, Fallback percent= age: 94.74% Iteration 11: swpout inc: 16, swpout fallback inc: 213, Fallback percent= age: 93.01% Iteration 12: swpout inc: 10, swpout fallback inc: 210, Fallback percent= age: 95.45% Iteration 13: swpout inc: 16, swpout fallback inc: 212, Fallback percent= age: 92.98% Iteration 14: swpout inc: 12, swpout fallback inc: 212, Fallback percent= age: 94.64% Iteration 15: swpout inc: 15, swpout fallback inc: 211, Fallback percent= age: 93.36% Iteration 16: swpout inc: 15, swpout fallback inc: 200, Fallback percent= age: 93.02% Iteration 17: swpout inc: 9, swpout fallback inc: 220, Fallback percenta= ge: 96.07% w/ this patch (always 0%): Iteration 1: swpout inc: 948, swpout fallback inc: 0, Fallback percentag= e: 0.00% Iteration 2: swpout inc: 953, swpout fallback inc: 0, Fallback percentag= e: 0.00% Iteration 3: swpout inc: 950, swpout fallback inc: 0, Fallback percentag= e: 0.00% Iteration 4: swpout inc: 952, swpout fallback inc: 0, Fallback percentag= e: 0.00% Iteration 5: swpout inc: 950, swpout fallback inc: 0, Fallback percentag= e: 0.00% Iteration 6: swpout inc: 950, swpout fallback inc: 0, Fallback percentag= e: 0.00% Iteration 7: swpout inc: 947, swpout fallback inc: 0, Fallback percentag= e: 0.00% Iteration 8: swpout inc: 950, swpout fallback inc: 0, Fallback percentag= e: 0.00% Iteration 9: swpout inc: 950, swpout fallback inc: 0, Fallback percentag= e: 0.00% Iteration 10: swpout inc: 945, swpout fallback inc: 0, Fallback percenta= ge: 0.00% Iteration 11: swpout inc: 947, swpout fallback inc: 0, Fallback percenta= ge: 0.00% ... 3. With both mTHP swap-out and swap-in supported, we offer the option to en= able zsmalloc compression/decompression with larger granularity[2]. The upcom= ing optimization in zsmalloc will significantly increase swap speed and impr= ove compression efficiency. Tested by running 100 iterations of swapping 100= MiB of anon memory, the swap speed improved dramatically: time consumption of swapin(ms) time consumption of swapou= t(ms) lz4 4k 45274 90540 lz4 64k 22942 55667 zstdn 4k 85035 186585 zstdn 64k 46558 118533 The compression ratio also improved, as evaluated with 1 GiB of data: granularity orig_data_size compr_data_size 4KiB-zstd 1048576000 246876055 64KiB-zstd 1048576000 199763892 Without mTHP swap-in, the potential optimizations in zsmalloc cannot be realized. 4. Even mTHP swap-in itself can reduce swap-in page faults by a factor of nr_pages. Swapping in content filled with the same data 0x11, w/o and w/ the patch for five rounds (Since the content is the same, decompression will be very fast. This primarily assesses the impact of reduced page faults): swp in bandwidth(bytes/ms) w/o w/ round1 624152 1127501 round2 631672 1127501 round3 620459 1139756 round4 606113 1139756 round5 624152 1152281 avg 621310 1137359 +83% 5. With both mTHP swap-out and swap-in supported, we offer the option to en= able hardware accelerators(Intel IAA) to do parallel decompression with which Kanchana reported 7X improvement on zRAM read latency[3]. [1] https://lore.kernel.org/all/20240730-swap-allocator-v5-0-cb9c148b9297@k= ernel.org/ [2] https://lore.kernel.org/all/20240327214816.31191-1-21cnbao@gmail.com/ [3] https://lore.kernel.org/all/cover.1714581792.git.andre.glover@linux.int= el.com/ Signed-off-by: Chuanhua Han Co-developed-by: Barry Song Signed-off-by: Barry Song Cc: Baolin Wang Cc: Chris Li Cc: Christoph Hellwig Cc: David Hildenbrand Cc: Gao Xiang Cc: "Huang, Ying" Cc: Hugh Dickins Cc: Johannes Weiner Cc: Kairui Song Cc: Kalesh Singh Cc: Matthew Wilcox (Oracle) Cc: Michal Hocko Cc: Minchan Kim Cc: Nhat Pham Cc: Ryan Roberts Cc: Sergey Senozhatsky Cc: Shakeel Butt Cc: Suren Baghdasaryan Cc: Yang Shi Cc: Yosry Ahmed Cc: Usama Arif Cc: Kanchana P Sridhar --- mm/memory.c | 261 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 234 insertions(+), 27 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index cdf03b39a92c..d35dd8d99c8a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3985,6 +3985,194 @@ static vm_fault_t handle_pte_marker(struct vm_fault= *vmf) return VM_FAULT_SIGBUS; } =20 +static struct folio *__alloc_swap_folio(struct vm_fault *vmf) +{ + struct vm_area_struct *vma =3D vmf->vma; + struct folio *folio; + swp_entry_t entry; + + folio =3D vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, + vmf->address, false); + if (!folio) + return NULL; + + entry =3D pte_to_swp_entry(vmf->orig_pte); + if (mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, + GFP_KERNEL, entry)) { + folio_put(folio); + return NULL; + } + + return folio; +} + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static inline int non_swapcache_batch(swp_entry_t entry, int max_nr) +{ + struct swap_info_struct *si =3D swp_swap_info(entry); + pgoff_t offset =3D swp_offset(entry); + int i; + + /* + * While allocating a large folio and doing swap_read_folio, 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. + */ + for (i =3D 0; i < max_nr; i++) { + if ((si->swap_map[offset + i] & SWAP_HAS_CACHE)) + return i; + } + + return i; +} + +/* + * Check if the PTEs within a range are contiguous swap entries + * and have consistent swapcache, zeromap. + */ +static bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep, int nr_pages) +{ + unsigned long addr; + swp_entry_t entry; + int idx; + 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); + if (swap_pte_batch(ptep, nr_pages, pte) !=3D nr_pages) + return false; + + /* + * swap_read_folio() can't handle the case a large folio is hybridly + * from different backends. And they are likely corner cases. Similar + * things might be added once zswap support large folios. + */ + if (unlikely(swap_zeromap_batch(entry, nr_pages, NULL) !=3D nr_pages)) + return false; + if (unlikely(non_swapcache_batch(entry, nr_pages) !=3D nr_pages)) + 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 that its first swap_offset + * is aligned with that number, as it was when the THP was swapped out. + * This helps 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; +} + +static struct folio *alloc_swap_folio(struct vm_fault *vmf) +{ + struct vm_area_struct *vma =3D vmf->vma; + 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, orders); + + 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) { + if (!mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, + gfp, entry)) + return folio; + folio_put(folio); + } + order =3D next_order(&orders, order); + } + +fallback: + return __alloc_swap_folio(vmf); +} +#else /* !CONFIG_TRANSPARENT_HUGEPAGE */ +static inline bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep, int n= r_pages) +{ + return false; +} + +static struct folio *alloc_swap_folio(struct vm_fault *vmf) +{ + return __alloc_swap_folio(vmf); +} +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + /* * We enter with non-exclusive mmap_lock (to exclude vma changes, * but allow concurrent faults), and pte mapped but not yet locked. @@ -4073,34 +4261,34 @@ 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, 1)) { - /* 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); if (folio) { __folio_set_locked(folio); __folio_set_swapbacked(folio); =20 - if (mem_cgroup_swapin_charge_folio(folio, - vma->vm_mm, GFP_KERNEL, - entry)) { - ret =3D VM_FAULT_OOM; + 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(entry, nr_pages)) { + /* + * Relax a bit to prevent rapid + * repeated page faults. + */ + schedule_timeout_uninterruptible(1); goto out_page; } - mem_cgroup_swapin_uncharge_swap(entry, 1); + need_clear_cache =3D true; + + mem_cgroup_swapin_uncharge_swap(entry, nr_pages); =20 shadow =3D get_shadow_from_swap_cache(entry); if (shadow) @@ -4206,6 +4394,24 @@ 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; + pte_t folio_pte =3D ptep_get(folio_ptep); + + if (!pte_same(folio_pte, pte_move_swp_offset(vmf->orig_pte, -idx)) || + swap_pte_batch(folio_ptep, nr, folio_pte) !=3D 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; @@ -4337,11 +4543,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 { @@ -4384,7 +4591,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, 1); + swapcache_clear(si, entry, nr_pages); if (si) put_swap_device(si); return ret; @@ -4400,7 +4607,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio_put(swapcache); } if (need_clear_cache) - swapcache_clear(si, entry, 1); + swapcache_clear(si, entry, nr_pages); if (si) put_swap_device(si); return ret; --=20 2.34.1