Skip to content

Commit 1835f47

Browse files
urezkipaulmckrcu
authored andcommitted
rcu: Introduce single argument kvfree_rcu() interface
Make kvfree_rcu() capable of freeing objects that will not embed an rcu_head within it. This saves storage overhead in such objects. Reclaiming headless objects this way requires only a single argument (pointer to the object). After this patch, there are two ways to use kvfree_rcu(): a) kvfree_rcu(ptr, rhf); struct X { struct rcu_head rhf; unsigned char data[100]; }; void *ptr = kvmalloc(sizeof(struct X), GFP_KERNEL); if (ptr) kvfree_rcu(ptr, rhf); b) kvfree_rcu(ptr); void *ptr = kvmalloc(some_bytes, GFP_KERNEL); if (ptr) kvfree_rcu(ptr); Note that the headless usage (example b) can only be used in a code that can sleep. This is enforced by the CONFIG_DEBUG_ATOMIC_SLEEP option. Co-developed-by: Joel Fernandes (Google) <[email protected]> Reviewed-by: Joel Fernandes (Google) <[email protected]> Signed-off-by: Uladzislau Rezki (Sony) <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 3042f83 commit 1835f47

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

include/linux/rcupdate.h

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -877,12 +877,42 @@ do { \
877877

878878
/**
879879
* kvfree_rcu() - kvfree an object after a grace period.
880-
* @ptr: pointer to kvfree
881-
* @rhf: the name of the struct rcu_head within the type of @ptr.
882880
*
883-
* Same as kfree_rcu(), just simple alias.
881+
* This macro consists of one or two arguments and it is
882+
* based on whether an object is head-less or not. If it
883+
* has a head then a semantic stays the same as it used
884+
* to be before:
885+
*
886+
* kvfree_rcu(ptr, rhf);
887+
*
888+
* where @ptr is a pointer to kvfree(), @rhf is the name
889+
* of the rcu_head structure within the type of @ptr.
890+
*
891+
* When it comes to head-less variant, only one argument
892+
* is passed and that is just a pointer which has to be
893+
* freed after a grace period. Therefore the semantic is
894+
*
895+
* kvfree_rcu(ptr);
896+
*
897+
* where @ptr is a pointer to kvfree().
898+
*
899+
* Please note, head-less way of freeing is permitted to
900+
* use from a context that has to follow might_sleep()
901+
* annotation. Otherwise, please switch and embed the
902+
* rcu_head structure within the type of @ptr.
884903
*/
885-
#define kvfree_rcu(ptr, rhf) kfree_rcu(ptr, rhf)
904+
#define kvfree_rcu(...) KVFREE_GET_MACRO(__VA_ARGS__, \
905+
kvfree_rcu_arg_2, kvfree_rcu_arg_1)(__VA_ARGS__)
906+
907+
#define KVFREE_GET_MACRO(_1, _2, NAME, ...) NAME
908+
#define kvfree_rcu_arg_2(ptr, rhf) kfree_rcu(ptr, rhf)
909+
#define kvfree_rcu_arg_1(ptr) \
910+
do { \
911+
typeof(ptr) ___p = (ptr); \
912+
\
913+
if (___p) \
914+
kvfree_call_rcu(NULL, (rcu_callback_t) (___p)); \
915+
} while (0)
886916

887917
/*
888918
* Place this after a lock-acquisition primitive to guarantee that

0 commit comments

Comments
 (0)