Skip to contentSkip to conversation

fuse: clean up offset and page count calculations

PATCH Superseded Joanne Koong 5 participants 17 messages Jan 16 – Jan 22

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover fuse: clean up offset and page count calculations

This patchset aims to improve code clarity by using standard kernel helper macros for common calculations:
* DIV_ROUND_UP() for page count calculations
* offset_in_folio() for large folio offset calculations
* offset_in_page() for page offset calculations

These helpers improve readability and consistency with patterns used elsewhere in the kernel. No functional changes intended.

This patchset is on top of Jingbo's patch in [1].

Thanks,
Joanne

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

Joanne Koong (3):
fuse: use DIV_ROUND_UP() for page count calculations
fuse: use offset_in_folio() for large folio offset calculations
fuse: use offset_in_page() for page offset calculations

 fs/fuse/dev.c     | 14 +++++++-------
 fs/fuse/file.c    |  2 +-
 fs/fuse/readdir.c |  8 ++++----
 3 files changed, 12 insertions(+), 12 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/3 fuse: use DIV_ROUND_UP() for page count calculations

Use DIV_ROUND_UP() instead of manually computing round-up division calculations.

---
 fs/fuse/dev.c  | 6 +++---
 fs/fuse/file.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
fs/fuse/dev.c +3 -3
index 6d59cbc877c6..698289b5539e 100644
@@ -1814,7 +1814,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 
 		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
 		nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
-		nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+		nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
 		err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);
 		if (!folio_test_uptodate(folio) && !err && offset == 0 &&
@@ -1883,7 +1883,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 	else if (outarg->offset + num > file_size)
 		num = file_size - outarg->offset;
 
-	num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	num_pages = DIV_ROUND_UP(num + offset, PAGE_SIZE);
 	num_pages = min(num_pages, fc->max_pages);
 	num = min(num, num_pages << PAGE_SHIFT);
 
@@ -1918,7 +1918,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 
 		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
 		nr_bytes = min(folio_size(folio) - folio_offset, num);
-		nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+		nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
 		ap->folios[ap->num_folios] = folio;
 		ap->descs[ap->num_folios].offset = folio_offset;
fs/fuse/file.c +1 -1
index eba70ebf6e77..a4342b269cb9 100644
@@ -2170,7 +2170,7 @@ static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos,
 	WARN_ON(!ap->num_folios);
 
 	/* Reached max pages */
-	if ((bytes + PAGE_SIZE - 1) >> PAGE_SHIFT > fc->max_pages)
+	if (DIV_ROUND_UP(bytes, PAGE_SIZE) > fc->max_pages)
 		return true;
 
 	if (bytes > max_bytes)
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH v1 1/3] fuse: use DIV_ROUND_UP() for page count calculations

Quoted 14 lines · Joanne Koong

Use DIV_ROUND_UP() instead of manually computing round-up division calculations.

---
 fs/fuse/dev.c  | 6 +++---
 fs/fuse/file.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
fs/fuse/dev.c +0 -0
index 6d59cbc877c6..698289b5539e 100644

fs/fuse/dev.c static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, · line 1820

Quoted 8 lines
fs/fuse/dev.c +1 -1
@@ -1814,7 +1814,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 
 		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
 		nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
