Skip to content

Commit 2da0299

Browse files
rientjestorvalds
authored andcommitted
mm: add dirty_background_bytes and dirty_bytes sysctls
This change introduces two new sysctls to /proc/sys/vm: dirty_background_bytes and dirty_bytes. dirty_background_bytes is the counterpart to dirty_background_ratio and dirty_bytes is the counterpart to dirty_ratio. With growing memory capacities of individual machines, it's no longer sufficient to specify dirty thresholds as a percentage of the amount of dirtyable memory over the entire system. dirty_background_bytes and dirty_bytes specify quantities of memory, in bytes, that represent the dirty limits for the entire system. If either of these values is set, its value represents the amount of dirty memory that is needed to commence either background or direct writeback. When a `bytes' or `ratio' file is written, its counterpart becomes a function of the written value. For example, if dirty_bytes is written to be 8096, 8K of memory is required to commence direct writeback. dirty_ratio is then functionally equivalent to 8K / the amount of dirtyable memory: dirtyable_memory = free pages + mapped pages + file cache dirty_background_bytes = dirty_background_ratio * dirtyable_memory -or- dirty_background_ratio = dirty_background_bytes / dirtyable_memory AND dirty_bytes = dirty_ratio * dirtyable_memory -or- dirty_ratio = dirty_bytes / dirtyable_memory Only one of dirty_background_bytes and dirty_background_ratio may be specified at a time, and only one of dirty_bytes and dirty_ratio may be specified. When one sysctl is written, the other appears as 0 when read. The `bytes' files operate on a page size granularity since dirty limits are compared with ZVC values, which are in page units. Prior to this change, the minimum dirty_ratio was 5 as implemented by get_dirty_limits() although /proc/sys/vm/dirty_ratio would show any user written value between 0 and 100. This restriction is maintained, but dirty_bytes has a lower limit of only one page. Also prior to this change, the dirty_background_ratio could not equal or exceed dirty_ratio. This restriction is maintained in addition to restricting dirty_background_bytes. If either background threshold equals or exceeds that of the dirty threshold, it is implicitly set to half the dirty threshold. Acked-by: Peter Zijlstra <peterz@infradead.org> Cc: Dave Chinner <david@fromorbit.com> Cc: Christoph Lameter <cl@linux-foundation.org> Signed-off-by: David Rientjes <rientjes@google.com> Cc: Andrea Righi <righi.andrea@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 364aeb2 commit 2da0299

5 files changed

Lines changed: 146 additions & 23 deletions

File tree

‎Documentation/filesystems/proc.txt‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,15 @@ swapcache reclaim. Decreasing vfs_cache_pressure causes the kernel to prefer
13851385
to retain dentry and inode caches. Increasing vfs_cache_pressure beyond 100
13861386
causes the kernel to prefer to reclaim dentries and inodes.
13871387

1388+
dirty_background_bytes
1389+
----------------------
1390+
1391+
Contains the amount of dirty memory at which the pdflush background writeback
1392+
daemon will start writeback.
1393+
1394+
If dirty_background_bytes is written, dirty_background_ratio becomes a function
1395+
of its value (dirty_background_bytes / the amount of dirtyable system memory).
1396+
13881397
dirty_background_ratio
13891398
----------------------
13901399

@@ -1393,14 +1402,29 @@ pages + file cache, not including locked pages and HugePages), the number of
13931402
pages at which the pdflush background writeback daemon will start writing out
13941403
dirty data.
13951404

1405+
If dirty_background_ratio is written, dirty_background_bytes becomes a function
1406+
of its value (dirty_background_ratio * the amount of dirtyable system memory).
1407+
1408+
dirty_bytes
1409+
-----------
1410+
1411+
Contains the amount of dirty memory at which a process generating disk writes
1412+
will itself start writeback.
1413+
1414+
If dirty_bytes is written, dirty_ratio becomes a function of its value
1415+
(dirty_bytes / the amount of dirtyable system memory).
1416+
13961417
dirty_ratio
1397-
-----------------
1418+
-----------
13981419

13991420
Contains, as a percentage of the dirtyable system memory (free pages + mapped
14001421
pages + file cache, not including locked pages and HugePages), the number of
14011422
pages at which a process which is generating disk writes will itself start
14021423
writing out dirty data.
14031424

1425+
If dirty_ratio is written, dirty_bytes becomes a function of its value
1426+
(dirty_ratio * the amount of dirtyable system memory).
1427+
14041428
dirty_writeback_centisecs
14051429
-------------------------
14061430

‎Documentation/sysctl/vm.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ Currently, these files are in /proc/sys/vm:
4141

4242
==============================================================
4343

