History

Merge tag 'kmalloc_obj-v7.0-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux

Pull kmalloc_obj updates from Kees Cook: "Introduce the kmalloc_obj* family of APIs for switching to type-based kmalloc allocations, away from purely size-based allocations. Discussed on lkml, with you, and at Linux Plumbers. It's been in -next for the entire dev cycle. Before the merge window closes, I'd like to send the treewide change (generated from the Coccinelle script included here), which mechanically converts almost 20k callsites from kmalloc* to kmalloc_obj*: 8007 files changed, 19980 insertions(+), 20838 deletions(-) This change needed fixes for mismatched types (since now the return type from allocations is a pointer to the requested type, not "void *"), and I've been fixing these over the last 4 releases. These fixes have mostly been trivial mismatches with const qualifiers or accidentally identical sizes (e.g. same object size: "struct kvec" vs "struct iovec", or differing pointers to pointers), but I did catch one case of too-small allocation. Summary: - Introduce kmalloc_obj*() family of type-based allocator APIs - checkpatch: Suggest kmalloc_obj family for sizeof allocations - coccinelle: Add kmalloc_objs conversion script" * tag 'kmalloc_obj-v7.0-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: coccinelle: Add kmalloc_objs conversion script slab: Introduce kmalloc_flex() and family compiler_types: Introduce __flex_counter() and family checkpatch: Suggest kmalloc_obj family for sizeof allocations slab: Introduce kmalloc_obj() and family

Linus Torvalds committed 9 months ago

Commit 958f7fb2 parents 85f24b0 bdc50716 files · +367 −6Browse files

Showing the changes to scripts/checkpatch.pl (1 of 6 files). Show all

Unified · Split

scripts/checkpatch.pl+33−6
@@ -7260,17 +7260,42 @@ sub process { · 7259 unmodified lines
7260 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);7260 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
7261 }7261 }
7262 7262
7263# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc7263# check for (kv|k)[mz]alloc that could be kmalloc_obj/kvmalloc_obj/kzalloc_obj/kvzalloc_obj
7264 if ($perl_version_ok &&
7265 defined $stat &&
7266 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*,/) {
7267 my $oldfunc = $3;
7268 my $a1 = $4;
7269 my $newfunc = "kmalloc_obj";
7270 $newfunc = "kvmalloc_obj" if ($oldfunc eq "kvmalloc");
7271 $newfunc = "kvzalloc_obj" if ($oldfunc eq "kvzalloc");
7272 $newfunc = "kzalloc_obj" if ($oldfunc eq "kzalloc");
7273
7274 if ($a1 =~ s/^sizeof\s*\S\(?([^\)]*)\)?$/$1/) {
7275 my $cnt = statement_rawlines($stat);
7276 my $herectx = get_stat_here($linenr, $cnt, $here);
7277
7278 if (WARN("ALLOC_WITH_SIZEOF",
7279 "Prefer $newfunc over $oldfunc with sizeof\n" . $herectx) &&
7280 $cnt == 1 &&
7281 $fix) {
7282 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*,/$1 = $newfunc($a1,/;
7283 }
7284 }
7285 }
7286
7287
7288# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_objs/kvmalloc_objs/kzalloc_objs/kvzalloc_objs
7264 if ($perl_version_ok &&7289 if ($perl_version_ok &&
7265 defined $stat &&7290 defined $stat &&
7266 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {7291 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
7267 my $oldfunc = $3;7292 my $oldfunc = $3;
7268 my $a1 = $4;7293 my $a1 = $4;
7269 my $a2 = $10;7294 my $a2 = $10;
7270 my $newfunc = "kmalloc_array";7295 my $newfunc = "kmalloc_objs";
7271 $newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");7296 $newfunc = "kvmalloc_objs" if ($oldfunc eq "kvmalloc");
7272 $newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");7297 $newfunc = "kvzalloc_objs" if ($oldfunc eq "kvzalloc");
7273 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");7298 $newfunc = "kzalloc_objs" if ($oldfunc eq "kzalloc");
7274 my $r1 = $a1;7299 my $r1 = $a1;
7275 my $r2 = $a2;7300 my $r2 = $a2;
7276 if ($a1 =~ /^sizeof\s*\S/) {7301 if ($a1 =~ /^sizeof\s*\S/) {
@@ -7286,7 +7311,9 @@ sub process { · 9 unmodified lines
7286 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&7311 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
7287 $cnt == 1 &&7312 $cnt == 1 &&
7288 $fix) {7313 $fix) {
7289 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;7314 my $sized = trim($r2);
7315 $sized =~ s/^sizeof\s*\S\(?([^\)]*)\)?$/$1/;
7316 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . $sized . ', ' . trim($r1)/e;
7290 }7317 }
7291 }7318 }
7292 }7319 }