-		nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+		nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
 		err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);
 		if (!folio_test_uptodate(folio) && !err && offset == 0 &&

IMHO, could we drop page offset, instead just update the file offset and re-calculate folio index and folio offset for each loop, i.e. something like what [1] did?

This could make the code simpler and cleaner.

BTW, it seems that if the grabbed folio is newly created on hand and the range described by the store notify doesn't cover the folio completely, the folio won't be set as Uptodate and thus the written data may be missed? I'm not sure if this is in design.

[1]
https://lore.kernel.org/linux-fsdevel/[email protected]/

Quoted 10 lines
fs/fuse/dev.c +1 -1
@@ -1883,7 +1883,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 	else if (outarg->offset + num > file_size)
 		num = file_size - outarg->offset;
 
-	num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	num_pages = DIV_ROUND_UP(num + offset, PAGE_SIZE);
 	num_pages = min(num_pages, fc->max_pages);
 	num = min(num, num_pages << PAGE_SHIFT);
 

fs/fuse/dev.c static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode, · line 1924

Quoted 8 lines
fs/fuse/dev.c +1 -1
@@ -1918,7 +1918,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 
 		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
 		nr_bytes = min(folio_size(folio) - folio_offset, num);
-		nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+		nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
 		ap->folios[ap->num_folios] = folio;
 		ap->descs[ap->num_folios].offset = folio_offset;

Ditto.

Quoted 13 lines
fs/fuse/file.c +1 -1
index eba70ebf6e77..a4342b269cb9 100644
@@ -2170,7 +2170,7 @@ static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos,
 	WARN_ON(!ap->num_folios);
 
 	/* Reached max pages */
-	if ((bytes + PAGE_SIZE - 1) >> PAGE_SHIFT > fc->max_pages)
+	if (DIV_ROUND_UP(bytes, PAGE_SIZE) > fc->max_pages)
 		return true;
 
 	if (bytes > max_bytes)
Signature · 2 lines
Thanks,
Jingbo
#3 lore raw

Re: [PATCH v1 1/3] fuse: use DIV_ROUND_UP() for page count calculations

Quoted 22 lines · Jingbo Xu
Quoted 20 lines · Joanne Koong

Use DIV_ROUND_UP() instead of manually computing round-up division calculations.

---
 fs/fuse/dev.c  | 6 +++---
 fs/fuse/file.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
fs/fuse/dev.c +1 -1
index 6d59cbc877c6..698289b5539e 100644
@@ -1814,7 +1814,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 
              folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
              nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
-             nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+             nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
Quoted 8 lines

fs/fuse/dev.c static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, · line 1820

Quoted 2 lines
fs/fuse/dev.c +0 -0
              err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);
              if (!folio_test_uptodate(folio) && !err && offset == 0 &&

IMHO, could we drop page offset, instead just update the file offset and re-calculate folio index and folio offset for each loop, i.e. something like what [1] did?

This could make the code simpler and cleaner.

Hi Jingbo,

I'll break this change out into a separate patch. I agree your proposed restructuring of the logic makes it simpler to parse.

Thanks,
Joanne

Quoted 49 lines

BTW, it seems that if the grabbed folio is newly created on hand and the range described by the store notify doesn't cover the folio completely, the folio won't be set as Uptodate and thus the written data may be missed? I'm not sure if this is in design.

[1]
https://lore.kernel.org/linux-fsdevel/[email protected]/

Quoted 10 lines
fs/fuse/dev.c +1 -1
@@ -1883,7 +1883,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
      else if (outarg->offset + num > file_size)
              num = file_size - outarg->offset;
 
-     num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
+     num_pages = DIV_ROUND_UP(num + offset, PAGE_SIZE);
      num_pages = min(num_pages, fc->max_pages);
      num = min(num, num_pages << PAGE_SHIFT);
 

fs/fuse/dev.c static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode, · line 1924

Quoted 8 lines
fs/fuse/dev.c +1 -1
@@ -1918,7 +1918,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 
              folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
              nr_bytes = min(folio_size(folio) - folio_offset, num);
-             nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+             nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
              ap->folios[ap->num_folios] = folio;
              ap->descs[ap->num_folios].offset = folio_offset;

Ditto.

fs/fuse/file.c static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos, · line 2176

Quoted 13 lines
fs/fuse/file.c +1 -1
index eba70ebf6e77..a4342b269cb9 100644
@@ -2170,7 +2170,7 @@ static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos,
      WARN_ON(!ap->num_folios);
 
      /* Reached max pages */
-     if ((bytes + PAGE_SIZE - 1) >> PAGE_SHIFT > fc->max_pages)
+     if (DIV_ROUND_UP(bytes, PAGE_SIZE) > fc->max_pages)
              return true;
 
      if (bytes > max_bytes)

--
Thanks,
Jingbo

#4 lore raw

Re: [PATCH v1 1/3] fuse: use DIV_ROUND_UP() for page count calculations

Quoted 33 lines · Joanne Koong
Quoted 22 lines · Jingbo Xu
Quoted 20 lines · Joanne Koong

Use DIV_ROUND_UP() instead of manually computing round-up division calculations.

---
 fs/fuse/dev.c  | 6 +++---
 fs/fuse/file.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
fs/fuse/dev.c +1 -1
index 6d59cbc877c6..698289b5539e 100644
@@ -1814,7 +1814,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 
              folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
              nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
-             nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+             nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
Quoted 8 lines

fs/fuse/dev.c static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, · line 1820

Quoted 2 lines
fs/fuse/dev.c +0 -0
              err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);
              if (!folio_test_uptodate(folio) && !err && offset == 0 &&

IMHO, could we drop page offset, instead just update the file offset and re-calculate folio index and folio offset for each loop, i.e. something like what [1] did?

This could make the code simpler and cleaner.

Quoted 13 lines

Hi Jingbo,

I'll break this change out into a separate patch. I agree your proposed restructuring of the logic makes it simpler to parse.

Thanks,
Joanne

Quoted 5 lines

BTW, it seems that if the grabbed folio is newly created on hand and the range described by the store notify doesn't cover the folio completely, the folio won't be set as Uptodate and thus the written data may be missed? I'm not sure if this is in design.

(sorry, forgot to respond to this part of your email)

I think this is intentional. By "thus the written data may be missed", I think you're talking about the writeback path? My understanding is it's the dirty bit, not uptodate, that determines whether the written data gets written back. I think Darrick had the same question about this. AFAICT, it's by design to not have writeback triggered for this path since the server is the one providing the data so they already know the state-of-truth for the folio contents and that should already be reflected on their backend.

Thanks,
Joanne

Quoted 44 lines
Quoted 44 lines

[1]
https://lore.kernel.org/linux-fsdevel/[email protected]/

Quoted 10 lines
fs/fuse/dev.c +1 -1
@@ -1883,7 +1883,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
      else if (outarg->offset + num > file_size)
              num = file_size - outarg->offset;
 
-     num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
+     num_pages = DIV_ROUND_UP(num + offset, PAGE_SIZE);
      num_pages = min(num_pages, fc->max_pages);
      num = min(num, num_pages << PAGE_SHIFT);
 

fs/fuse/dev.c static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode, · line 1924

Quoted 8 lines
fs/fuse/dev.c +1 -1
@@ -1918,7 +1918,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 
              folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
              nr_bytes = min(folio_size(folio) - folio_offset, num);
-             nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+             nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
              ap->folios[ap->num_folios] = folio;
              ap->descs[ap->num_folios].offset = folio_offset;

Ditto.

fs/fuse/file.c static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos, · line 2176

Quoted 13 lines
fs/fuse/file.c +1 -1
index eba70ebf6e77..a4342b269cb9 100644
@@ -2170,7 +2170,7 @@ static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos,
      WARN_ON(!ap->num_folios);
 
      /* Reached max pages */
-     if ((bytes + PAGE_SIZE - 1) >> PAGE_SHIFT > fc->max_pages)
+     if (DIV_ROUND_UP(bytes, PAGE_SIZE) > fc->max_pages)
              return true;
 
      if (bytes > max_bytes)

--
Thanks,
Jingbo

#5 lore raw

Re: [PATCH v1 1/3] fuse: use DIV_ROUND_UP() for page count calculations

Hi Joanne,

Thanks for the replying ;)