44-
dirty_ratio, dirty_background_ratio, dirty_expire_centisecs,
44+
dirty_bytes, dirty_ratio, dirty_background_bytes,
45+
dirty_background_ratio, dirty_expire_centisecs,
4546
dirty_writeback_centisecs, highmem_is_dirtyable,
4647
vfs_cache_pressure, laptop_mode, block_dump, swap_token_timeout,
4748
drop-caches, hugepages_treat_as_movable:

‎include/linux/writeback.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ void throttle_vm_writeout(gfp_t gfp_mask);
107107

108108
/* These are exported to sysctl. */
109109
extern int dirty_background_ratio;
110+
extern unsigned long dirty_background_bytes;
110111
extern int vm_dirty_ratio;
112+
extern unsigned long vm_dirty_bytes;
111113
extern int dirty_writeback_interval;
112114
extern int dirty_expire_interval;
113115
extern int vm_highmem_is_dirtyable;
@@ -116,9 +118,18 @@ extern int laptop_mode;
116118

117119
extern unsigned long determine_dirtyable_memory(void);
118120

121+
extern int dirty_background_ratio_handler(struct ctl_table *table, int write,
122+
struct file *filp, void __user *buffer, size_t *lenp,
123+
loff_t *ppos);
124+
extern int dirty_background_bytes_handler(struct ctl_table *table, int write,
125+
struct file *filp, void __user *buffer, size_t *lenp,
126+
loff_t *ppos);
119127
extern int dirty_ratio_handler(struct ctl_table *table, int write,
120128
struct file *filp, void __user *buffer, size_t *lenp,
121129
loff_t *ppos);
130+
extern int dirty_bytes_handler(struct ctl_table *table, int write,
131+
struct file *filp, void __user *buffer, size_t *lenp,
132+
loff_t *ppos);
122133

123134
struct ctl_table;
124135
struct file;

‎kernel/sysctl.c‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ extern int rcutorture_runnable;
8787
#endif /* #ifdef CONFIG_RCU_TORTURE_TEST */
8888

8989
/* Constants used for minimum and maximum */
90-
#if defined(CONFIG_HIGHMEM) || defined(CONFIG_DETECT_SOFTLOCKUP)
91-
static int one = 1;
92-
#endif
93-
9490
#ifdef CONFIG_DETECT_SOFTLOCKUP
9591
static int sixty = 60;
9692
static int neg_one = -1;
@@ -101,6 +97,7 @@ static int two = 2;
10197
#endif
10298

10399
static int zero;
100+
static int one = 1;
104101
static int one_hundred = 100;
105102

106103
/* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
@@ -952,11 +949,21 @@ static struct ctl_table vm_table[] = {
952949
.data = &dirty_background_ratio,
953950
.maxlen = sizeof(dirty_background_ratio),
954951
.mode = 0644,
955-
.proc_handler = &proc_dointvec_minmax,
952+
.proc_handler = &dirty_background_ratio_handler,
956953
.strategy = &sysctl_intvec,
957954
.extra1 = &zero,
958955
.extra2 = &one_hundred,
959956
},
957+
{
958+
.ctl_name = CTL_UNNUMBERED,
959+
.procname = "dirty_background_bytes",
960+
.data = &dirty_background_bytes,
961+
.maxlen = sizeof(dirty_background_bytes),
962+
.mode = 0644,
963+
.proc_handler = &dirty_background_bytes_handler,
964+
.strategy = &sysctl_intvec,
965+
.extra1 = &one,
966+
},
960967
{
961968
.ctl_name = VM_DIRTY_RATIO,
962969
.procname = "dirty_ratio",
@@ -968,6 +975,16 @@ static struct ctl_table vm_table[] = {
968975
.extra1 = &zero,
969976
.extra2 = &one_hundred,
970977
},
978+
{
979+
.ctl_name = CTL_UNNUMBERED,
980+
.procname = "dirty_bytes",
981+
.data = &vm_dirty_bytes,
982+
.maxlen = sizeof(vm_dirty_bytes),
983+
.mode = 0644,
984+
.proc_handler = &dirty_bytes_handler,
985+
.strategy = &sysctl_intvec,
986+
.extra1 = &one,
987+
},
971988
{
972989
.procname = "dirty_writeback_centisecs",
973990
.data = &dirty_writeback_interval,

‎mm/page-writeback.c‎

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ static inline long sync_writeback_pages(void)
6868
*/
6969
int dirty_background_ratio = 5;
7070

71+
/*
72+
* dirty_background_bytes starts at 0 (disabled) so that it is a function of
73+
* dirty_background_ratio * the amount of dirtyable memory
74+
*/
75+
unsigned long dirty_background_bytes;
76+
7177
/*
7278
* free highmem will not be subtracted from the total free memory
7379
* for calculating free ratios if vm_highmem_is_dirtyable is true
@@ -79,6 +85,12 @@ int vm_highmem_is_dirtyable;
7985
*/
8086
int vm_dirty_ratio = 10;
8187

