Skip to contentSkip to conversation

Revisiting threaded udevd

DISCUSSION Alan Jenkins 2 participants 5 messages Dec 22, 2008 – Dec 27, 2008 Kbuild

lore
Share & tools
lore
#0 lore raw

I have some time to look at this again now.

I think Scott was right to question the way I handled running external programs. The "udev-exec" daemon takes a fair amount of code, and it's not easy to justify. I also suspect it would have bad effects on process placement on many-cpu systems.

That said, "udev-exec" does actually improve performance. I would explain this as fork() being more efficient in processes with a smaller memory map. A multi-threaded udevd requires additional address space for each thread stack.

So I'm going to try limiting the number of threads. E.g. a maximum of 2
+ 2N running threads. "Running" is defined as "not waiting for an external program". The current limit on total threads would still be
applied. The hope is to reduce the number of threads, while still
allowing lots of parallel IO. Reducing the number of threads should help reduce overheads in general.

The second reason for "udev-exec" was the "close on exec" problem of leaking irrelevant file descriptors to helper programs. Scott suggests that I make udev depend on the latest system calls, e.g. pipe2(), in 2.6.27. But I want to be able to run older kernels for testing etc! So I'll take advantage of the new calls where available. On older kernels, I'll use an RW lock to make e.g.

pipe();
fcntl(fd, F_SETFD, FD_CLOEXEC)

atomic against fork().

For completeness, the last reason for udev-exec was more convenient timeouts. Without udev-exec I expect they'll be a bit clunky, but it can be done.

Regards
Alan

#1 lore raw
Quoted 2 lines · Alan Jenkins

I have some time to look at this again now.

I was just looking at this myself, since it formed a natural part of the performance testing I'm doing on Ubuntu.

