Skip to contentSkip to conversation

file: export functions for binder module

RFC PATCH In review Christian Brauner 7 participants 21 messages Jul 30, 2018 – Aug 2, 2018 VFS

lore
Share & tools
lore
#0 lore raw

cover file: export functions for binder module

Hey,

We currently plan on turning the Android binder and ashmem driver into a module. We have seen more and more requests by users to be able to use the binder and ashmem features without wanting to convince each distro to enable it by default in their kernel. Debian already started to carry patches for turning them into modules.
The main problem is that binder currently uses multiple functions that are not exported and are pretty low-level. The most obvious ones that fall into this category are __alloc_fd(), __fd_install(), get_files_struct(), and put_files_struct(). Being an IPC mechanism binder seems like a reasonable user of these functions.
I don't expect this patch to be mergeable but rather to kick-off a discussion if we can either simply export them as they are or how we can get supportable exports that allow access to struct files_struct.

Thanks!
Christian

Christian Brauner (4):
file: export __alloc_fd()
file: export __fd_install()
file: export get_files_struct()
file: export put_files_struct()

 fs/file.c | 4 ++++
 1 file changed, 4 insertions(+)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/4 file: export __alloc_fd()

The Android binder driver will be turned into a module. Since it uses __alloc_fd() we need to export this function.

Cc Todd Kjos <[email protected]>
Cc Robert Love <[email protected]>
Cc Ben Hutching <[email protected]>
Cc Martijn Coenen <[email protected]>
Cc Arve Hjønnevåg <[email protected]>
---
 fs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/file.c +1 -0
index 7ffd6e9d103d..90382c3ac6e7 100644
@@ -533,6 +533,7 @@ int __alloc_fd(struct files_struct *files,
 	spin_unlock(&files->file_lock);
 	return error;
 }
+EXPORT_SYMBOL(__alloc_fd);
 
 static int alloc_fd(unsigned start, unsigned flags)
 {
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 1/4] file: export __alloc_fd()

Quoted 2 lines · Christian Brauner

The Android binder driver will be turned into a module. Since it uses __alloc_fd() we need to export this function.

Err, hell no.

It should be using an anon fd probably.

#3 lore raw

Re: [PATCH 1/4] file: export __alloc_fd()

Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Christian Brauner

The Android binder driver will be turned into a module. Since it uses __alloc_fd() we need to export this function.

Err, hell no.

It should be using an anon fd probably.

I'm not entirely sure I understand the binder code (... does anyone?) but from what I can see, it intends to open a file descriptor in the process which is the target of the message being sent. That strikes me as wrong-headed; it should be allocating a struct file and passing that file to the other process. When that process receives the message, *it* allocates a file descriptor for itself.

But I think the binder user-space API relies on this. The userspace API seems to rely on passing fd numbers around ... but I'm having trouble figuring most of this user API out. Perhaps Martijn can help here.

maintainer
#4 lore raw

Re: [PATCH 1/4] file: export __alloc_fd()

Quoted 16 lines · Matthew Wilcox
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Christian Brauner

The Android binder driver will be turned into a module. Since it uses __alloc_fd() we need to export this function.

Err, hell no.

It should be using an anon fd probably.

I'm not entirely sure I understand the binder code (... does anyone?) but from what I can see, it intends to open a file descriptor in the process which is the target of the message being sent. That strikes me as wrong-headed; it should be allocating a struct file and passing that file to the other process. When that process receives the message, *it* allocates a file descriptor for itself.

Quoted 3 lines

But I think the binder user-space API relies on this. The userspace API seems to rely on passing fd numbers around ... but I'm having trouble figuring most of this user API out. Perhaps Martijn can help here.

... and there's a perfectly sane solution to that - it's called git rm.

#5 lore raw

Re: [PATCH 1/4] file: export __alloc_fd()

In reply to Matthew Wilcox
Quoted 3 lines · Matthew Wilcox

I'm not entirely sure I understand the binder code (... does anyone?) but from what I can see, it intends to open a file descriptor in the process which is the target of the message being sent.

You're right.

Quoted 4 lines

That strikes
me as wrong-headed; it should be allocating a struct file and passing that file to the other process. When that process receives the message, *it* allocates a file descriptor for itself.ho

We're looking into cleaning this up (historically it was done this way because VIVT caches made this not very efficient), and this is indeed a very good candidate for fixing it.

