All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yum Rayan <yum.rayan@gmail.com>
To: linux-kernel@vger.kernel.org
Subject: [PATCH] Reduce stack usage in sys.c
Date: Wed, 30 Mar 2005 23:44:38 -0800	[thread overview]
Message-ID: <df35dfeb05033023445c386d2d@mail.gmail.com> (raw)

Attempt to reduce stack usage in sys.c (linux-2.6.12-rc1-mm3). Stack
usage was noted using checkstack.pl. Specifically

Before patch
------------
sys_reboot - 256

After patch
-----------
sys_reboot - none (register usage only)

Along the way, wrap code to 80 column width and cleanup lock usage.

Signed-off-by: Yum Rayan <yum.rayan@gmail.com>

--- a/kernel/sys.c	2005-03-25 22:11:06.000000000 -0800
+++ b/kernel/sys.c	2005-03-30 22:34:03.000000000 -0800
@@ -369,9 +369,9 @@
  *
  * reboot doesn't sync: do that yourself before calling this.
  */
-asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd,
void __user * arg)
+asmlinkage long sys_reboot(int magic1, int magic2,
+			   unsigned int cmd, void __user * arg)
 {
-	char buffer[256];
 
 	/* We only trust the superuser with rebooting the system. */
 	if (!capable(CAP_SYS_BOOT))
@@ -385,14 +385,15 @@
 	                magic2 != LINUX_REBOOT_MAGIC2C))
 		return -EINVAL;
 
-	lock_kernel();
 	switch (cmd) {
 	case LINUX_REBOOT_CMD_RESTART:
+		lock_kernel();
 		notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
 		system_state = SYSTEM_RESTART;
 		device_shutdown();
 		printk(KERN_EMERG "Restarting system.\n");
 		machine_restart(NULL);
+		unlock_kernel();
 		break;
 
 	case LINUX_REBOOT_CMD_CAD_ON:
@@ -404,6 +405,7 @@
 		break;
 
 	case LINUX_REBOOT_CMD_HALT:
+		lock_kernel();
 		notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
 		system_state = SYSTEM_HALT;
 		device_shutdown();
@@ -414,6 +416,7 @@
 		break;
 
 	case LINUX_REBOOT_CMD_POWER_OFF:
+		lock_kernel();
 		notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
 		system_state = SYSTEM_POWER_OFF;
 		device_shutdown();
@@ -424,51 +427,60 @@
 		break;
 
 	case LINUX_REBOOT_CMD_RESTART2:
-		if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
-			unlock_kernel();
+	{
+		char *buffer;
+		buffer = kmalloc(256, GFP_KERNEL);
+		if (!buffer)
+			return -ENOMEM;
+		if (strncpy_from_user(buffer, arg, 255) < 0) {
+			kfree(buffer);
 			return -EFAULT;
 		}
-		buffer[sizeof(buffer) - 1] = '\0';
-
+		buffer[255] = '\0';
+		lock_kernel();
 		notifier_call_chain(&reboot_notifier_list, SYS_RESTART, buffer);
 		system_state = SYSTEM_RESTART;
 		device_shutdown();
-		printk(KERN_EMERG "Restarting system with command '%s'.\n", buffer);
+		printk(KERN_EMERG "Restarting system with command '%s'.\n",
+									buffer);
 		machine_restart(buffer);
+		unlock_kernel();
+		kfree(buffer);
 		break;
-
+	}
 #ifdef CONFIG_KEXEC
 	case LINUX_REBOOT_CMD_KEXEC:
 	{
 		struct kimage *image;
 		image = xchg(&kexec_image, 0);
 		if (!image) {
-			unlock_kernel();
 			return -EINVAL;
 		}
+		lock_kernel();
 		notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
 		system_state = SYSTEM_RESTART;
 		device_shutdown();
 		printk(KERN_EMERG "Starting new kernel\n");
 		machine_shutdown();
 		machine_kexec(image);
+		unlock_kernel();
 		break;
 	}
 #endif
 #ifdef CONFIG_SOFTWARE_SUSPEND
 	case LINUX_REBOOT_CMD_SW_SUSPEND:
-		{
-			int ret = software_suspend();
-			unlock_kernel();
-			return ret;
-		}
+	{
+		int ret;
+		lock_kernel();
+		ret = software_suspend();
+		unlock_kernel();
+		return ret;
+	}
 #endif
 
 	default:
-		unlock_kernel();
 		return -EINVAL;
 	}
-	unlock_kernel();
 	return 0;
 }

             reply	other threads:[~2005-03-31  7:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-31  7:44 Yum Rayan [this message]
2005-03-31  8:29 ` [PATCH] Reduce stack usage in sys.c Jeff Garzik
2005-04-02  7:05   ` [PATCH] Cleanup locking in sys_reboot() (was Re: [PATCH] Reduce stack usage in sys.c) Yum Rayan
2005-03-31 13:01 ` [PATCH] Reduce stack usage in sys.c Jörn Engel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=df35dfeb05033023445c386d2d@mail.gmail.com \
    --to=yum.rayan@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.