I got as far as reapplying the patches to current git head, but then hit a segfault :-(

Attached is the patch against head.

The segfault occurs in the udev_device_get_properties() call at the top of udev_exec(), because that calls update_envp_monitor_buf() which calls free().

I assume this is non-reentrant territory?

Scott

Signature · 2 lines
Scott James Remnant
[email protected]

=== modified file 'configure.ac'

configure.ac +4 -0
@@ -9,6 +9,8 @@
 AC_SYS_LARGEFILE
 AC_PROG_LIBTOOL
 
+sinclude(acx_pthread.m4)
+
 dnl /* libudev version */
 LIBUDEV_LT_CURRENT=0
 LIBUDEV_LT_REVISION=6
@@ -25,6 +27,8 @@
 AC_SUBST(VOLID_LT_REVISION)
 AC_SUBST(VOLID_LT_AGE)
 
+ACX_PTHREAD(AC_DEFINE(HAVE_PTHREAD), AC_MSG_FAILURE([pthreads unavailable]))
+
 AC_PATH_PROG([XSLTPROC], [xsltproc])
 
 AC_CHECK_LIB(c, inotify_init,

=== modified file 'udev/Makefile.am'

Makefile.am +5 -0
@@ -1,5 +1,9 @@
 include $(top_srcdir)/Makefile.am.inc
 
+LIBS += $(PTHREAD_LIBS)
+CFLAGS += $(PTHREAD_CFLAGS)
+CC=$(PTHREAD_CC)
+
 SUBDIRS = \
 	lib
 
@@ -18,6 +22,7 @@
 	udev-event.c \
 	udev-node.c \
 	udev-rules.c \
+	udev-exec.c \
 	udev-util.c \
 	lib/libudev.h \
 	lib/libudev-private.h \

=== modified file 'udev/test-udev.c'

test-udev.c +4 -7
@@ -60,6 +60,9 @@
 	info(udev, "version %s\n", VERSION);
 	udev_selinux_init(udev);
 
+	/* fork exec daemon before opening fds or changing signal handling */
+	udev_exec_init(udev);
+
 	/* set signal handlers */
 	memset(&act, 0x00, sizeof(act));
 	act.sa_handler = (void (*)(int)) sig_handler;
@@ -69,9 +72,6 @@
 	sigaction(SIGINT, &act, NULL);
 	sigaction(SIGTERM, &act, NULL);
 
-	/* trigger timeout to prevent hanging processes */
-	alarm(UDEV_EVENT_TIMEOUT);
-
 	action = getenv("ACTION");
 	devpath = getenv("DEVPATH");
 	subsystem = getenv("SUBSYSTEM");
@@ -99,10 +99,6 @@
 	event = udev_event_new(dev);
 	err = udev_event_execute_rules(event, rules);
 
-	/* rules may change/disable the timeout */
-	if (udev_device_get_event_timeout(dev) >= 0)
-		alarm(udev_device_get_event_timeout(dev));
-
 	if (err == 0 && !event->ignore_device && udev_get_run(udev))
 		udev_event_execute_run(event);
 
@@ -111,6 +107,7 @@
 fail:
 	udev_rules_unref(rules);
 exit:
+	udev_exec_cleanup(udev);
 	udev_selinux_exit(udev);
 	udev_unref(udev);
 	if (err != 0)

=== modified file 'udev/udev-event.c'

udev-event.c +1 -1
@@ -717,7 +717,7 @@
 				       udev_device_get_seqnum(event->dev),
 				       program);
 			envp = udev_device_get_properties_envp(event->dev);
-			if (util_run_program(event->udev, program, envp, NULL, 0, NULL) != 0) {
+			if (udev_exec(event->dev, program, NULL, 0, NULL) != 0) {
 				if (!udev_list_entry_get_flag(list_entry))
 					err = -1;
 			}

=== modified file 'udev/udev-rules.c'

udev-rules.c +2 -6
@@ -731,13 +731,11 @@
 static int import_program_into_properties(struct udev_device *dev, const char *program)
 {
 	struct udev *udev = udev_device_get_udev(dev);
-	char **envp;
 	char result[2048];
 	size_t reslen;
 	char *line;
 
-	envp = udev_device_get_properties_envp(dev);
-	if (util_run_program(udev, program, envp, result, sizeof(result), &reslen) != 0)
+	if (udev_exec(dev, program, result, sizeof(result), &reslen) != 0)
 		return -1;
 
 	line = result;
@@ -2190,19 +2188,17 @@
 		case TK_M_PROGRAM:
 			{
 				char program[UTIL_PATH_SIZE];
-				char **envp;
 				char result[UTIL_PATH_SIZE];
 
 				free(event->program_result);
 				event->program_result = NULL;
 				util_strlcpy(program, &rules->buf[cur->key.value_off], sizeof(program));
 				udev_event_apply_format(event, program, sizeof(program));
-				envp = udev_device_get_properties_envp(event->dev);
 				info(event->udev, "PROGRAM '%s' %s:%u\n",
 				     program,
 				     &rules->buf[rule->rule.filename_off],
 				     rule->rule.filename_line);
-				if (util_run_program(event->udev, program, envp, result, sizeof(result), NULL) != 0) {
+				if (udev_exec(event->udev, program, result, sizeof(result), NULL) != 0) {
 					if (cur->key.op != OP_NOMATCH)
 						goto nomatch;
 				} else {

=== modified file 'udev/udev-util.c'

udev-util.c +0 -213

222 lines not on this page · raw message

=== modified file 'udev/udev.h'

udev.h +10 -3
@@ -35,6 +35,9 @@
 #define READ_END				0
 #define WRITE_END				1
 
+#define UDEVD_PRIORITY			-4 /* nice level for daemon */
+#define UDEV_PRIORITY			-2 /* nice level for programs run by daemon*/
+
 static inline void logging_init(const char *program_name)
 {
 	openlog(program_name, LOG_PID | LOG_CONS, LOG_DAEMON);
@@ -64,7 +67,7 @@
 	uid_t uid;
 	gid_t gid;
 	struct udev_list_node run_list;
-	pid_t pid;
+	pthread_t thread;
 	int exitstatus;
 	time_t queue_time;
 	unsigned long long int delaying_seqnum;
@@ -100,14 +103,18 @@
 extern int udev_node_remove(struct udev_device *dev, int test);
 extern void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test);
 
+/* udev-exec.c */
+extern int udev_exec_init(struct udev *udev);
+extern void udev_exec_cleanup(struct udev *udev);
+extern int udev_exec(struct udev_device *dev, const char *command,
+                     char *result, size_t ressize, size_t *reslen);
+
 /* udev-util.c */
 extern int util_create_path(struct udev *udev, const char *path);
 extern int util_delete_path(struct udev *udev, const char *path);
 extern int util_unlink_secure(struct udev *udev, const char *filename);
 extern uid_t util_lookup_user(struct udev *udev, const char *user);
 extern gid_t util_lookup_group(struct udev *udev, const char *group);
-extern int util_run_program(struct udev *udev, const char *command, char **envp,
-			    char *result, size_t ressize, size_t *reslen);
 extern int util_resolve_subsys_kernel(struct udev *udev, const char *string,
 				      char *result, size_t maxsize, int read_value);
 

=== modified file 'udev/udevd.c'

udevd.c +145 -143

410 lines not on this page · raw message

#2 lore raw
In reply to Alan Jenkins
Quoted 12 lines · Scott James Remnant
Quoted 3 lines · Alan Jenkins

I have some time to look at this again now.

I was just looking at this myself, since it formed a natural part of the performance testing I'm doing on Ubuntu.

I got as far as reapplying the patches to current git head, but then hit a segfault :-(

Quoted 8 lines

Attached is the patch against head.

The segfault occurs in the udev_device_get_properties() call at the top of udev_exec(), because that calls update_envp_monitor_buf() which calls free().

I assume this is non-reentrant territory?

Odd. I have started and I've not seen anything like this. Maybe you're doing something wrong. I don't have time to look at or try your patch today :-).

libudev doesn't implement any locking itself. But AFAICS it's safe to use *different* libudev objects such as udev_devices from different threads. You generally don't need to modify the udev context object, so it seemed safe to share the context between threads (and just make sure to only call udev_ref/unref from a single thread).

Regards
Alan

#3 lore raw
In reply to Alan Jenkins
Quoted 11 lines · Alan Jenkins

Odd. I have started and I've not seen anything like this. Maybe you're
doing something wrong. I don't have time to look at or try your patch today :-).

libudev doesn't implement any locking itself. But AFAICS it's safe to use *different* libudev objects such as udev_devices from different threads. You generally don't need to modify the udev context object, so it seemed safe to share the context between threads (and just make sure to only call udev_ref/unref from a single thread).

I think I got it right, maybe I missed something that was in your original patch?

The only vaguely unusual thing I was doing was running udevd in an existing booted system, and running udevadm trigger on that - so there would already have been udevdb entries for everything.

Scott

Signature · 2 lines
Scott James Remnant
[email protected]
#4 lore raw
In reply to Alan Jenkins
Quoted 5 lines · Scott James Remnant

On Thu, 2008-12-25 at 17:36 +0000, Alan Jenkins wrote:

I think I got it right, maybe I missed something that was in your original patch?

Could be. Either way, I found your problem:

udev-rules.c: In function ‘udev_rules_apply_to_event’:
udev-rules.c:2201: warning: passing argument 1 of ‘udev_exec’ from incompatible pointer type

(The relevant line says event->udev, when it should probably be event->dev).

I'd already updated my original patches, so you might try them instead (attached). They're not quite as well tested as before, but they should still work. I'd be interested in any results of yours.

Quoted 4 lines

The only vaguely unusual thing I was doing was running udevd in an existing booted system, and running udevadm trigger on that - so there would already have been udevdb entries for everything.

That's exactly how I test.

Thanks
Alan

udev/Makefile.am +1 -0
index 2d185f4..5739442 100644
@@ -18,6 +18,7 @@ common_files = \
 	udev-event.c \
 	udev-node.c \
 	udev-rules.c \
+	udev-exec.c \
 	udev-util.c \
 	lib/libudev.h \
 	lib/libudev-private.h \
udev/test-udev.c +3 -7
index ff1c353..a26f308 100644
@@ -60,6 +60,9 @@ int main(int argc, char *argv[])
 	info(udev, "version %s\n", VERSION);
 	udev_selinux_init(udev);
 
+	/* fork exec daemon before opening fds or changing signal handling */
+	udev_exec_init(udev);
+
 	/* set signal handlers */
 	memset(&act, 0x00, sizeof(act));
 	act.sa_handler = (void (*)(int)) sig_handler;
@@ -69,9 +72,6 @@ int main(int argc, char *argv[])
 	sigaction(SIGINT, &act, NULL);
 	sigaction(SIGTERM, &act, NULL);
 
-	/* trigger timeout to prevent hanging processes */
-	alarm(UDEV_EVENT_TIMEOUT);
-
 	action = getenv("ACTION");
 	devpath = getenv("DEVPATH");
 	subsystem = getenv("SUBSYSTEM");
@@ -99,10 +99,6 @@ int main(int argc, char *argv[])
 	event = udev_event_new(dev);
 	err = udev_event_execute_rules(event, rules);
 
-	/* rules may change/disable the timeout */
-	if (udev_device_get_event_timeout(dev) >= 0)
-		alarm(udev_device_get_event_timeout(dev));
-
 	if (err == 0 && !event->ignore_device && udev_get_run(udev))
 		udev_event_execute_run(event);
 
udev/udev-event.c +1 -1
index bc69239..f98638c 100644
@@ -717,7 +717,7 @@ int udev_event_execute_run(struct udev_event *event)
 				       udev_device_get_seqnum(event->dev),
 				       program);
 			envp = udev_device_get_properties_envp(event->dev);
-			if (util_run_program(event->udev, program, envp, NULL, 0, NULL) != 0) {
+			if (udev_exec(event->dev, program, NULL, 0, NULL) != 0) {
 				if (!udev_list_entry_get_flag(list_entry))
 					err = -1;
 			}
udev/udev-exec.c new file +592 -0

592 lines not on this page · raw message

udev/udev-rules.c +2 -7
index afd2e88..736f8ef 100644
@@ -730,14 +730,11 @@ static int import_file_into_properties(struct udev_device *dev, const char *file
 
 static int import_program_into_properties(struct udev_device *dev, const char *program)
 {
-	struct udev *udev = udev_device_get_udev(dev);
-	char **envp;
 	char result[2048];
 	size_t reslen;
 	char *line;
 
-	envp = udev_device_get_properties_envp(dev);
-	if (util_run_program(udev, program, envp, result, sizeof(result), &reslen) != 0)
+	if (udev_exec(dev, program, result, sizeof(result), &reslen) != 0)
 		return -1;
 
 	line = result;
@@ -2190,19 +2187,17 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
 		case TK_M_PROGRAM:
 			{
 				char program[UTIL_PATH_SIZE];
-				char **envp;
 				char result[UTIL_PATH_SIZE];
 
 				free(event->program_result);
 				event->program_result = NULL;
 				util_strlcpy(program, &rules->buf[cur->key.value_off], sizeof(program));
 				udev_event_apply_format(event, program, sizeof(program));
-				envp = udev_device_get_properties_envp(event->dev);
 				info(event->udev, "PROGRAM '%s' %s:%u\n",
 				     program,
 				     &rules->buf[rule->rule.filename_off],
 				     rule->rule.filename_line);
-				if (util_run_program(event->udev, program, envp, result, sizeof(result), NULL) != 0) {
+				if (udev_exec(event->dev, program, result, sizeof(result), NULL) != 0) {
 					if (cur->key.op != OP_NOMATCH)
 						goto nomatch;
 				} else {
udev/udev-util.c +0 -213

222 lines not on this page · raw message

udev/udev.h +9 -2
index ea8c8d9..0bd6ca1 100644
@@ -35,6 +35,9 @@
 #define READ_END				0
 #define WRITE_END				1
 
+#define UDEVD_PRIORITY			-4 /* nice level for daemon */
+#define UDEV_PRIORITY			-2 /* nice level for programs run by daemon*/
+
 static inline void logging_init(const char *program_name)
 {
 	openlog(program_name, LOG_PID | LOG_CONS, LOG_DAEMON);
@@ -100,14 +103,18 @@ extern int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t
 extern int udev_node_remove(struct udev_device *dev, int test);
 extern void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test);
 
+/* udev-exec.c */
+extern int udev_exec_init(struct udev *udev);
+extern void udev_exec_cleanup(struct udev *udev);
+extern int udev_exec(struct udev_device *dev, const char *command,
+		     char *result, size_t ressize, size_t *reslen);
+
 /* udev-util.c */
 extern int util_create_path(struct udev *udev, const char *path);
 extern int util_delete_path(struct udev *udev, const char *path);
 extern int util_unlink_secure(struct udev *udev, const char *filename);
 extern uid_t util_lookup_user(struct udev *udev, const char *user);
 extern gid_t util_lookup_group(struct udev *udev, const char *group);
-extern int util_run_program(struct udev *udev, const char *command, char **envp,
-			    char *result, size_t ressize, size_t *reslen);
 extern int util_resolve_subsys_kernel(struct udev *udev, const char *string,
 				      char *result, size_t maxsize, int read_value);
 
udev/udevd.c +8 -11
index bfd7323..d57006b 100644
@@ -214,16 +214,9 @@ static void event_fork(struct udev_event *event)
 		sigaction(SIGCHLD, &act, NULL);
 		sigaction(SIGHUP, &act, NULL);
 
-		/* set timeout to prevent hanging processes */
-		alarm(UDEV_EVENT_TIMEOUT);
-
 		/* apply rules, create node, symlinks */
 		err = udev_event_execute_rules(event, rules);
 
-		/* rules may change/disable the timeout */
-		if (udev_device_get_event_timeout(event->dev) >= 0)
-			alarm(udev_device_get_event_timeout(event->dev));
-
 		/* execute RUN= */
 		if (err == 0 && !event->ignore_device && udev_get_run(event->udev))
 			udev_event_execute_run(event);
@@ -706,6 +699,13 @@ int main(int argc, char *argv[])
 	if (write(STDERR_FILENO, 0, 0) < 0)
 		dup2(fd, STDERR_FILENO);
 
+	chdir("/");
+	umask(022);
+	setsid();
+
+	/* fork exec daemon before opening more fds or changing signal handling */
+	udev_exec_init(udev);
+
 	/* init control socket, bind() ensures, that only one udevd instance is running */
 	udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
 	if (udev_ctrl == NULL) {
@@ -799,10 +799,6 @@ int main(int argc, char *argv[])
 	/* set scheduling priority for the daemon */
 	setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
 
-	chdir("/");
-	umask(022);
-	setsid();
-
 	/* OOM_DISABLE == -17 */
 	fd = open("/proc/self/oom_adj", O_RDWR);
 	if (fd < 0)
@@ -986,6 +982,7 @@ exit:
 	if (inotify_fd >= 0)
 		close(inotify_fd);
 	udev_monitor_unref(kernel_monitor);
+	udev_exec_cleanup(udev);
 	udev_selinux_exit(udev);
 	udev_unref(udev);
 	logging_close();
udev/udev-exec.c +3 -1
index ae385bf..6ad4fc1 100644
@@ -153,7 +153,9 @@ static int exec_msg(struct udev *udev, int sock, struct child **childp)
 	if (child == NULL)
 		return 0;
 
-	pid = fork();
+	/* CAUTION.  Child may (or may not) share memory - including stack.
+	   Child should do the minimum necessary before exec() */
+	pid = vfork();
 	switch(pid) {
 	case 0:
 		close(sock);
acx_pthread.m4 new file +275 -0

275 lines not on this page · raw message

configure.ac +4 -0
index 6e98f37..7f1c901 100644
@@ -9,6 +9,8 @@ AC_DISABLE_STATIC
 AC_SYS_LARGEFILE
 AC_PROG_LIBTOOL
 
+sinclude(acx_pthread.m4)
+
 dnl /* libudev version */
 LIBUDEV_LT_CURRENT=0
 LIBUDEV_LT_REVISION=3
@@ -25,6 +27,8 @@ AC_SUBST(VOLID_LT_CURRENT)
 AC_SUBST(VOLID_LT_REVISION)
 AC_SUBST(VOLID_LT_AGE)
 
+ACX_PTHREAD(AC_DEFINE(HAVE_PTHREAD), AC_MSG_FAILURE([pthreads unavailable]))
+
 AC_PATH_PROG([XSLTPROC], [xsltproc])
 
 AC_CHECK_LIB(c, inotify_init,
udev/Makefile.am +4 -0
index 2b35bd9..9368e9b 100644
@@ -1,5 +1,9 @@
 include $(top_srcdir)/Makefile.am.inc
 
+LIBS += $(PTHREAD_LIBS)
+CFLAGS += $(PTHREAD_CFLAGS)
+CC=$(PTHREAD_CC)
+
 SUBDIRS = \
 	lib
 
udev/test-udev.c +1 -0
index a26f308..ce7e3af 100644
@@ -107,6 +107,7 @@ int main(int argc, char *argv[])
 fail:
 	udev_rules_unref(rules);
 exit:
+	udev_exec_cleanup(udev);
 	udev_selinux_exit(udev);
 	udev_unref(udev);
 	if (err != 0)
udev/udev.h +2 -1
index 0bd6ca1..887f92d 100644
@@ -67,7 +67,7 @@ struct udev_event {
 	uid_t uid;
 	gid_t gid;
 	struct udev_list_node run_list;
-	pid_t pid;
+	pthread_t thread;
 	int exitstatus;
 	time_t queue_time;
 	unsigned long long int delaying_seqnum;
@@ -80,6 +80,7 @@ struct udev_event {
 	unsigned int ignore_device:1;
 	unsigned int trace:1;
 	unsigned int test:1;
+	unsigned int running:1;
 };
 
 /* udev-rules.c */
udev/udevd.c +135 -124

384 lines not on this page · raw message