Skip to contentSkip to conversation

ceph: improve error handling and short/overflow-read logic in __ceph_sync_read()

PATCH In review Alex Markuze 2 participants 6 messages Dec 9, 2024 – Dec 11, 2024 Ceph client

lore
Share & tools
lore

Alex Markuze

author maintainer
#0 lore raw

PATCH 1/1 ceph: improve error handling and short/overflow-read logic in __ceph_sync_read()

This patch refines the read logic in __ceph_sync_read() to ensure more predictable and efficient behavior in various edge cases.

- Return early if the requested read length is zero or if the file size (i_size) is zero.
- Initialize the index variable (idx) where needed and reorder some code to ensure it is always set before use.
- Improve error handling by checking for negative return values earlier.
- Remove redundant encrypted file checks after failures. Only attempt filesystem-level decryption if the read succeeded.
- Simplify leftover calculations to correctly handle cases where the read extends beyond the end of the file or stops short.
- This resolves multiple issues caused by integer overflow
- https://tracker.ceph.com/issues/67524
- https://tracker.ceph.com/issues/68981
- https://tracker.ceph.com/issues/68980

---
 fs/ceph/file.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)
fs/ceph/file.c +14 -15
index ce342a5d4b8b..8e0400d461a2 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 	if (ceph_inode_is_shutdown(inode))
 		return -EIO;
 
-	if (!len)
+	if (!len || !i_size)
 		return 0;
 	/*
 	 * flush any page cache pages in this range.  this
@@ -1086,7 +1086,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 		int num_pages;
 		size_t page_off;
 		bool more;
-		int idx;
+		int idx = 0;
 		size_t left;
 		struct ceph_osd_req_op *op;
 		u64 read_off = off;
@@ -1160,7 +1160,14 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 		else if (ret == -ENOENT)
 			ret = 0;
 
-		if (ret > 0 && IS_ENCRYPTED(inode)) {
+		if (ret < 0) {
+			ceph_osdc_put_request(req);
+			if (ret == -EBLOCKLISTED)
+				fsc->blocklisted = true;
+			break;
+		}
+
+		if (IS_ENCRYPTED(inode)) {
 			int fret;
 
 			fret = ceph_fscrypt_decrypt_extents(inode, pages,
@@ -1187,7 +1194,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 		}
 
 		/* Short read but not EOF? Zero out the remainder. */