Quoted 4 lines

But I think the binder user-space API relies on this. The userspace API seems to rely on passing fd numbers around ... but I'm having trouble figuring most of this user API out. Perhaps Martijn can help here.

The UAPI does expect a file descriptor, but we may be able to do the mapping from fd to struct file (and vice-versa) in the kernel driver, so userspace wouldn't notice.

Thanks,
Martijn

#6 lore raw

Re: [PATCH 1/4] file: export __alloc_fd()

Quoted 7 lines · Martijn Coenen
Quoted 3 lines · Matthew Wilcox

I'm not entirely sure I understand the binder code (... does anyone?) but from what I can see, it intends to open a file descriptor in the process which is the target of the message being sent.

You're right.

Quoted 8 lines
Quoted 4 lines

That strikes
me as wrong-headed; it should be allocating a struct file and passing that file to the other process. When that process receives the message, *it* allocates a file descriptor for itself.ho

We're looking into cleaning this up (historically it was done this way because VIVT caches made this not very efficient), and this is indeed a very good candidate for fixing it.

Ah, this wasn't brought up in the original thread when discussing to turn this into a module. If using internal functions like this is going away it makes sense to wait for this work to happen first. Is there a time-frame for this?

Thanks!
Christian

Quoted 12 lines
Quoted 5 lines

But I think the binder user-space API relies on this. The userspace API seems to rely on passing fd numbers around ... but I'm having trouble figuring most of this user API out. Perhaps Martijn can help here.

The UAPI does expect a file descriptor, but we may be able to do the mapping from fd to struct file (and vice-versa) in the kernel driver, so userspace wouldn't notice.

Thanks,
Martijn

#7 lore raw

Re: [PATCH 1/4] file: export __alloc_fd()

Quoted 4 lines · Christian Brauner

Ah, this wasn't brought up in the original thread when discussing to turn this into a module. If using internal functions like this is going away it makes sense to wait for this work to happen first. Is there a time-frame for this?

Not yet, it depends on the solution and who has cycles to work on it.

Martijn

Quoted 16 lines

Thanks!
Christian

Quoted 12 lines
Quoted 5 lines

But I think the binder user-space API relies on this. The userspace API seems to rely on passing fd numbers around ... but I'm having trouble figuring most of this user API out. Perhaps Martijn can help here.

The UAPI does expect a file descriptor, but we may be able to do the mapping from fd to struct file (and vice-versa) in the kernel driver, so userspace wouldn't notice.

Thanks,
Martijn

#8 lore raw

PATCH 2/4 file: export __fd_install()

In reply to Christian Brauner

The Android binder driver will be turned into a module. Since it uses __fd_install() we need to export this function.

Cc Todd Kjos <[email protected]>
Cc Robert Love <[email protected]>
Cc Ben Hutching <[email protected]>
Cc Martijn Coenen <[email protected]>
Cc Arve Hjønnevåg <[email protected]>
---
 fs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/file.c +1 -0
index 90382c3ac6e7..b1ffa7acff69 100644
@@ -607,6 +607,7 @@ void __fd_install(struct files_struct *files, unsigned int fd,
 	rcu_assign_pointer(fdt->fd[fd], file);
 	rcu_read_unlock_sched();
 }
+EXPORT_SYMBOL(__fd_install);
 
 void fd_install(unsigned int fd, struct file *file)
 {
Signature · 1 line
2.17.1
#9 lore raw

Re: [PATCH 2/4] file: export __fd_install()

Quoted 2 lines · Christian Brauner

The Android binder driver will be turned into a module. Since it uses __fd_install() we need to export this function.

Same as the previous one. No driver should be pooking this deep into fd internals (not even non-modular ones for that matter).

#10 lore raw

PATCH 3/4 file: export get_files_struct()

In reply to Christian Brauner

The Android binder driver will be turned into a module. Since it uses get_files_struct() we need to export this function.

Cc Todd Kjos <[email protected]>
Cc Robert Love <[email protected]>
Cc Ben Hutching <[email protected]>
Cc Martijn Coenen <[email protected]>
Cc Arve Hjønnevåg <[email protected]>
---
 fs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/file.c +1 -0
index b1ffa7acff69..430d74642e00 100644
@@ -409,6 +409,7 @@ struct files_struct *get_files_struct(struct task_struct *task)
 
 	return files;
 }
