Skip to contentSkip to conversation

binder: use standard functions to allocate fds

PATCH In review Todd Kjos 4 participants 4 messages Aug 28, 2018 – Aug 30, 2018 Android drivers

lore
Share & tools
lore

Todd Kjos

author maintainer
#0 lore raw

PATCH 1/1 binder: use standard functions to allocate fds

Binder uses internal fs interfaces to allocate and install fds:

__alloc_fd
__fd_install
__close_fd
get_files_struct
put_files_struct

These were used to support the passing of fds between processes as part of a transaction. The actual allocation and installation of the fds in the target process was handled by the sending process so the standard functions, alloc_fd() and fd_install() which assume task==current couldn't be used.

This patch refactors this mechanism so that the fds are allocated and installed by the target process allowing the standard functions to be used.

The sender now creates a list of fd fixups that contains the struct *file and the address to fixup with the new fd once it is allocated. This list is processed by the target process when the transaction is dequeued.

A new error case is introduced by this change. If an async transaction with file descriptors cannot allocate new fds in the target (probably due to out of file descriptors), the transaction is discarded with a log message. In the old implementation this would have been detected in the sender context and failed prior to sending.

---
 drivers/android/Kconfig        |   2 +-
 drivers/android/binder.c       | 387 ++++++++++++++++++++-------------
 drivers/android/binder_trace.h |  36 ++-
 3 files changed, 260 insertions(+), 165 deletions(-)
drivers/android/Kconfig +1 -1
index 432e9ad770703..51e8250d113fa 100644
@@ -10,7 +10,7 @@ if ANDROID
 
 config ANDROID_BINDER_IPC
 	bool "Android Binder IPC Driver"
-	depends on MMU
+	depends on MMU && !CPU_CACHE_VIVT
 	default n
 	---help---
 	  Binder is used in Android for both communication between processes,
drivers/android/binder.c +232 -155

585 lines not on this page · raw message

drivers/android/binder_trace.h +27 -9
index 588eb3ec35070..50f370cd10266 100644
@@ -223,22 +223,40 @@ TRACE_EVENT(binder_transaction_ref_to_ref,
 		  __entry->dest_ref_debug_id, __entry->dest_ref_desc)
 );
 
