Skip to content

Commit a25e853

Browse files
jankaragregkh
authored andcommitted
mm: avoid overflows in dirty throttling logic
commit 385d838 upstream. The dirty throttling logic is interspersed with assumptions that dirty limits in PAGE_SIZE units fit into 32-bit (so that various multiplications fit into 64-bits). If limits end up being larger, we will hit overflows, possible divisions by 0 etc. Fix these problems by never allowing so large dirty limits as they have dubious practical value anyway. For dirty_bytes / dirty_background_bytes interfaces we can just refuse to set so large limits. For dirty_ratio / dirty_background_ratio it isn't so simple as the dirty limit is computed from the amount of available memory which can change due to memory hotplug etc. So when converting dirty limits from ratios to numbers of pages, we just don't allow the result to exceed UINT_MAX. This is root-only triggerable problem which occurs when the operator sets dirty limits to >16 TB. Link: https://lkml.kernel.org/r/20240621144246.11148-2-jack@suse.cz Signed-off-by: Jan Kara <jack@suse.cz> Reported-by: Zach O'Keefe <zokeefe@google.com> Reviewed-By: Zach O'Keefe <zokeefe@google.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent df13f3c commit a25e853

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

mm/page-writeback.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,13 +426,20 @@ static void domain_dirty_limits(struct dirty_throttle_control *dtc)
426426
else
427427
bg_thresh = (bg_ratio * available_memory) / PAGE_SIZE;
428428

429-
if (bg_thresh >= thresh)
430-
bg_thresh = thresh / 2;
431429
tsk = current;
432430
if (rt_task(tsk)) {
433431
bg_thresh += bg_thresh / 4 + global_wb_domain.dirty_limit / 32;
434432
thresh += thresh / 4 + global_wb_domain.dirty_limit / 32;
435433
}
434+
/*
435+
* Dirty throttling logic assumes the limits in page units fit into
436+
* 32-bits. This gives 16TB dirty limits max which is hopefully enough.
437+
*/
438+
if (thresh > UINT_MAX)
439+
thresh = UINT_MAX;
440+
/* This makes sure bg_thresh is within 32-bits as well */
441+
if (bg_thresh >= thresh)
442+
bg_thresh = thresh / 2;
436443
dtc->thresh = thresh;
437444
dtc->bg_thresh = bg_thresh;
438445

@@ -482,7 +489,11 @@ static unsigned long node_dirty_limit(struct pglist_data *pgdat)
482489
if (rt_task(tsk))
483490
dirty += dirty / 4;
484491

485-
return dirty;
492+
/*
493+
* Dirty throttling logic assumes the limits in page units fit into
494+
* 32-bits. This gives 16TB dirty limits max which is hopefully enough.
495+
*/
496+
return min_t(unsigned long, dirty, UINT_MAX);
486497
}
487498

488499
/**
@@ -518,10 +529,17 @@ int dirty_background_bytes_handler(struct ctl_table *table, int write,
518529
void *buffer, size_t *lenp, loff_t *ppos)
519530
{
520531
int ret;
532+
unsigned long old_bytes = dirty_background_bytes;
521533

522534
ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
523-
if (ret == 0 && write)
535+
if (ret == 0 && write) {
536+
if (DIV_ROUND_UP(dirty_background_bytes, PAGE_SIZE) >
537+
UINT_MAX) {
538+
dirty_background_bytes = old_bytes;
539+
return -ERANGE;
540+
}
524541
dirty_background_ratio = 0;
542+
}
525543
return ret;
526544
}
527545

@@ -547,6 +565,10 @@ int dirty_bytes_handler(struct ctl_table *table, int write,
547565

548566
ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
549567
if (ret == 0 && write && vm_dirty_bytes != old_bytes) {
568+
if (DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE) > UINT_MAX) {
569+
vm_dirty_bytes = old_bytes;
570+
return -ERANGE;
571+
}
550572
writeback_set_ratelimit();
551573
vm_dirty_ratio = 0;
552574
}

0 commit comments

Comments
 (0)