linux-loongson/include/linux/util_macros.h
Linus Torvalds 7d4e49a77d - The 3 patch series "hung_task: extend blocking task stacktrace dump to
semaphore" from Lance Yang enhances the hung task detector.  The
   detector presently dumps the blocking tasks's stack when it is blocked
   on a mutex.  Lance's series extends this to semaphores.
 
 - The 2 patch series "nilfs2: improve sanity checks in dirty state
   propagation" from Wentao Liang addresses a couple of minor flaws in
   nilfs2.
 
 - The 2 patch series "scripts/gdb: Fixes related to lx_per_cpu()" from
   Illia Ostapyshyn fixes a couple of issues in the gdb scripts.
 
 - The 9 patch series "Support kdump with LUKS encryption by reusing LUKS
   volume keys" from Coiby Xu addresses a usability problem with kdump.
   When the dump device is LUKS-encrypted, the kdump kernel may not have
   the keys to the encrypted filesystem.  A full writeup of this is in the
   series [0/N] cover letter.
 
 - The 2 patch series "sysfs: add counters for lockups and stalls" from
   Max Kellermann adds /sys/kernel/hardlockup_count and
   /sys/kernel/hardlockup_count and /sys/kernel/rcu_stall_count.
 
 - The 3 patch series "fork: Page operation cleanups in the fork code"
   from Pasha Tatashin implements a number of code cleanups in fork.c.
 
 - The 3 patch series "scripts/gdb/symbols: determine KASLR offset on
   s390 during early boot" from Ilya Leoshkevich fixes some s390 issues in
   the gdb scripts.
 -----BEGIN PGP SIGNATURE-----
 
 iHUEABYKAB0WIQTTMBEPP41GrTpTJgfdBJ7gKXxAjgUCaDuCvQAKCRDdBJ7gKXxA
 jrkxAQCnFAp/uK9ckkbN4nfpJ0+OMY36C+A+dawSDtuRsIkXBAEAq3e6MNAUdg5W
 Ca0cXdgSIq1Op7ZKEA+66Km6Rfvfow8=
 =g45L
 -----END PGP SIGNATURE-----

Merge tag 'mm-nonmm-stable-2025-05-31-15-28' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Pull non-MM updates from Andrew Morton:

 - "hung_task: extend blocking task stacktrace dump to semaphore" from
   Lance Yang enhances the hung task detector.

   The detector presently dumps the blocking tasks's stack when it is
   blocked on a mutex. Lance's series extends this to semaphores

 - "nilfs2: improve sanity checks in dirty state propagation" from
   Wentao Liang addresses a couple of minor flaws in nilfs2

 - "scripts/gdb: Fixes related to lx_per_cpu()" from Illia Ostapyshyn
   fixes a couple of issues in the gdb scripts

 - "Support kdump with LUKS encryption by reusing LUKS volume keys" from
   Coiby Xu addresses a usability problem with kdump.

   When the dump device is LUKS-encrypted, the kdump kernel may not have
   the keys to the encrypted filesystem. A full writeup of this is in
   the series [0/N] cover letter

 - "sysfs: add counters for lockups and stalls" from Max Kellermann adds
   /sys/kernel/hardlockup_count and /sys/kernel/hardlockup_count and
   /sys/kernel/rcu_stall_count

 - "fork: Page operation cleanups in the fork code" from Pasha Tatashin
   implements a number of code cleanups in fork.c

 - "scripts/gdb/symbols: determine KASLR offset on s390 during early
   boot" from Ilya Leoshkevich fixes some s390 issues in the gdb
   scripts

* tag 'mm-nonmm-stable-2025-05-31-15-28' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (67 commits)
  llist: make llist_add_batch() a static inline
  delayacct: remove redundant code and adjust indentation
  squashfs: add optional full compressed block caching
  crash_dump, nvme: select CONFIGFS_FS as built-in
  scripts/gdb/symbols: determine KASLR offset on s390 during early boot
  scripts/gdb/symbols: factor out pagination_off()
  scripts/gdb/symbols: factor out get_vmlinux()
  kernel/panic.c: format kernel-doc comments
  mailmap: update and consolidate Casey Connolly's name and email
  nilfs2: remove wbc->for_reclaim handling
  fork: define a local GFP_VMAP_STACK
  fork: check charging success before zeroing stack
  fork: clean-up naming of vm_stack/vm_struct variables in vmap stacks code
  fork: clean-up ifdef logic around stack allocation
  kernel/rcu/tree_stall: add /sys/kernel/rcu_stall_count
  kernel/watchdog: add /sys/kernel/{hard,soft}lockup_count
  x86/crash: make the page that stores the dm crypt keys inaccessible
  x86/crash: pass dm crypt keys to kdump kernel
  Revert "x86/mm: Remove unused __set_memory_prot()"
  crash_dump: retrieve dm crypt keys in kdump kernel
  ...
2025-05-31 19:12:53 -07:00

164 lines
5.2 KiB
C

