scripts/coccinelle/api/kvmalloc.cocci

Source file repositories/reference/linux-study-clean/scripts/coccinelle/api/kvmalloc.cocci

File Facts

System
Linux kernel
Corpus path
scripts/coccinelle/api/kvmalloc.cocci
Extension
.cocci
Size
6287 bytes
Lines
257
Domain
Support Tooling And Documentation
Bucket
scripts
Inferred role
Support Tooling And Documentation: scripts
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0-only
///
/// Find if/else condition with kmalloc/vmalloc calls.
/// Suggest to use kvmalloc instead. Same for kvfree.
///
// Confidence: High
// Copyright: (C) 2020 Denis Efremov ISPRAS
// Options: --no-includes --include-headers
//

virtual patch
virtual report
virtual org
virtual context

@initialize:python@
@@
filter = frozenset(['kvfree'])

def relevant(p):
    return not (filter & {el.current_element for el in p})

@kvmalloc depends on !patch@
expression E, E1, size;
identifier flags;
binary operator cmp = {<=, <, ==, >, >=};
identifier x;
type T;
position p;
@@

(
* if (size cmp E1 || ...)@p {
    ...
*    E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
*          kmalloc_array\|kmalloc_array_node\|kcalloc_node\)
*          (..., size, \(flags\|GFP_KERNEL\|\(GFP_KERNEL\|flags\)|__GFP_NOWARN\), ...)
    ...
  } else {
    ...
*    E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
    ...
  }
|
* E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
*       kmalloc_array\|kmalloc_array_node\|kcalloc_node\)
*       (..., size, \(flags\|GFP_KERNEL\|\(GFP_KERNEL\|flags\)|__GFP_NOWARN\), ...)
  ... when != E = E1
      when != size = E1
      when any
* if (E == NULL)@p {
    ...
*   E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
    ...
  }
|
* T x = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
*         kmalloc_array\|kmalloc_array_node\|kcalloc_node\)
*         (..., size, \(flags\|GFP_KERNEL\|\(GFP_KERNEL\|flags\)|__GFP_NOWARN\), ...);
  ... when != x = E1
      when != size = E1
      when any
* if (x == NULL)@p {
    ...
*   x = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
    ...
  }
)

@kvfree depends on !patch@

Annotation

Implementation Notes