<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
From: Yum Rayan &lt;yum.rayan@gmail.com&gt;

This patch reduces the stack usage of the function serial_event() in
serial_cs from 2212 to 228.  I used a patched version of gcc 3.4.3 on i386
with -fno-unit-at-a-time disabled.

This patch is only compile tested.

Acked-by: Randy Dunlap &lt;rddunlap@osdl.org&gt;
Signed-off-by: Yum Rayan &lt;yum.rayan@gmail.com&gt;
Signed-off-by: Andrew Morton &lt;akpm@osdl.org&gt;
---

 drivers/serial/serial_cs.c |  173 +++++++++++++++++++++++++++++----------------
 1 files changed, 112 insertions(+), 61 deletions(-)

diff -puN drivers/serial/serial_cs.c~serial_cs-reduce-stack-usage-in-serial_event drivers/serial/serial_cs.c
--- 25/drivers/serial/serial_cs.c~serial_cs-reduce-stack-usage-in-serial_event	Fri Apr 29 16:13:22 2005
+++ 25-akpm/drivers/serial/serial_cs.c	Fri Apr 29 16:13:22 2005
@@ -107,6 +107,13 @@ struct serial_info {
 	int			line[4];
 };
 
+struct serial_cfg_mem {
+	tuple_t tuple;
+	cisparse_t parse;
+	u_char buf[256];
+};
+
+
 static void serial_config(dev_link_t * link);
 static int serial_event(event_t event, int priority,
 			event_callback_args_t * args);
@@ -357,14 +364,24 @@ static int simple_config(dev_link_t *lin
 	static int size_table[2] = { 8, 16 };
 	client_handle_t handle = link-&gt;handle;
 	struct serial_info *info = link-&gt;priv;
-	tuple_t tuple;
-	u_char buf[256];
-	cisparse_t parse;
-	cistpl_cftable_entry_t *cf = &amp;parse.cftable_entry;
+	struct serial_cfg_mem *cfg_mem;
+	tuple_t *tuple;
+	u_char *buf;
+	cisparse_t *parse;
+	cistpl_cftable_entry_t *cf;
 	config_info_t config;
 	int i, j, try;
 	int s;
 
+	cfg_mem = kmalloc(sizeof(struct serial_cfg_mem), GFP_KERNEL);
+	if (!cfg_mem)
+		return -1;
+
+	tuple = &amp;cfg_mem-&gt;tuple;
+	parse = &amp;cfg_mem-&gt;parse;
+	cf = &amp;parse-&gt;cftable_entry;
+	buf = cfg_mem-&gt;buf;
+
 	/* If the card is already configured, look up the port and irq */
 	i = pcmcia_get_configuration_info(handle, &amp;config);
 	if ((i == CS_SUCCESS) &amp;&amp; (config.Attributes &amp; CONF_VALID_CLIENT)) {
@@ -377,21 +394,23 @@ static int simple_config(dev_link_t *lin
 			port = config.BasePort1 + 0x28;
 			info-&gt;slave = 1;
 		}
-		if (info-&gt;slave)
+		if (info-&gt;slave) {
+			kfree(cfg_mem);
 			return setup_serial(handle, info, port, config.AssignedIRQ);
+		}
 	}
 	link-&gt;conf.Vcc = config.Vcc;
 
 	/* First pass: look for a config entry that looks normal. */
-	tuple.TupleData = (cisdata_t *) buf;
-	tuple.TupleOffset = 0;
-	tuple.TupleDataMax = 255;
-	tuple.Attributes = 0;
-	tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
+	tuple-&gt;TupleData = (cisdata_t *) buf;
+	tuple-&gt;TupleOffset = 0;
+	tuple-&gt;TupleDataMax = 255;
+	tuple-&gt;Attributes = 0;
+	tuple-&gt;DesiredTuple = CISTPL_CFTABLE_ENTRY;
 	/* Two tries: without IO aliases, then with aliases */
 	for (s = 0; s &lt; 2; s++) {
 		for (try = 0; try &lt; 2; try++) {
-			i = first_tuple(handle, &amp;tuple, &amp;parse);
+			i = first_tuple(handle, tuple, parse);
 			while (i != CS_NO_MORE_ITEMS) {
 				if (i != CS_SUCCESS)
 					goto next_entry;
@@ -409,14 +428,14 @@ static int simple_config(dev_link_t *lin
 						goto found_port;
 				}
 next_entry:
-				i = next_tuple(handle, &amp;tuple, &amp;parse);
+				i = next_tuple(handle, tuple, parse);
 			}
 		}
 	}
 	/* Second pass: try to find an entry that isn't picky about
 	   its base address, then try to grab any standard serial port
 	   address, and finally try to get any free port. */
-	i = first_tuple(handle, &amp;tuple, &amp;parse);
+	i = first_tuple(handle, tuple, parse);
 	while (i != CS_NO_MORE_ITEMS) {
 		if ((i == CS_SUCCESS) &amp;&amp; (cf-&gt;io.nwin &gt; 0) &amp;&amp;
 		    ((cf-&gt;io.flags &amp; CISTPL_IO_LINES_MASK) &lt;= 3)) {
@@ -429,7 +448,7 @@ next_entry:
 					goto found_port;
 			}
 		}
-		i = next_tuple(handle, &amp;tuple, &amp;parse);
+		i = next_tuple(handle, tuple, parse);
 	}
 
       found_port:
