2021-01-26 08:33:47 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
|
|
|
|
#ifndef BTRFS_SUBPAGE_H
|
|
|
|
#define BTRFS_SUBPAGE_H
|
|
|
|
|
|
|
|
#include <linux/spinlock.h>
|
2024-01-27 02:19:56 +00:00
|
|
|
#include <linux/atomic.h>
|
2024-08-05 05:32:54 +00:00
|
|
|
#include <linux/sizes.h>
|
2024-01-27 02:19:56 +00:00
|
|
|
|
|
|
|
struct address_space;
|
|
|
|
struct folio;
|
|
|
|
struct btrfs_fs_info;
|
2021-01-26 08:33:47 +00:00
|
|
|
|
2021-08-17 09:38:51 +00:00
|
|
|
/*
|
|
|
|
* Extra info for subpapge bitmap.
|
|
|
|
*
|
2023-05-31 06:04:57 +00:00
|
|
|
* For subpage we pack all uptodate/dirty/writeback/ordered bitmaps into
|
2021-08-17 09:38:51 +00:00
|
|
|
* one larger bitmap.
|
|
|
|
*
|
|
|
|
* This structure records how they are organized in the bitmap:
|
|
|
|
*
|
2024-08-26 06:14:50 +00:00
|
|
|
* /- uptodate /- dirty /- ordered
|
2021-08-17 09:38:51 +00:00
|
|
|
* | | |
|
|
|
|
* v v v
|
2023-05-31 06:04:57 +00:00
|
|
|
* |u|u|u|u|........|u|u|d|d|.......|d|d|o|o|.......|o|o|
|
2024-08-26 06:14:50 +00:00
|
|
|
* |< sectors_per_page >|
|
|
|
|
*
|
|
|
|
* Unlike regular macro-like enums, here we do not go upper-case names, as
|
|
|
|
* these names will be utilized in various macros to define function names.
|
2021-08-17 09:38:51 +00:00
|
|
|
*/
|
2024-08-26 06:14:50 +00:00
|
|
|
enum {
|
|
|
|
btrfs_bitmap_nr_uptodate = 0,
|
|
|
|
btrfs_bitmap_nr_dirty,
|
|
|
|
btrfs_bitmap_nr_writeback,
|
|
|
|
btrfs_bitmap_nr_ordered,
|
|
|
|
btrfs_bitmap_nr_checked,
|
|
|
|
btrfs_bitmap_nr_locked,
|
|
|
|
btrfs_bitmap_nr_max
|
2021-08-17 09:38:51 +00:00
|
|
|
};
|
|
|
|
|
2021-01-26 08:33:47 +00:00
|
|
|
/*
|
|
|
|
* Structure to trace status of each sector inside a page, attached to
|
|
|
|
* page::private for both data and metadata inodes.
|
|
|
|
*/
|
|
|
|
struct btrfs_subpage {
|
|
|
|
/* Common members for both data and metadata pages */
|
|
|
|
spinlock_t lock;
|
2021-01-26 08:33:48 +00:00
|
|
|
union {
|
btrfs: support subpage for extent buffer page release
In btrfs_release_extent_buffer_pages(), we need to add extra handling
for subpage.
Introduce a helper, detach_extent_buffer_page(), to do different
handling for regular and subpage cases.
For subpage case, handle detaching page private.
For unmapped (dummy or cloned) ebs, we can detach the page private
immediately as the page can only be attached to one unmapped eb.
For mapped ebs, we have to ensure there are no eb in the page range
before we delete it, as page->private is shared between all ebs in the
same page.
But there is a subpage specific race, where we can race with extent
buffer allocation, and clear the page private while new eb is still
being utilized, like this:
Extent buffer A is the new extent buffer which will be allocated,
while extent buffer B is the last existing extent buffer of the page.
T1 (eb A) | T2 (eb B)
-------------------------------+------------------------------
alloc_extent_buffer() | btrfs_release_extent_buffer_pages()
|- p = find_or_create_page() | |
|- attach_extent_buffer_page() | |
| | |- detach_extent_buffer_page()
| | |- if (!page_range_has_eb())
| | | No new eb in the page range yet
| | | As new eb A hasn't yet been
| | | inserted into radix tree.
| | |- btrfs_detach_subpage()
| | |- detach_page_private();
|- radix_tree_insert() |
Then we have a metadata eb whose page has no private bit.
To avoid such race, we introduce a subpage metadata-specific member,
btrfs_subpage::eb_refs.
In alloc_extent_buffer() we increase eb_refs in the critical section of
private_lock. Then page_range_has_eb() will return true for
detach_extent_buffer_page(), and will not detach page private.
The section is marked by:
- btrfs_page_inc_eb_refs()
- btrfs_page_dec_eb_refs()
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-01-26 08:33:50 +00:00
|
|
|
/*
|
|
|
|
* Structures only used by metadata
|
|
|
|
*
|
|
|
|
* @eb_refs should only be operated under private_lock, as it
|
|
|
|
* manages whether the subpage can be detached.
|
|
|
|
*/
|
|
|
|
atomic_t eb_refs;
|
2021-05-31 08:50:45 +00:00
|
|
|
|
2024-10-09 05:51:07 +00:00
|
|
|
/*
|
|
|
|
* Structures only used by data,
|
|
|
|
*
|
|
|
|
* How many sectors inside the page is locked.
|
|
|
|
*/
|
|
|
|
atomic_t nr_locked;
|
2021-01-26 08:33:48 +00:00
|
|
|
};
|
2021-08-17 09:38:52 +00:00
|
|
|
unsigned long bitmaps[];
|
2021-01-26 08:33:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum btrfs_subpage_type {
|
|
|
|
BTRFS_SUBPAGE_METADATA,
|
|
|
|
BTRFS_SUBPAGE_DATA,
|
|
|
|
};
|
|
|
|
|
2024-08-05 05:32:54 +00:00
|
|
|
#if PAGE_SIZE > SZ_4K
|
2023-12-06 23:09:28 +00:00
|
|
|
bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct address_space *mapping);
|
2024-08-05 05:32:54 +00:00
|
|
|
#else
|
|
|
|
static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
|
|
|
|
struct address_space *mapping)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
btrfs: make nodesize >= PAGE_SIZE case to reuse the non-subpage routine
The reason why we only support 64K page size for subpage is, for 64K
page size we can ensure no matter what the nodesize is, we can fit it
into one page.
When other page size come, especially like 16K, the limitation is a bit
limiting.
To remove such limitation, we allow nodesize >= PAGE_SIZE case to go the
non-subpage routine. By this, we can allow 4K sectorsize on 16K page
size.
Although this introduces another smaller limitation, the metadata can
not cross page boundary, which is already met by most recent mkfs.
Another small improvement is, we can avoid the overhead for metadata if
nodesize >= PAGE_SIZE.
For 4K sector size and 64K page size/node size, or 4K sector size and
16K page size/node size, we don't need to allocate extra memory for the
metadata pages.
Please note that, this patch will not yet enable other page size support
yet.
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-01-13 05:22:09 +00:00
|
|
|
|
2021-01-26 08:33:47 +00:00
|
|
|
int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
|
2023-12-12 02:28:37 +00:00
|
|
|
struct folio *folio, enum btrfs_subpage_type type);
|
|
|
|
void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info, struct folio *folio);
|
2021-01-26 08:33:47 +00:00
|
|
|
|
2021-01-26 08:33:48 +00:00
|
|
|
/* Allocate additional data where page represents more than one sector */
|
2021-08-17 09:38:50 +00:00
|
|
|
struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
|
|
|
|
enum btrfs_subpage_type type);
|
2021-01-26 08:33:48 +00:00
|
|
|
void btrfs_free_subpage(struct btrfs_subpage *subpage);
|
|
|
|
|
2023-12-06 23:09:28 +00:00
|
|
|
void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
|
|
|
|
void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
|
btrfs: support subpage for extent buffer page release
In btrfs_release_extent_buffer_pages(), we need to add extra handling
for subpage.
Introduce a helper, detach_extent_buffer_page(), to do different
handling for regular and subpage cases.
For subpage case, handle detaching page private.
For unmapped (dummy or cloned) ebs, we can detach the page private
immediately as the page can only be attached to one unmapped eb.
For mapped ebs, we have to ensure there are no eb in the page range
before we delete it, as page->private is shared between all ebs in the
same page.
But there is a subpage specific race, where we can race with extent
buffer allocation, and clear the page private while new eb is still
being utilized, like this:
Extent buffer A is the new extent buffer which will be allocated,
while extent buffer B is the last existing extent buffer of the page.
T1 (eb A) | T2 (eb B)
-------------------------------+------------------------------
alloc_extent_buffer() | btrfs_release_extent_buffer_pages()
|- p = find_or_create_page() | |
|- attach_extent_buffer_page() | |
| | |- detach_extent_buffer_page()
| | |- if (!page_range_has_eb())
| | | No new eb in the page range yet
| | | As new eb A hasn't yet been
| | | inserted into radix tree.
| | |- btrfs_detach_subpage()
| | |- detach_page_private();
|- radix_tree_insert() |
Then we have a metadata eb whose page has no private bit.
To avoid such race, we introduce a subpage metadata-specific member,
btrfs_subpage::eb_refs.
In alloc_extent_buffer() we increase eb_refs in the critical section of
private_lock. Then page_range_has_eb() will return true for
detach_extent_buffer_page(), and will not detach page private.
The section is marked by:
- btrfs_page_inc_eb_refs()
- btrfs_page_dec_eb_refs()
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-01-26 08:33:50 +00:00
|
|
|
|
2024-10-09 05:51:07 +00:00
|
|
|
void btrfs_folio_end_lock(const struct btrfs_fs_info *fs_info,
|
|
|
|
struct folio *folio, u64 start, u32 len);
|
|
|
|
void btrfs_folio_set_lock(const struct btrfs_fs_info *fs_info,
|
|
|
|
struct folio *folio, u64 start, u32 len);
|
|
|
|
void btrfs_folio_end_lock_bitmap(const struct btrfs_fs_info *fs_info,
|
|
|
|
struct folio *folio, unsigned long bitmap);
|
2021-01-26 08:33:52 +00:00
|
|
|
/*
|
|
|
|
* Template for subpage related operations.
|
|
|
|
*
|
2023-12-12 02:28:37 +00:00
|
|
|
* btrfs_subpage_*() are for call sites where the folio has subpage attached and
|
|
|
|
* the range is ensured to be inside the folio's single page.
|
2021-01-26 08:33:52 +00:00
|
|
|
*
|
2023-12-12 02:28:37 +00:00
|
|
|
* btrfs_folio_*() are for call sites where the page can either be subpage
|
|
|
|
* specific or regular folios. The function will handle both cases.
|
|
|
|
* But the range still needs to be inside one single page.
|
2021-05-31 08:50:39 +00:00
|
|
|
*
|
2023-12-12 02:28:37 +00:00
|
|
|
* btrfs_folio_clamp_*() are similar to btrfs_folio_*(), except the range doesn't
|
2021-05-31 08:50:39 +00:00
|
|
|
* need to be inside the page. Those functions will truncate the range
|
|
|
|
* automatically.
|
2021-01-26 08:33:52 +00:00
|
|
|
*/
|
|
|
|
#define DECLARE_BTRFS_SUBPAGE_OPS(name) \
|
|
|
|
void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
|
2023-12-12 02:28:37 +00:00
|
|
|
struct folio *folio, u64 start, u32 len); \
|
2021-01-26 08:33:52 +00:00
|
|
|
void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
|
2023-12-12 02:28:37 +00:00
|
|
|
struct folio *folio, u64 start, u32 len); \
|
2021-01-26 08:33:52 +00:00
|
|
|
bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
|
2023-12-12 02:28:37 +00:00
|
|
|
struct folio *folio, u64 start, u32 len); \
|
|
|
|
void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
|
|
|
|
struct folio *folio, u64 start, u32 len); \
|
|
|
|
void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
|
|
|
|
struct folio *folio, u64 start, u32 len); \
|
|
|
|
bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
|
|
|
|
struct folio *folio, u64 start, u32 len); \
|
|
|
|
void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
|
|
|
|
struct folio *folio, u64 start, u32 len); \
|
|
|
|
void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
|
|
|
|
struct folio *folio, u64 start, u32 len); \
|
|
|
|
bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
|
|
|
|
struct folio *folio, u64 start, u32 len);
|
2021-01-26 08:33:52 +00:00
|
|
|
|
|
|
|
DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
|
2021-03-25 07:14:37 +00:00
|
|
|
DECLARE_BTRFS_SUBPAGE_OPS(dirty);
|
2021-03-25 07:14:38 +00:00
|
|
|
DECLARE_BTRFS_SUBPAGE_OPS(writeback);
|
2021-05-31 08:50:45 +00:00
|
|
|
DECLARE_BTRFS_SUBPAGE_OPS(ordered);
|
2021-09-27 07:21:49 +00:00
|
|
|
DECLARE_BTRFS_SUBPAGE_OPS(checked);
|
2021-03-25 07:14:37 +00:00
|
|
|
|
|
|
|
bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
|
2023-12-12 02:28:37 +00:00
|
|
|
struct folio *folio, u64 start, u32 len);
|
2021-01-26 08:33:52 +00:00
|
|
|
|
btrfs: make __extent_writepage_io() to write specified range only
Function __extent_writepage_io() is designed to find all dirty ranges of
a page, and add the dirty ranges to the bio_ctrl for submission.
It requires all the dirtied ranges to be covered by an ordered extent.
It gets called in two locations, but one call site is not subpage aware:
- __extent_writepage()
It gets called when writepage_delalloc() returned 0, which means
writepage_delalloc() has handled delalloc for all subpage sectors
inside the page.
So this call site is OK.
- extent_write_locked_range()
This call site is utilized by zoned support, and in this case, we may
only run delalloc range for a subset of the page, like this: (64K page
size)
0 16K 32K 48K 64K
|/////| |///////| |
In the above case, if extent_write_locked_range() is only triggered for
range [0, 16K), __extent_writepage_io() would still try to submit
the dirty range of [32K, 48K), then it would not find any ordered
extent for it and triggers various ASSERT()s.
Fix this problem by:
- Introducing @start and @len parameters to specify the range
For the first call site, we just pass the whole page, and the behavior
is not touched, since run_delalloc_range() for the page should have
created all ordered extents for the page.
For the second call site, we avoid touching anything beyond the
range, thus avoiding the dirty range which is not yet covered by any
delalloc range.
- Making btrfs_folio_assert_not_dirty() subpage aware
The only caller is inside __extent_writepage_io(), and since that
caller now accepts a subpage range, we should also check the subpage
range other than the whole page.
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Reviewed-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-16 04:03:41 +00:00
|
|
|
void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info,
|
|
|
|
struct folio *folio, u64 start, u32 len);
|
btrfs: refactor __extent_writepage_io() to do sector-by-sector submission
Unlike the bitmap usage inside raid56, for __extent_writepage_io() we
handle the subpage submission not sector-by-sector, but for each dirty
range we found.
This is not a big deal normally, as the subpage complex code is already
mostly optimized out by the compiler for x86_64.
However for the sake of consistency and for the future of subpage
sector-perfect compression support, this patch does:
- Extract the sector submission code into submit_one_sector()
- Add the needed code to extract the dirty bitmap for subpage case
There is a small pitfall for non-subpage case, as we cleared page
dirty before starting writeback, so we have to manually set
the default dirty_bitmap to 1 for such case.
- Use bitmap_and() to calculate the target sectors we need to submit
This is done for both subpage and non-subpage cases, and will later
be expanded to skip inline/compression ranges.
For x86_64, the dirty bitmap will be fixed to 1, with the length of 1,
so we're still doing the same workload per sector.
For larger page sizes, the overhead will be a little larger, as previous
we only need to do one extent_map lookup per-dirty-range, but now it
will be one extent_map lookup per-sector.
But that is the same frequency as x86_64, so we're just aligning the
behavior to x86_64.
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-08-07 05:01:54 +00:00
|
|
|
void btrfs_get_subpage_dirty_bitmap(struct btrfs_fs_info *fs_info,
|
|
|
|
struct folio *folio,
|
|
|
|
unsigned long *ret_bitmap);
|
2023-05-26 12:30:53 +00:00
|
|
|
void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,
|
2023-12-12 02:28:37 +00:00
|
|
|
struct folio *folio, u64 start, u32 len);
|
btrfs: subpage: fix writeback which does not have ordered extent
[BUG]
When running fsstress with subpage RW support, there are random
BUG_ON()s triggered with the following trace:
kernel BUG at fs/btrfs/file-item.c:667!
Internal error: Oops - BUG: 0 [#1] SMP
CPU: 1 PID: 3486 Comm: kworker/u13:2 5.11.0-rc4-custom+ #43
Hardware name: Radxa ROCK Pi 4B (DT)
Workqueue: btrfs-worker-high btrfs_work_helper [btrfs]
pstate: 60000005 (nZCv daif -PAN -UAO -TCO BTYPE=--)
pc : btrfs_csum_one_bio+0x420/0x4e0 [btrfs]
lr : btrfs_csum_one_bio+0x400/0x4e0 [btrfs]
Call trace:
btrfs_csum_one_bio+0x420/0x4e0 [btrfs]
btrfs_submit_bio_start+0x20/0x30 [btrfs]
run_one_async_start+0x28/0x44 [btrfs]
btrfs_work_helper+0x128/0x1b4 [btrfs]
process_one_work+0x22c/0x430
worker_thread+0x70/0x3a0
kthread+0x13c/0x140
ret_from_fork+0x10/0x30
[CAUSE]
Above BUG_ON() means there is some bio range which doesn't have ordered
extent, which indeed is worth a BUG_ON().
Unlike regular sectorsize == PAGE_SIZE case, in subpage we have extra
subpage dirty bitmap to record which range is dirty and should be
written back.
This means, if we submit bio for a subpage range, we do not only need to
clear page dirty, but also need to clear subpage dirty bits.
In __extent_writepage_io(), we will call btrfs_page_clear_dirty() for
any range we submit a bio.
But there is loophole, if we hit a range which is beyond i_size, we just
call btrfs_writepage_endio_finish_ordered() to finish the ordered io,
then break out, without clearing the subpage dirty.
This means, if we hit above branch, the subpage dirty bits are still
there, if other range of the page get dirtied and we need to writeback
that page again, we will submit bio for the old range, leaving a wild
bio range which doesn't have ordered extent.
[FIX]
Fix it by always calling btrfs_page_clear_dirty() in
__extent_writepage_io().
Also to avoid such problem from happening again, add a new assert,
btrfs_page_assert_not_dirty(), to make sure both page dirty and subpage
dirty bits are cleared before exiting __extent_writepage_io().
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-07-26 06:34:58 +00:00
|
|
|
|
2021-01-26 08:33:47 +00:00
|
|
|
#endif
|