Module load notification take 3
Subsystems (6)
PATCH 1/2 Module load notification take 3
Implement a module load notifier for the benefit of OProfile, tested with .66 on UP.
This patch (1/2) provides the general support for registering notifiers, and calls the notifier when a module is loaded.
Please apply.
john
include/linux/module.h +16 -0
@@ -138,6 +138,7 @@const struct exception_table_entry *entry; }; +struct notifier_block ;#ifdef CONFIG_MODULES
@@ -348,6 +349,9 @@/* For extable.c to search modules' exception tables. */ const struct exception_table_entry *search_module_extables (unsigned long addr); +int register_module_notifier (struct notifier_block * nb); +int unregister_module_notifier (struct notifier_block * nb); +#else /* !CONFIG_MODULES... */ #define EXPORT_SYMBOL (sym)#define EXPORT_SYMBOL_GPL (sym)
@@ -392,6 +396,18 @@ {return NULL ; } + +static inline int register_module_notifier (struct notifier_block * nb) +{ +/* no events will happen anyway, so this can always succeed */ +return 0 ; +} + +static inline int unregister_module_notifier (struct notifier_block * nb) +{ +return 0 ; +} +#endif /* CONFIG_MODULES */ #ifdef MODULE
kernel/module.c +28 -0
@@ -31,6 +31,7 @@#include <linux/errno.h> #include <linux/err.h> #include <linux/vermagic.h> +#include <linux/notifier.h> #include <asm/uaccess.h> #include <asm/semaphore.h> #include <asm/pgalloc.h>
@@ -61,6 +62,29 @@static LIST_HEAD (symbols);static LIST_HEAD (extables); +static DECLARE_MUTEX (notify_mutex); +static struct notifier_block * module_notify_list; + +int register_module_notifier (struct notifier_block * nb) +{ +int err; +down (¬ify_mutex); + err =notifier_chain_register (&module_notify_list, nb); +up (¬ify_mutex); +return err; +} +EXPORT_SYMBOL (register_module_notifier); + +int unregister_module_notifier (struct notifier_block * nb) +{ +int err; +down (¬ify_mutex); + err =notifier_chain_unregister (&module_notify_list, nb); +up (¬ify_mutex); +return err; +} +EXPORT_SYMBOL (unregister_module_notifier); +/* We require a truly strong try_module_get() */ static inline int strong_try_module_get (struct module *mod) {
@@ -1368,6 +1392,10 @@/* Drop lock so they can recurse */ up (&module_mutex); +down (¬ify_mutex); +notifier_call_chain (&module_notify_list,MODULE_STATE_COMING , mod); +up (¬ify_mutex); +/* Start the module */ ret = mod->init ();if (ret <0 ) {
PATCH 2/2 Module load notification take 3
Implement a module load notifier for the benefit of OProfile, tested with .66 on UP.
This patch (2/2) hooks OProfile into the notifier. You should still be able to use OProfile 0.5.1 or thereabouts ...
thanks,
john
drivers/oprofile/buffer_sync.c +24 -0
@@ -24,6 +24,7 @@#include <linux/notifier.h> #include <linux/dcookies.h> #include <linux/profile.h> +#include <linux/module.h> #include <linux/fs.h> #include "oprofile_stats.h"
@@ -67,6 +68,19 @@ } +static int module_load_notify (struct notifier_block * self,unsigned long val,void * data) +{ +if (val !=MODULE_STATE_COMING ) +return 0 ; + +sync_cpu_buffers (); +down (&buffer_sem); +add_event_entry (ESCAPE_CODE ); +add_event_entry (MODULE_LOADED_CODE ); +up (&buffer_sem); +return 0 ; +} +static struct notifier_block exit_task_nb = { .notifier_call = exit_task_notify, };
@@ -79,6 +93,10 @@ .notifier_call = mm_notify, }; +static struct notifier_block module_load_nb = { + .notifier_call = module_load_notify, +}; +int sync_start (void ) {
@@ -98,9 +116,14 @@ err =profile_event_register (EXEC_UNMAP , &exec_unmap_nb);if (err) goto out3; + err =register_module_notifier (&module_load_nb); +if (err) + goto out4; out:return err; +out4: +profile_event_unregister (EXEC_UNMAP , &exec_unmap_nb); out3:profile_event_unregister (EXIT_MMAP , &exit_mmap_nb); out2:
@@ -113,6 +136,7 @@void sync_stop (void ) { +unregister_module_notifier (&module_load_nb);profile_event_unregister (EXIT_TASK , &exit_task_nb);profile_event_unregister (EXIT_MMAP , &exit_mmap_nb);profile_event_unregister (EXEC_UNMAP , &exec_unmap_nb);
drivers/oprofile/event_buffer.h +2 -1
@@ -24,12 +24,13 @@* then one of the following codes, then the * relevant data. */ -#define ESCAPE_CODE ~0UL +#define ESCAPE_CODE ~0UL#define CTX_SWITCH_CODE 1#define CPU_SWITCH_CODE 2#define COOKIE_SWITCH_CODE 3#define KERNEL_ENTER_SWITCH_CODE 4#define KERNEL_EXIT_SWITCH_CODE 5 +#define MODULE_LOADED_CODE 6/* add data to the event buffer */ void add_event_entry (unsigned long data);
In message <[email protected]> you write:
Quoted 3 lines
Implement a module load notifier for the benefit of OProfile, tested with .66 on UP.
Minor change to make unregister_module_notifier return void.
Either way Linus, don't really mind. Please apply.
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Name: Module Notifier Patch
Author: John Levon
Status: Experimental
D: Adds a notifier for when modules are inserted, for use of oprofile.
include/linux/module.h +15 -0
@@ -138,6 +138,7 @@ struct exception_tableconst struct exception_table_entry *entry; }; +struct notifier_block ;#ifdef CONFIG_MODULES
@@ -348,6 +349,9 @@ const char *module_address_lookup(unsign/* For extable.c to search modules' exception tables. */ const struct exception_table_entry *search_module_extables (unsigned long addr); +int register_module_notifier (struct notifier_block *nb); +void unregister_module_notifier (struct notifier_block *nb); +#else /* !CONFIG_MODULES... */ #define EXPORT_SYMBOL (sym)#define EXPORT_SYMBOL_GPL (sym)
@@ -392,6 +396,17 @@ static inline const char *module_address {return NULL ; } + +static inline int register_module_notifier (struct notifier_block *nb) +{ +/* no events will happen anyway, so this can always succeed */ +return 0 ; +} + +static inline void unregister_module_notifier (struct notifier_block *nb) +{ +} +#endif /* CONFIG_MODULES */ #ifdef MODULE
kernel/module.c +26 -0
@@ -31,6 +31,7 @@#include <linux/errno.h> #include <linux/err.h> #include <linux/vermagic.h> +#include <linux/notifier.h> #include <asm/uaccess.h> #include <asm/semaphore.h> #include <asm/pgalloc.h>
@@ -61,6 +62,27 @@ static LIST_HEAD(modules);static LIST_HEAD (symbols);static LIST_HEAD (extables); +static DECLARE_MUTEX (notify_mutex); +static struct notifier_block *module_notify_list; + +int register_module_notifier (struct notifier_block * nb) +{ +int err; +down (¬ify_mutex); + err =notifier_chain_register (&module_notify_list, nb); +up (¬ify_mutex); +return err; +} +EXPORT_SYMBOL (register_module_notifier); + +void unregister_module_notifier (struct notifier_block * nb) +{ +down (¬ify_mutex); +notifier_chain_unregister (&module_notify_list, nb); +up (¬ify_mutex); +} +EXPORT_SYMBOL (unregister_module_notifier); +/* We require a truly strong try_module_get() */ static inline int strong_try_module_get (struct module *mod) {
@@ -1368,6 +1390,10 @@ sys_init_module(void *umod,/* Drop lock so they can recurse */ up (&module_mutex); +down (¬ify_mutex); +notifier_call_chain (&module_notify_list,MODULE_STATE_COMING , mod); +up (¬ify_mutex); +/* Start the module */ ret = mod->init ();if (ret <0 ) {
Quoted 4 lines · Rusty Russell
Quoted 2 lines
Implement a module load notifier for the benefit of OProfile, tested with .66 on UP.
Minor change to make
unregister_module_notifierreturn void.
The -ENOENT return is there for a reason. If you don't want it, then you should remove it from notifier_call_register too.
john
In message <[email protected]> you write:
Quoted 8 lines · Rusty Russell
Quoted 5 lines
Quoted 3 lines
Implement a module load notifier for the benefit of OProfile, tested with .66 on UP.
Minor change to make
unregister_module_notifierreturn void.The
-ENOENTreturn is there for a reason.
What reason? I just grepped 2.5.66-bk2, and *noone* uses the return value, not even to BUG() (you have to grep for all the wrappers for notifier_call_unregister, too).
That's because everyone realizes that the return value is useless.
Quoted 2 lines
If you don't want it, then you should remove it from
notifier_call_registertoo.
(I assume you mean notifier_call_unregister). Yes, but that's another battle.
Meanwhile, at least I'm not adding to the problem. Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Quoted 3 lines · Rusty Russell
What reason? I just grepped 2.5.66-bk2, and *noone* uses the return value, not even to
BUG()(you have to grep for all the wrappers fornotifier_call_unregister, too).
Just more grist for the mill of the ill ...
Quoted 2 lines
(I assume you mean
notifier_call_unregister). Yes, but that's another battle.
... and it's the same ill. Half-cleaned up stuff sucks. Either remove it or don't.
Quoted 1 line
That's because everyone realizes that the return value is useless.
It detects one variant of unmatched register/unregister, so it cannot be said to be entirely useless. I would not call it entirely useful, though, I admit.
Beside the point though really, I appear to have made onto Linus' shitlist at last ... does this mean I finally graduated Linux School ?
regards
john
In message <[email protected]> you write:
Quoted 3 lines · Rusty Russell
... and it's the same ill. Half-cleaned up stuff sucks. Either remove it or don't.
I like that attitude: I'm not sure I have the energy to match it 8)
However, please look at patch below.
Quoted 5 lines
Quoted 1 line
That's because everyone realizes that the return value is useless.
It detects one variant of unmatched register/unregister, so it cannot be said to be entirely useless. I would not call it entirely useful, though, I admit.
BUG() in the unregister makes sense I think (see patch).
Quoted 2 lines
Beside the point though really, I appear to have made onto Linus' shitlist at last ... does this mean I finally graduated Linux School ?
Linus fades every so often. Has more to do with phase of the moon, I think, than individuals.
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Name: notifier_unregister should return void
Author: Rusty Russell
Status: Tested on 2.5.66-bk2
D: Noone uses the return value of notifier_chain_unregister and its
D: children: make it BUG() when unregistering a non-existent notifier,
D: and return void.
include/linux/cpufreq.h +1 -1
@@ -29,7 +29,7 @@*********************************************************************/ int cpufreq_register_notifier (struct notifier_block *nb,unsigned int list); -int cpufreq_unregister_notifier (struct notifier_block *nb,unsigned int list); +void cpufreq_unregister_notifier (struct notifier_block *nb,unsigned int list);#define CPUFREQ_TRANSITION_NOTIFIER (0)#define CPUFREQ_POLICY_NOTIFIER (1)
include/linux/inetdevice.h +1 -1
@@ -77,7 +77,7 @@ struct in_ifaddr };extern int register_inetaddr_notifier (struct notifier_block *nb); -extern int unregister_inetaddr_notifier (struct notifier_block *nb); +extern void unregister_inetaddr_notifier (struct notifier_block *nb);extern struct net_device *ip_dev_find (u32 addr);extern int inet_addr_onlink (struct in_device *in_dev,u32 a,u32 b);
include/linux/netdevice.h +1 -1
@@ -487,7 +487,7 @@ extern int dev_queue_xmit(struct sk_bufextern int register_netdevice (struct net_device *dev);extern int unregister_netdevice (struct net_device *dev);extern int register_netdevice_notifier (struct notifier_block *nb); -extern int unregister_netdevice_notifier (struct notifier_block *nb); +extern void unregister_netdevice_notifier (struct notifier_block *nb);extern int call_netdevice_notifiers (unsigned long val,void *v);extern int dev_new_index (void );extern struct net_device *dev_get_by_index (int ifindex);
include/linux/netlink.h +1 -1
@@ -114,7 +114,7 @@ extern int netlink_broadcast(struct sock __u32 group, int allocation);extern void netlink_set_err (struct sock *ssk,__u32 pid,__u32 group,int code);extern int netlink_register_notifier (struct notifier_block *nb); -extern int netlink_unregister_notifier (struct notifier_block *nb); +extern void netlink_unregister_notifier (struct notifier_block *nb);/* * skb should fit one page. This choice is good for headerless malloc.
include/linux/notifier.h +1 -1
@@ -22,7 +22,7 @@ struct notifier_block#ifdef __KERNEL__extern int notifier_chain_register (struct notifier_block **list,struct notifier_block *n); -extern int notifier_chain_unregister (struct notifier_block **nl,struct notifier_block *n); +extern void notifier_chain_unregister (struct notifier_block **nl,struct notifier_block *n);extern int notifier_call_chain (struct notifier_block **n,unsigned long val,void *v);#define NOTIFY_DONE 0x0000/* Don't care */
include/linux/profile.h +4 -6
@@ -43,10 +43,10 @@ void profile_exit_mmap(struct mm_structint profile_event_register (enum profile_type ,struct notifier_block * n); -int profile_event_unregister (enum profile_type ,struct notifier_block * n); +void profile_event_unregister (enum profile_type ,struct notifier_block * n);int register_profile_notifier (struct notifier_block * nb); -int unregister_profile_notifier (struct notifier_block * nb); +void unregister_profile_notifier (struct notifier_block * nb);/* profiling hook activated on each timer interrupt */ void profile_hook (struct pt_regs * regs);
@@ -58,9 +58,8 @@ static inline int profile_event_registerreturn -ENOSYS ; } -static inline int profile_event_unregister (enum profile_type t,struct notifier_block * n) +static inline void profile_event_unregister (enum profile_type t,struct notifier_block * n) { -return -ENOSYS ; }#define profile_exit_task (a) do { } while (0)
@@ -72,9 +71,8 @@ static inline int register_profile_notifreturn -ENOSYS ; } -static inline int unregister_profile_notifier (struct notifier_block * nb) +static inline void unregister_profile_notifier (struct notifier_block * nb) { -return -ENOSYS ; }#define profile_hook (regs) do { } while (0)
include/linux/reboot.h +1 -1
@@ -37,7 +37,7 @@#include <linux/notifier.h> extern int register_reboot_notifier (struct notifier_block *); -extern int unregister_reboot_notifier (struct notifier_block *); +extern void unregister_reboot_notifier (struct notifier_block *);/*
include/net/addrconf.h +1 -1
@@ -123,7 +123,7 @@ extern int ipv6_chk_acast_addr(struct/* Device notifier */ extern int register_inet6addr_notifier (struct notifier_block *nb); -extern int unregister_inet6addr_notifier (struct notifier_block *nb); +extern void unregister_inet6addr_notifier (struct notifier_block *nb);static inline struct inet6_dev *__in6_dev_get (struct net_device *dev)
include/net/bluetooth/hci_core.h +1 -1
@@ -467,7 +467,7 @@ static inline void hci_proto_encrypt_cfmint hci_register_proto (struct hci_proto *hproto);int hci_unregister_proto (struct hci_proto *hproto);int hci_register_notifier (struct notifier_block *nb); -int hci_unregister_notifier (struct notifier_block *nb); +void hci_unregister_notifier (struct notifier_block *nb);int hci_send_cmd (struct hci_dev *hdev,__u16 ogf,__u16 ocf,__u32 plen,void *param);int hci_send_acl (struct hci_conn *conn,struct sk_buff *skb,__u16 flags);
kernel/cpufreq.c +5 -10
@@ -484,27 +484,22 @@ EXPORT_SYMBOL(cpufreq_register_notifier)* * Remove a driver from the CPU frequency notifier list. * -* This function may sleep, and has the same return conditions as -* notifier_chain_unregister. +* This function may sleep. */ -int cpufreq_unregister_notifier (struct notifier_block *nb,unsigned int list) +void cpufreq_unregister_notifier (struct notifier_block *nb,unsigned int list) { -int ret; -down_write (&cpufreq_notifier_rwsem);switch (list) {case CPUFREQ_TRANSITION_NOTIFIER : - ret =notifier_chain_unregister (&cpufreq_transition_notifier_list, nb); +notifier_chain_unregister (&cpufreq_transition_notifier_list, nb);break ;case CPUFREQ_POLICY_NOTIFIER : - ret =notifier_chain_unregister (&cpufreq_policy_notifier_list, nb); +notifier_chain_unregister (&cpufreq_policy_notifier_list, nb);break ;default : - ret = -EINVAL ; +BUG (); }up_write (&cpufreq_notifier_rwsem); - -return ret; }EXPORT_SYMBOL (cpufreq_unregister_notifier);
kernel/profile.c +6 -11
@@ -97,26 +97,23 @@ int profile_event_register(enum profile_ } -int profile_event_unregister (enum profile_type type,struct notifier_block * n) +void profile_event_unregister (enum profile_type type,struct notifier_block * n) { -int err = -EINVAL ; -down_write (&profile_rwsem);switch (type) {case EXIT_TASK : - err =notifier_chain_unregister (&exit_task_notifier, n); +notifier_chain_unregister (&exit_task_notifier, n);break ;case EXIT_MMAP : - err =notifier_chain_unregister (&exit_mmap_notifier, n); +notifier_chain_unregister (&exit_mmap_notifier, n);break ;case EXEC_UNMAP : - err =notifier_chain_unregister (&exec_unmap_notifier, n); +notifier_chain_unregister (&exec_unmap_notifier, n);break ; }up_write (&profile_rwsem); -return err; }static struct notifier_block * profile_listeners;
@@ -132,13 +129,11 @@ int register_profile_notifier(struct not } -int unregister_profile_notifier (struct notifier_block * nb) +void unregister_profile_notifier (struct notifier_block * nb) { -int err;write_lock_irq (&profile_lock); - err =notifier_chain_unregister (&profile_listeners, nb); +notifier_chain_unregister (&profile_listeners, nb);write_unlock_irq (&profile_lock); -return err; }
kernel/sys.c +5 -9
@@ -111,11 +111,9 @@ int notifier_chain_register(struct notif* @n: New entry in notifier chain * * Removes a notifier from a notifier chain. -* -* Returns zero on success, or %-ENOENT on failure. */ -int notifier_chain_unregister (struct notifier_block **nl,struct notifier_block *n) +void notifier_chain_unregister (struct notifier_block **nl,struct notifier_block *n) {write_lock (¬ifier_lock);while ((*nl)!=NULL )
@@ -124,12 +122,12 @@ int notifier_chain_unregister(struct not { *nl=n->next;write_unlock (¬ifier_lock); -return 0 ; +return ; } nl=&((*nl)->next); }write_unlock (¬ifier_lock); -return -ENOENT ; +BUG (); }/**
@@ -187,13 +185,11 @@ int register_reboot_notifier(struct noti* * Unregisters a previously registered reboot * notifier function. -* -* Returns zero on success, or %-ENOENT on failure. */ -int unregister_reboot_notifier (struct notifier_block * nb) +void unregister_reboot_notifier (struct notifier_block * nb) { -return notifier_chain_unregister (&reboot_notifier_list, nb); +notifier_chain_unregister (&reboot_notifier_list, nb); }asmlinkage long sys_ni_syscall (void )
net/bluetooth/hci_core.c +2 -2
@@ -83,9 +83,9 @@ int hci_register_notifier(struct notifiereturn notifier_chain_register (&hci_notifier, nb); } -int hci_unregister_notifier (struct notifier_block *nb) +void hci_unregister_notifier (struct notifier_block *nb) { -return notifier_chain_unregister (&hci_notifier, nb); +notifier_chain_unregister (&hci_notifier, nb); }void hci_notify (struct hci_dev *hdev,int event)
net/core/dev.c +3 -4
@@ -912,13 +912,12 @@ int register_netdevice_notifier(struct n* * Unregister a notifier previously registered by * register_netdevice_notifier(). The notifier is unlinked into the -* kernel structures and may then be reused. A negative errno code -* is returned on a failure. +* kernel structures and may then be reused. */ -int unregister_netdevice_notifier (struct notifier_block *nb) +void unregister_netdevice_notifier (struct notifier_block *nb) { -return notifier_chain_unregister (&netdev_chain, nb); +notifier_chain_unregister (&netdev_chain, nb); }/**
net/ipv4/devinet.c +2 -2
@@ -818,9 +818,9 @@ int register_inetaddr_notifier(struct noreturn notifier_chain_register (&inetaddr_chain, nb); } -int unregister_inetaddr_notifier (struct notifier_block *nb) +void unregister_inetaddr_notifier (struct notifier_block *nb) { -return notifier_chain_unregister (&inetaddr_chain, nb); +notifier_chain_unregister (&inetaddr_chain, nb); }/* Rename ifa_labels for a device name change. Make some effort to preserve existing
net/ipv6/addrconf.c +2 -2
@@ -2636,9 +2636,9 @@ int register_inet6addr_notifier(struct nreturn notifier_chain_register (&inet6addr_chain, nb); } -int unregister_inet6addr_notifier (struct notifier_block *nb) +void unregister_inet6addr_notifier (struct notifier_block *nb) { -return notifier_chain_unregister (&inet6addr_chain,nb); +notifier_chain_unregister (&inet6addr_chain,nb); }/*
net/netlink/af_netlink.c +2 -2
@@ -1023,9 +1023,9 @@ int netlink_register_notifier(struct notreturn notifier_chain_register (&netlink_chain, nb); } -int netlink_unregister_notifier (struct notifier_block *nb) +void netlink_unregister_notifier (struct notifier_block *nb) { -return notifier_chain_unregister (&netlink_chain, nb); +notifier_chain_unregister (&netlink_chain, nb); }struct proto_ops netlink_ops = {