Skip to contentSkip to conversation

2.5.1-pre10 #ifdef CONFIG_KMOD Cleanup Part II.

PATCH In review Rusty Russell 2 participants 4 messages Dec 13, 2001 – Dec 15, 2001 Module support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 2.5.1-pre10 #ifdef CONFIG_KMOD Cleanup Part II.

This patch does three things to remove the requirement for #ifdef CONFIG_KMOD in code:

1) Makes request_module() take args like sprintf. eg.

	request_module("proto-%u", protonum);

2) Adds request_module_start()/request_module_end() macros, eg.

	struct protocol protoptr;

	request_module_start("proto-%u", protonum) {
		/* search for protocol, set protoptr. */

	} request_module_end(protoptr != NULL);

This loops once if !CONFIG_KMOD or protoptr != NULL after first iteration, otherwise calls request_module and loops a second time.

3) Adds a request_module_unless() macro, eg:

	protoptr = request_module_unless(protoptrs[proto],
					 "proto-%u", protonum);

The evaluates the condition (protoptrs[proto]), if true, returns it, otherwise calls request_module and returns the (re-evaluated) condition.

This patch doesn't actually convert any code,
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

Name: Enhanced kmod macros
Author: Rusty Russell
Status: Simple
Class: New Infrastructure
Section: Kmod

D: This patch makes request_module() take args like snprintf, and adds
D: three new support macros: request_module_start(), request_module_end()
D: and request_module_unless(). Now there should be no reason to have
D: gratuitous code or #ifdef CONFIG_KMOD throughout the kernel.

include/linux/kmod.h +33 -3
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal working-2.4.14-params/include/linux/kmod.h working-2.4.14-kmod-used/include/linux/kmod.h
@@ -23,9 +23,39 @@
 #include <linux/errno.h>
 
 #ifdef CONFIG_KMOD
-extern int request_module(const char * name);
-#else
-static inline int request_module(const char * name) { return -ENOSYS; }
+/* ISO C99 would allow a request_module_once() macro like
+   list_for_each, by declaring the var in the for().  Soon, soon... --RR */
+
+/* Start a request module loop: module will be requested the *second* time */
+#define request_module_start(format, arg...)				   \
+do {									   \
+	int __r;							   \
+	for (__r = 0; __r < 2; __r++ ?: request_module(format , ## arg)) {
+
+/* Stop first time (ie. don't request) if this condition is true. */
+#define request_module_end(cond) if (cond) break; } } while(0)
+
+/* Request module if this is false, then re-evaluate */
+#define request_module_unless(cond, format, arg...)		\
+({ if (!cond) request_module(format , ## arg); (cond); })
+
+/* Request a module (returns 0 on success, -errno otherwise).  Keep
+   name << a page please! */
+int request_module(const char *format, ...) 
+__attribute__ ((format (printf, 1, 2)));
+
+#else /* !CONFIG_KMOD */
+
+#define request_module_start(format, arg...) do {
+#define request_module_end(cond) while(0)
+
+/* Request a module (returns 0 on success, -errno otherwise) */
+static inline int request_module(const char *format, ...) 
+__attribute__ ((format (printf, 1, 2)));
+
+static inline int request_module(const char * name, ...) { return -ENOSYS; }
+
+#define request_module_unless(cond, format, arg...) (cond)
 #endif
 
 extern int exec_usermodehelper(char *program_path, char *argv[], char *envp[]);
kernel/kmod.c +46 -15

84 lines not on this page · raw message

#1 lore raw
Quoted 11 lines · Rusty Russell

2) Adds request_module_start()/request_module_end() macros, eg.

	struct protocol protoptr;

	request_module_start("proto-%u", protonum) {
		/* search for protocol, set protoptr. */

	} request_module_end(protoptr != NULL);

This loops once if !CONFIG_KMOD or protoptr != NULL after first iteration, otherwise calls request_module and loops a second time.

Clever, but very un-C-like. Perhaps something like this:

do {
/* search for protocol, set protoptr. */
} while (protoptr != NULL || request_module("proto-%u",protonum)==0);

..with request_module returning -EBUSY if the module is already loaded.

Quoted 4 lines

3) Adds a request_module_unless() macro, eg:

	protoptr = request_module_unless(protoptrs[proto],
					 "proto-%u", protonum);

Also weird.

Signature · 1 line
 "Love the dolphins," she advised him. "Write by W.A.S.T.E.."
#2 lore raw

In message <[email protected]> you write:

Quoted 14 lines · Rusty Russell
Quoted 10 lines

2) Adds request_module_start()/request_module_end() macros, eg.

	struct protocol protoptr;

	request_module_start("proto-%u", protonum) {
		/* search for protocol, set protoptr. */

	} request_module_end(protoptr != NULL);
Quoted 2 lines

This loops once if !CONFIG_KMOD or protoptr != NULL after first iteration, otherwise calls request_module and loops a second time.

Quoted 7 lines

Clever, but very un-C-like. Perhaps something like this:

do {
/* search for protocol, set protoptr. */
} while (protoptr != NULL || request_module("proto-%u",protonum)==0);

..with request_module returning -EBUSY if the module is already loaded.

This can spin forever 8(. I would love to have just a request_module_loop() macro, eg:

	request_module_loop("proto-%u", protonum) {
		...
	}

But it turns out not to be possible without more C 9x compliance (ie. local variable decls in for () loops).

Quoted 6 lines
Quoted 4 lines

3) Adds a request_module_unless() macro, eg:

	protoptr = request_module_unless(protoptrs[proto],
					 "proto-%u", protonum);

Also weird.

Ack. However, I was looking for positive suggestions 8)

Thanks,
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

#3 lore raw
Quoted 17 lines · Rusty Russell

In message <[email protected]> you write:

Quoted 16 lines · Rusty Russell
Quoted 10 lines

2) Adds request_module_start()/request_module_end() macros, eg.

	struct protocol protoptr;

	request_module_start("proto-%u", protonum) {
		/* search for protocol, set protoptr. */

	} request_module_end(protoptr != NULL);
Quoted 2 lines

This loops once if !CONFIG_KMOD or protoptr != NULL after first iteration, otherwise calls request_module and loops a second time.

Clever, but very un-C-like. Perhaps something like this:

Quoted 7 lines
Quoted 5 lines

do {
/* search for protocol, set protoptr. */
} while (protoptr != NULL || request_module("proto-%u",protonum)==0);

..with request_module returning -EBUSY if the module is already loaded.

This can spin forever 8(.

Well there is a thinko or two above, but provided we have a variant of request_module that only returns zero when it actually loads a module, this should only loop once or twice:

do {
/* search for protocol, set protoptr. */
} while (protoptr == NULL && request_module("proto-%u",protonum)==0);

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

3) Adds a request_module_unless() macro, eg:

	protoptr = request_module_unless(protoptrs[proto],
					 "proto-%u", protonum);

Also weird.

Ack. However, I was looking for positive suggestions 8)

I think the deeper problem is that request_module is expensive. If, say, it had a fast path on the order of an inline hash lookup on a cookie corresponding to the module identifier, we could just unconditionally request_module(cookie).

Cookie here might be also something like a dentry, which can be positive or negative, and has some of the reference counting and locking semantics we need for modules anyway.

Signature · 1 line
 "Love the dolphins," she advised him. "Write by W.A.S.T.E.."