xref: /linux/drivers/android/binderfs.c (revision 3b4d1b226dc5f065bfb685263eb27312287752f9)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/compiler_types.h>
4 #include <linux/errno.h>
5 #include <linux/fs.h>
6 #include <linux/fsnotify.h>
7 #include <linux/gfp.h>
8 #include <linux/idr.h>
9 #include <linux/init.h>
10 #include <linux/ipc_namespace.h>
11 #include <linux/kdev_t.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/namei.h>
15 #include <linux/magic.h>
16 #include <linux/major.h>
17 #include <linux/miscdevice.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/mount.h>
21 #include <linux/fs_parser.h>
22 #include <linux/sched.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock_types.h>
26 #include <linux/stddef.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29 #include <linux/uaccess.h>
30 #include <linux/user_namespace.h>
31 #include <linux/xarray.h>
32 #include <uapi/linux/android/binder.h>
33 #include <uapi/linux/android/binderfs.h>
34 
35 #include "binder_internal.h"
36 
37 #define FIRST_INODE 1
38 #define SECOND_INODE 2
39 #define INODE_OFFSET 3
40 #define BINDERFS_MAX_MINOR (1U << MINORBITS)
41 /* Ensure that the initial ipc namespace always has devices available. */
42 #define BINDERFS_MAX_MINOR_CAPPED (BINDERFS_MAX_MINOR - 4)
43 
44 static dev_t binderfs_dev;
45 static DEFINE_MUTEX(binderfs_minors_mutex);
46 static DEFINE_IDA(binderfs_minors);
47 
48 enum binderfs_param {
49 	Opt_max,
50 	Opt_stats_mode,
51 };
52 
53 enum binderfs_stats_mode {
54 	binderfs_stats_mode_unset,
55 	binderfs_stats_mode_global,
56 };
57 
58 struct binder_features {
59 	bool oneway_spam_detection;
60 	bool extended_error;
61 	bool freeze_notification;
62 	bool transaction_report;
63 };
64 
65 static const struct constant_table binderfs_param_stats[] = {
66 	{ "global", binderfs_stats_mode_global },
67 	{}
68 };
69 
70 static const struct fs_parameter_spec binderfs_fs_parameters[] = {
71 	fsparam_u32("max",	Opt_max),
72 	fsparam_enum("stats",	Opt_stats_mode, binderfs_param_stats),
73 	{}
74 };
75 
76 static struct binder_features binder_features = {
77 	.oneway_spam_detection = true,
78 	.extended_error = true,
79 	.freeze_notification = true,
80 	.transaction_report = true,
81 };
82 
83 static inline struct binderfs_info *BINDERFS_SB(const struct super_block *sb)
84 {
85 	return sb->s_fs_info;
86 }
87 
88 bool is_binderfs_device(const struct inode *inode)
89 {
90 	if (inode->i_sb->s_magic == BINDERFS_SUPER_MAGIC)
91 		return true;
92 
93 	return false;
94 }
95 
96 /**
97  * binderfs_binder_device_create - allocate inode from super block of a
98  *                                 binderfs mount
99  * @ref_inode: inode from which the super block will be taken
100  * @userp:     buffer to copy information about new device for userspace to
101  * @req:       struct binderfs_device as copied from userspace
102  *
103  * This function allocates a new binder_device and reserves a new minor
104  * number for it.
105  * Minor numbers are limited and tracked globally in binderfs_minors. The
106  * function will stash a struct binder_device for the specific binder
107  * device in i_private of the inode.
108  * It will go on to allocate a new inode from the super block of the
109  * filesystem mount, stash a struct binder_device in its i_private field
110  * and attach a dentry to that inode.
111  *
112  * Return: 0 on success, negative errno on failure
113  */
114 static int binderfs_binder_device_create(struct inode *ref_inode,
115 					 struct binderfs_device __user *userp,
116 					 struct binderfs_device *req)
117 {
118 	int minor, ret;
119 	struct dentry *dentry, *root;
120 	struct binder_device *device;
121 	char *name = NULL;
122 	struct inode *inode = NULL;
123 	struct super_block *sb = ref_inode->i_sb;
124 	struct binderfs_info *info = sb->s_fs_info;
125 #if defined(CONFIG_IPC_NS)
126 	bool use_reserve = (info->ipc_ns == &init_ipc_ns);
127 #else
128 	bool use_reserve = true;
129 #endif
130 
131 	/* Reserve new minor number for the new device. */
132 	mutex_lock(&binderfs_minors_mutex);
133 	if (++info->device_count <= info->mount_opts.max)
134 		minor = ida_alloc_max(&binderfs_minors,
135 				      use_reserve ? BINDERFS_MAX_MINOR :
136 						    BINDERFS_MAX_MINOR_CAPPED,
137 				      GFP_KERNEL);
138 	else
139 		minor = -ENOSPC;
140 	if (minor < 0) {
141 		--info->device_count;
142 		mutex_unlock(&binderfs_minors_mutex);
143 		return minor;
144 	}
145 	mutex_unlock(&binderfs_minors_mutex);
146 
147 	ret = -ENOMEM;
148 	device = kzalloc(sizeof(*device), GFP_KERNEL);
149 	if (!device)
150 		goto err;
151 
152 	inode = new_inode(sb);
153 	if (!inode)
154 		goto err;
155 
156 	inode->i_ino = minor + INODE_OFFSET;
157 	simple_inode_init_ts(inode);
158 	init_special_inode(inode, S_IFCHR | 0600,
159 			   MKDEV(MAJOR(binderfs_dev), minor));
160 	inode->i_fop = &binder_fops;
161 	inode->i_uid = info->root_uid;
162 	inode->i_gid = info->root_gid;
163 
164 	req->name[BINDERFS_MAX_NAME] = '\0'; /* NUL-terminate */
165 	name = kstrdup(req->name, GFP_KERNEL);
166 	if (!name)
167 		goto err;
168 
169 	refcount_set(&device->ref, 1);
170 	device->binderfs_inode = inode;
171 	device->context.binder_context_mgr_uid = INVALID_UID;
172 	device->context.name = name;
173 	device->miscdev.name = name;
174 	device->miscdev.minor = minor;
175 	mutex_init(&device->context.context_mgr_node_lock);
176 
177 	req->major = MAJOR(binderfs_dev);
178 	req->minor = minor;
179 
180 	if (userp && copy_to_user(userp, req, sizeof(*req))) {
181 		ret = -EFAULT;
182 		goto err;
183 	}
184 
185 	root = sb->s_root;
186 	inode_lock(d_inode(root));
187 
188 	/* look it up */
189 	dentry = lookup_noperm(&QSTR(name), root);
190 	if (IS_ERR(dentry)) {
191 		inode_unlock(d_inode(root));
192 		ret = PTR_ERR(dentry);
193 		goto err;
194 	}
195 
196 	if (d_really_is_positive(dentry)) {
197 		/* already exists */
198 		dput(dentry);
199 		inode_unlock(d_inode(root));
200 		ret = -EEXIST;
201 		goto err;
202 	}
203 
204 	inode->i_private = device;
205 	d_instantiate(dentry, inode);
206 	fsnotify_create(root->d_inode, dentry);
207 	inode_unlock(d_inode(root));
208 
209 	binder_add_device(device);
210 
211 	return 0;
212 
213 err:
214 	kfree(name);
215 	kfree(device);
216 	mutex_lock(&binderfs_minors_mutex);
217 	--info->device_count;
218 	ida_free(&binderfs_minors, minor);
219 	mutex_unlock(&binderfs_minors_mutex);
220 	iput(inode);
221 
222 	return ret;
223 }
224 
225 /**
226  * binder_ctl_ioctl - handle binder device node allocation requests
227  * @file: The file pointer for the binder-control device node.
228  * @cmd: The ioctl command.
229  * @arg: The ioctl argument.
230  *
231  * The request handler for the binder-control device. All requests operate on
232  * the binderfs mount the binder-control device resides in:
233  * - BINDER_CTL_ADD
234  *   Allocate a new binder device.
235  *
236  * Return: %0 on success, negative errno on failure.
237  */
238 static long binder_ctl_ioctl(struct file *file, unsigned int cmd,
239 			     unsigned long arg)
240 {
241 	int ret = -EINVAL;
242 	struct inode *inode = file_inode(file);
243 	struct binderfs_device __user *device = (struct binderfs_device __user *)arg;
244 	struct binderfs_device device_req;
245 
246 	switch (cmd) {
247 	case BINDER_CTL_ADD:
248 		ret = copy_from_user(&device_req, device, sizeof(device_req));
249 		if (ret) {
250 			ret = -EFAULT;
251 			break;
252 		}
253 
254 		ret = binderfs_binder_device_create(inode, device, &device_req);
255 		break;
256 	default:
257 		break;
258 	}
259 
260 	return ret;
261 }
262 
263 static void binderfs_evict_inode(struct inode *inode)
264 {
265 	struct binder_device *device = inode->i_private;
266 	struct binderfs_info *info = BINDERFS_SB(inode->i_sb);
267 
268 	clear_inode(inode);
269 
270 	if (!S_ISCHR(inode->i_mode) || !device)
271 		return;
272 
273 	mutex_lock(&binderfs_minors_mutex);
274 	--info->device_count;
275 	ida_free(&binderfs_minors, device->miscdev.minor);
276 	mutex_unlock(&binderfs_minors_mutex);
277 
278 	if (refcount_dec_and_test(&device->ref)) {
279 		binder_remove_device(device);
280 		kfree(device->context.name);
281 		kfree(device);
282 	}
283 }
284 
285 static int binderfs_fs_context_parse_param(struct fs_context *fc,
286 					   struct fs_parameter *param)
287 {
288 	int opt;
289 	struct binderfs_mount_opts *ctx = fc->fs_private;
290 	struct fs_parse_result result;
291 
292 	opt = fs_parse(fc, binderfs_fs_parameters, param, &result);
293 	if (opt < 0)
294 		return opt;
295 
296 	switch (opt) {
297 	case Opt_max:
298 		if (result.uint_32 > BINDERFS_MAX_MINOR)
299 			return invalfc(fc, "Bad value for '%s'", param->key);
300 
301 		ctx->max = result.uint_32;
302 		break;
303 	case Opt_stats_mode:
304 		if (!capable(CAP_SYS_ADMIN))
305 			return -EPERM;
306 
307 		ctx->stats_mode = result.uint_32;
308 		break;
309 	default:
310 		return invalfc(fc, "Unsupported parameter '%s'", param->key);
311 	}
312 
313 	return 0;
314 }
315 
316 static int binderfs_fs_context_reconfigure(struct fs_context *fc)
317 {
318 	struct binderfs_mount_opts *ctx = fc->fs_private;
319 	struct binderfs_info *info = BINDERFS_SB(fc->root->d_sb);
320 
321 	if (info->mount_opts.stats_mode != ctx->stats_mode)
322 		return invalfc(fc, "Binderfs stats mode cannot be changed during a remount");
323 
324 	info->mount_opts.stats_mode = ctx->stats_mode;
325 	info->mount_opts.max = ctx->max;
326 	return 0;
327 }
328 
329 static int binderfs_show_options(struct seq_file *seq, struct dentry *root)
330 {
331 	struct binderfs_info *info = BINDERFS_SB(root->d_sb);
332 
333 	if (info->mount_opts.max <= BINDERFS_MAX_MINOR)
334 		seq_printf(seq, ",max=%d", info->mount_opts.max);
335 
336 	switch (info->mount_opts.stats_mode) {
337 	case binderfs_stats_mode_unset:
338 		break;
339 	case binderfs_stats_mode_global:
340 		seq_printf(seq, ",stats=global");
341 		break;
342 	}
343 
344 	return 0;
345 }
346 
347 static const struct super_operations binderfs_super_ops = {
348 	.evict_inode    = binderfs_evict_inode,
349 	.show_options	= binderfs_show_options,
350 	.statfs         = simple_statfs,
351 };
352 
353 static inline bool is_binderfs_control_device(const struct dentry *dentry)
354 {
355 	struct binderfs_info *info = dentry->d_sb->s_fs_info;
356 
357 	return info->control_dentry == dentry;
358 }
359 
360 static int binderfs_rename(struct mnt_idmap *idmap,
361 			   struct inode *old_dir, struct dentry *old_dentry,
362 			   struct inode *new_dir, struct dentry *new_dentry,
363 			   unsigned int flags)
364 {
365 	if (is_binderfs_control_device(old_dentry) ||
366 	    is_binderfs_control_device(new_dentry))
367 		return -EPERM;
368 
369 	return simple_rename(idmap, old_dir, old_dentry, new_dir,
370 			     new_dentry, flags);
371 }
372 
373 static int binderfs_unlink(struct inode *dir, struct dentry *dentry)
374 {
375 	if (is_binderfs_control_device(dentry))
376 		return -EPERM;
377 
378 	return simple_unlink(dir, dentry);
379 }
380 
381 static const struct file_operations binder_ctl_fops = {
382 	.owner		= THIS_MODULE,
383 	.open		= nonseekable_open,
384 	.unlocked_ioctl	= binder_ctl_ioctl,
385 	.compat_ioctl	= binder_ctl_ioctl,
386 	.llseek		= noop_llseek,
387 };
388 
389 /**
390  * binderfs_binder_ctl_create - create a new binder-control device
391  * @sb: super block of the binderfs mount
392  *
393  * This function creates a new binder-control device node in the binderfs mount
394  * referred to by @sb.
395  *
396  * Return: 0 on success, negative errno on failure
397  */
398 static int binderfs_binder_ctl_create(struct super_block *sb)
399 {
400 	int minor, ret;
401 	struct dentry *dentry;
402 	struct binder_device *device;
403 	struct inode *inode = NULL;
404 	struct dentry *root = sb->s_root;
405 	struct binderfs_info *info = sb->s_fs_info;
406 #if defined(CONFIG_IPC_NS)
407 	bool use_reserve = (info->ipc_ns == &init_ipc_ns);
408 #else
409 	bool use_reserve = true;
410 #endif
411 
412 	device = kzalloc(sizeof(*device), GFP_KERNEL);
413 	if (!device)
414 		return -ENOMEM;
415 
416 	/* If we have already created a binder-control node, return. */
417 	if (info->control_dentry) {
418 		ret = 0;
419 		goto out;
420 	}
421 
422 	ret = -ENOMEM;
423 	inode = new_inode(sb);
424 	if (!inode)
425 		goto out;
426 
427 	/* Reserve a new minor number for the new device. */
428 	mutex_lock(&binderfs_minors_mutex);
429 	minor = ida_alloc_max(&binderfs_minors,
430 			      use_reserve ? BINDERFS_MAX_MINOR :
431 					    BINDERFS_MAX_MINOR_CAPPED,
432 			      GFP_KERNEL);
433 	mutex_unlock(&binderfs_minors_mutex);
434 	if (minor < 0) {
435 		ret = minor;
436 		goto out;
437 	}
438 
439 	inode->i_ino = SECOND_INODE;
440 	simple_inode_init_ts(inode);
441 	init_special_inode(inode, S_IFCHR | 0600,
442 			   MKDEV(MAJOR(binderfs_dev), minor));
443 	inode->i_fop = &binder_ctl_fops;
444 	inode->i_uid = info->root_uid;
445 	inode->i_gid = info->root_gid;
446 
447 	refcount_set(&device->ref, 1);
448 	device->binderfs_inode = inode;
449 	device->miscdev.minor = minor;
450 
451 	dentry = d_alloc_name(root, "binder-control");
452 	if (!dentry)
453 		goto out;
454 
455 	inode->i_private = device;
456 	info->control_dentry = dentry;
457 	d_add(dentry, inode);
458 
459 	return 0;
460 
461 out:
462 	kfree(device);
463 	iput(inode);
464 
465 	return ret;
466 }
467 
468 static const struct inode_operations binderfs_dir_inode_operations = {
469 	.lookup = simple_lookup,
470 	.rename = binderfs_rename,
471 	.unlink = binderfs_unlink,
472 };
473 
474 static struct inode *binderfs_make_inode(struct super_block *sb, int mode)
475 {
476 	struct inode *ret;
477 
478 	ret = new_inode(sb);
479 	if (ret) {
480 		ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);
481 		ret->i_mode = mode;
482 		simple_inode_init_ts(ret);
483 	}
484 	return ret;
485 }
486 
487 static struct dentry *binderfs_create_dentry(struct dentry *parent,
488 					     const char *name)
489 {
490 	struct dentry *dentry;
491 
492 	dentry = lookup_noperm(&QSTR(name), parent);
493 	if (IS_ERR(dentry))
494 		return dentry;
495 
496 	/* Return error if the file/dir already exists. */
497 	if (d_really_is_positive(dentry)) {
498 		dput(dentry);
499 		return ERR_PTR(-EEXIST);
500 	}
501 
502 	return dentry;
503 }
504 
505 struct dentry *binderfs_create_file(struct dentry *parent, const char *name,
506 				    const struct file_operations *fops,
507 				    void *data)
508 {
509 	struct dentry *dentry;
510 	struct inode *new_inode, *parent_inode;
511 	struct super_block *sb;
512 
513 	parent_inode = d_inode(parent);
514 	inode_lock(parent_inode);
515 
516 	dentry = binderfs_create_dentry(parent, name);
517 	if (IS_ERR(dentry))
518 		goto out;
519 
520 	sb = parent_inode->i_sb;
521 	new_inode = binderfs_make_inode(sb, S_IFREG | 0444);
522 	if (!new_inode) {
523 		dput(dentry);
524 		dentry = ERR_PTR(-ENOMEM);
525 		goto out;
526 	}
527 
528 	new_inode->i_fop = fops;
529 	new_inode->i_private = data;
530 	d_instantiate(dentry, new_inode);
531 	fsnotify_create(parent_inode, dentry);
532 
533 out:
534 	inode_unlock(parent_inode);
535 	return dentry;
536 }
537 
538 static struct dentry *binderfs_create_dir(struct dentry *parent,
539 					  const char *name)
540 {
541 	struct dentry *dentry;
542 	struct inode *new_inode, *parent_inode;
543 	struct super_block *sb;
544 
545 	parent_inode = d_inode(parent);
546 	inode_lock(parent_inode);
547 
548 	dentry = binderfs_create_dentry(parent, name);
549 	if (IS_ERR(dentry))
550 		goto out;
551 
552 	sb = parent_inode->i_sb;
553 	new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);
554 	if (!new_inode) {
555 		dput(dentry);
556 		dentry = ERR_PTR(-ENOMEM);
557 		goto out;
558 	}
559 
560 	new_inode->i_fop = &simple_dir_operations;
561 	new_inode->i_op = &simple_dir_inode_operations;
562 
563 	set_nlink(new_inode, 2);
564 	d_instantiate(dentry, new_inode);
565 	inc_nlink(parent_inode);
566 	fsnotify_mkdir(parent_inode, dentry);
567 
568 out:
569 	inode_unlock(parent_inode);
570 	return dentry;
571 }
572 
573 static int binder_features_show(struct seq_file *m, void *unused)
574 {
575 	bool *feature = m->private;
576 
577 	seq_printf(m, "%d\n", *feature);
578 
579 	return 0;
580 }
581 DEFINE_SHOW_ATTRIBUTE(binder_features);
582 
583 static int init_binder_features(struct super_block *sb)
584 {
585 	struct dentry *dentry, *dir;
586 
587 	dir = binderfs_create_dir(sb->s_root, "features");
588 	if (IS_ERR(dir))
589 		return PTR_ERR(dir);
590 
591 	dentry = binderfs_create_file(dir, "oneway_spam_detection",
592 				      &binder_features_fops,
593 				      &binder_features.oneway_spam_detection);
594 	if (IS_ERR(dentry))
595 		return PTR_ERR(dentry);
596 
597 	dentry = binderfs_create_file(dir, "extended_error",
598 				      &binder_features_fops,
599 				      &binder_features.extended_error);
600 	if (IS_ERR(dentry))
601 		return PTR_ERR(dentry);
602 
603 	dentry = binderfs_create_file(dir, "freeze_notification",
604 				      &binder_features_fops,
605 				      &binder_features.freeze_notification);
606 	if (IS_ERR(dentry))
607 		return PTR_ERR(dentry);
608 
609 	dentry = binderfs_create_file(dir, "transaction_report",
610 				      &binder_features_fops,
611 				      &binder_features.transaction_report);
612 	if (IS_ERR(dentry))
613 		return PTR_ERR(dentry);
614 
615 	return 0;
616 }
617 
618 static int init_binder_logs(struct super_block *sb)
619 {
620 	struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;
621 	const struct binder_debugfs_entry *db_entry;
622 	struct binderfs_info *info;
623 	int ret = 0;
624 
625 	binder_logs_root_dir = binderfs_create_dir(sb->s_root,
626 						   "binder_logs");
627 	if (IS_ERR(binder_logs_root_dir)) {
628 		ret = PTR_ERR(binder_logs_root_dir);
629 		goto out;
630 	}
631 
632 	binder_for_each_debugfs_entry(db_entry) {
633 		dentry = binderfs_create_file(binder_logs_root_dir,
634 					      db_entry->name,
635 					      db_entry->fops,
636 					      db_entry->data);
637 		if (IS_ERR(dentry)) {
638 			ret = PTR_ERR(dentry);
639 			goto out;
640 		}
641 	}
642 
643 	proc_log_dir = binderfs_create_dir(binder_logs_root_dir, "proc");
644 	if (IS_ERR(proc_log_dir)) {
645 		ret = PTR_ERR(proc_log_dir);
646 		goto out;
647 	}
648 	info = sb->s_fs_info;
649 	info->proc_log_dir = proc_log_dir;
650 
651 out:
652 	return ret;
653 }
654 
655 static int binderfs_fill_super(struct super_block *sb, struct fs_context *fc)
656 {
657 	int ret;
658 	struct binderfs_info *info;
659 	struct binderfs_mount_opts *ctx = fc->fs_private;
660 	struct inode *inode = NULL;
661 	struct binderfs_device device_info = {};
662 	const char *name;
663 	size_t len;
664 
665 	sb->s_blocksize = PAGE_SIZE;
666 	sb->s_blocksize_bits = PAGE_SHIFT;
667 
668 	/*
669 	 * The binderfs filesystem can be mounted by userns root in a
670 	 * non-initial userns. By default such mounts have the SB_I_NODEV flag
671 	 * set in s_iflags to prevent security issues where userns root can
672 	 * just create random device nodes via mknod() since it owns the
673 	 * filesystem mount. But binderfs does not allow to create any files
674 	 * including devices nodes. The only way to create binder devices nodes
675 	 * is through the binder-control device which userns root is explicitly
676 	 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
677 	 * necessary and safe.
678 	 */
679 	sb->s_iflags &= ~SB_I_NODEV;
680 	sb->s_iflags |= SB_I_NOEXEC;
681 	sb->s_magic = BINDERFS_SUPER_MAGIC;
682 	sb->s_op = &binderfs_super_ops;
683 	sb->s_time_gran = 1;
684 
685 	sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
686 	if (!sb->s_fs_info)
687 		return -ENOMEM;
688 	info = sb->s_fs_info;
689 
690 	info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
691 
692 	info->root_gid = make_kgid(sb->s_user_ns, 0);
693 	if (!gid_valid(info->root_gid))
694 		info->root_gid = GLOBAL_ROOT_GID;
695 	info->root_uid = make_kuid(sb->s_user_ns, 0);
696 	if (!uid_valid(info->root_uid))
697 		info->root_uid = GLOBAL_ROOT_UID;
698 	info->mount_opts.max = ctx->max;
699 	info->mount_opts.stats_mode = ctx->stats_mode;
700 
701 	inode = new_inode(sb);
702 	if (!inode)
703 		return -ENOMEM;
704 
705 	inode->i_ino = FIRST_INODE;
706 	inode->i_fop = &simple_dir_operations;
707 	inode->i_mode = S_IFDIR | 0755;
708 	simple_inode_init_ts(inode);
709 	inode->i_op = &binderfs_dir_inode_operations;
710 	set_nlink(inode, 2);
711 
712 	sb->s_root = d_make_root(inode);
713 	if (!sb->s_root)
714 		return -ENOMEM;
715 
716 	ret = binderfs_binder_ctl_create(sb);
717 	if (ret)
718 		return ret;
719 
720 	name = binder_devices_param;
721 	for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
722 		strscpy(device_info.name, name, len + 1);
723 		ret = binderfs_binder_device_create(inode, NULL, &device_info);
724 		if (ret)
725 			return ret;
726 		name += len;
727 		if (*name == ',')
728 			name++;
729 	}
730 
731 	ret = init_binder_features(sb);
732 	if (ret)
733 		return ret;
734 
735 	if (info->mount_opts.stats_mode == binderfs_stats_mode_global)
736 		return init_binder_logs(sb);
737 
738 	return 0;
739 }
740 
741 static int binderfs_fs_context_get_tree(struct fs_context *fc)
742 {
743 	return get_tree_nodev(fc, binderfs_fill_super);
744 }
745 
746 static void binderfs_fs_context_free(struct fs_context *fc)
747 {
748 	struct binderfs_mount_opts *ctx = fc->fs_private;
749 
750 	kfree(ctx);
751 }
752 
753 static const struct fs_context_operations binderfs_fs_context_ops = {
754 	.free		= binderfs_fs_context_free,
755 	.get_tree	= binderfs_fs_context_get_tree,
756 	.parse_param	= binderfs_fs_context_parse_param,
757 	.reconfigure	= binderfs_fs_context_reconfigure,
758 };
759 
760 static int binderfs_init_fs_context(struct fs_context *fc)
761 {
762 	struct binderfs_mount_opts *ctx;
763 
764 	ctx = kzalloc(sizeof(struct binderfs_mount_opts), GFP_KERNEL);
765 	if (!ctx)
766 		return -ENOMEM;
767 
768 	ctx->max = BINDERFS_MAX_MINOR;
769 	ctx->stats_mode = binderfs_stats_mode_unset;
770 
771 	fc->fs_private = ctx;
772 	fc->ops = &binderfs_fs_context_ops;
773 
774 	return 0;
775 }
776 
777 static void binderfs_kill_super(struct super_block *sb)
778 {
779 	struct binderfs_info *info = sb->s_fs_info;
780 
781 	/*
782 	 * During inode eviction struct binderfs_info is needed.
783 	 * So first wipe the super_block then free struct binderfs_info.
784 	 */
785 	kill_litter_super(sb);
786 
787 	if (info && info->ipc_ns)
788 		put_ipc_ns(info->ipc_ns);
789 
790 	kfree(info);
791 }
792 
793 static struct file_system_type binder_fs_type = {
794 	.name			= "binder",
795 	.init_fs_context	= binderfs_init_fs_context,
796 	.parameters		= binderfs_fs_parameters,
797 	.kill_sb		= binderfs_kill_super,
798 	.fs_flags		= FS_USERNS_MOUNT,
799 };
800 
801 int __init init_binderfs(void)
802 {
803 	int ret;
804 	const char *name;
805 	size_t len;
806 
807 	/* Verify that the default binderfs device names are valid. */
808 	name = binder_devices_param;
809 	for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
810 		if (len > BINDERFS_MAX_NAME)
811 			return -E2BIG;
812 		name += len;
813 		if (*name == ',')
814 			name++;
815 	}
816 
817 	/* Allocate new major number for binderfs. */
818 	ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
819 				  "binder");
820 	if (ret)
821 		return ret;
822 
823 	ret = register_filesystem(&binder_fs_type);
824 	if (ret) {
825 		unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
826 		return ret;
827 	}
828 
829 	return ret;
830 }
831