* [PATCH v2 0/5] Speed up f2fs truncate
@ 2024-10-30 10:31 Yi Sun
2024-10-30 10:31 ` [PATCH v2 1/5] f2fs: blocks need to belong to the same segment when using update_sit_entry() Yi Sun
` (4 more replies)
0 siblings, 5 replies; 15+ messages in thread
From: Yi Sun @ 2024-10-30 10:31 UTC (permalink / raw)
To: chao, jaegeuk
Cc: yi.sun, sunyibuaa, linux-f2fs-devel, linux-kernel, niuzhiguo84,
hao_hao.wang, ke.wang
Deleting large files is time-consuming, and a large part
of the time is spent in f2fs_invalidate_blocks()
->down_write(sit_info->sentry_lock) and up_write().
If some blocks are continuous, we can process these blocks
at the same time. This can reduce the number of calls to
the down_write() and the up_write(), thereby improving the
overall speed of doing truncate.
Test steps:
Set the CPU and DDR frequencies to the maximum.
dd if=/dev/random of=./test.txt bs=1M count=100000
sync
rm test.txt
Time Comparison of rm:
original optimization ratio
7.17s 3.27s 54.39%
Yi Sun (5):
f2fs: blocks need to belong to the same segment when using
update_sit_entry()
f2fs: expand f2fs_invalidate_compress_page() to
f2fs_invalidate_compress_pages_range()
f2fs: add parameter @len to f2fs_invalidate_internal_cache()
f2fs: add parameter @len to f2fs_invalidate_blocks()
f2fs: Optimize f2fs_truncate_data_blocks_range()
fs/f2fs/compress.c | 11 +++---
fs/f2fs/data.c | 2 +-
fs/f2fs/f2fs.h | 16 +++++----
fs/f2fs/file.c | 78 ++++++++++++++++++++++++++++++++++++++----
fs/f2fs/gc.c | 2 +-
fs/f2fs/node.c | 4 +--
fs/f2fs/segment.c | 84 +++++++++++++++++++++++++++++++++++++++-------
7 files changed, 161 insertions(+), 36 deletions(-)
--
2.25.1
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v2 1/5] f2fs: blocks need to belong to the same segment when using update_sit_entry()
2024-10-30 10:31 [PATCH v2 0/5] Speed up f2fs truncate Yi Sun
@ 2024-10-30 10:31 ` Yi Sun
2024-10-31 7:19 ` Chao Yu
2024-10-30 10:31 ` [PATCH v2 2/5] f2fs: expand f2fs_invalidate_compress_page() to f2fs_invalidate_compress_pages_range() Yi Sun
` (3 subsequent siblings)
4 siblings, 1 reply; 15+ messages in thread
From: Yi Sun @ 2024-10-30 10:31 UTC (permalink / raw)
To: chao, jaegeuk
Cc: yi.sun, sunyibuaa, linux-f2fs-devel, linux-kernel, niuzhiguo84,
hao_hao.wang, ke.wang
When using update_sit_entry() to release consecutive blocks,
ensure that the consecutive blocks belong to the same segment.
Because after update_sit_entry_for_realese(), @segno is still
in use in update_sit_entry().
Signed-off-by: Yi Sun <yi.sun@unisoc.com>
---
fs/f2fs/segment.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index bb2fd98331cd..a5bd101c63a1 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -2424,6 +2424,10 @@ static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr,
SIT_I(sbi)->max_mtime = ctime;
}
+/*
+ * NOTE: when updating multiple blocks at the same time, please ensure
+ * that the consecutive input blocks belong to the same segment.
+ */
static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_entry *se,
block_t blkaddr, unsigned int offset, int del)
{
@@ -2434,6 +2438,8 @@ static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_ent
int i;
int del_count = -del;
+ f2fs_bug_on(sbi, GET_SEGNO(sbi, blkaddr) != GET_SEGNO(sbi, blkaddr + del_count - 1));
+
for (i = 0; i < del_count; i++) {
exist = f2fs_test_and_clear_bit(offset + i, se->cur_valid_map);
#ifdef CONFIG_F2FS_CHECK_FS
@@ -2476,6 +2482,11 @@ static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_ent
return del;
}
+/*
+ * If releasing blocks, this function supports updating multiple consecutive blocks
+ * at one time, but please note that these consecutive blocks need to belong to the
+ * same segment.
+ */
static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
{
struct seg_entry *se;
--
2.25.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 2/5] f2fs: expand f2fs_invalidate_compress_page() to f2fs_invalidate_compress_pages_range()
2024-10-30 10:31 [PATCH v2 0/5] Speed up f2fs truncate Yi Sun
2024-10-30 10:31 ` [PATCH v2 1/5] f2fs: blocks need to belong to the same segment when using update_sit_entry() Yi Sun
@ 2024-10-30 10:31 ` Yi Sun
2024-10-31 7:26 ` Chao Yu
2024-10-30 10:31 ` [PATCH v2 3/5] f2fs: add parameter @len to f2fs_invalidate_internal_cache() Yi Sun
` (2 subsequent siblings)
4 siblings, 1 reply; 15+ messages in thread
From: Yi Sun @ 2024-10-30 10:31 UTC (permalink / raw)
To: chao, jaegeuk
Cc: yi.sun, sunyibuaa, linux-f2fs-devel, linux-kernel, niuzhiguo84,
hao_hao.wang, ke.wang
New function f2fs_invalidate_compress_pages_range() adds the @len
parameter. So it can process some consecutive blocks at a time.
Signed-off-by: Yi Sun <yi.sun@unisoc.com>
---
fs/f2fs/compress.c | 7 ++++---
fs/f2fs/f2fs.h | 9 +++++----
2 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
index 7f26440e8595..e607a7885b57 100644
--- a/fs/f2fs/compress.c
+++ b/fs/f2fs/compress.c
@@ -1903,11 +1903,12 @@ struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
return sbi->compress_inode->i_mapping;
}
-void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
+void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
+ block_t blkaddr, unsigned int len)
{
- if (!sbi->compress_inode)
+ if (!sbi->compress_inode || len == 0)
return;
- invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
+ invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr + len - 1);
}
void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 3c6f3cce5779..d3fe66a93a56 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4384,7 +4384,8 @@ void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
int __init f2fs_init_compress_cache(void);
void f2fs_destroy_compress_cache(void);
struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
-void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
+void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
+ block_t blkaddr, unsigned int len);
void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
nid_t ino, block_t blkaddr);
bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
@@ -4439,8 +4440,8 @@ static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return
static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
static inline int __init f2fs_init_compress_cache(void) { return 0; }
static inline void f2fs_destroy_compress_cache(void) { }
-static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
- block_t blkaddr) { }
+static inline void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
+ block_t blkaddr, unsigned int len) { }
static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
struct page *page, nid_t ino, block_t blkaddr) { }
static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
@@ -4759,7 +4760,7 @@ static inline void f2fs_invalidate_internal_cache(struct f2fs_sb_info *sbi,
block_t blkaddr)
{
f2fs_truncate_meta_inode_pages(sbi, blkaddr, 1);
- f2fs_invalidate_compress_page(sbi, blkaddr);
+ f2fs_invalidate_compress_pages_range(sbi, blkaddr, 1);
}
#define EFSBADCRC EBADMSG /* Bad CRC detected */
--
2.25.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 3/5] f2fs: add parameter @len to f2fs_invalidate_internal_cache()
2024-10-30 10:31 [PATCH v2 0/5] Speed up f2fs truncate Yi Sun
2024-10-30 10:31 ` [PATCH v2 1/5] f2fs: blocks need to belong to the same segment when using update_sit_entry() Yi Sun
2024-10-30 10:31 ` [PATCH v2 2/5] f2fs: expand f2fs_invalidate_compress_page() to f2fs_invalidate_compress_pages_range() Yi Sun
@ 2024-10-30 10:31 ` Yi Sun
2024-10-31 7:27 ` Chao Yu
2024-10-30 10:31 ` [PATCH v2 4/5] f2fs: add parameter @len to f2fs_invalidate_blocks() Yi Sun
2024-10-30 10:31 ` [PATCH v2 5/5] f2fs: Optimize f2fs_truncate_data_blocks_range() Yi Sun
4 siblings, 1 reply; 15+ messages in thread
From: Yi Sun @ 2024-10-30 10:31 UTC (permalink / raw)
To: chao, jaegeuk
Cc: yi.sun, sunyibuaa, linux-f2fs-devel, linux-kernel, niuzhiguo84,
hao_hao.wang, ke.wang
New function can process some consecutive blocks at a time.
Signed-off-by: Yi Sun <yi.sun@unisoc.com>
---
fs/f2fs/data.c | 2 +-
fs/f2fs/f2fs.h | 6 +++---
fs/f2fs/gc.c | 2 +-
fs/f2fs/segment.c | 6 +++---
4 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 90fa8ab85194..37bc747aac89 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -1420,7 +1420,7 @@ static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
return err;
if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
- f2fs_invalidate_internal_cache(sbi, old_blkaddr);
+ f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1);
f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
return 0;
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index d3fe66a93a56..addd49af57ec 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4757,10 +4757,10 @@ static inline void f2fs_truncate_meta_inode_pages(struct f2fs_sb_info *sbi,
}
static inline void f2fs_invalidate_internal_cache(struct f2fs_sb_info *sbi,
- block_t blkaddr)
+ block_t blkaddr, unsigned int len)
{
- f2fs_truncate_meta_inode_pages(sbi, blkaddr, 1);
- f2fs_invalidate_compress_pages_range(sbi, blkaddr, 1);
+ f2fs_truncate_meta_inode_pages(sbi, blkaddr, len);
+ f2fs_invalidate_compress_pages_range(sbi, blkaddr, len);
}
#define EFSBADCRC EBADMSG /* Bad CRC detected */
diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index e40bdd12e36d..155c1a4a0d74 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -1410,7 +1410,7 @@ static int move_data_block(struct inode *inode, block_t bidx,
page_address(mpage), PAGE_SIZE);
f2fs_put_page(mpage, 1);
- f2fs_invalidate_internal_cache(fio.sbi, fio.old_blkaddr);
+ f2fs_invalidate_internal_cache(fio.sbi, fio.old_blkaddr, 1);
set_page_dirty(fio.encrypted_page);
if (clear_page_dirty_for_io(fio.encrypted_page))
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index a5bd101c63a1..92ddff285a65 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -2567,7 +2567,7 @@ void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
return;
- f2fs_invalidate_internal_cache(sbi, addr);
+ f2fs_invalidate_internal_cache(sbi, addr, 1);
/* add it into sit main buffer */
down_write(&sit_i->sentry_lock);
@@ -3845,7 +3845,7 @@ static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
goto out;
}
if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO)
- f2fs_invalidate_internal_cache(fio->sbi, fio->old_blkaddr);
+ f2fs_invalidate_internal_cache(fio->sbi, fio->old_blkaddr, 1);
/* writeout dirty page into bdev */
f2fs_submit_page_write(fio);
@@ -4037,7 +4037,7 @@ void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
update_sit_entry(sbi, new_blkaddr, 1);
}
if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
- f2fs_invalidate_internal_cache(sbi, old_blkaddr);
+ f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1);
if (!from_gc)
update_segment_mtime(sbi, old_blkaddr, 0);
update_sit_entry(sbi, old_blkaddr, -1);
--
2.25.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 4/5] f2fs: add parameter @len to f2fs_invalidate_blocks()
2024-10-30 10:31 [PATCH v2 0/5] Speed up f2fs truncate Yi Sun
` (2 preceding siblings ...)
2024-10-30 10:31 ` [PATCH v2 3/5] f2fs: add parameter @len to f2fs_invalidate_internal_cache() Yi Sun
@ 2024-10-30 10:31 ` Yi Sun
2024-10-30 17:00 ` [f2fs-dev] " Daeho Jeong
2024-10-30 10:31 ` [PATCH v2 5/5] f2fs: Optimize f2fs_truncate_data_blocks_range() Yi Sun
4 siblings, 1 reply; 15+ messages in thread
From: Yi Sun @ 2024-10-30 10:31 UTC (permalink / raw)
To: chao, jaegeuk
Cc: yi.sun, sunyibuaa, linux-f2fs-devel, linux-kernel, niuzhiguo84,
hao_hao.wang, ke.wang
New function can process some consecutive blocks at a time.
Function f2fs_invalidate_blocks()->down_write() and up_write()
are very time-consuming, so if f2fs_invalidate_blocks() can
process consecutive blocks at one time, it will save a lot of time.
Signed-off-by: Yi Sun <yi.sun@unisoc.com>
---
fs/f2fs/compress.c | 4 +--
fs/f2fs/f2fs.h | 3 +-
fs/f2fs/file.c | 8 +++---
fs/f2fs/node.c | 4 +--
fs/f2fs/segment.c | 69 ++++++++++++++++++++++++++++++++++++++--------
5 files changed, 68 insertions(+), 20 deletions(-)
diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
index e607a7885b57..02ad0ff29cf2 100644
--- a/fs/f2fs/compress.c
+++ b/fs/f2fs/compress.c
@@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
if (blkaddr == COMPRESS_ADDR)
fio.compr_blocks++;
if (__is_valid_data_blkaddr(blkaddr))
- f2fs_invalidate_blocks(sbi, blkaddr);
+ f2fs_invalidate_blocks(sbi, blkaddr, 1);
f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
goto unlock_continue;
}
@@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
if (i > cc->valid_nr_cpages) {
if (__is_valid_data_blkaddr(blkaddr)) {
- f2fs_invalidate_blocks(sbi, blkaddr);
+ f2fs_invalidate_blocks(sbi, blkaddr, 1);
f2fs_update_data_blkaddr(&dn, NEW_ADDR);
}
goto unlock_continue;
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index addd49af57ec..4bb459157adf 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
-void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
+void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
+ unsigned int len);
bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 75a8b22da664..13594bb502d1 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
valid_blocks++;
}
- f2fs_invalidate_blocks(sbi, blkaddr);
+ f2fs_invalidate_blocks(sbi, blkaddr, 1);
if (!released || blkaddr != COMPRESS_ADDR)
nr_free++;
@@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
unsigned int i;
for (i = 0; i < ei.len; i++)
- f2fs_invalidate_blocks(sbi, ei.blk + i);
+ f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
dec_valid_block_count(sbi, inode, ei.len);
f2fs_update_time(sbi, REQ_TIME);
@@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
if (ret) {
dec_valid_block_count(sbi, inode, 1);
- f2fs_invalidate_blocks(sbi, *blkaddr);
+ f2fs_invalidate_blocks(sbi, *blkaddr, 1);
} else {
f2fs_update_data_blkaddr(&dn, *blkaddr);
}
@@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
break;
}
- f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
+ f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
f2fs_set_data_blkaddr(dn, NEW_ADDR);
}
diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
index af36c6d6542b..db15d6a90f67 100644
--- a/fs/f2fs/node.c
+++ b/fs/f2fs/node.c
@@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
}
/* Deallocate node address */
- f2fs_invalidate_blocks(sbi, ni.blk_addr);
+ f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
set_node_addr(sbi, &ni, NULL_ADDR, false);
@@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
if (err)
return err;
- f2fs_invalidate_blocks(sbi, ni.blk_addr);
+ f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
dec_valid_node_count(sbi, inode, false);
set_node_addr(sbi, &ni, NULL_ADDR, false);
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 92ddff285a65..67f2bfdeb6ec 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
if (!__is_valid_data_blkaddr(new_addr)) {
if (new_addr == NULL_ADDR)
dec_valid_block_count(sbi, inode, 1);
- f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
+ f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
f2fs_update_data_blkaddr(&dn, new_addr);
} else {
f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
@@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
get_sec_entry(sbi, segno)->valid_blocks += del;
}
-void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
+static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
+ block_t addr, block_t end)
{
unsigned int segno = GET_SEGNO(sbi, addr);
struct sit_info *sit_i = SIT_I(sbi);
+ unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
+ unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
+ block_t addr_start = addr;
- f2fs_bug_on(sbi, addr == NULL_ADDR);
- if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
- return;
-
- f2fs_invalidate_internal_cache(sbi, addr, 1);
+ f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
/* add it into sit main buffer */
down_write(&sit_i->sentry_lock);
- update_segment_mtime(sbi, addr, 0);
- update_sit_entry(sbi, addr, -1);
+ if (seg_num == 1)
+ len = end - addr + 1;
+ else
+ len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
- /* add it into dirty seglist */
- locate_dirty_segment(sbi, segno);
+ do {
+ update_segment_mtime(sbi, addr_start, 0);
+ update_sit_entry(sbi, addr_start, -len);
+
+ /* add it into dirty seglist */
+ locate_dirty_segment(sbi, segno);
+
+ /* update @addr_start and @len and @segno */
+ addr_start = START_BLOCK(sbi, ++segno);
+ if (++i == seg_num)
+ len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
+ else
+ len = max_blocks;
+ } while (i <= seg_num);
up_write(&sit_i->sentry_lock);
}
+void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
+ block_t addr, unsigned int len)
+{
+ unsigned int i;
+ /* Temporary record location */
+ block_t addr_start = addr, addr_end;
+
+ if (len == 0)
+ return;
+
+ for (i = 0; i < len; i++) {
+ addr_end = addr + i;
+
+ f2fs_bug_on(sbi, addr_end == NULL_ADDR);
+
+ if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
+ if (addr_start == addr_end) {
+ addr_end = addr_start = addr_end + 1;
+ continue;
+ }
+
+ __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
+ addr_end = addr_start = addr_end + 1;
+ }
+ }
+
+ if (addr_end >= (addr + len))
+ return;
+
+ __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
+
+}
+
bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
{
struct sit_info *sit_i = SIT_I(sbi);
--
2.25.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 5/5] f2fs: Optimize f2fs_truncate_data_blocks_range()
2024-10-30 10:31 [PATCH v2 0/5] Speed up f2fs truncate Yi Sun
` (3 preceding siblings ...)
2024-10-30 10:31 ` [PATCH v2 4/5] f2fs: add parameter @len to f2fs_invalidate_blocks() Yi Sun
@ 2024-10-30 10:31 ` Yi Sun
4 siblings, 0 replies; 15+ messages in thread
From: Yi Sun @ 2024-10-30 10:31 UTC (permalink / raw)
To: chao, jaegeuk
Cc: yi.sun, sunyibuaa, linux-f2fs-devel, linux-kernel, niuzhiguo84,
hao_hao.wang, ke.wang
Function f2fs_invalidate_blocks() can process continuous
blocks at a time, so f2fs_truncate_data_blocks_range() is
optimized to use the new functionality of
f2fs_invalidate_blocks().
Signed-off-by: Yi Sun <yi.sun@unisoc.com>
---
fs/f2fs/file.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 68 insertions(+), 4 deletions(-)
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 13594bb502d1..3dbabe6be8e6 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -612,6 +612,15 @@ static int f2fs_file_open(struct inode *inode, struct file *filp)
return finish_preallocate_blocks(inode);
}
+static bool check_curr_block_is_consecutive(struct f2fs_sb_info *sbi,
+ block_t curr, block_t end)
+{
+ if (curr - end == 1 || curr == end)
+ return true;
+ else
+ return false;
+}
+
void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
{
struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
@@ -621,8 +630,27 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
int cluster_index = 0, valid_blocks = 0;
int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
+ /*
+ * Temporary record location.
+ * When the current block and @blkaddr_end can be processed
+ * together, update the value of @blkaddr_end.
+ * When it is detected that current block is not continues with
+ * @blkaddr_end, it is necessary to process continues blocks
+ * range [blkaddr_start, blkaddr_end].
+ */
+ block_t blkaddr_start, blkaddr_end;
+ /*.
+ * To avoid processing various invalid data blocks.
+ * Because @blkaddr_start and @blkaddr_end may be assigned
+ * NULL_ADDR or invalid data blocks, @last_valid is used to
+ * record this situation.
+ */
+ bool last_valid = false;
+ /* Process the last block separately? */
+ bool last_one = true;
addr = get_dnode_addr(dn->inode, dn->node_page) + ofs;
+ blkaddr_start = blkaddr_end = le32_to_cpu(*addr);
/* Assumption: truncation starts with cluster */
for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
@@ -638,24 +666,60 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
}
if (blkaddr == NULL_ADDR)
- continue;
+ goto next;
f2fs_set_data_blkaddr(dn, NULL_ADDR);
if (__is_valid_data_blkaddr(blkaddr)) {
if (time_to_inject(sbi, FAULT_BLKADDR_CONSISTENCE))
- continue;
+ goto next;
if (!f2fs_is_valid_blkaddr_raw(sbi, blkaddr,
DATA_GENERIC_ENHANCE))
- continue;
+ goto next;
if (compressed_cluster)
valid_blocks++;
}
- f2fs_invalidate_blocks(sbi, blkaddr, 1);
+
+ if (check_curr_block_is_consecutive(sbi, blkaddr, blkaddr_end)) {
+ /*
+ * The current block @blkaddr is continuous with
+ * @blkaddr_end, so @blkaddr_end is updated.
+ * And the f2fs_invalidate_blocks() is skipped
+ * until @blkaddr that cannot be processed
+ * together is encountered.
+ */
+ blkaddr_end = blkaddr;
+ if (count == 1)
+ last_one = false;
+ else
+ goto skip_invalid;
+ }
+
+ f2fs_invalidate_blocks(sbi, blkaddr_start,
+ blkaddr_end - blkaddr_start + 1);
+ blkaddr_start = blkaddr_end = blkaddr;
+
+ if (count == 1 && last_one)
+ f2fs_invalidate_blocks(sbi, blkaddr, 1);
+
+skip_invalid:
+ last_valid = true;
if (!released || blkaddr != COMPRESS_ADDR)
nr_free++;
+
+ continue;
+
+next:
+ /* If consecutive blocks have been recorded, we need to process them. */
+ if (last_valid == true)
+ f2fs_invalidate_blocks(sbi, blkaddr_start,
+ blkaddr_end - blkaddr_start + 1);
+
+ blkaddr_start = blkaddr_end = le32_to_cpu(*(addr + 1));
+ last_valid = false;
+
}
if (compressed_cluster)
--
2.25.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [f2fs-dev] [PATCH v2 4/5] f2fs: add parameter @len to f2fs_invalidate_blocks()
2024-10-30 10:31 ` [PATCH v2 4/5] f2fs: add parameter @len to f2fs_invalidate_blocks() Yi Sun
@ 2024-10-30 17:00 ` Daeho Jeong
2024-10-31 3:00 ` yi sun
2024-11-01 3:39 ` yi sun
0 siblings, 2 replies; 15+ messages in thread
From: Daeho Jeong @ 2024-10-30 17:00 UTC (permalink / raw)
To: Yi Sun
Cc: chao, jaegeuk, ke.wang, linux-kernel, linux-f2fs-devel, sunyibuaa,
hao_hao.wang
On Wed, Oct 30, 2024 at 3:35 AM Yi Sun <yi.sun@unisoc.com> wrote:
>
> New function can process some consecutive blocks at a time.
>
> Function f2fs_invalidate_blocks()->down_write() and up_write()
> are very time-consuming, so if f2fs_invalidate_blocks() can
> process consecutive blocks at one time, it will save a lot of time.
>
> Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> ---
> fs/f2fs/compress.c | 4 +--
> fs/f2fs/f2fs.h | 3 +-
> fs/f2fs/file.c | 8 +++---
> fs/f2fs/node.c | 4 +--
> fs/f2fs/segment.c | 69 ++++++++++++++++++++++++++++++++++++++--------
> 5 files changed, 68 insertions(+), 20 deletions(-)
>
> diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> index e607a7885b57..02ad0ff29cf2 100644
> --- a/fs/f2fs/compress.c
> +++ b/fs/f2fs/compress.c
> @@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> if (blkaddr == COMPRESS_ADDR)
> fio.compr_blocks++;
> if (__is_valid_data_blkaddr(blkaddr))
> - f2fs_invalidate_blocks(sbi, blkaddr);
> + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
> goto unlock_continue;
> }
> @@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
>
> if (i > cc->valid_nr_cpages) {
> if (__is_valid_data_blkaddr(blkaddr)) {
> - f2fs_invalidate_blocks(sbi, blkaddr);
> + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> f2fs_update_data_blkaddr(&dn, NEW_ADDR);
> }
> goto unlock_continue;
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index addd49af57ec..4bb459157adf 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
> int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
> int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
> void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
> -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
> +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
> + unsigned int len);
> bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
> int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
> void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index 75a8b22da664..13594bb502d1 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
> valid_blocks++;
> }
>
> - f2fs_invalidate_blocks(sbi, blkaddr);
> + f2fs_invalidate_blocks(sbi, blkaddr, 1);
>
> if (!released || blkaddr != COMPRESS_ADDR)
> nr_free++;
> @@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
> unsigned int i;
>
> for (i = 0; i < ei.len; i++)
> - f2fs_invalidate_blocks(sbi, ei.blk + i);
> + f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
>
> dec_valid_block_count(sbi, inode, ei.len);
> f2fs_update_time(sbi, REQ_TIME);
> @@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
> ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
> if (ret) {
> dec_valid_block_count(sbi, inode, 1);
> - f2fs_invalidate_blocks(sbi, *blkaddr);
> + f2fs_invalidate_blocks(sbi, *blkaddr, 1);
> } else {
> f2fs_update_data_blkaddr(&dn, *blkaddr);
> }
> @@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
> break;
> }
>
> - f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
> + f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
> f2fs_set_data_blkaddr(dn, NEW_ADDR);
> }
>
> diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> index af36c6d6542b..db15d6a90f67 100644
> --- a/fs/f2fs/node.c
> +++ b/fs/f2fs/node.c
> @@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
> }
>
> /* Deallocate node address */
> - f2fs_invalidate_blocks(sbi, ni.blk_addr);
> + f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
> set_node_addr(sbi, &ni, NULL_ADDR, false);
>
> @@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
> if (err)
> return err;
>
> - f2fs_invalidate_blocks(sbi, ni.blk_addr);
> + f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> dec_valid_node_count(sbi, inode, false);
> set_node_addr(sbi, &ni, NULL_ADDR, false);
>
> diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> index 92ddff285a65..67f2bfdeb6ec 100644
> --- a/fs/f2fs/segment.c
> +++ b/fs/f2fs/segment.c
> @@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
> if (!__is_valid_data_blkaddr(new_addr)) {
> if (new_addr == NULL_ADDR)
> dec_valid_block_count(sbi, inode, 1);
> - f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
> + f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
> f2fs_update_data_blkaddr(&dn, new_addr);
> } else {
> f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
> @@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
> get_sec_entry(sbi, segno)->valid_blocks += del;
> }
>
> -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
> +static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> + block_t addr, block_t end)
> {
> unsigned int segno = GET_SEGNO(sbi, addr);
> struct sit_info *sit_i = SIT_I(sbi);
> + unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
> + unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
> + block_t addr_start = addr;
>
> - f2fs_bug_on(sbi, addr == NULL_ADDR);
> - if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
> - return;
> -
> - f2fs_invalidate_internal_cache(sbi, addr, 1);
> + f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
>
> /* add it into sit main buffer */
> down_write(&sit_i->sentry_lock);
>
> - update_segment_mtime(sbi, addr, 0);
> - update_sit_entry(sbi, addr, -1);
> + if (seg_num == 1)
> + len = end - addr + 1;
> + else
> + len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
>
> - /* add it into dirty seglist */
> - locate_dirty_segment(sbi, segno);
> + do {
> + update_segment_mtime(sbi, addr_start, 0);
> + update_sit_entry(sbi, addr_start, -len);
> +
> + /* add it into dirty seglist */
> + locate_dirty_segment(sbi, segno);
> +
> + /* update @addr_start and @len and @segno */
> + addr_start = START_BLOCK(sbi, ++segno);
> + if (++i == seg_num)
> + len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
> + else
> + len = max_blocks;
> + } while (i <= seg_num);
>
> up_write(&sit_i->sentry_lock);
> }
>
> +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> + block_t addr, unsigned int len)
> +{
> + unsigned int i;
> + /* Temporary record location */
> + block_t addr_start = addr, addr_end;
> +
> + if (len == 0)
> + return;
> +
> + for (i = 0; i < len; i++) {
> + addr_end = addr + i;
> +
> + f2fs_bug_on(sbi, addr_end == NULL_ADDR);
Looks like this line should be out of this loop, right?
> +
> + if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
ditto?
Could you help with enhancing the readability here? a little bit
confused with using addr_start, addr_end and NEW_ADDR, COMPRESS_ADDR,
here.
> + if (addr_start == addr_end) {
> + addr_end = addr_start = addr_end + 1;
> + continue;
> + }
> +
> + __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
> + addr_end = addr_start = addr_end + 1;
> + }
> + }
> +
> + if (addr_end >= (addr + len))
> + return;
> +
> + __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
> +
> +}
> +
> bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
> {
> struct sit_info *sit_i = SIT_I(sbi);
> --
> 2.25.1
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [f2fs-dev] [PATCH v2 4/5] f2fs: add parameter @len to f2fs_invalidate_blocks()
2024-10-30 17:00 ` [f2fs-dev] " Daeho Jeong
@ 2024-10-31 3:00 ` yi sun
2024-10-31 9:10 ` Zhiguo Niu
2024-11-01 3:39 ` yi sun
1 sibling, 1 reply; 15+ messages in thread
From: yi sun @ 2024-10-31 3:00 UTC (permalink / raw)
To: Daeho Jeong
Cc: Yi Sun, chao, jaegeuk, ke.wang, linux-kernel, linux-f2fs-devel,
hao_hao.wang, Zhiguo Niu
On Thu, Oct 31, 2024 at 1:00 AM Daeho Jeong <daeho43@gmail.com> wrote:
>
> On Wed, Oct 30, 2024 at 3:35 AM Yi Sun <yi.sun@unisoc.com> wrote:
> >
> > New function can process some consecutive blocks at a time.
> >
> > Function f2fs_invalidate_blocks()->down_write() and up_write()
> > are very time-consuming, so if f2fs_invalidate_blocks() can
> > process consecutive blocks at one time, it will save a lot of time.
> >
> > Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> > ---
> > fs/f2fs/compress.c | 4 +--
> > fs/f2fs/f2fs.h | 3 +-
> > fs/f2fs/file.c | 8 +++---
> > fs/f2fs/node.c | 4 +--
> > fs/f2fs/segment.c | 69 ++++++++++++++++++++++++++++++++++++++--------
> > 5 files changed, 68 insertions(+), 20 deletions(-)
> >
> > diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> > index e607a7885b57..02ad0ff29cf2 100644
> > --- a/fs/f2fs/compress.c
> > +++ b/fs/f2fs/compress.c
> > @@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> > if (blkaddr == COMPRESS_ADDR)
> > fio.compr_blocks++;
> > if (__is_valid_data_blkaddr(blkaddr))
> > - f2fs_invalidate_blocks(sbi, blkaddr);
> > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
> > goto unlock_continue;
> > }
> > @@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> >
> > if (i > cc->valid_nr_cpages) {
> > if (__is_valid_data_blkaddr(blkaddr)) {
> > - f2fs_invalidate_blocks(sbi, blkaddr);
> > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > f2fs_update_data_blkaddr(&dn, NEW_ADDR);
> > }
> > goto unlock_continue;
> > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > index addd49af57ec..4bb459157adf 100644
> > --- a/fs/f2fs/f2fs.h
> > +++ b/fs/f2fs/f2fs.h
> > @@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
> > int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
> > int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
> > void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
> > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
> > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
> > + unsigned int len);
> > bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
> > int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
> > void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index 75a8b22da664..13594bb502d1 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
> > valid_blocks++;
> > }
> >
> > - f2fs_invalidate_blocks(sbi, blkaddr);
> > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> >
> > if (!released || blkaddr != COMPRESS_ADDR)
> > nr_free++;
> > @@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
> > unsigned int i;
> >
> > for (i = 0; i < ei.len; i++)
> > - f2fs_invalidate_blocks(sbi, ei.blk + i);
> > + f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
> >
> > dec_valid_block_count(sbi, inode, ei.len);
> > f2fs_update_time(sbi, REQ_TIME);
> > @@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
> > ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
> > if (ret) {
> > dec_valid_block_count(sbi, inode, 1);
> > - f2fs_invalidate_blocks(sbi, *blkaddr);
> > + f2fs_invalidate_blocks(sbi, *blkaddr, 1);
> > } else {
> > f2fs_update_data_blkaddr(&dn, *blkaddr);
> > }
> > @@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
> > break;
> > }
> >
> > - f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
> > + f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
> > f2fs_set_data_blkaddr(dn, NEW_ADDR);
> > }
> >
> > diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> > index af36c6d6542b..db15d6a90f67 100644
> > --- a/fs/f2fs/node.c
> > +++ b/fs/f2fs/node.c
> > @@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
> > }
> >
> > /* Deallocate node address */
> > - f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > + f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
> > set_node_addr(sbi, &ni, NULL_ADDR, false);
> >
> > @@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
> > if (err)
> > return err;
> >
> > - f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > + f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > dec_valid_node_count(sbi, inode, false);
> > set_node_addr(sbi, &ni, NULL_ADDR, false);
> >
> > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> > index 92ddff285a65..67f2bfdeb6ec 100644
> > --- a/fs/f2fs/segment.c
> > +++ b/fs/f2fs/segment.c
> > @@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
> > if (!__is_valid_data_blkaddr(new_addr)) {
> > if (new_addr == NULL_ADDR)
> > dec_valid_block_count(sbi, inode, 1);
> > - f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
> > + f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
> > f2fs_update_data_blkaddr(&dn, new_addr);
> > } else {
> > f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
> > @@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
> > get_sec_entry(sbi, segno)->valid_blocks += del;
> > }
> >
> > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
> > +static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > + block_t addr, block_t end)
> > {
> > unsigned int segno = GET_SEGNO(sbi, addr);
> > struct sit_info *sit_i = SIT_I(sbi);
> > + unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
> > + unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
> > + block_t addr_start = addr;
> >
> > - f2fs_bug_on(sbi, addr == NULL_ADDR);
> > - if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
> > - return;
> > -
> > - f2fs_invalidate_internal_cache(sbi, addr, 1);
> > + f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
> >
> > /* add it into sit main buffer */
> > down_write(&sit_i->sentry_lock);
> >
> > - update_segment_mtime(sbi, addr, 0);
> > - update_sit_entry(sbi, addr, -1);
> > + if (seg_num == 1)
> > + len = end - addr + 1;
> > + else
> > + len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
> >
> > - /* add it into dirty seglist */
> > - locate_dirty_segment(sbi, segno);
> > + do {
> > + update_segment_mtime(sbi, addr_start, 0);
> > + update_sit_entry(sbi, addr_start, -len);
> > +
> > + /* add it into dirty seglist */
> > + locate_dirty_segment(sbi, segno);
> > +
> > + /* update @addr_start and @len and @segno */
> > + addr_start = START_BLOCK(sbi, ++segno);
> > + if (++i == seg_num)
> > + len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
> > + else
> > + len = max_blocks;
> > + } while (i <= seg_num);
> >
> > up_write(&sit_i->sentry_lock);
> > }
> >
> > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > + block_t addr, unsigned int len)
> > +{
> > + unsigned int i;
> > + /* Temporary record location */
> > + block_t addr_start = addr, addr_end;
> > +
> > + if (len == 0)
> > + return;
> > +
> > + for (i = 0; i < len; i++) {
> > + addr_end = addr + i;
> > +
> > + f2fs_bug_on(sbi, addr_end == NULL_ADDR);
>
> Looks like this line should be out of this loop, right?
>
> > +
> > + if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
>
> ditto?
The original f2fs_invalidate_blocks() can only process one block at a time,
and it will check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR for
each input block.
The new f2fs_invalidate_blocks() can process multiple blocks at a time.
In order to keep it consistent with the original f2fs_invalidate_blocks(),
the new function will also check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR
for each block, so these two lines are placed in the loop.
> Could you help with enhancing the readability here? a little bit
> confused with using addr_start, addr_end and NEW_ADDR, COMPRESS_ADDR,
> here.
>
The addr_start and addr_end will continue to move to filter out the range of
all valid data blkaddr and pass it to __f2fs_invalidate_blocks() for
further processing.
Assume that the input parameters of f2fs_invalidate_blocks() are addr=N, len=4,
and there is a blkaddr = NEW_ADDR.
Like this:
| N | N+1 |NEW_ADDR|N+3|
loop1 addr_start
addr_end
loop2 addr_end
loop3 addr_end
At this point we need to
pass range [addr_start, addr_end - 1] into __f2fs_invalidate_blocks()
for processing.
loop4
addr_start
addr_end
> > + if (addr_start == addr_end) {
> > + addr_end = addr_start = addr_end + 1;
> > + continue;
> > + }
> > +
> > + __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
> > + addr_end = addr_start = addr_end + 1;
> > + }
> > + }
> > +
> > + if (addr_end >= (addr + len))
> > + return;
> > +
> > + __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
> > +
> > +}
> > +
> > bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
> > {
> > struct sit_info *sit_i = SIT_I(sbi);
> > --
> > 2.25.1
> >
> >
> >
> > _______________________________________________
> > Linux-f2fs-devel mailing list
> > Linux-f2fs-devel@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 1/5] f2fs: blocks need to belong to the same segment when using update_sit_entry()
2024-10-30 10:31 ` [PATCH v2 1/5] f2fs: blocks need to belong to the same segment when using update_sit_entry() Yi Sun
@ 2024-10-31 7:19 ` Chao Yu
0 siblings, 0 replies; 15+ messages in thread
From: Chao Yu @ 2024-10-31 7:19 UTC (permalink / raw)
To: Yi Sun, jaegeuk
Cc: Chao Yu, sunyibuaa, linux-f2fs-devel, linux-kernel, niuzhiguo84,
hao_hao.wang, ke.wang
On 2024/10/30 18:31, Yi Sun wrote:
> When using update_sit_entry() to release consecutive blocks,
> ensure that the consecutive blocks belong to the same segment.
> Because after update_sit_entry_for_realese(), @segno is still
> in use in update_sit_entry().
I think this patch should be merged into
"f2fs: introduce update_sit_entry_for_release()".
Thanks,
>
> Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> ---
> fs/f2fs/segment.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> index bb2fd98331cd..a5bd101c63a1 100644
> --- a/fs/f2fs/segment.c
> +++ b/fs/f2fs/segment.c
> @@ -2424,6 +2424,10 @@ static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr,
> SIT_I(sbi)->max_mtime = ctime;
> }
>
> +/*
> + * NOTE: when updating multiple blocks at the same time, please ensure
> + * that the consecutive input blocks belong to the same segment.
> + */
> static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_entry *se,
> block_t blkaddr, unsigned int offset, int del)
> {
> @@ -2434,6 +2438,8 @@ static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_ent
> int i;
> int del_count = -del;
>
> + f2fs_bug_on(sbi, GET_SEGNO(sbi, blkaddr) != GET_SEGNO(sbi, blkaddr + del_count - 1));
> +
> for (i = 0; i < del_count; i++) {
> exist = f2fs_test_and_clear_bit(offset + i, se->cur_valid_map);
> #ifdef CONFIG_F2FS_CHECK_FS
> @@ -2476,6 +2482,11 @@ static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_ent
> return del;
> }
>
> +/*
> + * If releasing blocks, this function supports updating multiple consecutive blocks
> + * at one time, but please note that these consecutive blocks need to belong to the
> + * same segment.
> + */
> static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
> {
> struct seg_entry *se;
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 2/5] f2fs: expand f2fs_invalidate_compress_page() to f2fs_invalidate_compress_pages_range()
2024-10-30 10:31 ` [PATCH v2 2/5] f2fs: expand f2fs_invalidate_compress_page() to f2fs_invalidate_compress_pages_range() Yi Sun
@ 2024-10-31 7:26 ` Chao Yu
2024-10-31 9:02 ` yi sun
0 siblings, 1 reply; 15+ messages in thread
From: Chao Yu @ 2024-10-31 7:26 UTC (permalink / raw)
To: Yi Sun, jaegeuk
Cc: Chao Yu, sunyibuaa, linux-f2fs-devel, linux-kernel, niuzhiguo84,
hao_hao.wang, ke.wang
On 2024/10/30 18:31, Yi Sun wrote:
> New function f2fs_invalidate_compress_pages_range() adds the @len
> parameter. So it can process some consecutive blocks at a time.
>
> Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> ---
> fs/f2fs/compress.c | 7 ++++---
> fs/f2fs/f2fs.h | 9 +++++----
> 2 files changed, 9 insertions(+), 7 deletions(-)
>
> diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> index 7f26440e8595..e607a7885b57 100644
> --- a/fs/f2fs/compress.c
> +++ b/fs/f2fs/compress.c
> @@ -1903,11 +1903,12 @@ struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
> return sbi->compress_inode->i_mapping;
> }
>
> -void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
> +void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
> + block_t blkaddr, unsigned int len)
> {
> - if (!sbi->compress_inode)
> + if (!sbi->compress_inode || len == 0)
We can remove len == 0 check condition? Or any caller can pass 0 here?
Thanks,
> return;
> - invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
> + invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr + len - 1);
> }
>
> void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index 3c6f3cce5779..d3fe66a93a56 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -4384,7 +4384,8 @@ void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
> int __init f2fs_init_compress_cache(void);
> void f2fs_destroy_compress_cache(void);
> struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
> -void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
> +void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
> + block_t blkaddr, unsigned int len);
> void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
> nid_t ino, block_t blkaddr);
> bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
> @@ -4439,8 +4440,8 @@ static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return
> static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
> static inline int __init f2fs_init_compress_cache(void) { return 0; }
> static inline void f2fs_destroy_compress_cache(void) { }
> -static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
> - block_t blkaddr) { }
> +static inline void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
> + block_t blkaddr, unsigned int len) { }
> static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
> struct page *page, nid_t ino, block_t blkaddr) { }
> static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
> @@ -4759,7 +4760,7 @@ static inline void f2fs_invalidate_internal_cache(struct f2fs_sb_info *sbi,
> block_t blkaddr)
> {
> f2fs_truncate_meta_inode_pages(sbi, blkaddr, 1);
> - f2fs_invalidate_compress_page(sbi, blkaddr);
> + f2fs_invalidate_compress_pages_range(sbi, blkaddr, 1);
> }
>
> #define EFSBADCRC EBADMSG /* Bad CRC detected */
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 3/5] f2fs: add parameter @len to f2fs_invalidate_internal_cache()
2024-10-30 10:31 ` [PATCH v2 3/5] f2fs: add parameter @len to f2fs_invalidate_internal_cache() Yi Sun
@ 2024-10-31 7:27 ` Chao Yu
0 siblings, 0 replies; 15+ messages in thread
From: Chao Yu @ 2024-10-31 7:27 UTC (permalink / raw)
To: Yi Sun, jaegeuk
Cc: Chao Yu, sunyibuaa, linux-f2fs-devel, linux-kernel, niuzhiguo84,
hao_hao.wang, ke.wang
On 2024/10/30 18:31, Yi Sun wrote:
> New function can process some consecutive blocks at a time.
>
> Signed-off-by: Yi Sun <yi.sun@unisoc.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Thanks,
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 2/5] f2fs: expand f2fs_invalidate_compress_page() to f2fs_invalidate_compress_pages_range()
2024-10-31 7:26 ` Chao Yu
@ 2024-10-31 9:02 ` yi sun
0 siblings, 0 replies; 15+ messages in thread
From: yi sun @ 2024-10-31 9:02 UTC (permalink / raw)
To: Chao Yu
Cc: Yi Sun, jaegeuk, linux-f2fs-devel, linux-kernel, niuzhiguo84,
hao_hao.wang, ke.wang
On Thu, Oct 31, 2024 at 3:26 PM Chao Yu <chao@kernel.org> wrote:
>
> On 2024/10/30 18:31, Yi Sun wrote:
> > New function f2fs_invalidate_compress_pages_range() adds the @len
> > parameter. So it can process some consecutive blocks at a time.
> >
> > Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> > ---
> > fs/f2fs/compress.c | 7 ++++---
> > fs/f2fs/f2fs.h | 9 +++++----
> > 2 files changed, 9 insertions(+), 7 deletions(-)
> >
> > diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> > index 7f26440e8595..e607a7885b57 100644
> > --- a/fs/f2fs/compress.c
> > +++ b/fs/f2fs/compress.c
> > @@ -1903,11 +1903,12 @@ struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
> > return sbi->compress_inode->i_mapping;
> > }
> >
> > -void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
> > +void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
> > + block_t blkaddr, unsigned int len)
> > {
> > - if (!sbi->compress_inode)
> > + if (!sbi->compress_inode || len == 0)
>
> We can remove len == 0 check condition? Or any caller can pass 0 here?
>
> Thanks,
>
Yes, len==0 can be removed.
> > return;
> > - invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
> > + invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr + len - 1);
> > }
> >
> > void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
> > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > index 3c6f3cce5779..d3fe66a93a56 100644
> > --- a/fs/f2fs/f2fs.h
> > +++ b/fs/f2fs/f2fs.h
> > @@ -4384,7 +4384,8 @@ void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
> > int __init f2fs_init_compress_cache(void);
> > void f2fs_destroy_compress_cache(void);
> > struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
> > -void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
> > +void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
> > + block_t blkaddr, unsigned int len);
> > void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
> > nid_t ino, block_t blkaddr);
> > bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
> > @@ -4439,8 +4440,8 @@ static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return
> > static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
> > static inline int __init f2fs_init_compress_cache(void) { return 0; }
> > static inline void f2fs_destroy_compress_cache(void) { }
> > -static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
> > - block_t blkaddr) { }
> > +static inline void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
> > + block_t blkaddr, unsigned int len) { }
> > static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
> > struct page *page, nid_t ino, block_t blkaddr) { }
> > static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
> > @@ -4759,7 +4760,7 @@ static inline void f2fs_invalidate_internal_cache(struct f2fs_sb_info *sbi,
> > block_t blkaddr)
> > {
> > f2fs_truncate_meta_inode_pages(sbi, blkaddr, 1);
> > - f2fs_invalidate_compress_page(sbi, blkaddr);
> > + f2fs_invalidate_compress_pages_range(sbi, blkaddr, 1);
> > }
> >
> > #define EFSBADCRC EBADMSG /* Bad CRC detected */
>
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [f2fs-dev] [PATCH v2 4/5] f2fs: add parameter @len to f2fs_invalidate_blocks()
2024-10-31 3:00 ` yi sun
@ 2024-10-31 9:10 ` Zhiguo Niu
2024-10-31 10:51 ` yi sun
0 siblings, 1 reply; 15+ messages in thread
From: Zhiguo Niu @ 2024-10-31 9:10 UTC (permalink / raw)
To: yi sun
Cc: Daeho Jeong, Yi Sun, chao, jaegeuk, ke.wang, linux-kernel,
linux-f2fs-devel, hao_hao.wang
yi sun <sunyibuaa@gmail.com> 于2024年10月31日周四 11:00写道:
>
> On Thu, Oct 31, 2024 at 1:00 AM Daeho Jeong <daeho43@gmail.com> wrote:
> >
> > On Wed, Oct 30, 2024 at 3:35 AM Yi Sun <yi.sun@unisoc.com> wrote:
> > >
> > > New function can process some consecutive blocks at a time.
> > >
> > > Function f2fs_invalidate_blocks()->down_write() and up_write()
> > > are very time-consuming, so if f2fs_invalidate_blocks() can
> > > process consecutive blocks at one time, it will save a lot of time.
> > >
> > > Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> > > ---
> > > fs/f2fs/compress.c | 4 +--
> > > fs/f2fs/f2fs.h | 3 +-
> > > fs/f2fs/file.c | 8 +++---
> > > fs/f2fs/node.c | 4 +--
> > > fs/f2fs/segment.c | 69 ++++++++++++++++++++++++++++++++++++++--------
> > > 5 files changed, 68 insertions(+), 20 deletions(-)
> > >
> > > diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> > > index e607a7885b57..02ad0ff29cf2 100644
> > > --- a/fs/f2fs/compress.c
> > > +++ b/fs/f2fs/compress.c
> > > @@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> > > if (blkaddr == COMPRESS_ADDR)
> > > fio.compr_blocks++;
> > > if (__is_valid_data_blkaddr(blkaddr))
> > > - f2fs_invalidate_blocks(sbi, blkaddr);
> > > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > > f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
> > > goto unlock_continue;
> > > }
> > > @@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> > >
> > > if (i > cc->valid_nr_cpages) {
> > > if (__is_valid_data_blkaddr(blkaddr)) {
> > > - f2fs_invalidate_blocks(sbi, blkaddr);
> > > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > > f2fs_update_data_blkaddr(&dn, NEW_ADDR);
> > > }
> > > goto unlock_continue;
> > > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > > index addd49af57ec..4bb459157adf 100644
> > > --- a/fs/f2fs/f2fs.h
> > > +++ b/fs/f2fs/f2fs.h
> > > @@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
> > > int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
> > > int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
> > > void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
> > > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
> > > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
> > > + unsigned int len);
> > > bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
> > > int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
> > > void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
> > > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > > index 75a8b22da664..13594bb502d1 100644
> > > --- a/fs/f2fs/file.c
> > > +++ b/fs/f2fs/file.c
> > > @@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
> > > valid_blocks++;
> > > }
> > >
> > > - f2fs_invalidate_blocks(sbi, blkaddr);
> > > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > >
> > > if (!released || blkaddr != COMPRESS_ADDR)
> > > nr_free++;
> > > @@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
> > > unsigned int i;
> > >
> > > for (i = 0; i < ei.len; i++)
> > > - f2fs_invalidate_blocks(sbi, ei.blk + i);
> > > + f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
> > >
> > > dec_valid_block_count(sbi, inode, ei.len);
> > > f2fs_update_time(sbi, REQ_TIME);
> > > @@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
> > > ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
> > > if (ret) {
> > > dec_valid_block_count(sbi, inode, 1);
> > > - f2fs_invalidate_blocks(sbi, *blkaddr);
> > > + f2fs_invalidate_blocks(sbi, *blkaddr, 1);
> > > } else {
> > > f2fs_update_data_blkaddr(&dn, *blkaddr);
> > > }
> > > @@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
> > > break;
> > > }
> > >
> > > - f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
> > > + f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
> > > f2fs_set_data_blkaddr(dn, NEW_ADDR);
> > > }
> > >
> > > diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> > > index af36c6d6542b..db15d6a90f67 100644
> > > --- a/fs/f2fs/node.c
> > > +++ b/fs/f2fs/node.c
> > > @@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
> > > }
> > >
> > > /* Deallocate node address */
> > > - f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > > + f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > > dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
> > > set_node_addr(sbi, &ni, NULL_ADDR, false);
> > >
> > > @@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
> > > if (err)
> > > return err;
> > >
> > > - f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > > + f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > > dec_valid_node_count(sbi, inode, false);
> > > set_node_addr(sbi, &ni, NULL_ADDR, false);
> > >
> > > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> > > index 92ddff285a65..67f2bfdeb6ec 100644
> > > --- a/fs/f2fs/segment.c
> > > +++ b/fs/f2fs/segment.c
> > > @@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
> > > if (!__is_valid_data_blkaddr(new_addr)) {
> > > if (new_addr == NULL_ADDR)
> > > dec_valid_block_count(sbi, inode, 1);
> > > - f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
> > > + f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
> > > f2fs_update_data_blkaddr(&dn, new_addr);
> > > } else {
> > > f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
> > > @@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
> > > get_sec_entry(sbi, segno)->valid_blocks += del;
> > > }
> > >
> > > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
> > > +static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > > + block_t addr, block_t end)
> > > {
> > > unsigned int segno = GET_SEGNO(sbi, addr);
> > > struct sit_info *sit_i = SIT_I(sbi);
> > > + unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
> > > + unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
> > > + block_t addr_start = addr;
> > >
> > > - f2fs_bug_on(sbi, addr == NULL_ADDR);
> > > - if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
> > > - return;
> > > -
> > > - f2fs_invalidate_internal_cache(sbi, addr, 1);
> > > + f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
> > >
> > > /* add it into sit main buffer */
> > > down_write(&sit_i->sentry_lock);
> > >
> > > - update_segment_mtime(sbi, addr, 0);
> > > - update_sit_entry(sbi, addr, -1);
> > > + if (seg_num == 1)
> > > + len = end - addr + 1;
> > > + else
> > > + len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
> > >
> > > - /* add it into dirty seglist */
> > > - locate_dirty_segment(sbi, segno);
> > > + do {
> > > + update_segment_mtime(sbi, addr_start, 0);
> > > + update_sit_entry(sbi, addr_start, -len);
> > > +
> > > + /* add it into dirty seglist */
> > > + locate_dirty_segment(sbi, segno);
> > > +
> > > + /* update @addr_start and @len and @segno */
> > > + addr_start = START_BLOCK(sbi, ++segno);
> > > + if (++i == seg_num)
> > > + len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
> > > + else
> > > + len = max_blocks;
> > > + } while (i <= seg_num);
> > >
> > > up_write(&sit_i->sentry_lock);
> > > }
> > >
> > > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > > + block_t addr, unsigned int len)
> > > +{
> > > + unsigned int i;
> > > + /* Temporary record location */
> > > + block_t addr_start = addr, addr_end;
> > > +
> > > + if (len == 0)
> > > + return;
> > > +
> > > + for (i = 0; i < len; i++) {
> > > + addr_end = addr + i;
> > > +
> > > + f2fs_bug_on(sbi, addr_end == NULL_ADDR);
> >
> > Looks like this line should be out of this loop, right?
> >
> > > +
> > > + if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
> >
> > ditto?
>
> The original f2fs_invalidate_blocks() can only process one block at a time,
> and it will check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR for
> each input block.
>
> The new f2fs_invalidate_blocks() can process multiple blocks at a time.
> In order to keep it consistent with the original f2fs_invalidate_blocks(),
> the new function will also check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR
> for each block, so these two lines are placed in the loop.
>
> > Could you help with enhancing the readability here? a little bit
> > confused with using addr_start, addr_end and NEW_ADDR, COMPRESS_ADDR,
> > here.
> >
>
> The addr_start and addr_end will continue to move to filter out the range of
> all valid data blkaddr and pass it to __f2fs_invalidate_blocks() for
> further processing.
>
> Assume that the input parameters of f2fs_invalidate_blocks() are addr=N, len=4,
> and there is a blkaddr = NEW_ADDR.
>
> Like this:
> | N | N+1 |NEW_ADDR|N+3|
Is this actually not a continuous address case?
> loop1 addr_start
> addr_end
>
> loop2 addr_end
>
> loop3 addr_end
> At this point we need to
> pass range [addr_start, addr_end - 1] into __f2fs_invalidate_blocks()
> for processing.
>
> loop4
> addr_start
> addr_end
>
> > > + if (addr_start == addr_end) {
> > > + addr_end = addr_start = addr_end + 1;
> > > + continue;
> > > + }
> > > +
> > > + __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
> > > + addr_end = addr_start = addr_end + 1;
> > > + }
> > > + }
> > > +
> > > + if (addr_end >= (addr + len))
> > > + return;
> > > +
> > > + __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
> > > +
> > > +}
> > > +
> > > bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
> > > {
> > > struct sit_info *sit_i = SIT_I(sbi);
> > > --
> > > 2.25.1
> > >
> > >
> > >
> > > _______________________________________________
> > > Linux-f2fs-devel mailing list
> > > Linux-f2fs-devel@lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [f2fs-dev] [PATCH v2 4/5] f2fs: add parameter @len to f2fs_invalidate_blocks()
2024-10-31 9:10 ` Zhiguo Niu
@ 2024-10-31 10:51 ` yi sun
0 siblings, 0 replies; 15+ messages in thread
From: yi sun @ 2024-10-31 10:51 UTC (permalink / raw)
To: Zhiguo Niu
Cc: Daeho Jeong, Yi Sun, chao, jaegeuk, ke.wang, linux-kernel,
linux-f2fs-devel, hao_hao.wang
On Thu, Oct 31, 2024 at 5:10 PM Zhiguo Niu <niuzhiguo84@gmail.com> wrote:
>
> yi sun <sunyibuaa@gmail.com> 于2024年10月31日周四 11:00写道:
> >
> > On Thu, Oct 31, 2024 at 1:00 AM Daeho Jeong <daeho43@gmail.com> wrote:
> > >
> > > On Wed, Oct 30, 2024 at 3:35 AM Yi Sun <yi.sun@unisoc.com> wrote:
> > > >
> > > > New function can process some consecutive blocks at a time.
> > > >
> > > > Function f2fs_invalidate_blocks()->down_write() and up_write()
> > > > are very time-consuming, so if f2fs_invalidate_blocks() can
> > > > process consecutive blocks at one time, it will save a lot of time.
> > > >
> > > > Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> > > > ---
> > > > fs/f2fs/compress.c | 4 +--
> > > > fs/f2fs/f2fs.h | 3 +-
> > > > fs/f2fs/file.c | 8 +++---
> > > > fs/f2fs/node.c | 4 +--
> > > > fs/f2fs/segment.c | 69 ++++++++++++++++++++++++++++++++++++++--------
> > > > 5 files changed, 68 insertions(+), 20 deletions(-)
> > > >
> > > > diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> > > > index e607a7885b57..02ad0ff29cf2 100644
> > > > --- a/fs/f2fs/compress.c
> > > > +++ b/fs/f2fs/compress.c
> > > > @@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> > > > if (blkaddr == COMPRESS_ADDR)
> > > > fio.compr_blocks++;
> > > > if (__is_valid_data_blkaddr(blkaddr))
> > > > - f2fs_invalidate_blocks(sbi, blkaddr);
> > > > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > > > f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
> > > > goto unlock_continue;
> > > > }
> > > > @@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> > > >
> > > > if (i > cc->valid_nr_cpages) {
> > > > if (__is_valid_data_blkaddr(blkaddr)) {
> > > > - f2fs_invalidate_blocks(sbi, blkaddr);
> > > > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > > > f2fs_update_data_blkaddr(&dn, NEW_ADDR);
> > > > }
> > > > goto unlock_continue;
> > > > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > > > index addd49af57ec..4bb459157adf 100644
> > > > --- a/fs/f2fs/f2fs.h
> > > > +++ b/fs/f2fs/f2fs.h
> > > > @@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
> > > > int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
> > > > int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
> > > > void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
> > > > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
> > > > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
> > > > + unsigned int len);
> > > > bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
> > > > int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
> > > > void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
> > > > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > > > index 75a8b22da664..13594bb502d1 100644
> > > > --- a/fs/f2fs/file.c
> > > > +++ b/fs/f2fs/file.c
> > > > @@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
> > > > valid_blocks++;
> > > > }
> > > >
> > > > - f2fs_invalidate_blocks(sbi, blkaddr);
> > > > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > > >
> > > > if (!released || blkaddr != COMPRESS_ADDR)
> > > > nr_free++;
> > > > @@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
> > > > unsigned int i;
> > > >
> > > > for (i = 0; i < ei.len; i++)
> > > > - f2fs_invalidate_blocks(sbi, ei.blk + i);
> > > > + f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
> > > >
> > > > dec_valid_block_count(sbi, inode, ei.len);
> > > > f2fs_update_time(sbi, REQ_TIME);
> > > > @@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
> > > > ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
> > > > if (ret) {
> > > > dec_valid_block_count(sbi, inode, 1);
> > > > - f2fs_invalidate_blocks(sbi, *blkaddr);
> > > > + f2fs_invalidate_blocks(sbi, *blkaddr, 1);
> > > > } else {
> > > > f2fs_update_data_blkaddr(&dn, *blkaddr);
> > > > }
> > > > @@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
> > > > break;
> > > > }
> > > >
> > > > - f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
> > > > + f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
> > > > f2fs_set_data_blkaddr(dn, NEW_ADDR);
> > > > }
> > > >
> > > > diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> > > > index af36c6d6542b..db15d6a90f67 100644
> > > > --- a/fs/f2fs/node.c
> > > > +++ b/fs/f2fs/node.c
> > > > @@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
> > > > }
> > > >
> > > > /* Deallocate node address */
> > > > - f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > > > + f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > > > dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
> > > > set_node_addr(sbi, &ni, NULL_ADDR, false);
> > > >
> > > > @@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
> > > > if (err)
> > > > return err;
> > > >
> > > > - f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > > > + f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > > > dec_valid_node_count(sbi, inode, false);
> > > > set_node_addr(sbi, &ni, NULL_ADDR, false);
> > > >
> > > > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> > > > index 92ddff285a65..67f2bfdeb6ec 100644
> > > > --- a/fs/f2fs/segment.c
> > > > +++ b/fs/f2fs/segment.c
> > > > @@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
> > > > if (!__is_valid_data_blkaddr(new_addr)) {
> > > > if (new_addr == NULL_ADDR)
> > > > dec_valid_block_count(sbi, inode, 1);
> > > > - f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
> > > > + f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
> > > > f2fs_update_data_blkaddr(&dn, new_addr);
> > > > } else {
> > > > f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
> > > > @@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
> > > > get_sec_entry(sbi, segno)->valid_blocks += del;
> > > > }
> > > >
> > > > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
> > > > +static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > > > + block_t addr, block_t end)
> > > > {
> > > > unsigned int segno = GET_SEGNO(sbi, addr);
> > > > struct sit_info *sit_i = SIT_I(sbi);
> > > > + unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
> > > > + unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
> > > > + block_t addr_start = addr;
> > > >
> > > > - f2fs_bug_on(sbi, addr == NULL_ADDR);
> > > > - if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
> > > > - return;
> > > > -
> > > > - f2fs_invalidate_internal_cache(sbi, addr, 1);
> > > > + f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
> > > >
> > > > /* add it into sit main buffer */
> > > > down_write(&sit_i->sentry_lock);
> > > >
> > > > - update_segment_mtime(sbi, addr, 0);
> > > > - update_sit_entry(sbi, addr, -1);
> > > > + if (seg_num == 1)
> > > > + len = end - addr + 1;
> > > > + else
> > > > + len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
> > > >
> > > > - /* add it into dirty seglist */
> > > > - locate_dirty_segment(sbi, segno);
> > > > + do {
> > > > + update_segment_mtime(sbi, addr_start, 0);
> > > > + update_sit_entry(sbi, addr_start, -len);
> > > > +
> > > > + /* add it into dirty seglist */
> > > > + locate_dirty_segment(sbi, segno);
> > > > +
> > > > + /* update @addr_start and @len and @segno */
> > > > + addr_start = START_BLOCK(sbi, ++segno);
> > > > + if (++i == seg_num)
> > > > + len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
> > > > + else
> > > > + len = max_blocks;
> > > > + } while (i <= seg_num);
> > > >
> > > > up_write(&sit_i->sentry_lock);
> > > > }
> > > >
> > > > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > > > + block_t addr, unsigned int len)
> > > > +{
> > > > + unsigned int i;
> > > > + /* Temporary record location */
> > > > + block_t addr_start = addr, addr_end;
> > > > +
> > > > + if (len == 0)
> > > > + return;
> > > > +
> > > > + for (i = 0; i < len; i++) {
> > > > + addr_end = addr + i;
> > > > +
> > > > + f2fs_bug_on(sbi, addr_end == NULL_ADDR);
> > >
> > > Looks like this line should be out of this loop, right?
> > >
> > > > +
> > > > + if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
> > >
> > > ditto?
> >
> > The original f2fs_invalidate_blocks() can only process one block at a time,
> > and it will check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR for
> > each input block.
> >
> > The new f2fs_invalidate_blocks() can process multiple blocks at a time.
> > In order to keep it consistent with the original f2fs_invalidate_blocks(),
> > the new function will also check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR
> > for each block, so these two lines are placed in the loop.
> >
> > > Could you help with enhancing the readability here? a little bit
> > > confused with using addr_start, addr_end and NEW_ADDR, COMPRESS_ADDR,
> > > here.
> > >
> >
> > The addr_start and addr_end will continue to move to filter out the range of
> > all valid data blkaddr and pass it to __f2fs_invalidate_blocks() for
> > further processing.
> >
> > Assume that the input parameters of f2fs_invalidate_blocks() are addr=N, len=4,
> > and there is a blkaddr = NEW_ADDR.
> >
> > Like this:
> > | N | N+1 |NEW_ADDR|N+3|
>
> Is this actually not a continuous address case?
>
I'm not sure if this case actually exists, I just took all the
possibilities into consideration.
> > loop1 addr_start
> > addr_end
> >
> > loop2 addr_end
> >
> > loop3 addr_end
> > At this point we need to
> > pass range [addr_start, addr_end - 1] into __f2fs_invalidate_blocks()
> > for processing.
> >
> > loop4
> > addr_start
> > addr_end
> >
> > > > + if (addr_start == addr_end) {
> > > > + addr_end = addr_start = addr_end + 1;
> > > > + continue;
> > > > + }
> > > > +
> > > > + __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
> > > > + addr_end = addr_start = addr_end + 1;
> > > > + }
> > > > + }
> > > > +
> > > > + if (addr_end >= (addr + len))
> > > > + return;
> > > > +
> > > > + __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
> > > > +
> > > > +}
> > > > +
> > > > bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
> > > > {
> > > > struct sit_info *sit_i = SIT_I(sbi);
> > > > --
> > > > 2.25.1
> > > >
> > > >
> > > >
> > > > _______________________________________________
> > > > Linux-f2fs-devel mailing list
> > > > Linux-f2fs-devel@lists.sourceforge.net
> > > > https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [f2fs-dev] [PATCH v2 4/5] f2fs: add parameter @len to f2fs_invalidate_blocks()
2024-10-30 17:00 ` [f2fs-dev] " Daeho Jeong
2024-10-31 3:00 ` yi sun
@ 2024-11-01 3:39 ` yi sun
1 sibling, 0 replies; 15+ messages in thread
From: yi sun @ 2024-11-01 3:39 UTC (permalink / raw)
To: Daeho Jeong
Cc: Yi Sun, chao, jaegeuk, ke.wang, linux-kernel, linux-f2fs-devel,
hao_hao.wang
On Thu, Oct 31, 2024 at 1:00 AM Daeho Jeong <daeho43@gmail.com> wrote:
>
> On Wed, Oct 30, 2024 at 3:35 AM Yi Sun <yi.sun@unisoc.com> wrote:
> >
> > New function can process some consecutive blocks at a time.
> >
> > Function f2fs_invalidate_blocks()->down_write() and up_write()
> > are very time-consuming, so if f2fs_invalidate_blocks() can
> > process consecutive blocks at one time, it will save a lot of time.
> >
> > Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> > ---
> > fs/f2fs/compress.c | 4 +--
> > fs/f2fs/f2fs.h | 3 +-
> > fs/f2fs/file.c | 8 +++---
> > fs/f2fs/node.c | 4 +--
> > fs/f2fs/segment.c | 69 ++++++++++++++++++++++++++++++++++++++--------
> > 5 files changed, 68 insertions(+), 20 deletions(-)
> >
> > diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> > index e607a7885b57..02ad0ff29cf2 100644
> > --- a/fs/f2fs/compress.c
> > +++ b/fs/f2fs/compress.c
> > @@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> > if (blkaddr == COMPRESS_ADDR)
> > fio.compr_blocks++;
> > if (__is_valid_data_blkaddr(blkaddr))
> > - f2fs_invalidate_blocks(sbi, blkaddr);
> > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
> > goto unlock_continue;
> > }
> > @@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> >
> > if (i > cc->valid_nr_cpages) {
> > if (__is_valid_data_blkaddr(blkaddr)) {
> > - f2fs_invalidate_blocks(sbi, blkaddr);
> > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > f2fs_update_data_blkaddr(&dn, NEW_ADDR);
> > }
> > goto unlock_continue;
> > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > index addd49af57ec..4bb459157adf 100644
> > --- a/fs/f2fs/f2fs.h
> > +++ b/fs/f2fs/f2fs.h
> > @@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
> > int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
> > int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
> > void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
> > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
> > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
> > + unsigned int len);
> > bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
> > int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
> > void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index 75a8b22da664..13594bb502d1 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
> > valid_blocks++;
> > }
> >
> > - f2fs_invalidate_blocks(sbi, blkaddr);
> > + f2fs_invalidate_blocks(sbi, blkaddr, 1);
> >
> > if (!released || blkaddr != COMPRESS_ADDR)
> > nr_free++;
> > @@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
> > unsigned int i;
> >
> > for (i = 0; i < ei.len; i++)
> > - f2fs_invalidate_blocks(sbi, ei.blk + i);
> > + f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
> >
> > dec_valid_block_count(sbi, inode, ei.len);
> > f2fs_update_time(sbi, REQ_TIME);
> > @@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
> > ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
> > if (ret) {
> > dec_valid_block_count(sbi, inode, 1);
> > - f2fs_invalidate_blocks(sbi, *blkaddr);
> > + f2fs_invalidate_blocks(sbi, *blkaddr, 1);
> > } else {
> > f2fs_update_data_blkaddr(&dn, *blkaddr);
> > }
> > @@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
> > break;
> > }
> >
> > - f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
> > + f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
> > f2fs_set_data_blkaddr(dn, NEW_ADDR);
> > }
> >
> > diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> > index af36c6d6542b..db15d6a90f67 100644
> > --- a/fs/f2fs/node.c
> > +++ b/fs/f2fs/node.c
> > @@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
> > }
> >
> > /* Deallocate node address */
> > - f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > + f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
> > set_node_addr(sbi, &ni, NULL_ADDR, false);
> >
> > @@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
> > if (err)
> > return err;
> >
> > - f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > + f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > dec_valid_node_count(sbi, inode, false);
> > set_node_addr(sbi, &ni, NULL_ADDR, false);
> >
> > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> > index 92ddff285a65..67f2bfdeb6ec 100644
> > --- a/fs/f2fs/segment.c
> > +++ b/fs/f2fs/segment.c
> > @@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
> > if (!__is_valid_data_blkaddr(new_addr)) {
> > if (new_addr == NULL_ADDR)
> > dec_valid_block_count(sbi, inode, 1);
> > - f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
> > + f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
> > f2fs_update_data_blkaddr(&dn, new_addr);
> > } else {
> > f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
> > @@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
> > get_sec_entry(sbi, segno)->valid_blocks += del;
> > }
> >
> > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
> > +static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > + block_t addr, block_t end)
> > {
> > unsigned int segno = GET_SEGNO(sbi, addr);
> > struct sit_info *sit_i = SIT_I(sbi);
> > + unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
> > + unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
> > + block_t addr_start = addr;
> >
> > - f2fs_bug_on(sbi, addr == NULL_ADDR);
> > - if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
> > - return;
> > -
> > - f2fs_invalidate_internal_cache(sbi, addr, 1);
> > + f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
> >
> > /* add it into sit main buffer */
> > down_write(&sit_i->sentry_lock);
> >
> > - update_segment_mtime(sbi, addr, 0);
> > - update_sit_entry(sbi, addr, -1);
> > + if (seg_num == 1)
> > + len = end - addr + 1;
> > + else
> > + len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
> >
> > - /* add it into dirty seglist */
> > - locate_dirty_segment(sbi, segno);
> > + do {
> > + update_segment_mtime(sbi, addr_start, 0);
> > + update_sit_entry(sbi, addr_start, -len);
> > +
> > + /* add it into dirty seglist */
> > + locate_dirty_segment(sbi, segno);
> > +
> > + /* update @addr_start and @len and @segno */
> > + addr_start = START_BLOCK(sbi, ++segno);
> > + if (++i == seg_num)
> > + len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
> > + else
> > + len = max_blocks;
> > + } while (i <= seg_num);
> >
> > up_write(&sit_i->sentry_lock);
> > }
> >
> > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > + block_t addr, unsigned int len)
> > +{
> > + unsigned int i;
> > + /* Temporary record location */
> > + block_t addr_start = addr, addr_end;
> > +
> > + if (len == 0)
> > + return;
> > +
> > + for (i = 0; i < len; i++) {
> > + addr_end = addr + i;
> > +
> > + f2fs_bug_on(sbi, addr_end == NULL_ADDR);
>
> Looks like this line should be out of this loop, right?
>
> > +
> > + if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
>
> ditto?
> Could you help with enhancing the readability here? a little bit
> confused with using addr_start, addr_end and NEW_ADDR, COMPRESS_ADDR,
> here.
>
Hi Daeho,
Thanks for your feedback, I think you are right. I will correct this problem.
> > + if (addr_start == addr_end) {
> > + addr_end = addr_start = addr_end + 1;
> > + continue;
> > + }
> > +
> > + __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
> > + addr_end = addr_start = addr_end + 1;
> > + }
> > + }
> > +
> > + if (addr_end >= (addr + len))
> > + return;
> > +
> > + __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
> > +
> > +}
> > +
> > bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
> > {
> > struct sit_info *sit_i = SIT_I(sbi);
> > --
> > 2.25.1
> >
> >
> >
> > _______________________________________________
> > Linux-f2fs-devel mailing list
> > Linux-f2fs-devel@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2024-11-01 3:40 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-30 10:31 [PATCH v2 0/5] Speed up f2fs truncate Yi Sun
2024-10-30 10:31 ` [PATCH v2 1/5] f2fs: blocks need to belong to the same segment when using update_sit_entry() Yi Sun
2024-10-31 7:19 ` Chao Yu
2024-10-30 10:31 ` [PATCH v2 2/5] f2fs: expand f2fs_invalidate_compress_page() to f2fs_invalidate_compress_pages_range() Yi Sun
2024-10-31 7:26 ` Chao Yu
2024-10-31 9:02 ` yi sun
2024-10-30 10:31 ` [PATCH v2 3/5] f2fs: add parameter @len to f2fs_invalidate_internal_cache() Yi Sun
2024-10-31 7:27 ` Chao Yu
2024-10-30 10:31 ` [PATCH v2 4/5] f2fs: add parameter @len to f2fs_invalidate_blocks() Yi Sun
2024-10-30 17:00 ` [f2fs-dev] " Daeho Jeong
2024-10-31 3:00 ` yi sun
2024-10-31 9:10 ` Zhiguo Niu
2024-10-31 10:51 ` yi sun
2024-11-01 3:39 ` yi sun
2024-10-30 10:31 ` [PATCH v2 5/5] f2fs: Optimize f2fs_truncate_data_blocks_range() Yi Sun
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox