Skip to contentSkip to conversation

reduce stack in cpqarray.c::ida_ioctl()

PATCH In review Randy.Dunlap 3 participants 8 messages Apr 3, 2003 – Apr 4, 2003 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 reduce stack in cpqarray.c::ida_ioctl()

Hi,

This patch to 2.5.66 reduces stack usage in ida_ioctl() by about 0x500 bytes (on x86).

There is a possibility that the allocation here should be done one time only and the buffer pointer saved for re-use instead of allocating it on each call to ida_ioctl. If that's desirable, I'll have a few questions.

Comments on the patch?

Thanks,
--
~Randy

patch_name: cpqarray-stack.patch
patch_version: 2003-04-02.21:10:01
author: Randy.Dunlap <[email protected]>
description: reduce stack in block/cqparray.c::ida_ioctl()

		from 0x550 to 0x48;

product: Linux
product_versions: linux-2566
changelog: allocate and free <ida_ioctl_t>
maintainer: Steve Cameron: [email protected], [email protected]
diffstat: =

 drivers/block/cpqarray.c |   20 ++++++++++++++------
 1 files changed, 14 insertions(+), 6 deletions(-)
drivers/block/cpqarray.c renamed from drivers/block/cpqarray.c%IDAIOC +14 -6
diff -Naur ./drivers/block/cpqarray.c%IDAIOC ./drivers/block/cpqarray.c
@@ -1021,7 +1021,7 @@
 	int diskinfo[4];
 	struct hd_geometry *geo = (struct hd_geometry *)arg;
 	ida_ioctl_t *io = (ida_ioctl_t*)arg;