Quoted 48 lines · Joanne Koong
Quoted 33 lines · Joanne Koong
Quoted 22 lines · Jingbo Xu
Quoted 20 lines · Joanne Koong

Use DIV_ROUND_UP() instead of manually computing round-up division calculations.

---
 fs/fuse/dev.c  | 6 +++---
 fs/fuse/file.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
fs/fuse/dev.c +1 -1
index 6d59cbc877c6..698289b5539e 100644
@@ -1814,7 +1814,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 
              folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
              nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
-             nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+             nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
Quoted 8 lines

fs/fuse/dev.c static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, · line 1820

Quoted 2 lines
fs/fuse/dev.c +0 -0
              err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);
              if (!folio_test_uptodate(folio) && !err && offset == 0 &&

IMHO, could we drop page offset, instead just update the file offset and re-calculate folio index and folio offset for each loop, i.e. something like what [1] did?

This could make the code simpler and cleaner.

Quoted 13 lines

Hi Jingbo,

I'll break this change out into a separate patch. I agree your proposed restructuring of the logic makes it simpler to parse.

Thanks,
Joanne

Quoted 5 lines

BTW, it seems that if the grabbed folio is newly created on hand and the range described by the store notify doesn't cover the folio completely, the folio won't be set as Uptodate and thus the written data may be missed? I'm not sure if this is in design.

Quoted 5 lines

(sorry, forgot to respond to this part of your email)

I think this is intentional. By "thus the written data may be missed", I think you're talking about the writeback path? My understanding is it's the dirty bit, not uptodate,

Not exactly. What I'm concerned is the uptodate bit.

In the case where "the grabbed folio is newly created on hand and the range described by the store notify doesn't cover the folio completely, the folio won't be set as Uptodate", the following read(2) or write(2) on the folio will discard the content already in the folio, instead it triggers .readpage() to fetch data from FUSE server again.

It seems that it is a deliberate constraint for FUSE_NOTIFY_STORE.

that determines whether the written

Quoted 6 lines

data gets written back. I think Darrick had the same question about this. AFAICT, it's by design to not have writeback triggered for this path since the server is the one providing the data so they already know the state-of-truth for the folio contents and that should already be reflected on their backend.

Signature · 2 lines
Thanks,
Jingbo
#6 lore raw

Re: [PATCH v1 1/3] fuse: use DIV_ROUND_UP() for page count calculations

Quoted 60 lines · Jingbo Xu

Hi Joanne,

Thanks for the replying ;)

