Skip to contentSkip to conversation

ceph: fix writeback_count leak in write_folio_nounlock()

PATCH Superseded Wentao Liang 2 participants 2 messages Jun 3

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 ceph: fix writeback_count leak in write_folio_nounlock()

write_folio_nounlock() increments fsc->writeback_count to track in-flight writeback operations. On several error paths where the function returns early (folio lookup failure, snapshot context allocation failure, and writepages submission failure), the function returns without calling atomic_long_dec_return() to decrement the counter.

Each leaked increment keeps the counter above zero, which can prevent the filesystem from cleanly unmounting or suspending writes.

Add atomic_long_dec_return() calls on all error paths that currently return without decrementing the counter.

Fixes d55207717ded ("ceph: add encryption support to writepage and writepages")

---
Changes in v3:
- Also clear write_congested flag when decrementing writeback_count on error paths, as suggested by Viacheslav Dubeyko.
- Fix typo error.

---
 fs/ceph/addr.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/ceph/addr.c +9 -3
index a606378649c3..7fab73874068 100644
@@ -790,7 +790,9 @@ static int write_folio_nounlock(struct folio *folio,
 				    ceph_wbc.truncate_size, true);
 	if (IS_ERR(req)) {
 		folio_redirty_for_writepage(wbc, folio);
-		atomic_long_dec(&fsc->writeback_count);
+		if (atomic_long_dec_return(&fsc->writeback_count) <
+				CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
+			fsc->write_congested = false;
 		return PTR_ERR(req);
 	}
 
@@ -810,7 +812,9 @@ static int write_folio_nounlock(struct folio *folio,
 			folio_redirty_for_writepage(wbc, folio);
 			folio_end_writeback(folio);
 			ceph_osdc_put_request(req);
-			atomic_long_dec(&fsc->writeback_count);
+			if (atomic_long_dec_return(&fsc->writeback_count) <
+					CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
+				fsc->write_congested = false;
 			return PTR_ERR(bounce_page);
 		}
 	}
@@ -849,7 +853,9 @@ static int write_folio_nounlock(struct folio *folio,
 			      ceph_vinop(inode), folio);
 			folio_redirty_for_writepage(wbc, folio);
 			folio_end_writeback(folio);
-			atomic_long_dec_return(&fsc->writeback_count);
+			if (atomic_long_dec_return(&fsc->writeback_count) <
+					CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
+				fsc->write_congested = false;
 			return err;
 		}
 		if (err == -EBLOCKLISTED)
Signature · 1 line
2.34.1
#1 lore raw
Quoted 32 lines · Wentao Liang

write_folio_nounlock() increments fsc->writeback_count to track in-flight writeback operations. On several error paths where the function returns early (folio lookup failure, snapshot context allocation failure, and writepages submission failure), the function returns without calling atomic_long_dec_return() to decrement the counter.

Each leaked increment keeps the counter above zero, which can prevent the filesystem from cleanly unmounting or suspending writes.

Add atomic_long_dec_return() calls on all error paths that currently return without decrementing the counter.

Fixes d55207717ded ("ceph: add encryption support to writepage and

writepages")

---
Changes in v3:
- Also clear write_congested flag when decrementing writeback_count on error paths, as suggested by Viacheslav Dubeyko.
- Fix typo error.
---
 fs/ceph/addr.c | 12 +++++++++---

 1 file changed, 9 insertions(+), 3 deletions(-)
fs/ceph/addr.c +0 -0
index a606378649c3..7fab73874068 100644

fs/ceph/addr.c static int write_folio_nounlock(struct folio · line 794

Quoted 5 lines
fs/ceph/addr.c +0 -1
@@ -790,7 +790,9 @@ static int write_folio_nounlock(struct folio
 *folio,
  				    ceph_wbc.truncate_size, true);
  	if (IS_ERR(req)) {
  		folio_redirty_for_writepage(wbc, folio);
-		atomic_long_dec(&fsc->writeback_count);

Patch is not correctly prepared. Because, current state of the code hasn't atomic_long_dec().

Thanks,
Slava.

Quoted 36 lines
fs/ceph/addr.c +3 -0
+		if (atomic_long_dec_return(&fsc->writeback_count) <
+				CONGESTION_OFF_THRESH(fsc-
 >mount_options->congestion_kb))
+			fsc->write_congested = false;
  		return PTR_ERR(req);

  }

fs/ceph/addr.c +1 -1
@@ -810,7 +812,9 @@ static int write_folio_nounlock(struct folio
 *folio,
  			folio_redirty_for_writepage(wbc, folio);
  			folio_end_writeback(folio);
  			ceph_osdc_put_request(req);
-			atomic_long_dec(&fsc->writeback_count);
+			if (atomic_long_dec_return(&fsc-
Quoted 1 line

writeback_count) <

fs/ceph/addr.c +1 -0
+					CONGESTION_OFF_THRESH(fsc-
Quoted 1 line

mount_options->congestion_kb))

fs/ceph/addr.c +1 -0
+				fsc->write_congested = false;

  return PTR_ERR(bounce_page); }
  }

fs/ceph/addr.c +1 -1
@@ -849,7 +853,9 @@ static int write_folio_nounlock(struct folio
 *folio,
  			      ceph_vinop(inode), folio);
  			folio_redirty_for_writepage(wbc, folio);
  			folio_end_writeback(folio);
-			atomic_long_dec_return(&fsc-
 >writeback_count);
+			if (atomic_long_dec_return(&fsc-
Quoted 1 line

writeback_count) <

fs/ceph/addr.c +1 -0
+					CONGESTION_OFF_THRESH(fsc-
Quoted 1 line

mount_options->congestion_kb))

fs/ceph/addr.c +1 -0
+				fsc->write_congested = false;

  return err;
  }
  if (err == -EBLOCKLISTED)