Skip to contentSkip to conversation

mm: fix div by zero in bdi_ratio_from_pages

PATCH Superseded Stefan Roesch 2 participants 3 messages Jan 4, 2025 – Jan 8, 2025

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: fix div by zero in bdi_ratio_from_pages

During testing it has been detected, that it is possible to get div by zero error in bdi_set_min_bytes. The error is caused by the function bdi_ratio_from_pages(). bdi_ratio_from_pages() calls global_dirty_limits. If the dirty threshold is 0, the div by zero is raised. This can happen if the root user is setting:

echo 0 > /proc/sys/vm/dirty_ration.

The following is a test case:

echo 0 > /proc/sys/vm/dirty_ratio
cd /sys/class/bdi/<device>
echo 1 > strict_limit
echo 8192 > min_bytes

==> error is raised.

The problem is addressed by returning -EINVAL if dirty_ratio or dirty_bytes is set to 0.

---
 mm/page-writeback.c | 2 ++
 1 file changed, 2 insertions(+)
mm/page-writeback.c +2 -0
index d213ead95675..91aa7a5c0078 100644
@@ -692,6 +692,8 @@ static unsigned long bdi_ratio_from_pages(unsigned long pages)
 	unsigned long ratio;
 
 	global_dirty_limits(&background_thresh, &dirty_thresh);
+	if (!dirty_thresh)
+		return -EINVAL;
 	ratio = div64_u64(pages * 100ULL * BDI_RATIO_SCALE, dirty_thresh);
 
 	return ratio;

base-commit: 0bc21e701a6ffacfdde7f04f87d664d82e8a13bf

Signature · 1 line
2.47.1
#1 lore raw
Quoted 32 lines · Stefan Roesch

During testing it has been detected, that it is possible to get div by zero error in bdi_set_min_bytes. The error is caused by the function bdi_ratio_from_pages(). bdi_ratio_from_pages() calls global_dirty_limits. If the dirty threshold is 0, the div by zero is raised. This can happen if the root user is setting:

echo 0 > /proc/sys/vm/dirty_ration.

The following is a test case:

echo 0 > /proc/sys/vm/dirty_ratio
cd /sys/class/bdi/<device>
echo 1 > strict_limit
echo 8192 > min_bytes

==> error is raised.

The problem is addressed by returning -EINVAL if dirty_ratio or dirty_bytes is set to 0.

---
  mm/page-writeback.c | 2 ++
  1 file changed, 2 insertions(+)
mm/page-writeback.c +0 -0
index d213ead95675..91aa7a5c0078 100644

mm/page-writeback.c static unsigned long bdi_ratio_from_pages(unsigned long pages) · line 699

Quoted 8 lines
mm/page-writeback.c +2 -0
@@ -692,6 +692,8 @@ static unsigned long bdi_ratio_from_pages(unsigned long pages)
  	unsigned long ratio;
 
  	global_dirty_limits(&background_thresh, &dirty_thresh);
+	if (!dirty_thresh)
+		return -EINVAL;
  	ratio = div64_u64(pages * 100ULL * BDI_RATIO_SCALE, dirty_thresh);
 
  	return ratio;

bdi_set_min_bytes() calls bdi_ratio_from_pages() and passes the result to __bdi_set_min_ratio().

__bdi_set_min_ratio() expects an "unsigned int min_ratio". I assume this will work because "max_ratio > 100 * BDI_RATIO_SCALE", but it is rather confusing ...

Maybe we want something like:

/* Use 101% to indicate "invalid" */
#define BDI_RATIO_INVALID (101 * BDI_RATIO_SCALE)

Or alternatively, just handle it in the callers of bdi_ratio_from_pages(), checking for -EINVAL manually.

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
Quoted 39 lines · David Hildenbrand
Quoted 25 lines · Stefan Roesch

During testing it has been detected, that it is possible to get div by zero error in bdi_set_min_bytes. The error is caused by the function bdi_ratio_from_pages(). bdi_ratio_from_pages() calls global_dirty_limits. If the dirty threshold is 0, the div by zero is raised. This can happen if the root user is setting:
echo 0 > /proc/sys/vm/dirty_ration.
The following is a test case:
echo 0 > /proc/sys/vm/dirty_ratio
cd /sys/class/bdi/<device>
echo 1 > strict_limit
echo 8192 > min_bytes
==> error is raised.
The problem is addressed by returning -EINVAL if dirty_ratio or dirty_bytes is set to 0.

---
  mm/page-writeback.c | 2 ++
  1 file changed, 2 insertions(+)
mm/page-writeback.c +0 -0
index d213ead95675..91aa7a5c0078 100644

mm/page-writeback.c static unsigned long bdi_ratio_from_pages(unsigned long pages) · line 697

Quoted 6 lines
mm/page-writeback.c +2 -0
@@ -692,6 +692,8 @@ static unsigned long bdi_ratio_from_pages(unsigned long pages)
  	unsigned long ratio;
    	global_dirty_limits(&background_thresh, &dirty_thresh);
+	if (!dirty_thresh)
+		return -EINVAL;
  	ratio = div64_u64(pages * 100ULL * BDI_RATIO_SCALE, dirty_thresh);
    	return ratio;

bdi_set_min_bytes() calls bdi_ratio_from_pages() and passes the result to __bdi_set_min_ratio().

__bdi_set_min_ratio() expects an "unsigned int min_ratio". I assume this will work because "max_ratio > 100 * BDI_RATIO_SCALE", but it is rather confusing ...

Quoted 7 lines

Maybe we want something like:

/* Use 101% to indicate "invalid" */
#define BDI_RATIO_INVALID (101 * BDI_RATIO_SCALE)

Or alternatively, just handle it in the callers of bdi_ratio_from_pages(), checking for -EINVAL manually.

David, I prefer the second option, its a bit easier to follow.