Skip to content

Commit b7e6a89

Browse files
Martijn Coenengregkh
authored andcommitted
binder: Add BINDER_GET_NODE_INFO_FOR_REF ioctl.
This allows the context manager to retrieve information about nodes that it holds a reference to, such as the current number of references to those nodes. Such information can for example be used to determine whether the servicemanager is the only process holding a reference to a node. This information can then be passed on to the process holding the node, which can in turn decide whether it wants to shut down to reduce resource usage. Signed-off-by: Martijn Coenen <maco@android.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6b6642d commit b7e6a89

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎drivers/android/binder.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4661,6 +4661,42 @@ static int binder_ioctl_set_ctx_mgr(struct file *filp)
46614661
return ret;
46624662
}
46634663

4664+
static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4665+
struct binder_node_info_for_ref *info)
4666+
{
4667+
struct binder_node *node;
4668+
struct binder_context *context = proc->context;
4669+
__u32 handle = info->handle;
4670+
4671+
if (info->strong_count || info->weak_count || info->reserved1 ||
4672+
info->reserved2 || info->reserved3) {
4673+
binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4674+
proc->pid);
4675+
return -EINVAL;
4676+
}
4677+
4678+
/* This ioctl may only be used by the context manager */
4679+
mutex_lock(&context->context_mgr_node_lock);
4680+
if (!context->binder_context_mgr_node ||
4681+
context->binder_context_mgr_node->proc != proc) {
4682+
mutex_unlock(&context->context_mgr_node_lock);
4683+
return -EPERM;
4684+
}
4685+
mutex_unlock(&context->context_mgr_node_lock);
4686+
4687+
node = binder_get_node_from_ref(proc, handle, true, NULL);
4688+
if (!node)
4689+
return -EINVAL;
4690+
4691+
info->strong_count = node->local_strong_refs +
4692+
node->internal_strong_refs;
4693+
info->weak_count = node->local_weak_refs;
4694+
4695+
binder_put_node(node);
4696+
4697+
return 0;
4698+
}
4699+
46644700
static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
46654701
struct binder_node_debug_info *info)
46664702
{
@@ -4755,6 +4791,25 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
47554791
}
47564792
break;
47574793
}
4794+
case BINDER_GET_NODE_INFO_FOR_REF: {
4795+
struct binder_node_info_for_ref info;
4796+
4797+
if (copy_from_user(&info, ubuf, sizeof(info))) {
4798+
ret = -EFAULT;
4799+
goto err;
4800+
}
4801+
4802+
ret = binder_ioctl_get_node_info_for_ref(proc, &info);
4803+
if (ret < 0)
4804+
goto err;
4805+
4806+
if (copy_to_user(ubuf, &info, sizeof(info))) {
4807+
ret = -EFAULT;
4808+
goto err;
4809+
}
4810+
4811+
break;
4812+
}
47584813
case BINDER_GET_NODE_DEBUG_INFO: {
47594814
struct binder_node_debug_info info;
47604815

‎include/uapi/linux/android/binder.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@ struct binder_node_debug_info {
200200
__u32 has_weak_ref;
201201
};
202202

203+
struct binder_node_info_for_ref {
204+
__u32 handle;
205+
__u32 strong_count;
206+
__u32 weak_count;
207+
__u32 reserved1;
208+
__u32 reserved2;
209+
__u32 reserved3;
210+
};
211+
203212
#define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read)
204213
#define BINDER_SET_IDLE_TIMEOUT _IOW('b', 3, __s64)
205214
#define BINDER_SET_MAX_THREADS _IOW('b', 5, __u32)
@@ -208,6 +217,7 @@ struct binder_node_debug_info {
208217
#define BINDER_THREAD_EXIT _IOW('b', 8, __s32)
209218
#define BINDER_VERSION _IOWR('b', 9, struct binder_version)
210219
#define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info)
220+
#define BINDER_GET_NODE_INFO_FOR_REF _IOWR('b', 12, struct binder_node_info_for_ref)
211221

212222
/*
213223
* NOTE: Two special error codes you should check for when calling

0 commit comments

Comments
 (0)