Quoted 48 lines · Joanne Koong
Quoted 33 lines · Joanne Koong
Quoted 22 lines · Jingbo Xu
Quoted 20 lines · Joanne Koong

Use DIV_ROUND_UP() instead of manually computing round-up division calculations.

---
 fs/fuse/dev.c  | 6 +++---
 fs/fuse/file.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
fs/fuse/dev.c +1 -1
index 6d59cbc877c6..698289b5539e 100644
@@ -1814,7 +1814,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 
              folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
              nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
-             nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+             nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
Quoted 8 lines

fs/fuse/dev.c static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, · line 1820

Quoted 2 lines
fs/fuse/dev.c +0 -0
              err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);
              if (!folio_test_uptodate(folio) && !err && offset == 0 &&

IMHO, could we drop page offset, instead just update the file offset and re-calculate folio index and folio offset for each loop, i.e. something like what [1] did?

This could make the code simpler and cleaner.

Quoted 13 lines

Hi Jingbo,

I'll break this change out into a separate patch. I agree your proposed restructuring of the logic makes it simpler to parse.

Thanks,
Joanne

Quoted 5 lines

BTW, it seems that if the grabbed folio is newly created on hand and the range described by the store notify doesn't cover the folio completely, the folio won't be set as Uptodate and thus the written data may be missed? I'm not sure if this is in design.

Quoted 5 lines

(sorry, forgot to respond to this part of your email)

I think this is intentional. By "thus the written data may be missed", I think you're talking about the writeback path? My understanding is it's the dirty bit, not uptodate,

Quoted 7 lines

Not exactly. What I'm concerned is the uptodate bit.

In the case where "the grabbed folio is newly created on hand and the range described by the store notify doesn't cover the folio completely, the folio won't be set as Uptodate", the following read(2) or write(2) on the folio will discard the content already in the folio, instead it triggers .readpage() to fetch data from FUSE server again.

Could you elaborate on why this concerns you? Isn't this necessary behavior given that it needs to fetch the parts that the store notify didn't cover? Or is your concern that the contents are discarded? But the server already has that information stored on their side, so I'm not seeing why that's a problem.

Thanks,
Joanne

Quoted 14 lines

It seems that it is a deliberate constraint for FUSE_NOTIFY_STORE.

that determines whether the written

Quoted 6 lines

data gets written back. I think Darrick had the same question about this. AFAICT, it's by design to not have writeback triggered for this path since the server is the one providing the data so they already know the state-of-truth for the folio contents and that should already be reflected on their backend.

--
Thanks,
Jingbo

#7 lore raw

Re: [PATCH v1 1/3] fuse: use DIV_ROUND_UP() for page count calculations

Quoted 69 lines · Joanne Koong
Quoted 60 lines · Jingbo Xu

Hi Joanne,

Thanks for the replying ;)

