binder: optimize handle generation logic
Subsystems (2)
Carlos Llamas
maintainercover binder: optimize handle generation logic
This patchset adds the ability for userspace to select a faster handle generation logic. This is implemented through a new ioctl that enables certain functionality on a "per-proc" basis, this is required in order to maintain backwards compatibility.
Carlos Llamas (4):
binder: introduce BINDER_SET_PROC_FLAGS ioctl
binder: migrate ioctl to new PF_SPAM_DETECTION
binder: add support for PF_LARGE_HANDLES
binder: fix max_thread type inconsistency
drivers/android/binder.c | 78 +++++++++++++++++++++++------ drivers/android/binder_internal.h | 10 ++-- include/uapi/linux/android/binder.h | 13 ++++- 3 files changed, 83 insertions(+), 18 deletions(-)
Signature · 1 line
2.44.0.683.g7961c838ac-goog
Carlos Llamas
maintainerPATCH 1/4 binder: introduce BINDER_SET_PROC_FLAGS ioctl
This new ioctl enables userspace to control the individual behavior of the 'struct binder_proc' instance via flags. The driver validates and returns the supported subset. Some existing ioctls are migrated to use these flags in subsequent commits.
--- drivers/android/binder.c | 25 +++++++++++++++++++++++++ drivers/android/binder_internal.h | 4 +++- include/uapi/linux/android/binder.h | 6 ++++++ 3 files changed, 34 insertions(+), 1 deletion(-)
drivers/android/binder.c +25 -0
@@ -5334,6 +5334,26 @@ static int binder_ioctl_get_extended_error(struct binder_thread *thread,return 0 ; } +static int binder_ioctl_set_proc_flags (struct binder_proc *proc, +u32 __user *user) +{ +u32 flags; + +if (get_user (flags, user)) +return -EFAULT ; + +binder_inner_proc_lock (proc); + flags &=PF_SUPPORTED_FLAGS_MASK ; + proc->flags = flags; +binder_inner_proc_unlock (proc); + +/* confirm supported flags with user */ +if (put_user (flags, user)) +return -EFAULT ; + +return 0 ; +} +static long binder_ioctl (struct file *filp,unsigned int cmd,unsigned long arg) {int ret;
@@ -5542,6 +5562,11 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)if (ret <0 ) goto err;break ; +case BINDER_SET_PROC_FLAGS : + ret =binder_ioctl_set_proc_flags (proc, ubuf); +if (ret <0 ) + goto err; +break ;default : ret = -EINVAL ; goto err;
drivers/android/binder_internal.h +3 -1
@@ -346,6 +346,8 @@ struct binder_ref {* @cred struct cred associated with the `struct file` * in binder_open() * (invariant after initialized) +* @flags: enum proc_flags set via BINDER_SET_PROC_FLAGS. +* (protected by @inner_lock) * @deferred_work_node: element for binder_deferred_list * (protected by binder_deferred_lock) * @deferred_work: bitmap of deferred work to perform
@@ -409,6 +411,7 @@ struct binder_proc {int pid;struct task_struct *tsk;const struct cred *cred; +u32 flags;struct hlist_node deferred_work_node;int deferred_work;int outstanding_txns;
@@ -417,7 +420,6 @@ struct binder_proc {bool sync_recv;bool async_recv;wait_queue_head_t freeze_wait; -struct list_head todo;struct binder_stats stats;struct list_head delivered_death;
include/uapi/linux/android/binder.h +6 -0
@@ -251,6 +251,11 @@ struct binder_extended_error {__s32 param; }; +/* Used with BINDER_SET_PROC_FLAGS ioctl */ +enum proc_flags { +PF_SUPPORTED_FLAGS_MASK , +}; +enum { BINDER_WRITE_READ = _IOWR('b', 1, struct binder_write_read),BINDER_SET_IDLE_TIMEOUT =_IOW ('b' ,3 , __s64),
@@ -266,6 +271,7 @@ enum { BINDER_GET_FROZEN_INFO = _IOWR('b', 15, struct binder_frozen_status_info),BINDER_ENABLE_ONEWAY_SPAM_DETECTION =_IOW ('b' ,16 , __u32), BINDER_GET_EXTENDED_ERROR = _IOWR('b', 17, struct binder_extended_error), + BINDER_SET_PROC_FLAGS = _IOWR('b', 18, __u32), };/*
Signature · 1 line
2.44.0.683.g7961c838ac-goog
Alice Ryhl
maintainerRe: [PATCH 1/4] binder: introduce BINDER_SET_PROC_FLAGS ioctl
Quoted 30 lines · Carlos Llamas
This new ioctl enables userspace to control the individual behavior of the 'struct
binder_proc' instance via flags. The driver validates and returns the supported subset. Some existing ioctls are migrated to use these flags in subsequent commits.Suggested-by Arve Hjønnevåg <[email protected]>Signed-off-by Carlos Llamas <[email protected]>--- drivers/android/binder.c | 25 +++++++++++++++++++++++++ drivers/android/binder_internal.h | 4 +++- include/uapi/linux/android/binder.h | 6 ++++++ 3 files changed, 34 insertions(+), 1 deletion(-)drivers/android/binder.c +9
-0@@ -5334,6 +5334,26 @@ static int binder_ioctl_get_extended_error(struct binder_thread *thread,return 0 ; } +static int binder_ioctl_set_proc_flags (struct binder_proc *proc, +u32 __user *user) +{ +u32 flags; + +if (get_user (flags, user)) +return -EFAULT ; + +binder_inner_proc_lock (proc);
drivers/android/binder.c static int binder_ioctl_get_extended_error(struct binder_thread *thread, · line 5355
Quoted 10 lines
drivers/android/binder.c +10
-0+ flags &=PF_SUPPORTED_FLAGS_MASK ; + proc->flags = flags; +binder_inner_proc_unlock (proc); + +/* confirm supported flags with user */ +if (put_user (flags, user)) +return -EFAULT ; + +return 0 ; +}
I'm just thinking out loud here, but is this the best API for this ioctl? Using this API, if I want to toggle the oneway-spam-detection flag, then I can't do so without knowing the value of all other flags, and I also need to synchronize all calls to this ioctl.
That's fine for the current use-case where these flags are only set during startup, but are we confident that no future flag will be toggled while a process is active?
How about these alternatives?
1. Userspace passes two masks, one containing bits to set, and another containing bits to unset. Userspace returns new value of flags. (If the same bit is set in both masks, we can fail with EINVAL.)
2. Compare and swap. Userspace passes the expected previous value and the desired new value. The kernel returns the actual previous value and updates it only if userspace gave the right previous value.
3. Set or unset only. Userspace passes a boolean and a mask. Boolean determines whether userspace wants to set or unset the bits set in the mask.
I don't know what the usual kernel convention is for this kind of ioctl, so I'm happy with whatever you all think is best.
Alice
Carlos Llamas
maintainerRe: [PATCH 1/4] binder: introduce BINDER_SET_PROC_FLAGS ioctl
Quoted 47 lines · Alice Ryhl
Quoted 30 lines · Carlos Llamas
This new ioctl enables userspace to control the individual behavior of the 'struct
binder_proc' instance via flags. The driver validates and returns the supported subset. Some existing ioctls are migrated to use these flags in subsequent commits.Suggested-by Arve Hjønnevåg <[email protected]>Signed-off-by Carlos Llamas <[email protected]>--- drivers/android/binder.c | 25 +++++++++++++++++++++++++ drivers/android/binder_internal.h | 4 +++- include/uapi/linux/android/binder.h | 6 ++++++ 3 files changed, 34 insertions(+), 1 deletion(-)drivers/android/binder.c +9
-0@@ -5334,6 +5334,26 @@ static int binder_ioctl_get_extended_error(struct binder_thread *thread,return 0 ; } +static int binder_ioctl_set_proc_flags (struct binder_proc *proc, +u32 __user *user) +{ +u32 flags; + +if (get_user (flags, user)) +return -EFAULT ; + +binder_inner_proc_lock (proc);drivers/android/binder.c static int binder_ioctl_get_extended_error(struct binder_thread *thread, · line 5355
Quoted 10 lines
drivers/android/binder.c +10
-0+ flags &=PF_SUPPORTED_FLAGS_MASK ; + proc->flags = flags; +binder_inner_proc_unlock (proc); + +/* confirm supported flags with user */ +if (put_user (flags, user)) +return -EFAULT ; + +return 0 ; +}I'm just thinking out loud here, but is this the best API for this ioctl? Using this API, if I want to toggle the oneway-spam-detection flag, then I can't do so without knowing the value of all other flags, and I also need to synchronize all calls to this ioctl.
Quoted 3 lines
That's fine for the current use-case where these flags are only set during startup, but are we confident that no future flag will be toggled while a process is active?
hmmm, this is a very good point. It would probably lead to userspace having to cache its flags for every binder instance. This is not a good solution at all.
Quoted 11 lines
How about these alternatives?
1. Userspace passes two masks, one containing bits to set, and another containing bits to unset. Userspace returns new value of flags. (If the same bit is set in both masks, we can fail with EINVAL.)
2. Compare and swap. Userspace passes the expected previous value and the desired new value. The kernel returns the actual previous value and updates it only if userspace gave the right previous value.
Quoted 6 lines
3. Set or unset only. Userspace passes a boolean and a mask. Boolean determines whether userspace wants to set or unset the bits set in the mask.
I don't know what the usual kernel convention is for this kind of ioctl, so I'm happy with whatever you all think is best.
I've never come across these types of alternatives personally. What I've seen however, is the typical SET/GET ioctl pairs. This is a "simpler" interface I guess but it has the downside of an extra roundtrip. e.g.
ioctl(fd, BINDER_GET_PROC_FLAGS, &flags); flags |= BF_LARGE_HANDLES; ioctl(fd, BINDER_SET_PROC_FLAGS, &flags);
What seems tempting about the SET/GET pair is that we could replace the BINDER_ENABLE_ONEWAY_SPAM_DETECTION with the SET. Instead of maintaining legacy code for the "deprecated" ioctl.
wdyt?
I'll have a second look at the alternatives you mentioned. Perhaps I can reference some other existing ioctl that does something similar.
--
Carlos Llamas
Alice Ryhl
maintainerRe: [PATCH 1/4] binder: introduce BINDER_SET_PROC_FLAGS ioctl
Quoted 49 lines · Carlos Llamas
Quoted 47 lines · Alice Ryhl
Quoted 30 lines · Carlos Llamas
This new ioctl enables userspace to control the individual behavior of the 'struct
binder_proc' instance via flags. The driver validates and returns the supported subset. Some existing ioctls are migrated to use these flags in subsequent commits.Suggested-by Arve Hjønnevåg <[email protected]>Signed-off-by Carlos Llamas <[email protected]>--- drivers/android/binder.c | 25 +++++++++++++++++++++++++ drivers/android/binder_internal.h | 4 +++- include/uapi/linux/android/binder.h | 6 ++++++ 3 files changed, 34 insertions(+), 1 deletion(-)drivers/android/binder.c +9
-0@@ -5334,6 +5334,26 @@ static int binder_ioctl_get_extended_error(struct binder_thread *thread,return 0 ; } +static int binder_ioctl_set_proc_flags (struct binder_proc *proc, +u32 __user *user) +{ +u32 flags; + +if (get_user (flags, user)) +return -EFAULT ; + +binder_inner_proc_lock (proc);drivers/android/binder.c static int binder_ioctl_get_extended_error(struct binder_thread *thread, · line 5355
Quoted 10 lines
drivers/android/binder.c +10
-0+ flags &=PF_SUPPORTED_FLAGS_MASK ; + proc->flags = flags; +binder_inner_proc_unlock (proc); + +/* confirm supported flags with user */ +if (put_user (flags, user)) +return -EFAULT ; + +return 0 ; +}I'm just thinking out loud here, but is this the best API for this ioctl? Using this API, if I want to toggle the oneway-spam-detection flag, then I can't do so without knowing the value of all other flags, and I also need to synchronize all calls to this ioctl.
Quoted 14 lines
Quoted 3 lines
That's fine for the current use-case where these flags are only set during startup, but are we confident that no future flag will be toggled while a process is active?
hmmm, this is a very good point. It would probably lead to userspace having to cache its flags for every binder instance. This is not a good solution at all.
Quoted 6 lines
How about these alternatives?
1. Userspace passes two masks, one containing bits to set, and another containing bits to unset. Userspace returns new value of flags. (If the same bit is set in both masks, we can fail with EINVAL.)
To add to this one, one could also say that if a bit is set in both, then the value is toggled.
Quoted 19 lines
Quoted 10 lines
2. Compare and swap. Userspace passes the expected previous value and the desired new value. The kernel returns the actual previous value and updates it only if userspace gave the right previous value.
3. Set or unset only. Userspace passes a boolean and a mask. Boolean determines whether userspace wants to set or unset the bits set in the mask.
I don't know what the usual kernel convention is for this kind of ioctl, so I'm happy with whatever you all think is best.
I've never come across these types of alternatives personally. What I've seen however, is the typical SET/GET ioctl pairs. This is a "simpler" interface I guess but it has the downside of an extra roundtrip. e.g.
ioctl(fd, BINDER_GET_PROC_FLAGS, &flags); flags |= BF_LARGE_HANDLES; ioctl(fd, BINDER_SET_PROC_FLAGS, &flags);
Quoted 8 lines
What seems tempting about the SET/GET pair is that we could replace the
BINDER_ENABLE_ONEWAY_SPAM_DETECTIONwith the SET. Instead of maintaining legacy code for the "deprecated" ioctl.wdyt?
I'll have a second look at the alternatives you mentioned. Perhaps I can reference some other existing ioctl that does something similar.
Hmm. I don't think a get/set pair improves the situation much. Userspace still needs a global mutex for making changes to the flag in that case. Otherwise, two threads changing two different flags in parallel could result in a race condition where one of the changes is lost.
Alice
Carlos Llamas
maintainerRe: [PATCH 1/4] binder: introduce BINDER_SET_PROC_FLAGS ioctl
Quoted 93 lines · Alice Ryhl
Quoted 49 lines · Carlos Llamas
Quoted 47 lines · Alice Ryhl
Quoted 30 lines · Carlos Llamas
This new ioctl enables userspace to control the individual behavior of the 'struct
binder_proc' instance via flags. The driver validates and returns the supported subset. Some existing ioctls are migrated to use these flags in subsequent commits.Suggested-by Arve Hjønnevåg <[email protected]>Signed-off-by Carlos Llamas <[email protected]>--- drivers/android/binder.c | 25 +++++++++++++++++++++++++ drivers/android/binder_internal.h | 4 +++- include/uapi/linux/android/binder.h | 6 ++++++ 3 files changed, 34 insertions(+), 1 deletion(-)drivers/android/binder.c +9
-0@@ -5334,6 +5334,26 @@ static int binder_ioctl_get_extended_error(struct binder_thread *thread,return 0 ; } +static int binder_ioctl_set_proc_flags (struct binder_proc *proc, +u32 __user *user) +{ +u32 flags; + +if (get_user (flags, user)) +return -EFAULT ; + +binder_inner_proc_lock (proc);drivers/android/binder.c static int binder_ioctl_get_extended_error(struct binder_thread *thread, · line 5355
Quoted 10 lines
drivers/android/binder.c +10
-0+ flags &=PF_SUPPORTED_FLAGS_MASK ; + proc->flags = flags; +binder_inner_proc_unlock (proc); + +/* confirm supported flags with user */ +if (put_user (flags, user)) +return -EFAULT ; + +return 0 ; +}I'm just thinking out loud here, but is this the best API for this ioctl? Using this API, if I want to toggle the oneway-spam-detection flag, then I can't do so without knowing the value of all other flags, and I also need to synchronize all calls to this ioctl.
Quoted 14 lines
Quoted 3 lines
That's fine for the current use-case where these flags are only set during startup, but are we confident that no future flag will be toggled while a process is active?
hmmm, this is a very good point. It would probably lead to userspace having to cache its flags for every binder instance. This is not a good solution at all.
Quoted 6 lines
How about these alternatives?
1. Userspace passes two masks, one containing bits to set, and another containing bits to unset. Userspace returns new value of flags. (If the same bit is set in both masks, we can fail with EINVAL.)
To add to this one, one could also say that if a bit is set in both, then the value is toggled.
Quoted 25 lines
Quoted 10 lines
2. Compare and swap. Userspace passes the expected previous value and the desired new value. The kernel returns the actual previous value and updates it only if userspace gave the right previous value.
3. Set or unset only. Userspace passes a boolean and a mask. Boolean determines whether userspace wants to set or unset the bits set in the mask.
I don't know what the usual kernel convention is for this kind of ioctl, so I'm happy with whatever you all think is best.
I've never come across these types of alternatives personally. What I've seen however, is the typical SET/GET ioctl pairs. This is a "simpler" interface I guess but it has the downside of an extra roundtrip. e.g.
ioctl(fd, BINDER_GET_PROC_FLAGS, &flags); flags |= BF_LARGE_HANDLES; ioctl(fd, BINDER_SET_PROC_FLAGS, &flags);What seems tempting about the SET/GET pair is that we could replace the
BINDER_ENABLE_ONEWAY_SPAM_DETECTIONwith the SET. Instead of maintaining legacy code for the "deprecated" ioctl.wdyt?
Quoted 8 lines
Quoted 2 lines
I'll have a second look at the alternatives you mentioned. Perhaps I can reference some other existing ioctl that does something similar.
Hmm. I don't think a get/set pair improves the situation much. Userspace still needs a global mutex for making changes to the flag in that case. Otherwise, two threads changing two different flags in parallel could result in a race condition where one of the changes is lost.
I'm not sure this would ever be a problem, libbinder currently has a mutex for this kind of things already and it seems unlikely that these process-wide flags would be toggled outside of initial config. However, it is worth discussing for sure as things can change.
I'm mainly concern about overloading what should be a very simple ioctl. With that said, I think one more option that is fairly simple/common is a SET/CLEAR ioctl pair. A little adaption from your 3rd option I think? I would be fine with that.
Unfortunately, this would require new ioctl IDs so we would still need to maintain the ONEWAY_SPAM thing. I suppose that's ok.
Sounds good?
--
Carlos Llamas
Alice Ryhl
maintainerRe: [PATCH 1/4] binder: introduce BINDER_SET_PROC_FLAGS ioctl
Quoted 109 lines · Carlos Llamas
Quoted 93 lines · Alice Ryhl
Quoted 49 lines · Carlos Llamas
Quoted 47 lines · Alice Ryhl
Quoted 30 lines · Carlos Llamas
This new ioctl enables userspace to control the individual behavior of the 'struct
binder_proc' instance via flags. The driver validates and returns the supported subset. Some existing ioctls are migrated to use these flags in subsequent commits.Suggested-by Arve Hjønnevåg <[email protected]>Signed-off-by Carlos Llamas <[email protected]>--- drivers/android/binder.c | 25 +++++++++++++++++++++++++ drivers/android/binder_internal.h | 4 +++- include/uapi/linux/android/binder.h | 6 ++++++ 3 files changed, 34 insertions(+), 1 deletion(-)drivers/android/binder.c +9
-0@@ -5334,6 +5334,26 @@ static int binder_ioctl_get_extended_error(struct binder_thread *thread,return 0 ; } +static int binder_ioctl_set_proc_flags (struct binder_proc *proc, +u32 __user *user) +{ +u32 flags; + +if (get_user (flags, user)) +return -EFAULT ; + +binder_inner_proc_lock (proc);drivers/android/binder.c static int binder_ioctl_get_extended_error(struct binder_thread *thread, · line 5355
Quoted 10 lines
drivers/android/binder.c +10
-0+ flags &=PF_SUPPORTED_FLAGS_MASK ; + proc->flags = flags; +binder_inner_proc_unlock (proc); + +/* confirm supported flags with user */ +if (put_user (flags, user)) +return -EFAULT ; + +return 0 ; +}I'm just thinking out loud here, but is this the best API for this ioctl? Using this API, if I want to toggle the oneway-spam-detection flag, then I can't do so without knowing the value of all other flags, and I also need to synchronize all calls to this ioctl.
Quoted 14 lines
Quoted 3 lines
That's fine for the current use-case where these flags are only set during startup, but are we confident that no future flag will be toggled while a process is active?
hmmm, this is a very good point. It would probably lead to userspace having to cache its flags for every binder instance. This is not a good solution at all.
Quoted 6 lines
How about these alternatives?
1. Userspace passes two masks, one containing bits to set, and another containing bits to unset. Userspace returns new value of flags. (If the same bit is set in both masks, we can fail with EINVAL.)
To add to this one, one could also say that if a bit is set in both, then the value is toggled.
Quoted 25 lines
Quoted 10 lines
2. Compare and swap. Userspace passes the expected previous value and the desired new value. The kernel returns the actual previous value and updates it only if userspace gave the right previous value.
3. Set or unset only. Userspace passes a boolean and a mask. Boolean determines whether userspace wants to set or unset the bits set in the mask.
I don't know what the usual kernel convention is for this kind of ioctl, so I'm happy with whatever you all think is best.
I've never come across these types of alternatives personally. What I've seen however, is the typical SET/GET ioctl pairs. This is a "simpler" interface I guess but it has the downside of an extra roundtrip. e.g.
ioctl(fd, BINDER_GET_PROC_FLAGS, &flags); flags |= BF_LARGE_HANDLES; ioctl(fd, BINDER_SET_PROC_FLAGS, &flags);What seems tempting about the SET/GET pair is that we could replace the
BINDER_ENABLE_ONEWAY_SPAM_DETECTIONwith the SET. Instead of maintaining legacy code for the "deprecated" ioctl.wdyt?
Quoted 8 lines
Quoted 2 lines
I'll have a second look at the alternatives you mentioned. Perhaps I can reference some other existing ioctl that does something similar.
Hmm. I don't think a get/set pair improves the situation much. Userspace still needs a global mutex for making changes to the flag in that case. Otherwise, two threads changing two different flags in parallel could result in a race condition where one of the changes is lost.
I'm not sure this would ever be a problem, libbinder currently has a mutex for this kind of things already and it seems unlikely that these process-wide flags would be toggled outside of initial config. However, it is worth discussing for sure as things can change.
Quoted 7 lines
I'm mainly concern about overloading what should be a very simple ioctl. With that said, I think one more option that is fairly simple/common is a SET/CLEAR ioctl pair. A little adaption from your 3rd option I think? I would be fine with that.
Unfortunately, this would require new ioctl IDs so we would still need to maintain the
ONEWAY_SPAMthing. I suppose that's ok.
As long as your decision has taken the things I mentioned into account, I'm happy with whatever you think is best. If you think a GET/SET pair is reasonable because binder already has a mutex, then that's fine with me.
Alice
Carlos Llamas
maintainerPATCH 2/4 binder: migrate ioctl to new PF_SPAM_DETECTION
In reply to Carlos LlamasDeprecate the BINDER_ENABLE_ONEWAY_SPAM_DETECTION ioctl in favor of the new PF_SPAM_DETECTION flag. The ioctl can be lumped together with other flags to avoid a separate system call. The driver still supports the old ioctl for backwards compatibility.
--- drivers/android/binder.c | 7 ++++--- drivers/android/binder_internal.h | 1 - include/uapi/linux/android/binder.h | 8 ++++++-- 3 files changed, 10 insertions(+), 6 deletions(-)
drivers/android/binder.c +4 -3
@@ -4486,8 +4486,8 @@ static int binder_thread_read(struct binder_proc *proc,case BINDER_WORK_TRANSACTION_COMPLETE :case BINDER_WORK_TRANSACTION_PENDING :case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT : { -if (proc->oneway_spam_detection_enabled && - w->type ==BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT ) +if (proc->flags &PF_SPAM_DETECTION && + w->type ==BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT ) cmd =BR_ONEWAY_SPAM_SUSPECT ;else if (w->type ==BINDER_WORK_TRANSACTION_PENDING ) cmd =BR_TRANSACTION_PENDING_FROZEN ;
@@ -5553,7 +5553,8 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) goto err; }binder_inner_proc_lock (proc); - proc->oneway_spam_detection_enabled = (bool )enable; + proc->flags &= ~PF_SPAM_DETECTION ; + proc->flags |= enable &PF_SPAM_DETECTION ;binder_inner_proc_unlock (proc);break ; }
drivers/android/binder_internal.h +0 -1
@@ -434,7 +434,6 @@ struct binder_proc {spinlock_t inner_lock;spinlock_t outer_lock;struct dentry *binderfs_entry; -bool oneway_spam_detection_enabled; };/**
include/uapi/linux/android/binder.h +6 -2
@@ -253,7 +253,9 @@ struct binder_extended_error {/* Used with BINDER_SET_PROC_FLAGS ioctl */ enum proc_flags { -PF_SUPPORTED_FLAGS_MASK , +PF_SPAM_DETECTION = (1 <<0 ),/* enable oneway spam detection */ + +PF_SUPPORTED_FLAGS_MASK =PF_SPAM_DETECTION , };enum {
@@ -269,9 +271,11 @@ enum { BINDER_SET_CONTEXT_MGR_EXT = _IOW('b', 13, struct flat_binder_object), BINDER_FREEZE = _IOW('b', 14, struct binder_freeze_info), BINDER_GET_FROZEN_INFO = _IOWR('b', 15, struct binder_frozen_status_info), -BINDER_ENABLE_ONEWAY_SPAM_DETECTION =_IOW ('b' ,16 , __u32), BINDER_GET_EXTENDED_ERROR = _IOWR('b', 17, struct binder_extended_error),BINDER_SET_PROC_FLAGS =_IOWR ('b' ,18 , __u32), + +/* This is deprecated, use BINDER_SET_PROC_FLAGS instead. */ + BINDER_ENABLE_ONEWAY_SPAM_DETECTION = _IOW('b', 16, __u32), };/*
Signature · 1 line
2.44.0.683.g7961c838ac-goog
Alice Ryhl
maintainerRe: [PATCH 2/4] binder: migrate ioctl to new PF_SPAM_DETECTION
drivers/android/binder.c static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) · line 5557
Quoted 7 lines · Carlos Llamas
drivers/android/binder.c +2
-1@@ -5553,7 +5553,8 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) goto err; }binder_inner_proc_lock (proc); - proc->oneway_spam_detection_enabled = (bool )enable; + proc->flags &= ~PF_SPAM_DETECTION ; + proc->flags |= enable &PF_SPAM_DETECTION ;
The bitwise and in enable & PF_SPAM_DETECTION only works because PF_SPAM_DETECTION happens to be equal to 1. This seems pretty fragile to me. Would you be willing to do this instead?
proc->flags &= ~PF_SPAM_DETECTION;
if (enable)
proc->flags |= PF_SPAM_DETECTION;
drivers/android/binder.c static int binder_thread_read(struct binder_proc *proc, · line 4490
Quoted 4 lines · Carlos Llamas
drivers/android/binder.c +2
-2-if (proc->oneway_spam_detection_enabled && - w->type ==BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT ) +if (proc->flags &PF_SPAM_DETECTION && + w->type ==BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT )
Maybe I am just not sufficiently familiar with C, but I had to look up the operator precedence rules for this one. Could we add parenthesises around proc->flags & PF_SPAM_DETECTION? Or even define a macro for it?
Alice
Carlos Llamas
maintainerRe: [PATCH 2/4] binder: migrate ioctl to new PF_SPAM_DETECTION
Quoted 13 lines · Alice Ryhl
drivers/android/binder.c static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) · line 5557
Quoted 7 lines · Carlos Llamas
drivers/android/binder.c +2
-1@@ -5553,7 +5553,8 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) goto err; }binder_inner_proc_lock (proc); - proc->oneway_spam_detection_enabled = (bool )enable; + proc->flags &= ~PF_SPAM_DETECTION ; + proc->flags |= enable &PF_SPAM_DETECTION ;The bitwise and in
enable & PF_SPAM_DETECTIONonly works becausePF_SPAM_DETECTIONhappens to be equal to 1. This seems pretty fragile to me. Would you be willing to do this instead?
Quoted 4 lines
proc->flags&= ~PF_SPAM_DETECTION;
if (enable)proc->flags |= PF_SPAM_DETECTION;
I don't think it is fragile since PF_SPAM_DETECTION is fixed. However, I agree the code is missing context about the flag being bit 0 and your version addresses this problem. So I'll take it for v2, thanks!
Quoted 10 lines
drivers/android/binder.c static int binder_thread_read(struct binder_proc *proc, · line 4490
Quoted 4 lines · Carlos Llamas
drivers/android/binder.c +2
-2-if (proc->oneway_spam_detection_enabled && - w->type ==BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT ) +if (proc->flags &PF_SPAM_DETECTION && + w->type ==BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT )Maybe I am just not sufficiently familiar with C, but I had to look up the operator precedence rules for this one. Could we add parenthesises around
proc->flags & PF_SPAM_DETECTION? Or even define a macro for it?
I think this is fairly common in C but I can definitly add the extra paranthesis if it helps.
--
Carlos Llamas
Re: [PATCH 2/4] binder: migrate ioctl to new PF_SPAM_DETECTION
Quoted 15 lines · Carlos Llamas
Quoted 13 lines · Alice Ryhl
drivers/android/binder.c static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) · line 5557
Quoted 7 lines · Carlos Llamas
drivers/android/binder.c +2
-1@@ -5553,7 +5553,8 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) goto err; }binder_inner_proc_lock (proc); - proc->oneway_spam_detection_enabled = (bool )enable; + proc->flags &= ~PF_SPAM_DETECTION ; + proc->flags |= enable &PF_SPAM_DETECTION ;The bitwise and in
enable & PF_SPAM_DETECTIONonly works becausePF_SPAM_DETECTIONhappens to be equal to 1. This seems pretty fragile to me. Would you be willing to do this instead?
Quoted 8 lines
Quoted 4 lines
proc->flags&= ~PF_SPAM_DETECTION;
if (enable)proc->flags |= PF_SPAM_DETECTION;I don't think it is fragile since
PF_SPAM_DETECTIONis fixed. However, I agree the code is missing context about the flag being bit 0 and your version addresses this problem. So I'll take it for v2, thanks!
Thanks! By fragile I mean that it could result in future mistakes, e.g. somebody could copy this code and use it elsewhere with a different bit flag that might not be bit 0.
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines · Carlos Llamas
drivers/android/binder.c +2
-2-if (proc->oneway_spam_detection_enabled && - w->type ==BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT ) +if (proc->flags &PF_SPAM_DETECTION && + w->type ==BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT )
Quoted 6 lines
Quoted 3 lines
Maybe I am just not sufficiently familiar with C, but I had to look up the operator precedence rules for this one. Could we add parenthesises around
proc->flags & PF_SPAM_DETECTION? Or even define a macro for it?I think this is fairly common in C but I can definitly add the extra paranthesis if it helps.
Yeah, makes sense. Thanks!
With the mentioned changes, you may add:
Alice
Carlos Llamas
maintainerRe: [PATCH 2/4] binder: migrate ioctl to new PF_SPAM_DETECTION
Quoted 21 lines · Alice Ryhl
Quoted 20 lines · Carlos Llamas
Quoted 17 lines · Alice Ryhl
drivers/android/binder.c static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) · line 5557
Quoted 7 lines · Carlos Llamas
drivers/android/binder.c +2
-1@@ -5553,7 +5553,8 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) goto err; }binder_inner_proc_lock (proc); - proc->oneway_spam_detection_enabled = (bool )enable; + proc->flags &= ~PF_SPAM_DETECTION ; + proc->flags |= enable &PF_SPAM_DETECTION ;The bitwise and in
enable & PF_SPAM_DETECTIONonly works becausePF_SPAM_DETECTIONhappens to be equal to 1. This seems pretty fragile to me. Would you be willing to do this instead?
proc->flags&= ~PF_SPAM_DETECTION;
if (enable)proc->flags |= PF_SPAM_DETECTION;
Quoted 7 lines
Quoted 3 lines
I don't think it is fragile since
PF_SPAM_DETECTIONis fixed. However, I agree the code is missing context about the flag being bit 0 and your version addresses this problem. So I'll take it for v2, thanks!Thanks! By fragile I mean that it could result in future mistakes, e.g. somebody could copy this code and use it elsewhere with a different bit flag that might not be bit 0.
Oh, I see. Yeah that would be a problem.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
drivers/android/binder.c static int binder_thread_read(struct binder_proc *proc, · line 4490
Quoted 4 lines · Carlos Llamas
drivers/android/binder.c +2
-2-if (proc->oneway_spam_detection_enabled && - w->type ==BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT ) +if (proc->flags &PF_SPAM_DETECTION && + w->type ==BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT )Maybe I am just not sufficiently familiar with C, but I had to look up the operator precedence rules for this one. Could we add parenthesises around
proc->flags & PF_SPAM_DETECTION? Or even define a macro for it?
Quoted 7 lines
Quoted 2 lines
I think this is fairly common in C but I can definitly add the extra paranthesis if it helps.
Yeah, makes sense. Thanks!
With the mentioned changes, you may add:
Reviewed-by Alice Ryhl <[email protected]>
Done. Thanks!
Carlos Llamas
maintainerPATCH 3/4 binder: add support for PF_LARGE_HANDLES
In reply to Carlos LlamasIntroduce the PF_LARGE_HANDLES flag to enable the use of monotonically increasing counters to generate handles. This improves performance in transactions when dealing with a large number of references.
The legacy logic performs an inorder traversal of an rbtree to find the smallest unused handle. This limitation is due to userspace using the handles as indexes (e.g. in vectors). The new logic scales much better but requires userspace to support large handle numbers.
The benchmark below with 100,000 references shows the performance gains in binder_get_ref_for_node_olocked() calls with PF_LARGE_HANDLES.
[ 167.855945] binder_get_ref_for_node_olocked: 17us (flag on) [ 237.088072] binder_get_ref_for_node_olocked: 18178us (flag off)
--- drivers/android/binder.c | 44 ++++++++++++++++++++++------- drivers/android/binder_internal.h | 3 ++ include/uapi/linux/android/binder.h | 3 +- 3 files changed, 39 insertions(+), 11 deletions(-)
drivers/android/binder.c +34 -10
@@ -1045,6 +1045,35 @@ static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,return NULL ; } +static u32 next_ref_desc (struct binder_proc *proc,struct binder_node *node) +{ +struct binder_ref *ref; +struct rb_node *n; +u32 desc; + +/* Handle 0 is reserved for context manager refs */ +if (node == proc->context->binder_context_mgr_node) +return 0 ; + +/* Get the next handle (non-zero) */ +if (proc->flags &PF_LARGE_HANDLES ) +return proc->next_ref_desc++ ? : proc->next_ref_desc++; + +/* +* Userspace doesn't support large handles. Find the smallest +* unused descriptor by doing an in-order traversal (slow). +*/ + desc =1 ; +for (n =rb_first (&proc->refs_by_desc); n; n =rb_next (n)) { + ref = rb_entry(n, struct binder_ref, rb_node_desc); +if (ref->data.desc > desc) +break ; + desc = ref->data.desc +1 ; + } + +return desc; +} +/** * binder_get_ref_for_node_olocked() - get the ref associated with given node * @proc: binder_proc that owns the ref
@@ -1068,11 +1097,9 @@ static struct binder_ref *binder_get_ref_for_node_olocked(struct binder_node *node, struct binder_ref *new_ref) { - struct binder_context *context = proc->context; struct rb_node **p = &proc->refs_by_node.rb_node;struct rb_node *parent =NULL ;struct binder_ref *ref; -struct rb_node *n;while (*p) { parent = *p;
@@ -1095,14 +1122,8 @@ static struct binder_ref *binder_get_ref_for_node_olocked(rb_link_node (&new_ref->rb_node_node, parent, p);rb_insert_color (&new_ref->rb_node_node, &proc->refs_by_node); - new_ref->data.desc = (node == context->binder_context_mgr_node) ?0 :1 ; -for (n =rb_first (&proc->refs_by_desc); n !=NULL ; n =rb_next (n)) { - ref = rb_entry(n, struct binder_ref, rb_node_desc); -if (ref->data.desc > new_ref->data.desc) -break ; - new_ref->data.desc = ref->data.desc +1 ; - } - +retry: + new_ref->data.desc =next_ref_desc (proc, node); p = &proc->refs_by_desc.rb_node;while (*p) { parent = *p;
@@ -1112,6 +1133,8 @@ static struct binder_ref *binder_get_ref_for_node_olocked( p = &(*p)->rb_left; else if (new_ref->data.desc > ref->data.desc) p = &(*p)->rb_right; + else if (proc->flags & PF_LARGE_HANDLES) + goto retry; elseBUG (); }
@@ -5663,6 +5686,7 @@ static int binder_open(struct inode *nodp, struct file *filp)get_task_struct (current->group_leader); proc->tsk = current->group_leader; proc->cred =get_cred (filp->f_cred); + proc->next_ref_desc =1 ;INIT_LIST_HEAD (&proc->todo);init_waitqueue_head (&proc->freeze_wait); proc->default_priority =task_nice (current);
drivers/android/binder_internal.h +3 -0
@@ -337,6 +337,8 @@ struct binder_ref {* (protected by @outer_lock) * @refs_by_node: rbtree of refs ordered by ref->node * (protected by @outer_lock) +* @next_ref_desc: monotonic wrap-around counter to get the next handle +* (protected by @outer_lock) * @waiting_threads: threads currently waiting for proc work * (protected by @inner_lock) * @pid PID of group_leader of process
@@ -407,6 +409,7 @@ struct binder_proc {struct rb_root nodes;struct rb_root refs_by_desc;struct rb_root refs_by_node; +u32 next_ref_desc;struct list_head waiting_threads;int pid;struct task_struct *tsk;
include/uapi/linux/android/binder.h +2 -1
@@ -254,8 +254,9 @@ struct binder_extended_error {/* Used with BINDER_SET_PROC_FLAGS ioctl */ enum proc_flags {PF_SPAM_DETECTION = (1 <<0 ),/* enable oneway spam detection */ +PF_LARGE_HANDLES = (1 <<1 ),/* use large reference handles */ -PF_SUPPORTED_FLAGS_MASK =PF_SPAM_DETECTION , +PF_SUPPORTED_FLAGS_MASK = (PF_SPAM_DETECTION |PF_LARGE_HANDLES ), };enum {
Signature · 1 line
2.44.0.683.g7961c838ac-goog
Re: [PATCH 3/4] binder: add support for PF_LARGE_HANDLES
Quoted 13 lines · Carlos Llamas
Introduce the
PF_LARGE_HANDLESflag to enable the use of monotonically increasing counters to generate handles. This improves performance in transactions when dealing with a large number of references.The legacy logic performs an inorder traversal of an rbtree to find the smallest unused handle. This limitation is due to userspace using the handles as indexes (e.g. in vectors). The new logic scales much better but requires userspace to support large handle numbers.
The benchmark below with 100,000 references shows the performance gains in
binder_get_ref_for_node_olocked()calls withPF_LARGE_HANDLES.
Quoted 6 lines
[ 167.855945] binder_get_ref_for_node_olocked: 17us (flag on) [ 237.088072] binder_get_ref_for_node_olocked: 18178us (flag off)Suggested-by Tim Murray <[email protected]>Suggested-by Alice Ryhl <[email protected]>Signed-off-by Carlos Llamas <[email protected]>
Carlos Llamas
maintainerPATCH 4/4 binder: fix max_thread type inconsistency
In reply to Carlos LlamasThe type defined for the BINDER_SET_MAX_THREADS ioctl was changed from size_t to __u32 in order to avoid incompatibility issues between 32 and 64-bit kernels. However, the internal types used to copy from user and store the value were never updated. Use u32 to fix the inconsistency.
--- drivers/android/binder.c | 2 +- drivers/android/binder_internal.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/android/binder.c +1 -1
@@ -5408,7 +5408,7 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) goto err;break ;case BINDER_SET_MAX_THREADS : { -int max_threads; +u32 max_threads;if (copy_from_user (&max_threads, ubuf,sizeof (max_threads))) {
drivers/android/binder_internal.h +1 -1
@@ -426,7 +426,7 @@ struct binder_proc {struct list_head todo;struct binder_stats stats;struct list_head delivered_death; -int max_threads; +u32 max_threads;int requested_threads;int requested_threads_started;int tmp_ref;
Signature · 1 line
2.44.0.683.g7961c838ac-goog
Greg Kroah-Hartman
maintainerRe: [PATCH 4/4] binder: fix max_thread type inconsistency
Quoted 5 lines · Carlos Llamas
The type defined for the
BINDER_SET_MAX_THREADSioctl was changed fromsize_tto__u32in order to avoid incompatibility issues between 32 and 64-bit kernels. However, the internal types used to copy from user and store the value were never updated. Use u32 to fix the inconsistency.
Quoted 8 lines
Fixes a9350fc859ae ("staging: android: binder: fix BINDER_SET_MAX_THREADS declaration")Reported-by Arve Hjønnevåg <[email protected]>Signed-off-by Carlos Llamas <[email protected]>--- drivers/android/binder.c | 2 +- drivers/android/binder_internal.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Why does only patch 4/4 need to go into the tree now, and as a stable backport, but the first 3 do not? Shouldn't this be two different series of patches, one 3 long, and one 1 long, to go to the different branches (next and linus)?
thanks,
greg k-h
Carlos Llamas
maintainerRe: [PATCH 4/4] binder: fix max_thread type inconsistency
Quoted 15 lines · Greg Kroah-Hartman
Quoted 5 lines · Carlos Llamas
The type defined for the
BINDER_SET_MAX_THREADSioctl was changed fromsize_tto__u32in order to avoid incompatibility issues between 32 and 64-bit kernels. However, the internal types used to copy from user and store the value were never updated. Use u32 to fix the inconsistency.Quoted 8 lines
Fixes a9350fc859ae ("staging: android: binder: fix BINDER_SET_MAX_THREADS declaration")Reported-by Arve Hjønnevåg <[email protected]>Signed-off-by Carlos Llamas <[email protected]>--- drivers/android/binder.c | 2 +- drivers/android/binder_internal.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Quoted 4 lines
Why does only patch 4/4 need to go into the tree now, and as a stable backport, but the first 3 do not? Shouldn't this be two different series of patches, one 3 long, and one 1 long, to go to the different branches (next and linus)?
Yes, that is correct. Only patch 4/4 would need to be picked for linus now and for stable. The others would go to next. Sorry, I was not aware that sending them separately would be preferred.
I'll drop 4/4 patch from the series in v2. Let me know if you still need me to send it again separately.
Thanks,
Carlos Llamas
Greg Kroah-Hartman
maintainerRe: [PATCH 4/4] binder: fix max_thread type inconsistency
Quoted 21 lines · Carlos Llamas
Quoted 15 lines · Greg Kroah-Hartman
Quoted 5 lines · Carlos Llamas
The type defined for the
BINDER_SET_MAX_THREADSioctl was changed fromsize_tto__u32in order to avoid incompatibility issues between 32 and 64-bit kernels. However, the internal types used to copy from user and store the value were never updated. Use u32 to fix the inconsistency.Quoted 8 lines
Fixes a9350fc859ae ("staging: android: binder: fix BINDER_SET_MAX_THREADS declaration")Reported-by Arve Hjønnevåg <[email protected]>Signed-off-by Carlos Llamas <[email protected]>--- drivers/android/binder.c | 2 +- drivers/android/binder_internal.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)Quoted 4 lines
Why does only patch 4/4 need to go into the tree now, and as a stable backport, but the first 3 do not? Shouldn't this be two different series of patches, one 3 long, and one 1 long, to go to the different branches (next and linus)?
Quoted 6 lines
Yes, that is correct. Only patch 4/4 would need to be picked for linus now and for stable. The others would go to next. Sorry, I was not aware that sending them separately would be preferred.
I'll drop 4/4 patch from the series in v2. Let me know if you still need me to send it again separately.
Please do, thanks!
greg k-h
Carlos Llamas
maintainerRe: [PATCH 4/4] binder: fix max_thread type inconsistency
Quoted 26 lines · Greg Kroah-Hartman
Quoted 25 lines · Carlos Llamas
Quoted 15 lines · Greg Kroah-Hartman
Quoted 5 lines · Carlos Llamas
The type defined for the
BINDER_SET_MAX_THREADSioctl was changed fromsize_tto__u32in order to avoid incompatibility issues between 32 and 64-bit kernels. However, the internal types used to copy from user and store the value were never updated. Use u32 to fix the inconsistency.Quoted 8 lines
Fixes a9350fc859ae ("staging: android: binder: fix BINDER_SET_MAX_THREADS declaration")Reported-by Arve Hjønnevåg <[email protected]>Signed-off-by Carlos Llamas <[email protected]>--- drivers/android/binder.c | 2 +- drivers/android/binder_internal.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)Quoted 4 lines
Why does only patch 4/4 need to go into the tree now, and as a stable backport, but the first 3 do not? Shouldn't this be two different series of patches, one 3 long, and one 1 long, to go to the different branches (next and linus)?
Yes, that is correct. Only patch 4/4 would need to be picked for linus now and for stable. The others would go to next. Sorry, I was not aware that sending them separately would be preferred.
Quoted 7 lines
Quoted 2 lines
I'll drop 4/4 patch from the series in v2. Let me know if you still need me to send it again separately.
Please do, thanks!
greg k-h
Ok, done. The separated patch is here:
https://lore.kernel.org/all/[email protected]/
Thanks,
Carlos Llamas