Skip to contentSkip to conversation

compat-wireless: backporting kfree_rcu()?

DISCUSSION Johannes Berg 3 participants 6 messages Oct 18, 2011 – Nov 14, 2011

lore
Share & tools
lore
#0 lore raw

So I was looking at backporting kfree_rcu(), and came up with this:

#define kfree_rcu(data, rcuhead)                do {                    \
                void __kfree_rcu_fn(struct rcu_head *rcu_head)  \
                {                                                       \
                        void *___ptr;                                   \
                        ___ptr = container_of(rcu_head, typeof(*(data)), rcuhead);\
                        kfree(___ptr);                                  \
                }                                                       \
                call_rcu(&(data)->rcuhead, __kfree_rcu_fn);             \
        } while (0)

This works, thanks to gcc supporting nested functions, but has one major
issue: any module using call_rcu() must have an rcu_barrier() in its module_exit() because __kfree_rcu_fn() might be called after the module is unloaded. For kfree_rcu() this isn't needed since the function called lives in the base kernel.

I played around with injecting a call to rcu_barrier() into module exit by modifying module_exit() but I couldn't make the CPP do that. Anyone else have any ideas?

Another idea I had was to insert the __kfree_rcu_fn() code into the compat module instead, but I can't see how to do that short of using some source post-processing scripts.

johannes

#1 lore raw

A complete different idea:

/* a larger rcu head struct */

struct compat_rcu_head {
	struct rcu_head rh; /* must be first */
	void *free_ptr;
};

/* define kfree_rcu */
extern void compat_free_rcu(struct rcu_head *rh);

#define kfree_rcu(ptr, head)	\
	do {
		(ptr)->head.free_ptr = ptr;
		call_rcu(&(ptr)->head.rh, compat_free_rcu);
	} while (0)

/* use it in all code */
#define rcu_head compat_rcu_head

and somewhere in a compat.ko file:

#undef rcu_head
void compat_free_rcu(struct rcu_head *rh)

{
	struct compat_rcu_head *crh = (void *)rh;
	kfree(crh->free_ptr);
}

thoughts?

johannes

#2 lore raw
Quoted 25 lines · Johannes Berg

A complete different idea:

/* a larger rcu head struct */

struct compat_rcu_head {
	struct rcu_head rh; /* must be first */
	void *free_ptr;
};

/* define kfree_rcu */
extern void compat_free_rcu(struct rcu_head *rh);

#define kfree_rcu(ptr, head)	\
	do {
		(ptr)->head.free_ptr = ptr;
		call_rcu(&(ptr)->head.rh, compat_free_rcu);
	} while (0)

/* use it in all code */
#define rcu_head compat_rcu_head

and somewhere in a compat.ko file:

#undef rcu_head
void compat_free_rcu(struct rcu_head *rh)

Quoted 7 lines
{
	struct compat_rcu_head *crh = (void *)rh;
	kfree(crh->free_ptr);
}

thoughts?

Doesn't work either -- the new rcu_head will be included in existing header files like dst.h and they'd get messed up if included after compat-3.0.h

johannes

#3 lore raw
In reply to Johannes Berg

Hi Johannes,

now your patch using kfree_rcu went into linux-next. ;-)

Quoted 13 lines · Johannes Berg

So I was looking at backporting kfree_rcu(), and came up with this:

#define kfree_rcu(data, rcuhead)                do {                    \
                void __kfree_rcu_fn(struct rcu_head *rcu_head)  \
                {                                                       \
                        void *___ptr;                                   \
                        ___ptr = container_of(rcu_head, typeof(*(data)), rcuhead);\
                        kfree(___ptr);                                  \
                }                                                       \
                call_rcu(&(data)->rcuhead, __kfree_rcu_fn);             \
        } while (0)
Quoted 5 lines

