All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs: respect the max size in the header when activating swap file
@ 2021-12-16 15:00 fdmanana
  2021-12-17 15:08 ` Josef Bacik
  2022-01-03 18:30 ` David Sterba
  0 siblings, 2 replies; 3+ messages in thread
From: fdmanana @ 2021-12-16 15:00 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

If we extended the size of a swapfile after its header was created (by the
mkswap utility) and then try to activate it, we will map the entire file
when activating the swap file, instead of limiting to the max size defined
in the swap file's header.

Currently test case generic/643 from fstests fails because we do not
respect that size limit defined in the swap file's header.

So fix this by not mapping file ranges beyond the max size defined in the
swap header.

This is the same type of bug that iomap used to have, and was fixed in
commit 36ca7943ac18ae ("mm/swap: consider max pages in
iomap_swapfile_add_extent").

Fixes: ed46ff3d423780 ("Btrfs: support swap files")
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/inode.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 97fd33d599eb..b17d14ec4d46 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -10263,9 +10263,19 @@ static int btrfs_add_swap_extent(struct swap_info_struct *sis,
 				 struct btrfs_swap_info *bsi)
 {
 	unsigned long nr_pages;
+	unsigned long max_pages;
 	u64 first_ppage, first_ppage_reported, next_ppage;
 	int ret;
 
+	/*
+	 * Our swapfile may have had its size extended after the swap header was
+	 * written. In that case activating the swapfile should not go beyond
+	 * the max size set in the swap header.
+	 */
+	if (bsi->nr_pages >= sis->max)
+		return 0;
+
+	max_pages = sis->max - bsi->nr_pages;
 	first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
 	next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
 				PAGE_SIZE) >> PAGE_SHIFT;
@@ -10273,6 +10283,7 @@ static int btrfs_add_swap_extent(struct swap_info_struct *sis,
 	if (first_ppage >= next_ppage)
 		return 0;
 	nr_pages = next_ppage - first_ppage;
+	nr_pages = min(nr_pages, max_pages);
 
 	first_ppage_reported = first_ppage;
 	if (bsi->start == 0)
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] btrfs: respect the max size in the header when activating swap file
  2021-12-16 15:00 [PATCH] btrfs: respect the max size in the header when activating swap file fdmanana
@ 2021-12-17 15:08 ` Josef Bacik
  2022-01-03 18:30 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: Josef Bacik @ 2021-12-17 15:08 UTC (permalink / raw)
  To: fdmanana; +Cc: linux-btrfs

On Thu, Dec 16, 2021 at 03:00:32PM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> If we extended the size of a swapfile after its header was created (by the
> mkswap utility) and then try to activate it, we will map the entire file
> when activating the swap file, instead of limiting to the max size defined
> in the swap file's header.
> 
> Currently test case generic/643 from fstests fails because we do not
> respect that size limit defined in the swap file's header.
> 
> So fix this by not mapping file ranges beyond the max size defined in the
> swap header.
> 
> This is the same type of bug that iomap used to have, and was fixed in
> commit 36ca7943ac18ae ("mm/swap: consider max pages in
> iomap_swapfile_add_extent").
> 
> Fixes: ed46ff3d423780 ("Btrfs: support swap files")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Validated it to make sure it fixes the problem, you can add

Reviewed-and-tested-by: Josef Bacik <josef@toxicpanda.com>

Thanks,

Josef

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] btrfs: respect the max size in the header when activating swap file
  2021-12-16 15:00 [PATCH] btrfs: respect the max size in the header when activating swap file fdmanana
  2021-12-17 15:08 ` Josef Bacik
@ 2022-01-03 18:30 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2022-01-03 18:30 UTC (permalink / raw)
  To: fdmanana; +Cc: linux-btrfs

On Thu, Dec 16, 2021 at 03:00:32PM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> If we extended the size of a swapfile after its header was created (by the
> mkswap utility) and then try to activate it, we will map the entire file
> when activating the swap file, instead of limiting to the max size defined
> in the swap file's header.
> 
> Currently test case generic/643 from fstests fails because we do not
> respect that size limit defined in the swap file's header.
> 
> So fix this by not mapping file ranges beyond the max size defined in the
> swap header.
> 
> This is the same type of bug that iomap used to have, and was fixed in
> commit 36ca7943ac18ae ("mm/swap: consider max pages in
> iomap_swapfile_add_extent").
> 
> Fixes: ed46ff3d423780 ("Btrfs: support swap files")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Added to misc-next, thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-03 18:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-16 15:00 [PATCH] btrfs: respect the max size in the header when activating swap file fdmanana
2021-12-17 15:08 ` Josef Bacik
2022-01-03 18:30 ` David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.