-	ida_ioctl_t my_io;
+	ida_ioctl_t *my_io;
 
 	switch(cmd) {
 	case HDIO_GETGEO:
@@ -1046,11 +1046,19 @@
 		return 0;
 	case IDAPASSTHRU:
 		if (!capable(CAP_SYS_RAWIO)) return -EPERM;
-		if (copy_from_user(&my_io, io, sizeof(my_io)))
-			return -EFAULT;
-		error = ida_ctlr_ioctl(ctlr, dsk, &my_io);
-		if (error) return error;
-		return copy_to_user(io, &my_io, sizeof(my_io)) ? -EFAULT : 0;
+		my_io = kmalloc(sizeof(ida_ioctl_t), GFP_KERNEL);
+		if (!my_io)
+			return -ENOMEM;
+		if (copy_from_user(my_io, io, sizeof(*my_io))) {
+			error = -EFAULT;
+			goto iocfree;
+		}
+		error = ida_ctlr_ioctl(ctlr, dsk, my_io);
+		if (error) goto iocfree;
+		error = copy_to_user(io, my_io, sizeof(*my_io)) ? -EFAULT : 0;
+  iocfree:
+		kfree(my_io);
+		return error;
 	case IDAGETCTLRSIG:
 		if (!arg) return -EINVAL;
 		put_user(hba[ctlr]->ctlr_sig, (int*)arg);
#1 lore raw
Quoted 2 lines · Randy.Dunlap

Comments on the patch?

Your patch looks just fine. The original code is a bit odd, though. If you want to change that in one go, read on. If not, please ignore this.

Quoted 20 lines

diff -Naur ./drivers/block/cpqarray.c%IDAIOC ./drivers/block/cpqarray.c

drivers/block/cpqarray.c renamed from drivers/block/cpqarray.c%IDAIOC +1 -5
@@ -1021,7 +1021,7 @@
 	int diskinfo[4];
 	struct hd_geometry *geo = (struct hd_geometry *)arg;
 	ida_ioctl_t *io = (ida_ioctl_t*)arg;
-	ida_ioctl_t my_io;
+	ida_ioctl_t *my_io;
 
 	switch(cmd) {
 	case HDIO_GETGEO:
@@ -1046,11 +1046,19 @@
 		return 0;
 	case IDAPASSTHRU:
 		if (!capable(CAP_SYS_RAWIO)) return -EPERM;
-		if (copy_from_user(&my_io, io, sizeof(my_io)))
-			return -EFAULT;
-		error = ida_ctlr_ioctl(ctlr, dsk, &my_io);
-		if (error) return error;

drivers/block/cpqarray.c line 1057

Quoted 10 lines
drivers/block/cpqarray.c renamed from drivers/block/cpqarray.c%IDAIOC +9 -1
-		return copy_to_user(io, &my_io, sizeof(my_io)) ? -EFAULT : 0;
+		my_io = kmalloc(sizeof(ida_ioctl_t), GFP_KERNEL);
+		if (!my_io)
+			return -ENOMEM;
+		if (copy_from_user(my_io, io, sizeof(*my_io))) {
+			error = -EFAULT;
+			goto iocfree;
+		}
+		error = ida_ctlr_ioctl(ctlr, dsk, my_io);
+		if (error) goto iocfree;

Wouldn't an extra line be nicer?

drivers/block/cpqarray.c line 1058

Quoted 1 line
drivers/block/cpqarray.c renamed from drivers/block/cpqarray.c%IDAIOC +1 -0
+		error = copy_to_user(io, my_io, sizeof(*my_io)) ? -EFAULT : 0;

copy_to_user returns the bytes successfully copied. error is set to -EFAULT, if there was actually data transferred?

How about:
+ error = copy_to_user(io, my_io, sizeof(*my_io)) < sizeof(*my_io) ? -EFAULT : 0;

drivers/block/cpqarray.c line 1064

Quoted 6 lines
drivers/block/cpqarray.c renamed from drivers/block/cpqarray.c%IDAIOC +3 -0
+  iocfree:
+		kfree(my_io);
+		return error;
 	case IDAGETCTLRSIG:
 		if (!arg) return -EINVAL;
 		put_user(hba[ctlr]->ctlr_sig, (int*)arg);

Jörn

Signature · 2 lines
Good warriors cause others to come to them and do not go to others.
-- Sun Tzu
#2 lore raw

On Fri, 4 Apr 2003 02:30:44 +0200 Jörn Engel <[email protected]> wrote:

| On Thu, 3 April 2003 12:03:08 +0000, Randy.Dunlap wrote:
| > 
| > Comments on the patch?
| 
| Your patch looks just fine. The original code is a bit odd, though.
| If you want to change that in one go, read on. If not, please ignore
| this.
| 
| > +		error = ida_ctlr_ioctl(ctlr, dsk, my_io);
| > +		if (error) goto iocfree;
| 
| Wouldn't an extra line be nicer?

Probably. I would normally do that. In this case I was just maintaining the style that is already used in that source file.

| > +		error = copy_to_user(io, my_io, sizeof(*my_io)) ? -EFAULT : 0;
| 
| copy_to_user returns the bytes successfully copied.
| error is set to -EFAULT, if there was actually data transferred?

Did you verify that?

| How about:
| + error = copy_to_user(io, my_io, sizeof(*my_io)) < sizeof(*my_io) ? -EFAULT : 0;

--
~Randy

#3 lore raw
Quoted 7 lines · Randy.Dunlap
| > +		error = copy_to_user(io, my_io, sizeof(*my_io)) ? -EFAULT : 0;
| 
| copy_to_user returns the bytes successfully copied.
| error is set to -EFAULT, if there was actually data transferred?

Did you verify that?

Yes, but I do make mistakes. Better double-check it yourself.

Jörn

Signature · 2 lines
"Error protection by error detection and correction."
-- from a university class
#4 lore raw

On Fri, 4 Apr 2003 09:56:52 +0200 Jörn Engel <[email protected]> wrote:

| On Thu, 3 April 2003 17:33:52 +0000, Randy.Dunlap wrote:
| > 
| > | > +		error = copy_to_user(io, my_io, sizeof(*my_io)) ? -EFAULT : 0;
| > | 
| > | copy_to_user returns the bytes successfully copied.
| > | error is set to -EFAULT, if there was actually data transferred?
| > 
| > Did you verify that?
| 
| Yes, but I do make mistakes. Better double-check it yourself.

Oh, I did. That's why I am asking you.

--
~Randy

#5 lore raw
Quoted 13 lines · Randy.Dunlap

On Fri, 4 Apr 2003 09:56:52 +0200 Jörn Engel <[email protected]> wrote:

| On Thu, 3 April 2003 17:33:52 +0000, Randy.Dunlap wrote:
| > 
| > | > +		error = copy_to_user(io, my_io, sizeof(*my_io)) ? -EFAULT : 0;
| > | 
| > | copy_to_user returns the bytes successfully copied.
| > | error is set to -EFAULT, if there was actually data transferred?
| > 
| > Did you verify that?
| 
| Yes, but I do make mistakes. Better double-check it yourself.
Quoted 1 line

Oh, I did. That's why I am asking you.

It was my mistake. I got confused reading uaccess.h. Sorry about that.

Jörn

Signature · 2 lines
Time? What's that? Time is only worth what you do with it.
-- Theo de Raadt
#6 lore raw
In reply to Jörn Engel

Review

Quoted 7 lines · Jörn Engel

drivers/block/cpqarray.c line 1058

Quoted 1 line
drivers/block/cpqarray.c renamed from drivers/block/cpqarray.c%IDAIOC +1 -0
+		error = copy_to_user(io, my_io, sizeof(*my_io)) ? -EFAULT : 0;

copy_to_user returns the bytes successfully copied. error is set to -EFAULT, if there was actually data transferred?

How about:

(quoted diff) +1 -0
+		error = copy_to_user(io, my_io, sizeof(*my_io)) < sizeof(*my_io) ? -EFAULT : 0;

Pure nonsense! Correct logic, and much nicer to read IMO is:

	if (copy_to_user(io, my_io, sizeof(*my_io))
		error = -EFAULT;
Signature · 1 line
Jens Axboe
#7 lore raw
Quoted 6 lines · Jens Axboe
Quoted 5 lines · Jörn Engel

drivers/block/cpqarray.c line 1058

Quoted 1 line
drivers/block/cpqarray.c renamed from drivers/block/cpqarray.c%IDAIOC +1 -0
+		error = copy_to_user(io, my_io, sizeof(*my_io)) ? -EFAULT : 0;

copy_to_user returns the bytes successfully copied. error is set to -EFAULT, if there was actually data transferred?

Quoted 7 lines

Review

Quoted 2 lines

How about:

(quoted diff) +1 -0
+		error = copy_to_user(io, my_io, sizeof(*my_io)) < sizeof(*my_io) ? -EFAULT : 0;

Pure nonsense! Correct logic, and much nicer to read IMO is:

	if (copy_to_user(io, my_io, sizeof(*my_io))
		error = -EFAULT;

Yes, you are right. I just couldn't read inline assembler properly.

Jörn

Signature · 3 lines
With a PC, I always felt limited by the software available. On Unix, 
I am limited only by my knowledge.
-- Peter J. Schoenster