+EXPORT_SYMBOL(get_files_struct);
 
 void put_files_struct(struct files_struct *files)
 {
Signature · 1 line
2.17.1
#11 lore raw

Re: [PATCH 3/4] file: export get_files_struct()

Quoted 2 lines · Christian Brauner

The Android binder driver will be turned into a module. Since it uses get_files_struct() we need to export this function.

Hell no. Please explain why so that we can find a better way.

#12 lore raw

PATCH 4/4 file: export put_files_struct()

In reply to Christian Brauner

The Android binder driver will be turned into a module. Since it uses put_files_struct() we need to export this function.

Cc Todd Kjos <[email protected]>
Cc Robert Love <[email protected]>
Cc Ben Hutching <[email protected]>
Cc Martijn Coenen <[email protected]>
Cc Arve Hjønnevåg <[email protected]>
---
 fs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/file.c +1 -0
index 430d74642e00..89a62165c5a0 100644
@@ -422,6 +422,7 @@ void put_files_struct(struct files_struct *files)
 		kmem_cache_free(files_cachep, files);
 	}
 }
+EXPORT_SYMBOL(put_files_struct);
 
 void reset_files_struct(struct files_struct *files)
 {
Signature · 1 line
2.17.1
#13 lore raw

Re: [PATCH 4/4] file: export put_files_struct()

Quoted 2 lines · Christian Brauner

The Android binder driver will be turned into a module. Since it uses put_files_struct() we need to export this function.

Same as above.

#14 lore raw
In reply to Christian Brauner
Quoted 7 lines · Christian Brauner

Hey,

We currently plan on turning the Android binder and ashmem driver into a module. We have seen more and more requests by users to be able to use the binder and ashmem features without wanting to convince each distro to enable it by default in their kernel. Debian already started to carry patches for turning them into modules.

Yikes. I really wish Debian would stick more to upstream rather than picking random crap like this up.

#15 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 7 lines · Christian Brauner

Hey,

We currently plan on turning the Android binder and ashmem driver into a module. We have seen more and more requests by users to be able to use the binder and ashmem features without wanting to convince each distro to enable it by default in their kernel. Debian already started to carry patches for turning them into modules.

Yikes. I really wish Debian would stick more to upstream rather than picking random crap like this up.

Thanks for the review, Christoph. Unfortunately the gist of the message got cut off:

Quoted 3 lines

I don't expect this patch to be mergeable but rather to kick-off a discussion if we can either simply export them as they are or how we can get supportable exports that allow access to struct files_struct.

Maybe that wasn't obvious from the first message. Is there any way we can come up with a way to have versions of these functions that you would be fine with exporting?
The point is that otherwise we would have to either duplicate the code or come up with something way more complex. If you have any pointer that would already help.

Christian

#16 lore raw
Quoted 10 lines · Christian Brauner
Quoted 3 lines

I don't expect this patch to be mergeable but rather to kick-off a discussion if we can either simply export them as they are or how we can get supportable exports that allow access to struct files_struct.

Maybe that wasn't obvious from the first message. Is there any way we can come up with a way to have versions of these functions that you would be fine with exporting?
The point is that otherwise we would have to either duplicate the code or come up with something way more complex. If you have any pointer that would already help.

He said in the first reply this should probably be using an anonfd. If you do that, I think all four of these exports go away.

And there was really no reason to post each of the four exports as separate patches. That just makes review harder on everyone.

#17 lore raw
Quoted 12 lines · Matthew Wilcox
Quoted 10 lines · Christian Brauner
Quoted 3 lines

I don't expect this patch to be mergeable but rather to kick-off a discussion if we can either simply export them as they are or how we can get supportable exports that allow access to struct files_struct.

Maybe that wasn't obvious from the first message. Is there any way we can come up with a way to have versions of these functions that you would be fine with exporting?
The point is that otherwise we would have to either duplicate the code or come up with something way more complex. If you have any pointer that would already help.

Quoted 2 lines

He said in the first reply this should probably be using an anonfd. If you do that, I think all four of these exports go away.

I try and see if that is possible.

Quoted 3 lines

And there was really no reason to post each of the four exports as separate patches. That just makes review harder on everyone.

Sorry about that. It usually depends on the preferences of each maintainer how fine-grained such minor changes should be.

Christian

maintainer
#18 lore raw
Quoted 16 lines · Christian Brauner
Quoted 12 lines · Matthew Wilcox
Quoted 10 lines · Christian Brauner
Quoted 3 lines

I don't expect this patch to be mergeable but rather to kick-off a discussion if we can either simply export them as they are or how we can get supportable exports that allow access to struct files_struct.

Maybe that wasn't obvious from the first message. Is there any way we can come up with a way to have versions of these functions that you would be fine with exporting?
The point is that otherwise we would have to either duplicate the code or come up with something way more complex. If you have any pointer that would already help.

Quoted 2 lines

He said in the first reply this should probably be using an anonfd. If you do that, I think all four of these exports go away.

Quoted 8 lines

I try and see if that is possible.

Quoted 3 lines

And there was really no reason to post each of the four exports as separate patches. That just makes review harder on everyone.

Sorry about that. It usually depends on the preferences of each maintainer how fine-grained such minor changes should be.

The fundamental problem here (besides "who the hell thought that this Fine Piece Of Software belongs anywhere other than in /dev/null?") is that messing with other's descriptor table is Fucking Wrong(tm). It's not going to become a general-purpose interface. That kludge is just that - a kludge caused by atrocious API design.

Exports NAKed, and if brought again they'll get NAKed with extreme prejudice (sensu PTerry).

#19 lore raw
Quoted 26 lines · Al Viro
Quoted 16 lines · Christian Brauner
Quoted 12 lines · Matthew Wilcox
Quoted 10 lines · Christian Brauner
Quoted 3 lines

I don't expect this patch to be mergeable but rather to kick-off a discussion if we can either simply export them as they are or how we can get supportable exports that allow access to struct files_struct.

Maybe that wasn't obvious from the first message. Is there any way we can come up with a way to have versions of these functions that you would be fine with exporting?
The point is that otherwise we would have to either duplicate the code or come up with something way more complex. If you have any pointer that would already help.

Quoted 2 lines

He said in the first reply this should probably be using an anonfd. If you do that, I think all four of these exports go away.

Quoted 8 lines

I try and see if that is possible.

Quoted 3 lines

And there was really no reason to post each of the four exports as separate patches. That just makes review harder on everyone.

Sorry about that. It usually depends on the preferences of each maintainer how fine-grained such minor changes should be.

Quoted 7 lines

The fundamental problem here (besides "who the hell thought that this Fine Piece Of Software belongs anywhere other than in /dev/null?") is that messing with other's descriptor table is Fucking Wrong(tm). It's not going to become a general-purpose interface. That kludge is just that - a kludge caused by atrocious API design.

Exports NAKed, and if brought again they'll get NAKed with extreme prejudice

That's fair. When this discussion of turning them into modules was started it was expected that this would never fly as is. The question was whether there's any way for binder to touch struct_files of another process directly at all. Now we know, there isn't. What I was hoping for is that this would cause a redesign that avoids touching these helpers. There's an effort from the Android folks now, which is good!

Thanks!
Christian

#20 lore raw
In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig
Quoted 7 lines · Christian Brauner

Hey,

We currently plan on turning the Android binder and ashmem driver into a module. We have seen more and more requests by users to be able to use the binder and ashmem features without wanting to convince each distro to enable it by default in their kernel. Debian already started to carry patches for turning them into modules.

Yikes. I really wish Debian would stick more to upstream rather than picking random crap like this up.

My hope is that this is a temporary bodge.

The way this happened was:

1. Anbox was proposed as an addition to Debian, including the ashmem and binder drivers as out-of-tree modules.
2. It was objected that these drivers were already part of the linux package (though not currently built), and it was bad practice to add a second copy that would need separate security updates.
3. The kernel team was requested to enable these drivers.
4. I agreed to enable them as modules (like most other drivers).
5. I then discovered that they couldn't be built as modules without patching, due to these missing exports.

(So how does Anbox build them as modules? Well, you're really not going to like this:
https://github.com/anbox/anbox-modules/blob/master/binder/deps.c )

Ben.

Signature · 5 lines
Ben Hutchings
[W]e found...that it wasn't as easy to get programs right as we had
thought. I realized that a large part of my life from then on was going
to be spent in finding mistakes in my own programs.
                                                 - Maurice Wilkes, 1949