Skip to contentSkip to conversation

seq_file: account everything

PATCH In review Alexey Dobriyan 2 participants 2 messages Mar 10, 2018 – Mar 16, 2018 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 seq_file: account everything

All it takes to open a file and read 1 byte from it.

seq_file will be allocated along with any private allocations, and more importantly seq file buffer which is 1 page by default.

---

 fs/seq_file.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/seq_file.c +4 -4
@@ -29,7 +29,7 @@ static void seq_set_overflow(struct seq_file *m)
 
 static void *seq_buf_alloc(unsigned long size)
 {
-	return kvmalloc(size, GFP_KERNEL);
+	return kvmalloc(size, GFP_KERNEL_ACCOUNT);
 }
 
 /**
@@ -566,7 +566,7 @@ static void single_stop(struct seq_file *p, void *v)
 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
 		void *data)
 {
-	struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
+	struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
 	int res = -ENOMEM;
 
 	if (op) {
@@ -628,7 +628,7 @@ void *__seq_open_private(struct file *f, const struct seq_operations *ops,
 	void *private;
 	struct seq_file *seq;
 
-	private = kzalloc(psize, GFP_KERNEL);
+	private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
 	if (private == NULL)
 		goto out;
 
@@ -1112,5 +1112,5 @@ EXPORT_SYMBOL(seq_hlist_next_percpu);
 
 void __init seq_file_init(void)
 {
-	seq_file_cache = KMEM_CACHE(seq_file, SLAB_PANIC);
+	seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
 }
Acked-by on v1 2/2
#1 lore raw
Quoted 4 lines · Alexey Dobriyan

All it takes to open a file and read 1 byte from it.

seq_file will be allocated along with any private allocations, and more importantly seq file buffer which is 1 page by default.

OOM badness heuristic will not know about this memory when choosing an oom victim so we might kill a wrong victim which is not all that great but at least the consumption will be contained within a memcg which sounds like the sufficient protection for now. This is the case basically for all kmem charges...

The seq file life time should be bound to a process life time so this is OK.

Quoted 1 line
Quoted 41 lines

---

 fs/seq_file.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/seq_file.c +4 -4
@@ -29,7 +29,7 @@ static void seq_set_overflow(struct seq_file *m)
 
 static void *seq_buf_alloc(unsigned long size)
 {
-	return kvmalloc(size, GFP_KERNEL);
+	return kvmalloc(size, GFP_KERNEL_ACCOUNT);
 }
 
 /**
@@ -566,7 +566,7 @@ static void single_stop(struct seq_file *p, void *v)
 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
 		void *data)
 {
-	struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
+	struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
 	int res = -ENOMEM;
 
 	if (op) {
@@ -628,7 +628,7 @@ void *__seq_open_private(struct file *f, const struct seq_operations *ops,
 	void *private;
 	struct seq_file *seq;
 
-	private = kzalloc(psize, GFP_KERNEL);
+	private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
 	if (private == NULL)
 		goto out;
 
@@ -1112,5 +1112,5 @@ EXPORT_SYMBOL(seq_hlist_next_percpu);
 
 void __init seq_file_init(void)
 {
-	seq_file_cache = KMEM_CACHE(seq_file, SLAB_PANIC);
+	seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
 }
Signature · 2 lines
Michal Hocko
SUSE Labs