@@ -437,6 +456,7 @@ next_entry:
 		printk(KERN_NOTICE
 		       "serial_cs: no usable port range found, giving up\n");
 		cs_error(link-&gt;handle, RequestIO, i);
+		kfree(cfg_mem);
 		return -1;
 	}
 
@@ -450,9 +470,10 @@ next_entry:
 	i = pcmcia_request_configuration(link-&gt;handle, &amp;link-&gt;conf);
 	if (i != CS_SUCCESS) {
 		cs_error(link-&gt;handle, RequestConfiguration, i);
+		kfree(cfg_mem);
 		return -1;
 	}
-
+	kfree(cfg_mem);
 	return setup_serial(handle, info, link-&gt;io.BasePort1, link-&gt;irq.AssignedIRQ);
 }
 
@@ -460,29 +481,39 @@ static int multi_config(dev_link_t * lin
 {
 	client_handle_t handle = link-&gt;handle;
 	struct serial_info *info = link-&gt;priv;
-	tuple_t tuple;
-	u_char buf[256];
-	cisparse_t parse;
-	cistpl_cftable_entry_t *cf = &amp;parse.cftable_entry;
+	struct serial_cfg_mem *cfg_mem;
+	tuple_t *tuple;
+	u_char *buf;
+	cisparse_t *parse;
+	cistpl_cftable_entry_t *cf;
 	config_info_t config;
-	int i, base2 = 0;
+	int i, rc, base2 = 0;
+
+	cfg_mem = kmalloc(sizeof(struct serial_cfg_mem), GFP_KERNEL);
+	if (!cfg_mem)
+		return -1;
+	tuple = &amp;cfg_mem-&gt;tuple;
+	parse = &amp;cfg_mem-&gt;parse;
+	cf = &amp;parse-&gt;cftable_entry;
+	buf = cfg_mem-&gt;buf;
 
 	i = pcmcia_get_configuration_info(handle, &amp;config);
 	if (i != CS_SUCCESS) {
 		cs_error(handle, GetConfigurationInfo, i);
-		return -1;
+		rc = -1;
+		goto free_cfg_mem;
 	}
 	link-&gt;conf.Vcc = config.Vcc;
 
-	tuple.TupleData = (cisdata_t *) buf;
-	tuple.TupleOffset = 0;
-	tuple.TupleDataMax = 255;
-	tuple.Attributes = 0;
-	tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
+	tuple-&gt;TupleData = (cisdata_t *) buf;
+	tuple-&gt;TupleOffset = 0;
+	tuple-&gt;TupleDataMax = 255;
+	tuple-&gt;Attributes = 0;
+	tuple-&gt;DesiredTuple = CISTPL_CFTABLE_ENTRY;
 
 	/* First, look for a generic full-sized window */
 	link-&gt;io.NumPorts1 = info-&gt;multi * 8;
-	i = first_tuple(handle, &amp;tuple, &amp;parse);
+	i = first_tuple(handle, tuple, parse);
 	while (i != CS_NO_MORE_ITEMS) {
 		/* The quad port cards have bad CIS's, so just look for a
 		   window larger than 8 ports and assume it will be right */
@@ -497,14 +528,14 @@ static int multi_config(dev_link_t * lin
 			if (i == CS_SUCCESS)
 				break;
 		}
-		i = next_tuple(handle, &amp;tuple, &amp;parse);
+		i = next_tuple(handle, tuple, parse);
 	}
 
 	/* If that didn't work, look for two windows */
 	if (i != CS_SUCCESS) {
 		link-&gt;io.NumPorts1 = link-&gt;io.NumPorts2 = 8;
 		info-&gt;multi = 2;
-		i = first_tuple(handle, &amp;tuple, &amp;parse);
+		i = first_tuple(handle, tuple, parse);
 		while (i != CS_NO_MORE_ITEMS) {
 			if ((i == CS_SUCCESS) &amp;&amp; (cf-&gt;io.nwin == 2)) {
 				link-&gt;conf.ConfigIndex = cf-&gt;index;
@@ -517,13 +548,14 @@ static int multi_config(dev_link_t * lin
 				if (i == CS_SUCCESS)
 					break;
 			}
-			i = next_tuple(handle, &amp;tuple, &amp;parse);
+			i = next_tuple(handle, tuple, parse);
 		}
 	}
 
 	if (i != CS_SUCCESS) {
 		cs_error(link-&gt;handle, RequestIO, i);
-		return -1;
+		rc = -1;
+		goto free_cfg_mem;
 	}
 
 	i = pcmcia_request_irq(link-&gt;handle, &amp;link-&gt;irq);
@@ -541,7 +573,8 @@ static int multi_config(dev_link_t * lin
 	i = pcmcia_request_configuration(link-&gt;handle, &amp;link-&gt;conf);
 	if (i != CS_SUCCESS) {
 		cs_error(link-&gt;handle, RequestConfiguration, i);
-		return -1;
+		rc = -1;
+		goto free_cfg_mem;
 	}
 
 	/* The Oxford Semiconductor OXCF950 cards are in fact single-port:
@@ -554,17 +587,23 @@ static int multi_config(dev_link_t * lin
 			setup_serial(handle, info, link-&gt;io.BasePort1, link-&gt;irq.AssignedIRQ);
 			outb(12, base2 + 1);
 		}
-		return 0;
+		rc = 0;
+		goto free_cfg_mem;
 	}
 
 	setup_serial(handle, info, link-&gt;io.BasePort1, link-&gt;irq.AssignedIRQ);
 	/* The Nokia cards are not really multiport cards */
-	if (info-&gt;manfid == MANFID_NOKIA)
-		return 0;
+	if (info-&gt;manfid == MANFID_NOKIA) {
+		rc = 0;
+		goto free_cfg_mem;
+	}
 	for (i = 0; i &lt; info-&gt;multi - 1; i++)
-		setup_serial(handle, info, base2 + (8 * i), link-&gt;irq.AssignedIRQ);
-
-	return 0;
+		setup_serial(handle, info, base2 + (8 * i),
+				link-&gt;irq.AssignedIRQ);
+	rc = 0;
+free_cfg_mem:
+	kfree(cfg_mem);
+	return rc;
 }
 
 /*======================================================================
@@ -579,39 +618,49 @@ void serial_config(dev_link_t * link)
 {
 	client_handle_t handle = link-&gt;handle;
 	struct serial_info *info = link-&gt;priv;
-	tuple_t tuple;
-	u_short buf[128];
-	cisparse_t parse;
-	cistpl_cftable_entry_t *cf = &amp;parse.cftable_entry;
+	struct serial_cfg_mem *cfg_mem;
+	tuple_t *tuple;
+	u_char *buf;
+	cisparse_t *parse;
+	cistpl_cftable_entry_t *cf;
 	int i, last_ret, last_fn;
 
 	DEBUG(0, "serial_config(0x%p)\n", link);
 
-	tuple.TupleData = (cisdata_t *) buf;
-	tuple.TupleOffset = 0;
-	tuple.TupleDataMax = 255;
-	tuple.Attributes = 0;
+	cfg_mem = kmalloc(sizeof(struct serial_cfg_mem), GFP_KERNEL);
+	if (!cfg_mem)
+		goto failed;
+
+	tuple = &amp;cfg_mem-&gt;tuple;
+	parse = &amp;cfg_mem-&gt;parse;
+	cf = &amp;parse-&gt;cftable_entry;
+	buf = cfg_mem-&gt;buf;
+
+	tuple-&gt;TupleData = (cisdata_t *) buf;
+	tuple-&gt;TupleOffset = 0;
+	tuple-&gt;TupleDataMax = 255;
+	tuple-&gt;Attributes = 0;
 	/* Get configuration register information */
-	tuple.DesiredTuple = CISTPL_CONFIG;
-	last_ret = first_tuple(handle, &amp;tuple, &amp;parse);
+	tuple-&gt;DesiredTuple = CISTPL_CONFIG;
+	last_ret = first_tuple(handle, tuple, parse);
 	if (last_ret != CS_SUCCESS) {
 		last_fn = ParseTuple;
 		goto cs_failed;
 	}
-	link-&gt;conf.ConfigBase = parse.config.base;
-	link-&gt;conf.Present = parse.config.rmask[0];
+	link-&gt;conf.ConfigBase = parse-&gt;config.base;
+	link-&gt;conf.Present = parse-&gt;config.rmask[0];
 
 	/* Configure card */
 	link-&gt;state |= DEV_CONFIG;
 
 	/* Is this a compliant multifunction card? */
-	tuple.DesiredTuple = CISTPL_LONGLINK_MFC;
-	tuple.Attributes = TUPLE_RETURN_COMMON | TUPLE_RETURN_LINK;
-	info-&gt;multi = (first_tuple(handle, &amp;tuple, &amp;parse) == CS_SUCCESS);
+	tuple-&gt;DesiredTuple = CISTPL_LONGLINK_MFC;
+	tuple-&gt;Attributes = TUPLE_RETURN_COMMON | TUPLE_RETURN_LINK;
+	info-&gt;multi = (first_tuple(handle, tuple, parse) == CS_SUCCESS);
 
 	/* Is this a multiport card? */
-	tuple.DesiredTuple = CISTPL_MANFID;
-	if (first_tuple(handle, &amp;tuple, &amp;parse) == CS_SUCCESS) {
+	tuple-&gt;DesiredTuple = CISTPL_MANFID;
+	if (first_tuple(handle, tuple, parse) == CS_SUCCESS) {
 		info-&gt;manfid = le16_to_cpu(buf[0]);
 		for (i = 0; i &lt; MULTI_COUNT; i++)
 			if ((info-&gt;manfid == multi_id[i].manfid) &amp;&amp;
@@ -623,13 +672,13 @@ void serial_config(dev_link_t * link)
 
 	/* Another check for dual-serial cards: look for either serial or
 	   multifunction cards that ask for appropriate IO port ranges */
-	tuple.DesiredTuple = CISTPL_FUNCID;
+	tuple-&gt;DesiredTuple = CISTPL_FUNCID;
 	if ((info-&gt;multi == 0) &amp;&amp;
-	    ((first_tuple(handle, &amp;tuple, &amp;parse) != CS_SUCCESS) ||
-	     (parse.funcid.func == CISTPL_FUNCID_MULTI) ||
-	     (parse.funcid.func == CISTPL_FUNCID_SERIAL))) {
-		tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
-		if (first_tuple(handle, &amp;tuple, &amp;parse) == CS_SUCCESS) {
+	    ((first_tuple(handle, tuple, parse) != CS_SUCCESS) ||
+	     (parse-&gt;funcid.func == CISTPL_FUNCID_MULTI) ||
+	     (parse-&gt;funcid.func == CISTPL_FUNCID_SERIAL))) {
+		tuple-&gt;DesiredTuple = CISTPL_CFTABLE_ENTRY;
+		if (first_tuple(handle, tuple, parse) == CS_SUCCESS) {
 			if ((cf-&gt;io.nwin == 1) &amp;&amp; (cf-&gt;io.win[0].len % 8 == 0))
 				info-&gt;multi = cf-&gt;io.win[0].len &gt;&gt; 3;
 			if ((cf-&gt;io.nwin == 2) &amp;&amp; (cf-&gt;io.win[0].len == 8) &amp;&amp;
@@ -664,6 +713,7 @@ void serial_config(dev_link_t * link)
 
 	link-&gt;dev = &amp;info-&gt;node[0];
 	link-&gt;state &amp;= ~DEV_CONFIG_PENDING;
+	kfree(cfg_mem);
 	return;
 
  cs_failed:
@@ -671,6 +721,7 @@ void serial_config(dev_link_t * link)
  failed:
 	serial_remove(link);
 	link-&gt;state &amp;= ~DEV_CONFIG_PENDING;
+	kfree(cfg_mem);
 }
 
 /*======================================================================
_
</pre></body></html>