Quoted 48 lines · Joanne Koong
Quoted 33 lines · Joanne Koong
Quoted 22 lines · Jingbo Xu
Quoted 20 lines · Joanne Koong

Use DIV_ROUND_UP() instead of manually computing round-up division calculations.

---
 fs/fuse/dev.c  | 6 +++---
 fs/fuse/file.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
fs/fuse/dev.c +1 -1
index 6d59cbc877c6..698289b5539e 100644
@@ -1814,7 +1814,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 
              folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
              nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
-             nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+             nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
Quoted 8 lines

fs/fuse/dev.c static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, · line 1820

Quoted 2 lines
fs/fuse/dev.c +0 -0
              err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);
              if (!folio_test_uptodate(folio) && !err && offset == 0 &&

IMHO, could we drop page offset, instead just update the file offset and re-calculate folio index and folio offset for each loop, i.e. something like what [1] did?

This could make the code simpler and cleaner.

Quoted 13 lines

Hi Jingbo,

I'll break this change out into a separate patch. I agree your proposed restructuring of the logic makes it simpler to parse.

Thanks,
Joanne

Quoted 5 lines

BTW, it seems that if the grabbed folio is newly created on hand and the range described by the store notify doesn't cover the folio completely, the folio won't be set as Uptodate and thus the written data may be missed? I'm not sure if this is in design.

Quoted 5 lines

(sorry, forgot to respond to this part of your email)

I think this is intentional. By "thus the written data may be missed", I think you're talking about the writeback path? My understanding is it's the dirty bit, not uptodate,

Quoted 7 lines

Not exactly. What I'm concerned is the uptodate bit.

In the case where "the grabbed folio is newly created on hand and the range described by the store notify doesn't cover the folio completely, the folio won't be set as Uptodate", the following read(2) or write(2) on the folio will discard the content already in the folio, instead it triggers .readpage() to fetch data from FUSE server again.

Quoted 6 lines

Could you elaborate on why this concerns you? Isn't this necessary behavior given that it needs to fetch the parts that the store notify didn't cover? Or is your concern that the contents are discarded? But the server already has that information stored on their side, so I'm not seeing why that's a problem.

I'm not thinking it as a problem. As said, I guess it is just a design constraint for FUSE_NOTIFY_STORE.

Thanks.

Signature · 2 lines
Thanks,
Jingbo
#8 lore raw

PATCH 2/3 fuse: use offset_in_folio() for large folio offset calculations

In reply to Joanne Koong

Use offset_in_folio() instead of manually calculating the folio offset.

---
 fs/fuse/dev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/fuse/dev.c +2 -2
index 698289b5539e..4dda4e24cc90 100644
@@ -1812,7 +1812,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 		if (IS_ERR(folio))
 			goto out_iput;
 
-		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+		folio_offset = offset_in_folio(folio, outarg.offset);
 		nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
 		nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
@@ -1916,7 +1916,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 		if (IS_ERR(folio))
 			break;
 
-		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+		folio_offset = offset_in_folio(folio, outarg->offset);
 		nr_bytes = min(folio_size(folio) - folio_offset, num);
 		nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
Signature · 1 line
2.47.3
#9 lore raw

Re: [PATCH v1 2/3] fuse: use offset_in_folio() for large folio offset calculations

Quoted 12 lines · Joanne Koong

Use offset_in_folio() instead of manually calculating the folio offset.

---
  fs/fuse/dev.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fs/fuse/dev.c +0 -0
index 698289b5539e..4dda4e24cc90 100644

fs/fuse/dev.c static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, · line 1815

Quoted 4 lines
fs/fuse/dev.c +1 -0
@@ -1812,7 +1812,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
          if (IS_ERR(folio))
              goto out_iput;
  -        folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+        folio_offset = offset_in_folio(folio, outarg.offset);

offset is a loop variable, and later offset maybe set to 0. Replacing it with outarg.offset here would change the behavior. The same below. Will this cause any problems?

Thanks,
Chunsheng Luo

Quoted 10 lines
fs/fuse/dev.c +1 -0
          nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
          nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
  @@ -1916,7 +1916,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
          if (IS_ERR(folio))
              break;
  -        folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+        folio_offset = offset_in_folio(folio, outarg->offset);
          nr_bytes = min(folio_size(folio) - folio_offset, num);
          nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
#10 lore raw

Re: [PATCH v1 2/3] fuse: use offset_in_folio() for large folio offset calculations

In reply to Joanne Koong
Quoted 12 lines · Joanne Koong

Use offset_in_folio() instead of manually calculating the folio offset.

---
  fs/fuse/dev.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fs/fuse/dev.c +0 -0
index 698289b5539e..4dda4e24cc90 100644

fs/fuse/dev.c static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, · line 1815

Quoted 5 lines
fs/fuse/dev.c +1 -1
@@ -1812,7 +1812,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
  		if (IS_ERR(folio))
  			goto out_iput;
 
-		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+		folio_offset = offset_in_folio(folio, outarg.offset);

offset is a loop variable, and later offset will be set to 0. Replacing it with outarg.offset here would change the behavior. The same applies to the cases below. Will there be any problem here?

Thanks,
Chunsheng Luo

Quoted 12 lines
fs/fuse/dev.c +1 -1
  		nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
  		nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
@@ -1916,7 +1916,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
  		if (IS_ERR(folio))
  			break;
 
-		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+		folio_offset = offset_in_folio(folio, outarg->offset);
  		nr_bytes = min(folio_size(folio) - folio_offset, num);
  		nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
#11 lore raw

Re: [PATCH v1 2/3] fuse: use offset_in_folio() for large folio offset calculations

Quoted 17 lines · Chunsheng Luo
Quoted 15 lines · Joanne Koong

Use offset_in_folio() instead of manually calculating the folio offset.

---
  fs/fuse/dev.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fs/fuse/dev.c +0 -0
index 698289b5539e..4dda4e24cc90 100644
@@ -1812,7 +1812,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
              if (IS_ERR(folio))
                      goto out_iput;
 
Quoted 6 lines

fs/fuse/dev.c static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, · line 1815

Quoted 2 lines
fs/fuse/dev.c +1 -1
-             folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+             folio_offset = offset_in_folio(folio, outarg.offset);

offset is a loop variable, and later offset will be set to 0. Replacing it with outarg.offset here would change the behavior. The same applies to the cases below. Will there be any problem here?

Hi Chunsheng,

Good catch, the offset variable should get replaced entirely by outarg.offset. I'll make this change in v2.

Thanks,
Joanne

Quoted 17 lines

Thanks,
Chunsheng Luo

Quoted 12 lines
fs/fuse/dev.c +1 -1
              nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
              nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
@@ -1916,7 +1916,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
              if (IS_ERR(folio))
                      break;
 
-             folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+             folio_offset = offset_in_folio(folio, outarg->offset);
              nr_bytes = min(folio_size(folio) - folio_offset, num);
              nr_pages = DIV_ROUND_UP(offset + nr_bytes, PAGE_SIZE);
 
#12 lore raw

PATCH 3/3 fuse: use offset_in_page() for page offset calculations

In reply to Joanne Koong

Replace open-coded (x & ~PAGE_MASK) with offset_in_page().

---
 fs/fuse/dev.c     | 4 ++--
 fs/fuse/readdir.c | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
fs/fuse/dev.c +2 -2
index 4dda4e24cc90..9ba8ca796ff3 100644
@@ -1792,7 +1792,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 
 	mapping = inode->i_mapping;
 	index = outarg.offset >> PAGE_SHIFT;
-	offset = outarg.offset & ~PAGE_MASK;
+	offset = offset_in_page(outarg.offset);
 	file_size = i_size_read(inode);
 	end = outarg.offset + outarg.size;
 	if (end > file_size) {
@@ -1874,7 +1874,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 	struct fuse_args_pages *ap;
 	struct fuse_args *args;
 
-	offset = outarg->offset & ~PAGE_MASK;
+	offset = offset_in_page(outarg->offset);
 	file_size = i_size_read(inode);
 
 	num = min(outarg->size, fc->max_write);
fs/fuse/readdir.c +4 -4
index c2aae2eef086..c88194e52d18 100644
@@ -52,7 +52,7 @@ static void fuse_add_dirent_to_cache(struct file *file,
 	}
 	version = fi->rdc.version;
 	size = fi->rdc.size;
-	offset = size & ~PAGE_MASK;
+	offset = offset_in_page(size);
 	index = size >> PAGE_SHIFT;
 	/* Dirent doesn't fit in current page?  Jump to next page. */
 	if (offset + reclen > PAGE_SIZE) {
@@ -392,7 +392,7 @@ static enum fuse_parse_result fuse_parse_cache(struct fuse_file *ff,
 					       void *addr, unsigned int size,
 					       struct dir_context *ctx)
 {
-	unsigned int offset = ff->readdir.cache_off & ~PAGE_MASK;
+	unsigned int offset = offset_in_page(ff->readdir.cache_off);
 	enum fuse_parse_result res = FOUND_NONE;
 
 	WARN_ON(offset >= size);
@@ -518,13 +518,13 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 	index = ff->readdir.cache_off >> PAGE_SHIFT;
 
 	if (index == (fi->rdc.size >> PAGE_SHIFT))
-		size = fi->rdc.size & ~PAGE_MASK;
+		size = offset_in_page(fi->rdc.size);
 	else
 		size = PAGE_SIZE;
 	spin_unlock(&fi->rdc.lock);
 
 	/* EOF? */
-	if ((ff->readdir.cache_off & ~PAGE_MASK) == size)
+	if (offset_in_page(ff->readdir.cache_off) == size)
 		return 0;
 
 	page = find_get_page_flags(file->f_mapping, index,
Signature · 1 line
2.47.3
Reviewed-by on the v1 series
#13 lore raw
In reply to Joanne Koong
Quoted 10 lines · Joanne Koong

This patchset aims to improve code clarity by using standard kernel helper macros for common calculations:
* DIV_ROUND_UP() for page count calculations
* offset_in_folio() for large folio offset calculations
* offset_in_page() for page offset calculations

These helpers improve readability and consistency with patterns used elsewhere in the kernel. No functional changes intended.

This patchset is on top of Jingbo's patch in [1].

As a straight conversion this looks fine to me so

OTOH I just learned that fuse has a backchannel for fuse servers to inject pagecache data for a regular file. That might be kinda nice for an HSM or something? Though that would be covered by FUSE_READ.

Hrmm, I wonder how well that interacts with iomap... we don't mark the folios dirty or update timestamps, so I'm guessing the contents could disappear at any time if the page cache gets reclaimed?

Weiiiird.....

--D

Quoted 19 lines

Thanks,
Joanne

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

Joanne Koong (3):
fuse: use DIV_ROUND_UP() for page count calculations
fuse: use offset_in_folio() for large folio offset calculations
fuse: use offset_in_page() for page offset calculations

 fs/fuse/dev.c     | 14 +++++++-------
 fs/fuse/file.c    |  2 +-
 fs/fuse/readdir.c |  8 ++++----
 3 files changed, 12 insertions(+), 12 deletions(-)
Signature · 1 line
2.47.3
#14 lore raw
Quoted 16 lines · Darrick J. Wong
Quoted 10 lines · Joanne Koong

This patchset aims to improve code clarity by using standard kernel helper macros for common calculations:
* DIV_ROUND_UP() for page count calculations
* offset_in_folio() for large folio offset calculations
* offset_in_page() for page offset calculations

These helpers improve readability and consistency with patterns used elsewhere in the kernel. No functional changes intended.

This patchset is on top of Jingbo's patch in [1].

As a straight conversion this looks fine to me so

Quoted 7 lines

OTOH I just learned that fuse has a backchannel for fuse servers to inject pagecache data for a regular file. That might be kinda nice for an HSM or something? Though that would be covered by FUSE_READ.

Hrmm, I wonder how well that interacts with iomap... we don't mark the folios dirty or update timestamps, so I'm guessing the contents could disappear at any time if the page cache gets reclaimed?

My interpretation of it is that it doesn't need to be marked dirty because the server is the one injecting this data into the folio so theoretically it should already have all of this data already committed on its backend.

Thanks,
Joanne

Quoted 25 lines

Weiiiird.....

--D

Quoted 19 lines

Thanks,
Joanne

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

Joanne Koong (3):
fuse: use DIV_ROUND_UP() for page count calculations
fuse: use offset_in_folio() for large folio offset calculations
fuse: use offset_in_page() for page offset calculations

 fs/fuse/dev.c     | 14 +++++++-------
 fs/fuse/file.c    |  2 +-
 fs/fuse/readdir.c |  8 ++++----
 3 files changed, 12 insertions(+), 12 deletions(-)

--
2.47.3

Reviewed-by on the v1 series
#15 lore raw
In reply to Joanne Koong
Quoted 21 lines · Joanne Koong

This patchset aims to improve code clarity by using standard kernel helper macros for common calculations:
* DIV_ROUND_UP() for page count calculations
* offset_in_folio() for large folio offset calculations
* offset_in_page() for page offset calculations

These helpers improve readability and consistency with patterns used elsewhere in the kernel. No functional changes intended.

This patchset is on top of Jingbo's patch in [1].

Thanks,
Joanne

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

Joanne Koong (3):
fuse: use DIV_ROUND_UP() for page count calculations
fuse: use offset_in_folio() for large folio offset calculations
fuse: use offset_in_page() for page offset calculations

Quoted 8 lines
 fs/fuse/dev.c     | 14 +++++++-------
 fs/fuse/file.c    |  2 +-
 fs/fuse/readdir.c |  8 ++++----
 3 files changed, 12 insertions(+), 12 deletions(-)
Signature · 1 line
2.47.3

Looks good to me.

#16 lore raw
Quoted 28 lines · Horst Birthelmer
Quoted 26 lines · Joanne Koong

This patchset aims to improve code clarity by using standard kernel helper macros for common calculations:
* DIV_ROUND_UP() for page count calculations
* offset_in_folio() for large folio offset calculations
* offset_in_page() for page offset calculations

These helpers improve readability and consistency with patterns used elsewhere in the kernel. No functional changes intended.

This patchset is on top of Jingbo's patch in [1].

Thanks,
Joanne

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

Joanne Koong (3):
fuse: use DIV_ROUND_UP() for page count calculations
fuse: use offset_in_folio() for large folio offset calculations
fuse: use offset_in_page() for page offset calculations

 fs/fuse/dev.c     | 14 +++++++-------
 fs/fuse/file.c    |  2 +-
 fs/fuse/readdir.c |  8 ++++----
 3 files changed, 12 insertions(+), 12 deletions(-)
Quoted 7 lines
Quoted 3 lines

--
2.47.3

Looks good to me.

Thanks for taking the time to look at this, Horst!