-TRACE_EVENT(binder_transaction_fd,
-	TP_PROTO(struct binder_transaction *t, int src_fd, int dest_fd),
-	TP_ARGS(t, src_fd, dest_fd),
+TRACE_EVENT(binder_transaction_fd_send,
+	TP_PROTO(struct binder_transaction *t, int fd, size_t offset),
+	TP_ARGS(t, fd, offset),
 
 	TP_STRUCT__entry(
 		__field(int, debug_id)
-		__field(int, src_fd)
-		__field(int, dest_fd)
+		__field(int, fd)
+		__field(size_t, offset)
+	),
+	TP_fast_assign(
+		__entry->debug_id = t->debug_id;
+		__entry->fd = fd;
+		__entry->offset = offset;
+	),
+	TP_printk("transaction=%d src_fd=%d offset=%ld",
+		  __entry->debug_id, __entry->fd, __entry->offset)
+);
+
+TRACE_EVENT(binder_transaction_fd_recv,
+	TP_PROTO(struct binder_transaction *t, int fd, size_t offset),
+	TP_ARGS(t, fd, offset),
+
+	TP_STRUCT__entry(
+		__field(int, debug_id)
+		__field(int, fd)
+		__field(size_t, offset)
 	),
 	TP_fast_assign(
 		__entry->debug_id = t->debug_id;
-		__entry->src_fd = src_fd;
-		__entry->dest_fd = dest_fd;
+		__entry->fd = fd;
+		__entry->offset = offset;
 	),
-	TP_printk("transaction=%d src_fd=%d ==> dest_fd=%d",
-		  __entry->debug_id, __entry->src_fd, __entry->dest_fd)
+	TP_printk("transaction=%d dest_fd=%d offset=%ld",
+		  __entry->debug_id, __entry->fd, __entry->offset)
 );
 
 DECLARE_EVENT_CLASS(binder_buffer_class,
Signature · 1 line
2.19.0.rc0.228.g281dcd1b4d0-goog
#1 lore raw
I love your patch! Perhaps something to improve:

Hi Todd,

I love your patch! Perhaps something to improve:

[auto build test WARNING on staging/staging-testing]
[also build test WARNING on v4.19-rc1 next-20180828]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Todd-Kjos/binder-use-standard-functions-to-allocate-fds/20180829-022204
config: i386-randconfig-x016-201834 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All warnings (new ones prefixed by >>):

In file included from include/trace/define_trace.h:96:0,

                    from drivers/android/binder_trace.h:408,
                    from drivers/android/binder.c:5781:
   drivers/android/./binder_trace.h: In function 'trace_raw_output_binder_transaction_fd_send':
>> drivers/android/./binder_trace.h:240:12: warning: format '%ld' expects argument of type 'long int', but argument 5 has type 'size_t {aka unsigned int}' [-Wformat=]
     TP_printk("transaction=%d src_fd=%d offset=%ld",
               ^
   include/trace/trace_events.h:360:22: note: in definition of macro 'DECLARE_EVENT_CLASS'
     trace_seq_printf(s, print);     \
                         ^~~~~
   include/trace/trace_events.h:79:9: note: in expansion of macro 'PARAMS'
            PARAMS(print));         \
            ^~~~~~
>> drivers/android/./binder_trace.h:226:1: note: in expansion of macro 'TRACE_EVENT'
    TRACE_EVENT(binder_transaction_fd_send,
    ^~~~~~~~~~~
>> drivers/android/./binder_trace.h:240:2: note: in expansion of macro 'TP_printk'
     TP_printk("transaction=%d src_fd=%d offset=%ld",
     ^~~~~~~~~
   In file included from include/trace/trace_events.h:394:0,
                    from include/trace/define_trace.h:96,
                    from drivers/android/binder_trace.h:408,
                    from drivers/android/binder.c:5781:
   drivers/android/./binder_trace.h:240:47: note: format string is defined here
     TP_printk("transaction=%d src_fd=%d offset=%ld",
                                                ~~^
                                                %d
   In file included from include/trace/define_trace.h:96:0,
                    from drivers/android/binder_trace.h:408,
                    from drivers/android/binder.c:5781:
   drivers/android/./binder_trace.h: In function 'trace_raw_output_binder_transaction_fd_recv':
   drivers/android/./binder_trace.h:258:12: warning: format '%ld' expects argument of type 'long int', but argument 5 has type 'size_t {aka unsigned int}' [-Wformat=]
     TP_printk("transaction=%d dest_fd=%d offset=%ld",
               ^
   include/trace/trace_events.h:360:22: note: in definition of macro 'DECLARE_EVENT_CLASS'
     trace_seq_printf(s, print);     \
                         ^~~~~
   include/trace/trace_events.h:79:9: note: in expansion of macro 'PARAMS'
            PARAMS(print));         \
            ^~~~~~
   drivers/android/./binder_trace.h:244:1: note: in expansion of macro 'TRACE_EVENT'
    TRACE_EVENT(binder_transaction_fd_recv,
    ^~~~~~~~~~~
   drivers/android/./binder_trace.h:258:2: note: in expansion of macro 'TP_printk'
     TP_printk("transaction=%d dest_fd=%d offset=%ld",
     ^~~~~~~~~
   In file included from include/trace/trace_events.h:394:0,
                    from include/trace/define_trace.h:96,
                    from drivers/android/binder_trace.h:408,
                    from drivers/android/binder.c:5781:
   drivers/android/./binder_trace.h:258:48: note: format string is defined here
     TP_printk("transaction=%d dest_fd=%d offset=%ld",
                                                 ~~^
                                                 %d

vim +240 drivers/android/./binder_trace.h

225

 > 226	TRACE_EVENT(binder_transaction_fd_send,
   227		TP_PROTO(struct binder_transaction *t, int fd, size_t offset),
   228		TP_ARGS(t, fd, offset),
   229	
   230		TP_STRUCT__entry(
   231			__field(int, debug_id)
   232			__field(int, fd)
   233			__field(size_t, offset)
   234		),
   235		TP_fast_assign(
   236			__entry->debug_id = t->debug_id;
   237			__entry->fd = fd;
   238			__entry->offset = offset;
   239		),
 > 240		TP_printk("transaction=%d src_fd=%d offset=%ld",

241 __entry->debug_id, __entry->fd, __entry->offset)
242 );
243

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#2 lore raw
In reply to Todd Kjos

drivers/android/Kconfig if ANDROID · line 13

Quoted 4 lines

config ANDROID_BINDER_IPC

drivers/android/Kconfig +1 -1
 	bool "Android Binder IPC Driver"
-	depends on MMU
+	depends on MMU && !CPU_CACHE_VIVT

Thats is a purely arm specific symbol which should not be used in common code. Nevermind that there generally should be no good reason for it.

drivers/android/binder.c static int binder_translate_fd(int fd, · line 2489

Quoted 1 line
drivers/android/binder.c +1 -0
+	fixup->offset = (uintptr_t)fdp - (uintptr_t)t->buffer->data;

This looks completely broken. Why would you care at what exact place the fd is placed? Oh, because you share an array with fds with userspace, which is a hell of a bad idea, and then maninpulate that buffer mapped to userspace from kernel threads.

I think we just need to rm -rf drivers/android/binder*.c and be done with it, as this piece of crap should never have been merged to start with.

#3 lore raw
Quoted 9 lines · Christoph Hellwig

drivers/android/Kconfig if ANDROID · line 13

Quoted 5 lines

config ANDROID_BINDER_IPC
bool "Android Binder IPC Driver"

drivers/android/Kconfig +1 -1
-     depends on MMU
+     depends on MMU && !CPU_CACHE_VIVT

Thats is a purely arm specific symbol which should not be used in common code. Nevermind that there generally should be no good reason for it.

It is true that the current design (10+ years old) has this flaw. VIVT cache support was the rationale for using the non-standard functions to allocate file descriptors from the sender context. There are no known cases of recent android releases running on a device with a VIVT cache architecture, but I did want to call this out.

We're working on refactoring to eliminate this issue.

Quoted 7 lines
Quoted 2 lines

+ fixup->offset = (uintptr_t)fdp - (uintptr_t)t->buffer->data;

This looks completely broken. Why would you care at what exact place the fd is placed? Oh, because you share an array with fds with userspace, which is a hell of a bad idea, and then maninpulate that buffer mapped to userspace from kernel threads.

Well, android apps rely on this behavior (and have for 10+ years) so there is no way to avoid the need to manipulate the buffer from the kernel. We are working to refactor the driver to do this using standard mechanisms instead of relying on low-level internal functions.

Quoted 4 lines

I think we just need to rm -rf drivers/android/binder*.c and be done with it, as this piece of crap should never have been merged to start with.