88+
/*
89+
* vm_dirty_bytes starts at 0 (disabled) so that it is a function of
90+
* vm_dirty_ratio * the amount of dirtyable memory
91+
*/
92+
unsigned long vm_dirty_bytes;
93+
8294
/*
8395
* The interval between `kupdate'-style writebacks, in jiffies
8496
*/
@@ -135,23 +147,75 @@ static int calc_period_shift(void)
135147
{
136148
unsigned long dirty_total;
137149

138-
dirty_total = (vm_dirty_ratio * determine_dirtyable_memory()) / 100;
150+
if (vm_dirty_bytes)
151+
dirty_total = vm_dirty_bytes / PAGE_SIZE;
152+
else
153+
dirty_total = (vm_dirty_ratio * determine_dirtyable_memory()) /
154+
100;
139155
return 2 + ilog2(dirty_total - 1);
140156
}
141157

142158
/*
143-
* update the period when the dirty ratio changes.
159+
* update the period when the dirty threshold changes.
144160
*/
161+
static void update_completion_period(void)
162+
{
163+
int shift = calc_period_shift();
164+
prop_change_shift(&vm_completions, shift);
165+
prop_change_shift(&vm_dirties, shift);
166+
}
167+
168+
int dirty_background_ratio_handler(struct ctl_table *table, int write,
169+
struct file *filp, void __user *buffer, size_t *lenp,
170+
loff_t *ppos)
171+
{
172+
int ret;
173+
174+
ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
175+
if (ret == 0 && write)
176+
dirty_background_bytes = 0;
177+
return ret;
178+
}
179+
180+
int dirty_background_bytes_handler(struct ctl_table *table, int write,
181+
struct file *filp, void __user *buffer, size_t *lenp,
182+
loff_t *ppos)
183+
{
184+
int ret;
185+
186+
ret = proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos);
187+
if (ret == 0 && write)
188+
dirty_background_ratio = 0;
189+
return ret;
190+
}
191+
145192
int dirty_ratio_handler(struct ctl_table *table, int write,
146193
struct file *filp, void __user *buffer, size_t *lenp,
147194
loff_t *ppos)
148195
{
149196
int old_ratio = vm_dirty_ratio;
150-
int ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
197+
int ret;
198+
199+
ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
151200
if (ret == 0 && write && vm_dirty_ratio != old_ratio) {
152-
int shift = calc_period_shift();
153-
prop_change_shift(&vm_completions, shift);
154-
prop_change_shift(&vm_dirties, shift);
201+
update_completion_period();
202+
vm_dirty_bytes = 0;
203+
}
204+
return ret;
205+
}
206+
207+
208+
int dirty_bytes_handler(struct ctl_table *table, int write,
209+
struct file *filp, void __user *buffer, size_t *lenp,
210+
loff_t *ppos)
211+
{
212+
int old_bytes = vm_dirty_bytes;
213+
int ret;
214+
215+
ret = proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos);
216+
if (ret == 0 && write && vm_dirty_bytes != old_bytes) {
217+
update_completion_period();
218+
vm_dirty_ratio = 0;
155219
}
156220
return ret;
157221
}
@@ -365,23 +429,29 @@ void
365429
get_dirty_limits(unsigned long *pbackground, unsigned long *pdirty,
366430
unsigned long *pbdi_dirty, struct backing_dev_info *bdi)
367431
{
368-
int background_ratio; /* Percentages */
369-
int dirty_ratio;
370432
unsigned long background;
371433
unsigned long dirty;
372434
unsigned long available_memory = determine_dirtyable_memory();
373435
struct task_struct *tsk;
374436

375-
dirty_ratio = vm_dirty_ratio;
376-
if (dirty_ratio < 5)
377-
dirty_ratio = 5;
437+
if (vm_dirty_bytes)
438+
dirty = DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE);
439+
else {
440+
int dirty_ratio;
378441

379-
background_ratio = dirty_background_ratio;
380-
if (background_ratio >= dirty_ratio)
381-
background_ratio = dirty_ratio / 2;
442+
dirty_ratio = vm_dirty_ratio;
443+
if (dirty_ratio < 5)
444+
dirty_ratio = 5;
445+
dirty = (dirty_ratio * available_memory) / 100;
446+
}
447+
448+
if (dirty_background_bytes)
449+
background = DIV_ROUND_UP(dirty_background_bytes, PAGE_SIZE);
450+
else
451+
background = (dirty_background_ratio * available_memory) / 100;
382452

383-
background = (background_ratio * available_memory) / 100;
384-
dirty = (dirty_ratio * available_memory) / 100;
453+
if (background >= dirty)
454+
background = dirty / 2;
385455
tsk = current;
386456
if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
387457
background += background / 4;

0 commit comments

Comments
 (0)