Skip to content

Commit

Permalink
ANDROID: vendor_hooks: Add hooks for oem futex optimization
Browse files Browse the repository at this point in the history
If an important task is going to sleep through do_futex(),
find out it's futex-owner by the pid comes from userspace,
and boost the owner by some means to shorten the sleep time.
How to boost? Depends on these hooks:
commit 53e8099 ("ANDROID: vendor_hooks: Add hooks for scheduler")

Bug: 243110112
Signed-off-by: xieliujie <xieliujie@oppo.com>
Change-Id: I9a315cfb414fd34e0ef7a2cf9d57df50d4dd984f
(cherry picked from commit 548da5d)
  • Loading branch information
xieliujie authored and Treehugger Robot committed Aug 29, 2022
1 parent eb6c8ea commit cc72404
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions drivers/android/vendor_hooks.c
Expand Up @@ -103,6 +103,12 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_binder_wakeup_ilocked);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_do_send_sig_info);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_alter_futex_plist_add);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_futex_sleep_start);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_do_futex);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_futex_wait_start);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_futex_wait_end);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_futex_wake_traverse_plist);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_futex_wake_this);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_futex_wake_up_q_finish);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_mutex_wait_start);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_mutex_wait_finish);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_mutex_init);
Expand Down
34 changes: 34 additions & 0 deletions include/trace/hooks/futex.h
Expand Up @@ -7,6 +7,10 @@
#define _TRACE_HOOK_FUTEX_H
#include <trace/hooks/vendor_hooks.h>
#include <linux/plist.h>
#ifndef __GENKSYMS__
#include <linux/futex.h>
#endif

/*
* Following tracepoints are not exported in tracefs and provide a
* mechanism for vendor modules to hook and extend functionality
Expand All @@ -21,6 +25,36 @@ DECLARE_HOOK(android_vh_futex_sleep_start,
TP_PROTO(struct task_struct *p),
TP_ARGS(p));

DECLARE_HOOK(android_vh_do_futex,
TP_PROTO(int cmd,
unsigned int *flags,
u32 __user *uaddr2),
TP_ARGS(cmd, flags, uaddr2));

DECLARE_HOOK(android_vh_futex_wait_start,
TP_PROTO(unsigned int flags,
u32 bitset),
TP_ARGS(flags, bitset));

DECLARE_HOOK(android_vh_futex_wait_end,
TP_PROTO(unsigned int flags,
u32 bitset),
TP_ARGS(flags, bitset));

DECLARE_HOOK(android_vh_futex_wake_traverse_plist,
TP_PROTO(struct plist_head *chain, int *target_nr,
union futex_key key, u32 bitset),
TP_ARGS(chain, target_nr, key, bitset));

DECLARE_HOOK(android_vh_futex_wake_this,
TP_PROTO(int ret, int nr_wake, int target_nr,
struct task_struct *p),
TP_ARGS(ret, nr_wake, target_nr, p));

DECLARE_HOOK(android_vh_futex_wake_up_q_finish,
TP_PROTO(int nr_wake, int target_nr),
TP_ARGS(nr_wake, target_nr));

#endif /* _TRACE_HOOK_FUTEX_H */
/* This part must be outside protection */
#include <trace/define_trace.h>
7 changes: 7 additions & 0 deletions kernel/futex.c
Expand Up @@ -1655,6 +1655,7 @@ futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
struct futex_q *this, *next;
union futex_key key = FUTEX_KEY_INIT;
int ret;
int target_nr;
DEFINE_WAKE_Q(wake_q);

if (!bitset)
Expand All @@ -1672,6 +1673,7 @@ futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)

spin_lock(&hb->lock);

trace_android_vh_futex_wake_traverse_plist(&hb->chain, &target_nr, key, bitset);
plist_for_each_entry_safe(this, next, &hb->chain, list) {
if (match_futex (&this->key, &key)) {
if (this->pi_state || this->rt_waiter) {
Expand All @@ -1683,6 +1685,7 @@ futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
if (!(this->bitset & bitset))
continue;

trace_android_vh_futex_wake_this(ret, nr_wake, target_nr, this->task);
mark_wake_futex(&wake_q, this);
if (++ret >= nr_wake)
break;
Expand All @@ -1691,6 +1694,7 @@ futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)

spin_unlock(&hb->lock);
wake_up_q(&wake_q);
trace_android_vh_futex_wake_up_q_finish(nr_wake, target_nr);
return ret;
}

Expand Down Expand Up @@ -2944,6 +2948,7 @@ static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
if (!bitset)
return -EINVAL;
q.bitset = bitset;
trace_android_vh_futex_wait_start(flags, bitset);

to = futex_setup_timer(abs_time, &timeout, flags,
current->timer_slack_ns);
Expand Down Expand Up @@ -2992,6 +2997,7 @@ static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
hrtimer_cancel(&to->timer);
destroy_hrtimer_on_stack(&to->timer);
}
trace_android_vh_futex_wait_end(flags, bitset);
return ret;
}

Expand Down Expand Up @@ -3962,6 +3968,7 @@ long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
return -ENOSYS;
}

trace_android_vh_do_futex(cmd, &flags, uaddr2);
switch (cmd) {
case FUTEX_WAIT:
val3 = FUTEX_BITSET_MATCH_ANY;
Expand Down

0 comments on commit cc72404

Please sign in to comment.