^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #ifndef DEFERRED_FREE_HELPER_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #define DEFERRED_FREE_HELPER_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * df_reason - enum for reason why item was freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * This provides a reason for why the free function was called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * on the item. This is useful when deferred_free is used in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * combination with a pagepool, so under pressure the page can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * be immediately freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * DF_NORMAL: Normal deferred free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * DF_UNDER_PRESSURE: Free was called because the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * is under memory pressure. Usually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * from a shrinker. Avoid allocating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * memory in the free call, as it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) enum df_reason {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) DF_NORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) DF_UNDER_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * deferred_freelist_item - item structure for deferred freelist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * This is to be added to the structure for whatever you want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * defer freeing on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * @nr_pages: number of pages used by item to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * @free: function pointer to be called when freeing the item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @list: list entry for the deferred list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct deferred_freelist_item {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) size_t nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void (*free)(struct deferred_freelist_item *i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) enum df_reason reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * deferred_free - call to add item to the deferred free list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * @item: Pointer to deferred_freelist_item field of a structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * @free: Function pointer to the free call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @nr_pages: number of pages to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) void deferred_free(struct deferred_freelist_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) void (*free)(struct deferred_freelist_item *i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) enum df_reason reason),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) size_t nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned long get_freelist_nr_pages(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #endif