History

mm: introduce kv[mz]alloc helpers

Patch series "kvmalloc", v5. There are many open coded kmalloc with vmalloc fallback instances in the tree. Most of them are not careful enough or simply do not care about the underlying semantic of the kmalloc/page allocator which means that a) some vmalloc fallbacks are basically unreachable because the kmalloc part will keep retrying until it succeeds b) the page allocator can invoke a really disruptive steps like the OOM killer to move forward which doesn't sound appropriate when we consider that the vmalloc fallback is available. As it can be seen implementing kvmalloc requires quite an intimate knowledge if the page allocator and the memory reclaim internals which strongly suggests that a helper should be implemented in the memory subsystem proper. Most callers, I could find, have been converted to use the helper instead. This is patch 6. There are some more relying on __GFP_REPEAT in the networking stack which I have converted as well and Eric Dumazet was not opposed [2] to convert them as well. [1] http://lkml.kernel.org/r/20170130094940.13546-1-mhocko@kernel.org [2] http://lkml.kernel.org/r/1485273626.16328.301.camel@edumazet-glaptop3.roam.corp.google.com This patch (of 9): Using kmalloc with the vmalloc fallback for larger allocations is a common pattern in the kernel code. Yet we do not have any common helper for that and so users have invented their own helpers. Some of them are really creative when doing so. Let's just add kv[mz]alloc and make sure it is implemented properly. This implementation makes sure to not make a large memory pressure for > PAGE_SZE requests (__GFP_NORETRY) and also to not warn about allocation failures. This also rules out the OOM killer as the vmalloc is a more approapriate fallback than a disruptive user visible action. This patch also changes some existing users and removes helpers which are specific for them. In some cases this is not possible (e.g. ext4_kvmalloc, libcfs_kvzalloc) because those seems to be broken and require GFP_NO{FS,IO} context which is not vmalloc compatible in general (note that the page table allocation is GFP_KERNEL). Those need to be fixed separately. While we are at it, document that __vmalloc{_node} about unsupported gfp mask because there seems to be a lot of confusion out there. kvmalloc_node will warn about GFP_KERNEL incompatible (which are not superset) flags to catch new abusers. Existing ones would have to die slowly. [sfr@canb.auug.org.au: f2fs fixup] Link: http://lkml.kernel.org/r/20170320163735.332e64b7@canb.auug.org.au Link: http://lkml.kernel.org/r/20170306103032.2540-2-mhocko@kernel.org Signed-off-by: Michal Hocko <mhocko@suse.com> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au> Reviewed-by: Andreas Dilger <adilger@dilger.ca> [ext4 part] Acked-by: Vlastimil Babka <vbabka@suse.cz> Cc: John Hubbard <jhubbard@nvidia.com> Cc: David Miller <davem@davemloft.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Michal Hocko committed 9 years ago

Commit a7c3e901 parent 60f3e0024 files · +103 −130Browse files

Showing the changes to security (5 of 24 files). Show all

Unified · Split

5 files
  1. security/apparmor/apparmorfs.c +1 −1
  2. security/apparmor/include/lib.h +0 −11
  3. security/apparmor/lib.c +0 −30
  4. security/apparmor/match.c +1 −1
  5. security/apparmor/policy_unpack.c +1 −1
security/apparmor/apparmorfs.c+1−1
@@ -98,7 +98,7 @@ static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf, · 97 unmodified lines
9898 return ERR_PTR(-ESPIPE);
9999
100100 /* freed by caller to simple_write_to_buffer */
101− data = kvmalloc(sizeof(*data) + alloc_size);
101+ data = kvmalloc(sizeof(*data) + alloc_size, GFP_KERNEL);
102102 if (data == NULL)
103103 return ERR_PTR(-ENOMEM);
104104 kref_init(&data->count);
security/apparmor/include/lib.h+0−11
@@ -64,17 +64,6 @@ char *aa_split_fqname(char *args, char **ns_name); · 63 unmodified lines
6464 const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
6565 size_t *ns_len);
6666 void aa_info_message(const char *str);
67−void *__aa_kvmalloc(size_t size, gfp_t flags);
68−
69−static inline void *kvmalloc(size_t size)
70−{
71− return __aa_kvmalloc(size, 0);
72−}
73−
74−static inline void *kvzalloc(size_t size)
75−{
76− return __aa_kvmalloc(size, __GFP_ZERO);
77−}
7867
7968 /**
8069 * aa_strneq - compare null terminated @str to a non null terminated substring
security/apparmor/lib.c+0−30
@@ -128,36 +128,6 @@ void aa_info_message(const char *str) · 127 unmodified lines
128128 printk(KERN_INFO "AppArmor: %s\n", str);
129129 }
130130
131−/**
132− * __aa_kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
133− * @size: how many bytes of memory are required
134− * @flags: the type of memory to allocate (see kmalloc).
135− *
136− * Return: allocated buffer or NULL if failed
137− *
138− * It is possible that policy being loaded from the user is larger than
139− * what can be allocated by kmalloc, in those cases fall back to vmalloc.
140− */
141−void *__aa_kvmalloc(size_t size, gfp_t flags)
142−{
143− void *buffer = NULL;
144−
145− if (size == 0)
146− return NULL;
147−
148− /* do not attempt kmalloc if we need more than 16 pages at once */
149− if (size <= (16*PAGE_SIZE))
150− buffer = kmalloc(size, flags | GFP_KERNEL | __GFP_NORETRY |
151− __GFP_NOWARN);
152− if (!buffer) {
153− if (flags & __GFP_ZERO)
154− buffer = vzalloc(size);
155− else
156− buffer = vmalloc(size);
157− }
158− return buffer;
159−}
160−
161131 /**
162132 * aa_policy_init - initialize a policy structure
163133 * @policy: policy to initialize (NOT NULL)
security/apparmor/match.c+1−1
@@ -88,7 +88,7 @@ static struct table_header *unpack_table(char *blob, size_t bsize) · 87 unmodified lines
8888 if (bsize < tsize)
8989 goto out;
9090
91− table = kvzalloc(tsize);
91+ table = kvzalloc(tsize, GFP_KERNEL);
9292 if (table) {
9393 table->td_id = th.td_id;
9494 table->td_flags = th.td_flags;
security/apparmor/policy_unpack.c+1−1
@@ -487,7 +487,7 @@ fail: · 486 unmodified lines
487487
488488 static void *kvmemdup(const void *src, size_t len)
489489 {
490− void *p = kvmalloc(len);
490+ void *p = kvmalloc(len, GFP_KERNEL);
491491
492492 if (p)
493493 memcpy(p, src, len);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t