@@ -1237,6 +1237,27 @@ void kprobes_inc_nmissed_count(struct kprobe *p)
12371237}
12381238NOKPROBE_SYMBOL (kprobes_inc_nmissed_count );
12391239
1240+ static struct kprobe kprobe_busy = {
1241+ .addr = (void * ) get_kprobe ,
1242+ };
1243+
1244+ void kprobe_busy_begin (void )
1245+ {
1246+ struct kprobe_ctlblk * kcb ;
1247+
1248+ preempt_disable ();
1249+ __this_cpu_write (current_kprobe , & kprobe_busy );
1250+ kcb = get_kprobe_ctlblk ();
1251+ kcb -> kprobe_status = KPROBE_HIT_ACTIVE ;
1252+ }
1253+
1254+ void kprobe_busy_end (void )
1255+ {
1256+ __this_cpu_write (current_kprobe , NULL );
1257+ preempt_enable ();
1258+ }
1259+
1260+ #if !defined(CONFIG_KRETPROBE_ON_RETHOOK )
12401261static void free_rp_inst_rcu (struct rcu_head * head )
12411262{
12421263 struct kretprobe_instance * ri = container_of (head , struct kretprobe_instance , rcu );
@@ -1258,26 +1279,6 @@ static void recycle_rp_inst(struct kretprobe_instance *ri)
12581279}
12591280NOKPROBE_SYMBOL (recycle_rp_inst );
12601281
1261- static struct kprobe kprobe_busy = {
1262- .addr = (void * ) get_kprobe ,
1263- };
1264-
1265- void kprobe_busy_begin (void )
1266- {
1267- struct kprobe_ctlblk * kcb ;
1268-
1269- preempt_disable ();
1270- __this_cpu_write (current_kprobe , & kprobe_busy );
1271- kcb = get_kprobe_ctlblk ();
1272- kcb -> kprobe_status = KPROBE_HIT_ACTIVE ;
1273- }
1274-
1275- void kprobe_busy_end (void )
1276- {
1277- __this_cpu_write (current_kprobe , NULL );
1278- preempt_enable ();
1279- }
1280-
12811282/*
12821283 * This function is called from delayed_put_task_struct() when a task is
12831284 * dead and cleaned up to recycle any kretprobe instances associated with
@@ -1327,6 +1328,7 @@ static inline void free_rp_inst(struct kretprobe *rp)
13271328 rp -> rph = NULL ;
13281329 }
13291330}
1331+ #endif /* !CONFIG_KRETPROBE_ON_RETHOOK */
13301332
13311333/* Add the new probe to 'ap->list'. */
13321334static int add_new_kprobe (struct kprobe * ap , struct kprobe * p )
@@ -1925,6 +1927,7 @@ static struct notifier_block kprobe_exceptions_nb = {
19251927
19261928#ifdef CONFIG_KRETPROBES
19271929
1930+ #if !defined(CONFIG_KRETPROBE_ON_RETHOOK )
19281931/* This assumes the 'tsk' is the current task or the is not running. */
19291932static kprobe_opcode_t * __kretprobe_find_ret_addr (struct task_struct * tsk ,
19301933 struct llist_node * * cur )
@@ -2087,6 +2090,57 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
20872090 return 0 ;
20882091}
20892092NOKPROBE_SYMBOL (pre_handler_kretprobe );
2093+ #else /* CONFIG_KRETPROBE_ON_RETHOOK */
2094+ /*
2095+ * This kprobe pre_handler is registered with every kretprobe. When probe
2096+ * hits it will set up the return probe.
2097+ */
2098+ static int pre_handler_kretprobe (struct kprobe * p , struct pt_regs * regs )
2099+ {
2100+ struct kretprobe * rp = container_of (p , struct kretprobe , kp );
2101+ struct kretprobe_instance * ri ;
2102+ struct rethook_node * rhn ;
2103+
2104+ rhn = rethook_try_get (rp -> rh );
2105+ if (!rhn ) {
2106+ rp -> nmissed ++ ;
2107+ return 0 ;
2108+ }
2109+
2110+ ri = container_of (rhn , struct kretprobe_instance , node );
2111+
2112+ if (rp -> entry_handler && rp -> entry_handler (ri , regs ))
2113+ rethook_recycle (rhn );
2114+ else
2115+ rethook_hook (rhn , regs , kprobe_ftrace (p ));
2116+
2117+ return 0 ;
2118+ }
2119+ NOKPROBE_SYMBOL (pre_handler_kretprobe );
2120+
2121+ static void kretprobe_rethook_handler (struct rethook_node * rh , void * data ,
2122+ struct pt_regs * regs )
2123+ {
2124+ struct kretprobe * rp = (struct kretprobe * )data ;
2125+ struct kretprobe_instance * ri ;
2126+ struct kprobe_ctlblk * kcb ;
2127+
2128+ /* The data must NOT be null. This means rethook data structure is broken. */
2129+ if (WARN_ON_ONCE (!data ))
2130+ return ;
2131+
2132+ __this_cpu_write (current_kprobe , & rp -> kp );
2133+ kcb = get_kprobe_ctlblk ();
2134+ kcb -> kprobe_status = KPROBE_HIT_ACTIVE ;
2135+
2136+ ri = container_of (rh , struct kretprobe_instance , node );
2137+ rp -> handler (ri , regs );
2138+
2139+ __this_cpu_write (current_kprobe , NULL );
2140+ }
2141+ NOKPROBE_SYMBOL (kretprobe_rethook_handler );
2142+
2143+ #endif /* !CONFIG_KRETPROBE_ON_RETHOOK */
20902144
20912145/**
20922146 * kprobe_on_func_entry() -- check whether given address is function entry
@@ -2155,6 +2209,29 @@ int register_kretprobe(struct kretprobe *rp)
21552209 rp -> maxactive = num_possible_cpus ();
21562210#endif
21572211 }
2212+ #ifdef CONFIG_KRETPROBE_ON_RETHOOK
2213+ rp -> rh = rethook_alloc ((void * )rp , kretprobe_rethook_handler );
2214+ if (!rp -> rh )
2215+ return - ENOMEM ;
2216+
2217+ for (i = 0 ; i < rp -> maxactive ; i ++ ) {
2218+ inst = kzalloc (sizeof (struct kretprobe_instance ) +
2219+ rp -> data_size , GFP_KERNEL );
2220+ if (inst == NULL ) {
2221+ rethook_free (rp -> rh );
2222+ rp -> rh = NULL ;
2223+ return - ENOMEM ;
2224+ }
2225+ rethook_add_node (rp -> rh , & inst -> node );
2226+ }
2227+ rp -> nmissed = 0 ;
2228+ /* Establish function entry probe point */
2229+ ret = register_kprobe (& rp -> kp );
2230+ if (ret != 0 ) {
2231+ rethook_free (rp -> rh );
2232+ rp -> rh = NULL ;
2233+ }
2234+ #else /* !CONFIG_KRETPROBE_ON_RETHOOK */
21582235 rp -> freelist .head = NULL ;
21592236 rp -> rph = kzalloc (sizeof (struct kretprobe_holder ), GFP_KERNEL );
21602237 if (!rp -> rph )
@@ -2179,6 +2256,7 @@ int register_kretprobe(struct kretprobe *rp)
21792256 ret = register_kprobe (& rp -> kp );
21802257 if (ret != 0 )
21812258 free_rp_inst (rp );
2259+ #endif
21822260 return ret ;
21832261}
21842262EXPORT_SYMBOL_GPL (register_kretprobe );
@@ -2217,15 +2295,21 @@ void unregister_kretprobes(struct kretprobe **rps, int num)
22172295 for (i = 0 ; i < num ; i ++ ) {
22182296 if (__unregister_kprobe_top (& rps [i ]-> kp ) < 0 )
22192297 rps [i ]-> kp .addr = NULL ;
2298+ #ifdef CONFIG_KRETPROBE_ON_RETHOOK
2299+ rethook_free (rps [i ]-> rh );
2300+ #else
22202301 rps [i ]-> rph -> rp = NULL ;
2302+ #endif
22212303 }
22222304 mutex_unlock (& kprobe_mutex );
22232305
22242306 synchronize_rcu ();
22252307 for (i = 0 ; i < num ; i ++ ) {
22262308 if (rps [i ]-> kp .addr ) {
22272309 __unregister_kprobe_bottom (& rps [i ]-> kp );
2310+ #ifndef CONFIG_KRETPROBE_ON_RETHOOK
22282311 free_rp_inst (rps [i ]);
2312+ #endif
22292313 }
22302314 }
22312315}
0 commit comments