Skip to content

Commit 385d838

Browse files
jankaraakpm00
authored andcommitted
mm: avoid overflows in dirty throttling logic
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/[email protected] Signed-off-by: Jan Kara <[email protected]> Reported-by: Zach O'Keefe <[email protected]> Reviewed-By: Zach O'Keefe <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 30139c7 commit 385d838

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
@@ -415,13 +415,20 @@ static void domain_dirty_limits(struct dirty_throttle_control *dtc)
415415
else
416416
bg_thresh = (bg_ratio * available_memory) / PAGE_SIZE;
417417

418-
if (bg_thresh >= thresh)
419-
bg_thresh = thresh / 2;
420418
tsk = current;
421419
if (rt_task(tsk)) {
422420
bg_thresh += bg_thresh / 4 + global_wb_domain.dirty_limit / 32;
423421
thresh += thresh / 4 + global_wb_domain.dirty_limit / 32;
424422
}
423+
/*
424+
* Dirty throttling logic assumes the limits in page units fit into
425+
* 32-bits. This gives 16TB dirty limits max which is hopefully enough.
426+
*/
427+
if (thresh > UINT_MAX)
428+
thresh = UINT_MAX;
429+
/* This makes sure bg_thresh is within 32-bits as well */
430+
if (bg_thresh >= thresh)
431+
bg_thresh = thresh / 2;
425432
dtc->thresh = thresh;
426433
dtc->bg_thresh = bg_thresh;
427434

@@ -471,7 +478,11 @@ static unsigned long node_dirty_limit(struct pglist_data *pgdat)
471478
if (rt_task(tsk))
472479
dirty += dirty / 4;
473480

474-
return dirty;
481+
/*
482+
* Dirty throttling logic assumes the limits in page units fit into
483+
* 32-bits. This gives 16TB dirty limits max which is hopefully enough.
484+
*/
485+
return min_t(unsigned long, dirty, UINT_MAX);
475486
}
476487

477488
/**
@@ -508,10 +519,17 @@ static int dirty_background_bytes_handler(struct ctl_table *table, int write,
508519
void *buffer, size_t *lenp, loff_t *ppos)
509520
{
510521
int ret;
522+
unsigned long old_bytes = dirty_background_bytes;
511523

512524
ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
513-
if (ret == 0 && write)
525+
if (ret == 0 && write) {
526+
if (DIV_ROUND_UP(dirty_background_bytes, PAGE_SIZE) >
527+
UINT_MAX) {
528+
dirty_background_bytes = old_bytes;
529+
return -ERANGE;
530+
}
514531
dirty_background_ratio = 0;
532+
}
515533
return ret;
516534
}
517535

@@ -537,6 +555,10 @@ static int dirty_bytes_handler(struct ctl_table *table, int write,
537555

538556
ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
539557
if (ret == 0 && write && vm_dirty_bytes != old_bytes) {
558+
if (DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE) > UINT_MAX) {
559+
vm_dirty_bytes = old_bytes;
560+
return -ERANGE;
561+
}
540562
writeback_set_ratelimit();
541563
vm_dirty_ratio = 0;
542564
}

0 commit comments

Comments
 (0)