-		if (ret >= 0 && ret < len && (off + ret < i_size)) {
+		if (ret < len && (off + ret < i_size)) {
 			int zlen = min(len - ret, i_size - off - ret);
 			int zoff = page_off + ret;
 
@@ -1197,13 +1204,11 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 			ret += zlen;
 		}
 
-		idx = 0;
-		if (ret <= 0)
-			left = 0;
-		else if (off + ret > i_size)
-			left = i_size - off;
+		if (off + ret > i_size)
+			left = (i_size > off) ? i_size - off : 0;
 		else
 			left = ret;
+
 		while (left > 0) {
 			size_t plen, copied;
 
@@ -1222,12 +1227,6 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 
 		ceph_osdc_put_request(req);
 
-		if (ret < 0) {
-			if (ret == -EBLOCKLISTED)
-				fsc->blocklisted = true;
-			break;
-		}
-
 		if (off >= i_size || !more)
 			break;
 	}
Signature · 1 line
2.34.1
#1 lore raw
Quoted 61 lines · Alex Markuze

This patch refines the read logic in __ceph_sync_read() to ensure more
predictable and efficient behavior in various edge cases.

- Return early if the requested read length is zero or if the file size
  (i_size) is zero.
- Initialize the index variable (idx) where needed and reorder some code to ensure it is always set before use.
- Improve error handling by checking for negative return values earlier.
- Remove redundant encrypted file checks after failures. Only attempt filesystem-level decryption if the read succeeded.
- Simplify leftover calculations to correctly handle cases where the read
  extends beyond the end of the file or stops short.
- This resolves multiple issues caused by integer overflow -
https://tracker.ceph.com/issues/67524

  -
https://tracker.ceph.com/issues/68981

  -
https://tracker.ceph.com/issues/68980

---
 fs/ceph/file.c | 29 ++++++++++++++---------------

 1 file changed, 14 insertions(+), 15 deletions(-)
fs/ceph/file.c +0 -0
index ce342a5d4b8b..8e0400d461a2 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
  	if (ceph_inode_is_shutdown(inode))
  		return -EIO;

fs/ceph/file.c +1 -1
-	if (!len)
+	if (!len || !i_size)

  return 0;
  /*
  * flush any page cache pages in this range.  this

fs/ceph/file.c +1 -1
@@ -1086,7 +1086,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
  		int num_pages;
  		size_t page_off;
  		bool more;
-		int idx;
+		int idx = 0;
  		size_t left;
  		struct ceph_osd_req_op *op;

  u64 read_off = off;

fs/ceph/file.c +0 -0
@@ -1160,7 +1160,14 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
  		else if (ret == -ENOENT)
  			ret = 0;

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1163

Quoted 1 line
fs/ceph/file.c +0 -1
-		if (ret > 0 && IS_ENCRYPTED(inode)) {

The whole function contains multiple places of checking ret > 0 condition. Frankly speaking, it looks very weird. It is clear that it is effort to distinguish normal and erroneous execution flow. But, for my taste, it could be a ground for bugs. I have feelings that __ceph_sync_read() logic requires refactoring:

if (ret < 0) {
<report error and stop execution>
} else {
<continue normal execution flow>
}

What do you think?

Quoted 53 lines
fs/ceph/file.c +8 -0
+		if (ret < 0) {
+			ceph_osdc_put_request(req);
+			if (ret == -EBLOCKLISTED)
+				fsc->blocklisted = true;
+			break;
+		}
+
+		if (IS_ENCRYPTED(inode)) {

  int fret;

  fret = ceph_fscrypt_decrypt_extents(inode, pages,

fs/ceph/file.c +0 -0
@@ -1187,7 +1194,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
  		}


  /* Short read but not EOF? Zero out the remainder. */

fs/ceph/file.c +1 -1
-		if (ret >= 0 && ret < len && (off + ret < i_size)) {
+		if (ret < len && (off + ret < i_size)) {

  int zlen = min(len - ret, i_size - off - ret);
  int zoff = page_off + ret;

fs/ceph/file.c +0 -0
@@ -1197,13 +1204,11 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
  			ret += zlen;
  		}

fs/ceph/file.c +2 -5
-		idx = 0;
-		if (ret <= 0)
-			left = 0;
-		else if (off + ret > i_size)
-			left = i_size - off;
+		if (off + ret > i_size)
+			left = (i_size > off) ? i_size - off : 0;

  else
  left = ret;
+
  while (left > 0) {
  size_t plen, copied;

fs/ceph/file.c +0 -0
@@ -1222,12 +1227,6 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,


  ceph_osdc_put_request(req);

fs/ceph/file.c +0 -6
-		if (ret < 0) {
-			if (ret == -EBLOCKLISTED)
-				fsc->blocklisted = true;
-			break;
-		}
-
Quoted 3 lines

  if (off >= i_size || !more)
  break;
  }

Mostly, cleanup looks good. But I have feelings that this function requires
more refactoring efforts.

Thanks,
Slava.

author maintainer
#2 lore raw

The main goal of this patch is to solve erroneous read sizes and overflows. The convoluted 'if else' chain is a recipe for disaster. Currently, exec stops immediately on first ret that indicates an error.
If you have additional refactoring thoughts feel free to add more patches, This is mainly a bug fix, that solves both the immediate overflow bug and attempts to make this code more manageable to mitigate future bugs.

Quoted 143 lines · Viacheslav Dubeyko
Quoted 57 lines · Alex Markuze

This patch refines the read logic in __ceph_sync_read() to ensure more
predictable and efficient behavior in various edge cases.

- Return early if the requested read length is zero or if the file size
(i_size) is zero.
- Initialize the index variable (idx) where needed and reorder some code to ensure it is always set before use.
- Improve error handling by checking for negative return values earlier.
- Remove redundant encrypted file checks after failures. Only attempt filesystem-level decryption if the read succeeded.
- Simplify leftover calculations to correctly handle cases where the read
extends beyond the end of the file or stops short.
- This resolves multiple issues caused by integer overflow -
https://tracker.ceph.com/issues/67524

-
https://tracker.ceph.com/issues/68981

-
https://tracker.ceph.com/issues/68980

---
 fs/ceph/file.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)
fs/ceph/file.c +1 -1
index ce342a5d4b8b..8e0400d461a2 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
      if (ceph_inode_is_shutdown(inode))
              return -EIO;
 
-     if (!len)
+     if (!len || !i_size)
              return 0;
      /*

* flush any page cache pages in this range. this

fs/ceph/file.c +1 -1
@@ -1086,7 +1086,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
              int num_pages;
              size_t page_off;
              bool more;
-             int idx;
+             int idx = 0;
              size_t left;
              struct ceph_osd_req_op *op;
              u64 read_off = off;

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, · line 1164

Quoted 5 lines
fs/ceph/file.c +0 -1
@@ -1160,7 +1160,14 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
              else if (ret == -ENOENT)
                      ret = 0;
 
-             if (ret > 0 && IS_ENCRYPTED(inode)) {

The whole function contains multiple places of checking ret > 0 condition. Frankly speaking, it looks very weird. It is clear that it is effort to distinguish normal and erroneous execution flow. But, for my taste, it could be a ground for bugs. I have feelings that __ceph_sync_read() logic requires refactoring:

if (ret < 0) {
<report error and stop execution>
} else {
<continue normal execution flow>
}

What do you think?

Quoted 55 lines
fs/ceph/file.c +8 -0
+             if (ret < 0) {
+                     ceph_osdc_put_request(req);
+                     if (ret == -EBLOCKLISTED)
+                             fsc->blocklisted = true;
+                     break;
+             }
+
+             if (IS_ENCRYPTED(inode)) {
                      int fret;
 

fret = ceph_fscrypt_decrypt_extents(inode,
pages,

fs/ceph/file.c +1 -1
@@ -1187,7 +1194,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
              }
 
              /* Short read but not EOF? Zero out the remainder.
 */
-             if (ret >= 0 && ret < len && (off + ret < i_size)) {
+             if (ret < len && (off + ret < i_size)) {
                      int zlen = min(len - ret, i_size - off -
ret);
                      int zoff = page_off + ret;
fs/ceph/file.c +3 -11
@@ -1197,13 +1204,11 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                      ret += zlen;
              }
 
-             idx = 0;
-             if (ret <= 0)
-                     left = 0;
-             else if (off + ret > i_size)
-                     left = i_size - off;
+             if (off + ret > i_size)
+                     left = (i_size > off) ? i_size - off : 0;
              else
                      left = ret;
+
              while (left > 0) {
                      size_t plen, copied;
@@ -1222,12 +1227,6 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
 
              ceph_osdc_put_request(req);
 
-             if (ret < 0) {
-                     if (ret == -EBLOCKLISTED)
-                             fsc->blocklisted = true;
-                     break;
-             }
-
              if (off >= i_size || !more)
                      break;
Quoted 1 line
      }

Mostly, cleanup looks good. But I have feelings that this function requires
more refactoring efforts.

Thanks,
Slava.

#3 lore raw
Quoted 8 lines · Alex Markuze

The main goal of this patch is to solve erroneous read sizes and overflows.
The convoluted 'if else' chain is a recipe for disaster. Currently, exec stops immediately on first ret that indicates an error. If you have additional refactoring thoughts feel free to add more patches, This is mainly a bug fix, that solves both the immediate overflow bug and attempts to make this code more manageable to mitigate future bugs.

I see your point. I simply see several cases of ret > 0 check:

https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1150
https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1158
https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1163 <- your fix here
https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1192 <- your fix here too
https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1236

And there are places to check ret for negative values:

https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1160
https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1226

These checks distributes in the function's code, it could be confusing and, potentially, be the source of new bugs during modifications. I simply have feelings that this logic somehow requires refactoring to improve the execution flow. But if you would like not to do it, then I am OK with it.

Thanks,
Slava.

author maintainer
#4 lore raw

I agree this function needs work, there is a major performance issue in there as well. One step at a time.
Meanwhile I need this patch to be acked so It can move to the main branch, as it fixes multiple bugs seen in production.

Quoted 36 lines · Viacheslav Dubeyko
Quoted 8 lines · Alex Markuze

The main goal of this patch is to solve erroneous read sizes and overflows.
The convoluted 'if else' chain is a recipe for disaster. Currently, exec stops immediately on first ret that indicates an error. If you have additional refactoring thoughts feel free to add more patches, This is mainly a bug fix, that solves both the immediate overflow bug and attempts to make this code more manageable to mitigate future bugs.

I see your point. I simply see several cases of ret > 0 check:

https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1150
https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1158
https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1163 <- your fix here
https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1192 <- your fix here too
https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1236

And there are places to check ret for negative values:

https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1160
https://elixir.bootlin.com/linux/v6.12.3/source/fs/ceph/file.c#L1226

These checks distributes in the function's code, it could be confusing and, potentially, be the source of new bugs during modifications. I simply have feelings that this logic somehow requires refactoring to improve the execution flow. But if you would like not to do it, then I am OK with it.

Thanks,
Slava.

Reviewed-by
#5 lore raw
Quoted 5 lines · Alex Markuze

I agree this function needs work, there is a major performance issue in there as well. One step at a time.
Meanwhile I need this patch to be acked so It can move to the main branch, as it fixes multiple bugs seen in production.

Makes sense to me. The patch looks good.

Thanks,
Slava.