/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _LINUX_HELPER_MACROS_H_
#define _LINUX_HELPER_MACROS_H_
#include <linux/compiler_attributes.h>
#include <linux/math.h>
#include <linux/typecheck.h>
#include <linux/stddef.h>
/**
* for_each_if - helper for handling conditionals in various for_each macros
* @condition: The condition to check
*
* Typical use::
*
* #define for_each_foo_bar(x, y) \'
* list_for_each_entry(x, y->list, head) \'
* for_each_if(x->something == SOMETHING)
*
* The for_each_if() macro makes the use of for_each_foo_bar() less error
* prone.
*/
#define for_each_if(condition) if (!(condition)) {} else
/**
* find_closest - locate the closest element in a sorted array
* @x: The reference value.
* @a: The array in which to look for the closest element. Must be sorted
* in ascending order.
* @as: Size of 'a'.
*
* Returns the index of the element closest to 'x'.
* Note: If using an array of negative numbers (or mixed positive numbers),
* then be sure that 'x' is of a signed-type to get good results.
*/
#define find_closest(x, a, as) \
({ \
typeof(as) __fc_i, __fc_as = (as) - 1; \
long __fc_mid_x, __fc_x = (x); \
long __fc_left, __fc_right; \
typeof(*a) const *__fc_a = (a); \
for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \
__fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i + 1]) / 2; \
if (__fc_x <= __fc_mid_x) { \
__fc_left = __fc_x - __fc_a[__fc_i]; \
__fc_right = __fc_a[__fc_i + 1] - __fc_x; \
if (__fc_right < __fc_left) \
__fc_i++; \
break; \
} \
} \
(__fc_i); \
})
/**
* find_closest_descending - locate the closest element in a sorted array
* @x: The reference value.
* @a: The array in which to look for the closest element. Must be sorted
* in descending order.
* @as: Size of 'a'.
*
* Similar to find_closest() but 'a' is expected to be sorted in descending
* order. The iteration is done in reverse order, so that the comparison
* of '__fc_right' & '__fc_left' also works for unsigned numbers.
*/
#define find_closest_descending(x, a, as) \
({ \
typeof(as) __fc_i, __fc_as = (as) - 1; \
long __fc_mid_x, __fc_x = (x); \
long __fc_left, __fc_right; \
typeof(*a) const *__fc_a = (a); \
for (__fc_i = __fc_as; __fc_i >= 1; __fc_i--) { \
__fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i - 1]) / 2; \
if (__fc_x <= __fc_mid_x) { \
__fc_left = __fc_x - __fc_a[__fc_i]; \
__fc_right = __fc_a[__fc_i - 1] - __fc_x; \
if (__fc_right < __fc_left) \
__fc_i--; \
break; \
} \
} \
(__fc_i); \
})
/**
* PTR_IF - evaluate to @ptr if @cond is true, or to NULL otherwise.
* @cond: A conditional, usually in a form of IS_ENABLED(CONFIG_FOO)
* @ptr: A pointer to assign if @cond is true.
*
* PTR_IF(IS_ENABLED(CONFIG_FOO), ptr) evaluates to @ptr if CONFIG_FOO is set
* to 'y' or 'm', or to NULL otherwise. The @ptr argument must be a pointer.
*
* The macro can be very useful to help compiler dropping dead code.
*
* For instance, consider the following::
*
* #ifdef CONFIG_FOO_SUSPEND
* static int foo_suspend(struct device *dev)
* {
* ...
* }
* #endif
*
* static struct pm_ops foo_ops = {
* #ifdef CONFIG_FOO_SUSPEND
* .suspend = foo_suspend,
* #endif
* };
*
* While this works, the foo_suspend() macro is compiled conditionally,
* only when CONFIG_FOO_SUSPEND is set. This is problematic, as there could
* be a build bug in this function, we wouldn't have a way to know unless
* the configuration option is set.
*
* An alternative is to declare foo_suspend() always, but mark it
* as __maybe_unused. This works, but the __maybe_unused attribute
* is required to instruct the compiler that the function may not
* be referenced anywhere, and is safe to remove without making
* a fuss about it. This makes the programmer responsible for tagging
* the functions that can be garbage-collected.
*
* With the macro it is possible to write the following:
*
* static int foo_suspend(struct device *dev)
* {
* ...
* }
*
* static struct pm_ops foo_ops = {
* .suspend = PTR_IF(IS_ENABLED(CONFIG_FOO_SUSPEND), foo_suspend),
* };
*
* The foo_suspend() function will now be automatically dropped by the
* compiler, and it does not require any specific attribute.
*/
#define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
/**
* to_user_ptr - cast a pointer passed as u64 from user space to void __user *
* @x: The u64 value from user space, usually via IOCTL
*
* to_user_ptr() simply casts a pointer passed as u64 from user space to void
* __user * correctly. Using this lets us get rid of all the tiresome casts.
*/
#define u64_to_user_ptr(x) \
({ \
typecheck(u64, (x)); \
(void __user *)(uintptr_t)(x); \
})
/**
* is_insidevar - check if the @ptr points inside the @var memory range.
* @ptr: the pointer to a memory address.
* @var: the variable which address and size identify the memory range.
*
* Evaluates to true if the address in @ptr lies within the memory
* range allocated to @var.
*/
#define is_insidevar(ptr, var) \
((uintptr_t)(ptr) >= (uintptr_t)(var) && \
(uintptr_t)(ptr) < (uintptr_t)(var) + sizeof(var))
#endif