This works, thanks to gcc supporting nested functions, but has one major
issue: any module using call_rcu() must have an rcu_barrier() in its module_exit() because __kfree_rcu_fn() might be called after the module is unloaded. For kfree_rcu() this isn't needed since the function called lives in the base kernel.

This looks nice to me.

Quoted 3 lines

I played around with injecting a call to rcu_barrier() into module exit by modifying module_exit() but I couldn't make the CPP do that. Anyone else have any ideas?

I played around with module_exit and modified the define into the following. Now rcu_barrier() gets called on every module_exit. As modules are not unloaded so often it should not hurd that this is also done when unloading a module not using kfree_rcu().

#undef module_exit

#define module_exit(exitfn)					\
	void __exit __exit_rcu_barrier(void)			\
		{						\
			exitfn();				\
			rcu_barrier();				\
		}						\
	static inline exitcall_t __exittest(void)		\
	{ return exitfn; }					\
	void cleanup_module(void) __attribute__((alias("__exit_rcu_barrier")));
Quoted 4 lines

Another idea I had was to insert the __kfree_rcu_fn() code into the compat module instead, but I can't see how to do that short of using some source post-processing scripts.

Johannes are you fine with this?

Hauke

#4 lore raw

Hi,

Quoted 11 lines
Quoted 11 lines
#define kfree_rcu(data, rcuhead)                do {                    \
                void __kfree_rcu_fn(struct rcu_head *rcu_head)  \
                {                                                       \
                        void *___ptr;                                   \
                        ___ptr = container_of(rcu_head, typeof(*(data)), rcuhead);\
                        kfree(___ptr);                                  \
                }                                                       \
                call_rcu(&(data)->rcuhead, __kfree_rcu_fn);             \
        } while (0)
Quoted 7 lines
Quoted 5 lines

This works, thanks to gcc supporting nested functions, but has one major
issue: any module using call_rcu() must have an rcu_barrier() in its module_exit() because __kfree_rcu_fn() might be called after the module is unloaded. For kfree_rcu() this isn't needed since the function called lives in the base kernel.

This looks nice to me.

:-)

Quoted 8 lines
Quoted 3 lines

I played around with injecting a call to rcu_barrier() into module exit by modifying module_exit() but I couldn't make the CPP do that. Anyone else have any ideas?

I played around with module_exit and modified the define into the following. Now rcu_barrier() gets called on every module_exit. As modules are not unloaded so often it should not hurd that this is also done when unloading a module not using kfree_rcu().

Ok.

Quoted 11 lines

#undef module_exit

#define module_exit(exitfn)					\
	void __exit __exit_rcu_barrier(void)			\
		{						\
			exitfn();				\
			rcu_barrier();				\
		}						\
	static inline exitcall_t __exittest(void)		\
	{ return exitfn; }					\
	void cleanup_module(void) __attribute__((alias("__exit_rcu_barrier")));
Quoted 1 line

Johannes are you fine with this?

Frankly, I don't think I really understand it, but if it works, why not? I guess I could stick a printk into that function there to verify it ;-)

johannes

#5 lore raw

T24gTW9uLCBOb3YgMTQsIDIwMTEgYXQgMTI6MjQgUE0sIEpvaGFubmVzIEJlcmcKPGpvaGFubmVz QHNpcHNvbHV0aW9ucy5uZXQ+IHdyb3RlOgo+IEhpLAo+Cj4+ID4gI2RlZmluZSBrZnJlZV9yY3Uo ZGF0YSwgcmN1aGVhZCkgwqAgwqAgwqAgwqAgwqAgwqAgwqAgwqBkbyB7IMKgIMKgIMKgIMKgIMKg IMKgIMKgIMKgIMKgIMKgXAo+PiA+IMKgIMKgIMKgIMKgIMKgIMKgIMKgIMKgIHZvaWQgX19rZnJl ZV9yY3VfZm4oc3RydWN0IHJjdV9oZWFkICpyY3VfaGVh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