Skip to contentSkip to conversation

ioctl32 consolidation

PATCH Pavel Machek 7 participants 12 messages Feb 20, 2003 – Mar 1, 2003

lore
Share & tools
lore
#0 lore raw

Hi!

Currently, 32-bit emulation in kernel has *5* copies, and its >1000 lines each. Plus, locking of all but x86-64 architectures is broken (I'm told by andi ;-).

So, here's patch that starts sharing sys32_ioctl() [as a first step], which should rmove locking problems.

I've done the work for x86-64 and sparc64; if it looks good I'll attempt to do other architectures. [Unless maintainers prefer to do it
themselves: I don't have easy access to 64-bit machines besides hammer.]

Pavel

arch/sparc64/kernel/ioctl32.c +3 -140
@@ -677,7 +677,7 @@
 	return (void *) (usp - len);
 }
 
-static int siocdevprivate_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
+int siocdevprivate_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
 {
 	struct ifreq *u_ifreq64;
 	struct ifreq32 *u_ifreq32 = (struct ifreq32 *) arg;
@@ -4264,16 +4264,10 @@
 	return sys_ioctl(fd, BLKGETSIZE64, arg);
 }
 
-struct ioctl_trans {
-	unsigned int cmd;
-	unsigned int handler;
-	unsigned int next;
-};
-
 #define COMPATIBLE_IOCTL(cmd) asm volatile(".word %0, sys_ioctl, 0" : : "i" (cmd));
 #define HANDLE_IOCTL(cmd,handler) asm volatile(".word %0, %1, 0" : : "i" (cmd), "i" (handler));
-#define IOCTL_TABLE_START void ioctl32_foo(void) { asm volatile(".data\nioctl_translations:");
-#define IOCTL_TABLE_END asm volatile("\nioctl_translations_end:\n\t.previous"); }
+#define IOCTL_TABLE_START void ioctl32_foo(void) { asm volatile(".data\n.global ioctl_start\nioctl_start:");
+#define IOCTL_TABLE_END asm volatile("\n.global ioctl_end\nioctl_end:\n\t.previous"); }
 
 IOCTL_TABLE_START
 /* List here exlicitly which ioctl's are known to have
@@ -5197,134 +5191,3 @@
 HANDLE_IOCTL(BLKBSZSET_32, do_blkbszset)
 HANDLE_IOCTL(BLKGETSIZE64_32, do_blkgetsize64)
 IOCTL_TABLE_END
-
-unsigned int ioctl32_hash_table[1024];
-
-static inline unsigned long ioctl32_hash(unsigned long cmd)
-{
-	return ((cmd >> 6) ^ (cmd >> 4) ^ cmd) & 0x3ff;
-}
-
-static void ioctl32_insert_translation(struct ioctl_trans *trans)
-{
-	unsigned long hash;
-	struct ioctl_trans *t;
-
-	hash = ioctl32_hash (trans->cmd);
-	if (!ioctl32_hash_table[hash])
-		ioctl32_hash_table[hash] = (u32)(long)trans;
-	else {
-		t = (struct ioctl_trans *)(long)ioctl32_hash_table[hash];
-		while (t->next)
-			t = (struct ioctl_trans *)(long)t->next;
-		trans->next = 0;
-		t->next = (u32)(long)trans;
-	}
-}
-
-static int __init init_sys32_ioctl(void)
-{
-	int i;
-	extern struct ioctl_trans ioctl_translations[], ioctl_translations_end[];
-
-	for (i = 0; &ioctl_translations[i] < &ioctl_translations_end[0]; i++)
-		ioctl32_insert_translation(&ioctl_translations[i]);
-	return 0;
-}
-
-__initcall(init_sys32_ioctl);
-
-static struct ioctl_trans *additional_ioctls;
-
-/* Always call these with kernel lock held! */
-
-int register_ioctl32_conversion(unsigned int cmd, int (*handler)(unsigned int, unsigned int, unsigned long, struct file *))
-{
-	int i;
-	if (!additional_ioctls) {
-		additional_ioctls = vmalloc(PAGE_SIZE);
-		if (!additional_ioctls)
-			return -ENOMEM;
-		memset(additional_ioctls, 0, PAGE_SIZE);
-	}
-	for (i = 0; i < PAGE_SIZE/sizeof(struct ioctl_trans); i++)
-		if (!additional_ioctls[i].cmd)
-			break;
-	if (i == PAGE_SIZE/sizeof(struct ioctl_trans))
-		return -ENOMEM;
-	additional_ioctls[i].cmd = cmd;
-	if (!handler)
-		additional_ioctls[i].handler = (u32)(long)sys_ioctl;
-	else
-		additional_ioctls[i].handler = (u32)(long)handler;
-	ioctl32_insert_translation(&additional_ioctls[i]);
-	return 0;
-}
-
-int unregister_ioctl32_conversion(unsigned int cmd)
-{
-	unsigned long hash = ioctl32_hash(cmd);
-	struct ioctl_trans *t, *t1;
-
-	t = (struct ioctl_trans *)(long)ioctl32_hash_table[hash];
-	if (!t) return -EINVAL;
-	if (t->cmd == cmd && t >= additional_ioctls &&
-	    (unsigned long)t < ((unsigned long)additional_ioctls) + PAGE_SIZE) {
-		ioctl32_hash_table[hash] = t->next;
-		t->cmd = 0;
-		t->next = 0;
-		return 0;
-	} else while (t->next) {
-		t1 = (struct ioctl_trans *)(long)t->next;
-		if (t1->cmd == cmd && t1 >= additional_ioctls &&
-		    (unsigned long)t1 < ((unsigned long)additional_ioctls) + PAGE_SIZE) {
-			t->next = t1->next;
-			t1->cmd = 0;
-			t1->next = 0;
-			return 0;
-		}
-		t = t1;
-	}
-	return -EINVAL;
-}
-
-asmlinkage int sys32_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
-{
-	struct file * filp;
-	int error = -EBADF;
-	int (*handler)(unsigned int, unsigned int, unsigned long, struct file * filp);
-	struct ioctl_trans *t;
-
-	filp = fget(fd);
-	if(!filp)
-		goto out2;
-
-	if (!filp->f_op || !filp->f_op->ioctl) {
-		error = sys_ioctl (fd, cmd, arg);
-		goto out;
-	}
-
-	t = (struct ioctl_trans *)(long)ioctl32_hash_table [ioctl32_hash (cmd)];
-
-	while (t && t->cmd != cmd)
-		t = (struct ioctl_trans *)(long)t->next;
-	if (t) {
-		handler = (void *)(long)t->handler;
-		error = handler(fd, cmd, arg, filp);
-	} else if (cmd >= SIOCDEVPRIVATE &&
-		   cmd <= (SIOCDEVPRIVATE + 15)) {
-		error = siocdevprivate_ioctl(fd, cmd, arg);
-	} else {
-		static int count;
-		if (++count <= 20)
-			printk("sys32_ioctl(%s:%d): Unknown cmd fd(%d) "
-			       "cmd(%08x) arg(%08x)\n",
-			       current->comm, current->pid,
-			       (int)fd, (unsigned int)cmd, (unsigned int)arg);
-		error = -EINVAL;
-	}
-out:
-	fput(filp);
-out2:
-	return error;
-}
arch/sparc64/kernel/sparc64_ksyms.c +2 -2
@@ -89,7 +89,7 @@
 extern int svr4_getcontext(svr4_ucontext_t *uc, struct pt_regs *regs);
 extern int svr4_setcontext(svr4_ucontext_t *uc, struct pt_regs *regs);
 extern int sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
-extern int sys32_ioctl(unsigned int fd, unsigned int cmd, u32 arg);
+extern int compat_ioctl(unsigned int fd, unsigned int cmd, u32 arg);
 extern int (*handle_mathemu)(struct pt_regs *, struct fpustate *);
 extern long sparc32_open(const char * filename, int flags, int mode);
 extern int register_ioctl32_conversion(unsigned int cmd, int (*handler)(unsigned int, unsigned int, unsigned long, struct file *));
@@ -316,7 +316,7 @@
 EXPORT_SYMBOL(svr4_setcontext);
 EXPORT_SYMBOL(prom_cpu_nodes);
 EXPORT_SYMBOL(sys_ioctl);
-EXPORT_SYMBOL(sys32_ioctl);
+EXPORT_SYMBOL(compat_ioctl);
 EXPORT_SYMBOL(sparc32_open);
 #endif
 
arch/sparc64/kernel/sunos_ioctl32.c +26 -26

135 lines not on this page · raw message

arch/sparc64/kernel/systbls.S +1 -1
diff -ur clean/arch/sparc64/kernel/systbls.S linux-2.5.62/arch/sparc64/kernel/systbls.S
@@ -29,7 +29,7 @@
 	.word sys_chown, sys_sync, sys_kill, compat_sys_newstat, sys32_sendfile
 /*40*/	.word compat_sys_newlstat, sys_dup, sys_pipe, compat_sys_times, sys_getuid
 	.word sys_umount, sys32_setgid16, sys32_getgid16, sys_signal, sys32_geteuid16
-/*50*/	.word sys32_getegid16, sys_acct, sys_nis_syscall, sys_getgid, sys32_ioctl
+/*50*/	.word sys32_getegid16, sys_acct, sys_nis_syscall, sys_getgid, compat_ioctl
 	.word sys_reboot, sys32_mmap2, sys_symlink, sys_readlink, sys32_execve
 /*60*/	.word sys_umask, sys_chroot, compat_sys_newfstat, sys_fstat64, sys_getpagesize
 	.word sys_msync, sys_vfork, sys32_pread64, sys32_pwrite64, sys_geteuid
arch/x86_64/ia32/ia32_ioctl.c +3 -223

245 lines not on this page · raw message

arch/x86_64/ia32/ia32entry.S +1 -1
diff -ur clean/arch/x86_64/ia32/ia32entry.S linux-2.5.62/arch/x86_64/ia32/ia32entry.S
@@ -254,7 +254,7 @@
 	.quad sys_acct
 	.quad sys_umount			/* new_umount */
 	.quad ni_syscall			/* old lock syscall holder */
-	.quad sys32_ioctl
+	.quad compat_ioctl
 	.quad sys32_fcntl64		/* 55 */
 	.quad ni_syscall			/* old mpx syscall holder */
 	.quad sys_setpgid
include/linux/ioctl32.h +5 -0
diff -ur clean/include/linux/ioctl32.h linux-2.5.62/include/linux/ioctl32.h
@@ -19,5 +19,10 @@
 
 extern int unregister_ioctl32_conversion(unsigned int cmd);
 
+struct ioctl_trans {
+	unsigned long cmd;
+	int (*handler)(unsigned int, unsigned int, unsigned long, struct file * filp);
+	struct ioctl_trans *next;
+};
 
 #endif
kernel/compat.c +222 -0

231 lines not on this page · raw message

Signature · 2 lines
When do you have a heart between your knees?
[Johanka's followup: and *two* hearts?]
#1 lore raw
Quoted 2 lines · Pavel Machek

Currently, 32-bit emulation in kernel has *5* copies, and its >1000 lines each.

Yes :/ Consolidating all these copies into a single layer has been a "project to be" for quite some time.

I do not know if it is too late in 2.5.x to begin this work, however. We _are_ in a feature freeze... I suppose it is up to the consensus of arch maintainers, because it [obviously] does not affect ia32.

Jeff

#2 lore raw

Hi!

Quoted 9 lines
Quoted 2 lines

Currently, 32-bit emulation in kernel has *5* copies, and its >1000 lines each.

Yes :/ Consolidating all these copies into a single layer has been a "project to be" for quite some time.

I do not know if it is too late in 2.5.x to begin this work, however. We _are_ in a feature freeze... I suppose it is up to the consensus of arch maintainers, because it [obviously] does not affect ia32.

Actually Andi asked me to do the work. Dave, is it okay with you? What about other maintainers?
Pavel

Signature · 2 lines
Casualities in World Trade Center: ~3k dead inside the building,
cryptography in U.S.A. and free speech in Czech Republic.
#3 lore raw
Quoted 9 lines · Pavel Machek

Hi!

Quoted 6 lines
Quoted 2 lines

Currently, 32-bit emulation in kernel has *5* copies, and its >1000 lines each.

Yes :/ Consolidating all these copies into a single layer has been a "project to be" for quite some time.

Quoted 6 lines
Quoted 3 lines

I do not know if it is too late in 2.5.x to begin this work, however. We _are_ in a feature freeze... I suppose it is up to the consensus of arch maintainers, because it [obviously] does not affect ia32.

Actually Andi asked me to do the work. Dave, is it okay with you? What about other maintainers?

One issue you need to be careful about is that long long has different alignment between 32bit and 64bit on ia64 and x86-64. On sparc64/mips64/ppc64 etc. that isn't the case. The x86-64 handlers convert sometimes more than the later ones because of that.

Also some ioctl handlers are endian dependent, at least in x86-64 (I think I commented them all). Not sure if Dave did the same.

First step probably is to just get register_ioctl32_translation into a common header and implementation file.

-Andi

#4 lore raw
In reply to Pavel Machek

From: Pavel Machek <[email protected]>
Date: Fri, 21 Feb 2003 12:34:29 +0100

Quoted 3 lines

I do not know if it is too late in 2.5.x to begin this work, however. We _are_ in a feature freeze... I suppose it is up to the consensus of arch maintainers, because it [obviously] does not affect ia32.

Actually Andi asked me to do the work. Dave, is it okay with you? What about other maintainers?

I'm totally fine with it.

#5 lore raw
In reply to Pavel Machek
Quoted 5 lines

Actually Andi asked me to do the work. Dave, is it okay with you? What about other maintainers?

From a ppc64 perspective Im happy for this to be done ASAP :)

Anton

#6 lore raw
In reply to Pavel Machek
Quoted 6 lines · Pavel Machek

Hi!

Currently, 32-bit emulation in kernel has *5* copies, and its >1000 lines each. Plus, locking of all but x86-64 architectures is broken (I'm told by andi ;-).

Quoted 7 lines

So, here's patch that starts sharing sys32_ioctl() [as a first step], which should rmove locking problems.

I've done the work for x86-64 and sparc64; if it looks good I'll attempt to do other architectures. [Unless maintainers prefer to do it
themselves: I don't have easy access to 64-bit machines besides hammer.]

Nice. I'm glad that somebody started looking at this (I was going to work on that but didn't have time for it).
Patch looks ok to me.

Eventually we'll be able to kill ugly mess like arch/sparc64/kernel/ioctl32.c. That stuff really belongs to the actual subsystems that implement those ioctls.

Thanks
Max

#7 lore raw
Quoted 2 lines · Max Krasnyansky

Eventually we'll be able to kill ugly mess like arch/sparc64/kernel/ioctl32.c. That stuff really belongs to the actual subsystems that implement those ioctls.

Yes.

Note that ALSA is already doing it right: sound/core/ioctl32/*

Jeff

#8 lore raw
In reply to Max Krasnyansky

From: Max Krasnyansky <[email protected]>
Date: Thu, 20 Feb 2003 14:56:22 -0800

Eventually we'll be able to kill ugly mess like arch/sparc64/kernel/ioctl32.c. That stuff really belongs to the actual subsystems that implement those ioctls.

Not really possible with things like SIOCDEVPRIVATE... Those need special processing and even that is insufficient.

#9 lore raw
Quoted 8 lines · David S. Miller

From: Max Krasnyansky <[email protected]>
Date: Thu, 20 Feb 2003 14:56:22 -0800

Eventually we'll be able to kill ugly mess like arch/sparc64/kernel/ioctl32.c. That stuff really belongs to the actual subsystems that implement those ioctls.

Not really possible with things like SIOCDEVPRIVATE... Those need special processing and even that is insufficient.

Hmm. It seems to that all you need for SIOCDEVPRIVATE is ability to register ranges of ioctls.
i.e. something like this

        int register_ioctl32_conversion_rage(uint start, uint end, handler);

net/core/dev.c

        register_ioctl32_conversion_range(SIOCDEVPRIVATE, SIOCDEVPRIVATE + 15, siocdevprivate_ioctl);

Am I missing something here ?

Max

#10 lore raw
Quoted 8 lines · Max Krasnyansky

Hmm. It seems to that all you need for SIOCDEVPRIVATE is ability to register ranges of ioctls. i.e. something like this

        int register_ioctl32_conversion_rage(uint start, uint end, handler);

net/core/dev.c

        register_ioctl32_conversion_range(SIOCDEVPRIVATE, SIOCDEVPRIVATE + 15, siocdevprivate_ioctl);

Am I missing something here ?

Yes. Think about the name of the ioctl :)

It is impossible for generic arch code to implement support for driver-private ioctls, because these naturally differ between each driver.

Jeff

#11 lore raw
In reply to Max Krasnyansky

From: Max Krasnyansky <[email protected]>
Date: Thu, 20 Feb 2003 17:31:43 -0800

Am I missing something here ?

The data type for things passed into SIOCDEVPRIVATE is totally opaque.

Multiple drivers, passing in different kinds of data, use SIOCDEVPRIVATE. It is inherenly untranslatable at the point where we are at sys_ioctl32().