Documentation: adopt new coding style of type-aware kmalloc-family
Subsystems (13)
v2 cover Documentation: adopt new coding style of type-aware kmalloc-family
Update the documentation to reflect new type-aware kmalloc-family as suggested in commit 2932ba8d9c99 ("slab: Introduce kmalloc_obj() and family")
ptr = kmalloc(sizeof(*ptr), gfp);
-> ptr = kmalloc_obj(*ptr);
ptr = kmalloc(sizeof(struct some_obj_name), gfp);
-> ptr = kmalloc_obj(*ptr);
ptr = kzalloc(sizeof(*ptr), gfp);
-> ptr = kzalloc_obj(*ptr);
ptr = kmalloc_array(count, sizeof(*ptr), gfp);
-> ptr = kmalloc_objs(*ptr, count);
ptr = kcalloc(count, sizeof(*ptr), gfp);
-> ptr = kzalloc_objs(*ptr, count);
I have also thought about adding a few cases to checkpatch.pl, but this will take me more time, and i want to get this series finished.
[v1] -> [v2]:
put RCU/* in a seperate patch [Patch 2/3]
Remove GFP_KERNEL as suggested by https://lwn.net/Articles/1062856/deprecated.rst: change the argument gfp to optional [Patch 3/3]
PATCH v2 1/3 Documentation: adopt new coding style of type-aware kmalloc-family
Update the documentation to reflect new type-aware kmalloc-family as suggested in commit 2932ba8d9c99 ("slab: Introduce kmalloc_obj() and family")
ptr = kmalloc(sizeof(*ptr), gfp);
-> ptr = kmalloc_obj(*ptr);
ptr = kmalloc(sizeof(struct some_obj_name), gfp);
-> ptr = kmalloc_obj(*ptr);
ptr = kzalloc(sizeof(*ptr), gfp);
-> ptr = kzalloc_obj(*ptr);
ptr = kmalloc_array(count, sizeof(*ptr), gfp);
-> ptr = kmalloc_objs(*ptr, count);
ptr = kcalloc(count, sizeof(*ptr), gfp);
-> ptr = kzalloc_objs(*ptr, count);
--- Documentation/core-api/kref.rst | 4 ++-- Documentation/core-api/list.rst | 4 ++-- Documentation/core-api/memory-allocation.rst | 4 ++-- Documentation/driver-api/mailbox.rst | 4 ++-- Documentation/driver-api/media/v4l2-fh.rst | 2 +- Documentation/kernel-hacking/locking.rst | 4 ++-- Documentation/locking/locktypes.rst | 4 ++-- Documentation/process/coding-style.rst | 8 ++++---- .../sound/kernel-api/writing-an-alsa-driver.rst | 12 ++++++------ Documentation/spi/spi-summary.rst | 4 ++-- .../translations/it_IT/kernel-hacking/locking.rst | 4 ++-- .../translations/it_IT/locking/locktypes.rst | 4 ++-- .../translations/it_IT/process/coding-style.rst | 2 +- .../translations/sp_SP/process/coding-style.rst | 2 +- Documentation/translations/zh_CN/core-api/kref.rst | 4 ++-- .../translations/zh_CN/process/coding-style.rst | 2 +- .../zh_CN/video4linux/v4l2-framework.txt | 2 +- .../translations/zh_TW/process/coding-style.rst | 2 +- 18 files changed, 36 insertions(+), 36 deletions(-)
Documentation/core-api/kref.rst +2 -2
@@ -40,7 +40,7 @@ kref_init as so:: struct my_data *data; - data = kmalloc(sizeof(*data), GFP_KERNEL); + data = kmalloc_obj(*data); if (!data) return -ENOMEM; kref_init(&data->refcount);
@@ -100,7 +100,7 @@ thread to process:: int rv = 0; struct my_data *data; struct task_struct *task; - data = kmalloc(sizeof(*data), GFP_KERNEL); + data = kmalloc_obj(*data); if (!data) return -ENOMEM; kref_init(&data->refcount);
Documentation/core-api/list.rst +2 -2
@@ -112,7 +112,7 @@ list: /* State 1 */ - grock = kzalloc(sizeof(*grock), GFP_KERNEL); + grock = kzalloc_obj(*grock); if (!grock) return -ENOMEM; grock->name = "Grock";
@@ -123,7 +123,7 @@ list: /* State 2 */ - dimitri = kzalloc(sizeof(*dimitri), GFP_KERNEL); + dimitri = kzalloc_obj(*dimitri); if (!dimitri) return -ENOMEM; dimitri->name = "Dimitri";
Documentation/core-api/memory-allocation.rst +2 -2
@@ -135,7 +135,7 @@ Selecting memory allocator The most straightforward way to allocate memory is to use a function from the kmalloc() family. And, to be on the safe side it's best to use routines that set memory to zero, like kzalloc(). If you need to -allocate memory for an array, there are kmalloc_array() and kcalloc() +allocate memory for an array, there are kmalloc_objs() and kzalloc_objs() helpers. The helpers struct_size(), array_size() and array3_size() can be used to safely calculate object sizes without overflowing.
@@ -151,7 +151,7 @@ sizes, the alignment is guaranteed to be at least the largest power-of-two divisor of the size. Chunks allocated with kmalloc() can be resized with krealloc(). Similarly -to kmalloc_array(): a helper for resizing arrays is provided in the form of +to kmalloc_objs(): a helper for resizing arrays is provided in the form of krealloc_array(). For large allocations you can use vmalloc() and vzalloc(), or directly
Documentation/driver-api/mailbox.rst +2 -2
@@ -87,8 +87,8 @@ a message and a callback function to the API and return immediately). struct async_pkt ap; struct sync_pkt sp; - dc_sync = kzalloc(sizeof(*dc_sync), GFP_KERNEL); - dc_async = kzalloc(sizeof(*dc_async), GFP_KERNEL); + dc_sync = kzalloc_obj(*dc_sync); + dc_async = kzalloc_obj(*dc_async); /* Populate non-blocking mode client */ dc_async->cl.dev = &pdev->dev;
Documentation/driver-api/media/v4l2-fh.rst +1 -1
@@ -42,7 +42,7 @@ Example: ... - my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL); + my_fh = kzalloc_obj(*my_fh); ...
Documentation/kernel-hacking/locking.rst +2 -2
@@ -442,7 +442,7 @@ to protect the cache and all the objects within it. Here's the code:: { struct object *obj; - if ((obj = kmalloc(sizeof(*obj), GFP_KERNEL)) == NULL) + if ((obj = kmalloc_obj(*obj)) == NULL) return -ENOMEM; strscpy(obj->name, name, sizeof(obj->name));
@@ -517,7 +517,7 @@ which are taken away, and the ``+`` are lines which are added. struct object *obj; + unsigned long flags; - if ((obj = kmalloc(sizeof(*obj), GFP_KERNEL)) == NULL) + if ((obj = kmalloc_obj(*obj)) == NULL) return -ENOMEM; @@ -63,30 +64,33 @@ obj->id = id;
Documentation/locking/locktypes.rst +2 -2
@@ -498,7 +498,7 @@ allocating memory. Thus, on a non-PREEMPT_RT kernel the following code works perfectly:: raw_spin_lock(&lock); - p = kmalloc(sizeof(*p), GFP_ATOMIC); + p = kmalloc_obj(*p, GFP_ATOMIC); But this code fails on PREEMPT_RT kernels because the memory allocator is fully preemptible and therefore cannot be invoked from truly atomic
@@ -507,7 +507,7 @@ while holding normal non-raw spinlocks because they do not disable preemption on PREEMPT_RT kernels:: spin_lock(&lock); - p = kmalloc(sizeof(*p), GFP_ATOMIC); + p = kmalloc_obj(*p, GFP_ATOMIC); bit spinlocks
Documentation/process/coding-style.rst +4 -4
@@ -936,7 +936,7 @@ used. --------------------- The kernel provides the following general purpose memory allocators: -kmalloc(), kzalloc(), kmalloc_array(), kcalloc(), vmalloc(), and +kmalloc(), kzalloc(), kmalloc_objs(), kzalloc_objs(), vmalloc(), and vzalloc(). Please refer to the API documentation for further information about them. :ref:`Documentation/core-api/memory-allocation.rst <memory_allocation>`
@@ -945,7 +945,7 @@ The preferred form for passing a size of a struct is the following: .. code-block:: c - p = kmalloc(sizeof(*p), ...); + p = kmalloc_obj(*p, ...); The alternative form where struct name is spelled out hurts readability and introduces an opportunity for a bug when the pointer variable type is changed
@@ -959,13 +959,13 @@ The preferred form for allocating an array is the following: .. code-block:: c - p = kmalloc_array(n, sizeof(...), ...); + p = kmalloc_objs(*ptr, n, ...); The preferred form for allocating a zeroed array is the following: .. code-block:: c - p = kcalloc(n, sizeof(...), ...); + p = kzalloc_objs(*ptr, n, ...); Both forms check for overflow on the allocation size n * sizeof(...), and return NULL if that occurred.
Documentation/sound/kernel-api/writing-an-alsa-driver.rst +6 -6
@@ -266,7 +266,7 @@ to details explained in the following section. .... /* allocate a chip-specific data with zero filled */ - chip = kzalloc(sizeof(*chip), GFP_KERNEL); + chip = kzalloc_obj(*chip); if (chip == NULL) return -ENOMEM;
@@ -628,7 +628,7 @@ After allocating a card instance via :c:func:`snd_card_new()` err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 0, &card); ..... - chip = kzalloc(sizeof(*chip), GFP_KERNEL); + chip = kzalloc_obj(*chip); The chip record should have the field to hold the card pointer at least,
@@ -747,7 +747,7 @@ destructor and PCI entries. Example code is shown first, below:: return -ENXIO; } - chip = kzalloc(sizeof(*chip), GFP_KERNEL); + chip = kzalloc_obj(*chip); if (chip == NULL) { pci_disable_device(pci); return -ENOMEM;
@@ -1737,7 +1737,7 @@ callback:: { struct my_pcm_data *data; .... - data = kmalloc(sizeof(*data), GFP_KERNEL); + data = kmalloc_obj(*data); substream->runtime->private_data = data; .... }
@@ -3301,7 +3301,7 @@ You can then pass any pointer value to the ``private_data``. If you assign private data, you should define a destructor, too. The destructor function is set in the ``private_free`` field:: - struct mydata *p = kmalloc(sizeof(*p), GFP_KERNEL); + struct mydata *p = kmalloc_obj(*p); hw->private_data = p; hw->private_free = mydata_free;
@@ -3833,7 +3833,7 @@ chip data individually:: err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 0, &card); .... - chip = kzalloc(sizeof(*chip), GFP_KERNEL); + chip = kzalloc_obj(*chip); .... card->private_data = chip; ....
Documentation/spi/spi-summary.rst +2 -2
@@ -249,7 +249,7 @@ And SOC-specific utility code might look something like:: { struct mysoc_spi_data *pdata2; - pdata2 = kmalloc(sizeof *pdata2, GFP_KERNEL); + pdata2 = kmalloc_obj(*pdata2); *pdata2 = pdata; ... if (n == 2) {
@@ -373,7 +373,7 @@ a bus (appearing under /sys/class/spi_master). return -ENODEV; /* get memory for driver's per-chip state */ - chip = kzalloc(sizeof *chip, GFP_KERNEL); + chip = kzalloc(*chip); if (!chip) return -ENOMEM; spi_set_drvdata(spi, chip);
Documentation/translations/it_IT/kernel-hacking/locking.rst +2 -2
@@ -462,7 +462,7 @@ e tutti gli oggetti che contiene. Ecco il codice:: { struct object *obj; - if ((obj = kmalloc(sizeof(*obj), GFP_KERNEL)) == NULL) + if ((obj = kmalloc_obj(*obj)) == NULL) return -ENOMEM; strscpy(obj->name, name, sizeof(obj->name));
@@ -537,7 +537,7 @@ sono quelle rimosse, mentre quelle ``+`` sono quelle aggiunte. struct object *obj; + unsigned long flags; - if ((obj = kmalloc(sizeof(*obj), GFP_KERNEL)) == NULL) + if ((obj = kmalloc_obj(*obj)) == NULL) return -ENOMEM; @@ -63,30 +64,33 @@ obj->id = id;
Documentation/translations/it_IT/locking/locktypes.rst +2 -2
@@ -488,7 +488,7 @@ o rwlock_t. Per esempio, la sezione critica non deve fare allocazioni di memoria. Su un kernel non-PREEMPT_RT il seguente codice funziona perfettamente:: raw_spin_lock(&lock); - p = kmalloc(sizeof(*p), GFP_ATOMIC); + p = kmalloc_obj(*p, GFP_ATOMIC); Ma lo stesso codice non funziona su un kernel PREEMPT_RT perché l'allocatore di memoria può essere oggetto di prelazione e quindi non può essere chiamato in un
@@ -497,7 +497,7 @@ trattiene un blocco *non-raw* perché non disabilitano la prelazione sui kernel PREEMPT_RT:: spin_lock(&lock); - p = kmalloc(sizeof(*p), GFP_ATOMIC); + p = kmalloc_obj(*p, GFP_ATOMIC); bit spinlocks
Documentation/translations/it_IT/process/coding-style.rst +1 -1
@@ -943,7 +943,7 @@ Il modo preferito per passare la dimensione di una struttura è il seguente: .. code-block:: c - p = kmalloc(sizeof(*p), ...); + p = kmalloc_obj(*p, ...); La forma alternativa, dove il nome della struttura viene scritto interamente, peggiora la leggibilità e introduce possibili bachi quando il tipo di
Documentation/translations/sp_SP/process/coding-style.rst +1 -1
@@ -955,7 +955,7 @@ La forma preferida para pasar el tamaño de una estructura es la siguiente: .. code-block:: c - p = kmalloc(sizeof(*p), ...); + p = kmalloc_obj(*p, ...); La forma alternativa donde se deletrea el nombre de la estructura perjudica la legibilidad, y presenta una oportunidad para un error cuando se cambia
Documentation/translations/zh_CN/core-api/kref.rst +2 -2
@@ -52,7 +52,7 @@ kref可以出现在数据结构体中的任何地方。 struct my_data *data; - data = kmalloc(sizeof(*data), GFP_KERNEL); + data = kmalloc_obj(*data); if (!data) return -ENOMEM; kref_init(&data->refcount);
@@ -106,7 +106,7 @@ Kref规则 int rv = 0; struct my_data *data; struct task_struct *task; - data = kmalloc(sizeof(*data), GFP_KERNEL); + data = kmalloc_obj(*data); if (!data) return -ENOMEM; kref_init(&data->refcount);
Documentation/translations/zh_CN/process/coding-style.rst +1 -1
@@ -813,7 +813,7 @@ Documentation/translations/zh_CN/core-api/memory-allocation.rst 。 .. code-block:: c - p = kmalloc(sizeof(*p), ...); + p = kmalloc_obj(*p, ...); 另外一种传递方式中,sizeof 的操作数是结构体的名字,这样会降低可读性,并且可能 会引入 bug。有可能指针变量类型被改变时,而对应的传递给内存分配函数的 sizeof
Documentation/translations/zh_CN/video4linux/v4l2-framework.txt +1 -1
@@ -799,7 +799,7 @@ int my_open(struct file *file) ... - my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL); + my_fh = kzalloc_obj(*my_fh); ...
Documentation/translations/zh_TW/process/coding-style.rst +1 -1
@@ -827,7 +827,7 @@ Documentation/translations/zh_CN/core-api/memory-allocation.rst 。 .. code-block:: c - p = kmalloc(sizeof(*p), ...); + p = kmalloc_obj(*p, ...); 另外一種傳遞方式中,sizeof 的操作數是結構體的名字,這樣會降低可讀性,並且可能 會引入 bug。有可能指針變量類型被改變時,而對應的傳遞給內存分配函數的 sizeof
Signature · 1 line
2.53.0
PATCH v2 2/3 Documentation: RCU: adopt new coding style of type-aware kmalloc-family
In reply to Manuel EbnerUpdate Documentation/RCU/* to reflect new type-aware kmalloc-family as suggested in commit 2932ba8d9c99 ("slab: Introduce kmalloc_obj() and family")
ptr = kmalloc(sizeof(*ptr), gfp);
-> ptr = kmalloc_obj(*ptr);
--- Documentation/RCU/Design/Requirements/Requirements.rst | 6 +++--- Documentation/RCU/listRCU.rst | 2 +- Documentation/RCU/whatisRCU.rst | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
Documentation/RCU/Design/Requirements/Requirements.rst +3 -3
@@ -206,7 +206,7 @@ non-\ ``NULL``, locklessly accessing the ``->a`` and ``->b`` fields. 1 bool add_gp_buggy(int a, int b) 2 { - 3 p = kmalloc(sizeof(*p), GFP_KERNEL); + 3 p = kmalloc_obj(*p); 4 if (!p) 5 return -ENOMEM; 6 spin_lock(&gp_lock);
@@ -228,7 +228,7 @@ their rights to reorder this code as follows: 1 bool add_gp_buggy_optimized(int a, int b) 2 { - 3 p = kmalloc(sizeof(*p), GFP_KERNEL); + 3 p = kmalloc_obj(*p); 4 if (!p) 5 return -ENOMEM; 6 spin_lock(&gp_lock);
@@ -264,7 +264,7 @@ shows an example of insertion: 1 bool add_gp(int a, int b) 2 { - 3 p = kmalloc(sizeof(*p), GFP_KERNEL); + 3 p = kmalloc_obj(*p); 4 if (!p) 5 return -ENOMEM; 6 spin_lock(&gp_lock);
Documentation/RCU/listRCU.rst +1 -1
@@ -276,7 +276,7 @@ The RCU version of audit_upd_rule() is as follows:: list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { - ne = kmalloc(sizeof(*entry), GFP_ATOMIC); + ne = kmalloc_obj(*entry, GFP_ATOMIC); if (ne == NULL) return -ENOMEM; audit_copy_rule(&ne->rule, &e->rule);
Documentation/RCU/whatisRCU.rst +2 -2
@@ -468,7 +468,7 @@ uses of RCU may be found in listRCU.rst and NMI-RCU.rst. struct foo *new_fp; struct foo *old_fp; - new_fp = kmalloc(sizeof(*new_fp), GFP_KERNEL); + new_fp = kmalloc_obj(*new_fp); spin_lock(&foo_mutex); old_fp = rcu_dereference_protected(gbl_foo, lockdep_is_held(&foo_mutex)); *new_fp = *old_fp;
@@ -570,7 +570,7 @@ The foo_update_a() function might then be written as follows:: struct foo *new_fp; struct foo *old_fp; - new_fp = kmalloc(sizeof(*new_fp), GFP_KERNEL); + new_fp = kmalloc_obj(*new_fp); spin_lock(&foo_mutex); old_fp = rcu_dereference_protected(gbl_foo, lockdep_is_held(&foo_mutex)); *new_fp = *old_fp;
Signature · 1 line
2.53.0
PATCH v2 3/3 Documentation: deprecated.rst: type-aware kmalloc-family: mark optional argument as such
In reply to Manuel Ebnerput the optional argument (gfp) in square brackets
eg. ptr = kmalloc_obj(*ptr, gfp);
-> ptr = kmalloc_obj(*ptr, [gfp]);
--- Documentation/process/deprecated.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
Documentation/process/deprecated.rst +6 -6
@@ -392,12 +392,12 @@ allocations. For example, these open coded assignments:: become, respectively:: - ptr = kmalloc_obj(*ptr, gfp); - ptr = kzalloc_obj(*ptr, gfp); - ptr = kmalloc_objs(*ptr, count, gfp); - ptr = kzalloc_objs(*ptr, count, gfp); - ptr = kmalloc_flex(*ptr, flex_member, count, gfp); - __auto_type ptr = kmalloc_obj(struct foo, gfp); + ptr = kmalloc_obj(*ptr, [gfp]); + ptr = kzalloc_obj(*ptr, [gfp]); + ptr = kmalloc_objs(*ptr, count, [gfp]); + ptr = kzalloc_objs(*ptr, count, [gfp]); + ptr = kmalloc_flex(*ptr, flex_member, count, [gfp]); + __auto_type ptr = kmalloc_obj(struct foo, [gfp]); If `ptr->flex_member` is annotated with __counted_by(), the allocation will automatically fail if `count` is larger than the maximum
Signature · 1 line
2.53.0