Hasty Briefsbeta

Bilingual

A safer kmalloc() for Linux 7.0

7 hours ago
  • #kernel
  • #memory-management
  • #slab-allocator
  • kmalloc() is a general-purpose interface to the slab allocator for small memory chunks in the kernel.
  • The kmalloc() API has remained largely unchanged since its introduction in 1992, with minor type adjustments.
  • Common errors with kmalloc() include incorrect size allocation due to untyped return values and lack of size checks.
  • The 7.0 kernel introduces safer memory allocation functions like kmalloc_obj(), kmalloc_objs(), kzalloc_obj(), and kzalloc_objs().
  • New functions provide type safety, eliminate the need for sizeof(), and prevent common allocation mistakes.
  • kmalloc_flex() is introduced for structures with flexible array members to ensure correct sizing.
  • Large-scale patching converted much of the kernel code to use the new allocation functions.
  • Torvalds made the GFP_KERNEL argument optional in the new functions, defaulting to GFP_KERNEL when not specified.
  • The changes aim to reduce silly mistakes and improve code readability, though they may cause merge conflicts.