Skip to contentSkip to conversation

pktgen: reduce stack usage

PATCH Applied Randy.Dunlap 5 participants 10 messages Feb 18, 2005 – Feb 24, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pktgen: reduce stack usage

(resend)
Any comments this time?

pktgen: proc_if_write: reduce stack usage (on i386) from 776 to 296 bytes by combining/reusing locals.

diffstat:=

 net/core/pktgen.c |   94 ++++++++++++++++++++++++------------------------------
 1 files changed, 42 insertions(+), 52 deletions(-)
net/core/pktgen.c renamed from net/core/pktgen.c~pktgen_stack +42 -52
diff -Naurp ./net/core/pktgen.c~pktgen_stack ./net/core/pktgen.c
@@ -811,6 +811,8 @@ static int proc_if_write(struct file *fi
         struct pktgen_dev *pkt_dev = (struct pktgen_dev*)(data);
         char* pg_result = NULL;
         int tmp = 0;
+	char buf4[IP_NAME_SZ];
+	char buf6[128];
         
         pg_result = &(pkt_dev->result[0]);
         
@@ -1071,16 +1073,15 @@ static int proc_if_write(struct file *fi
 		return count;
 	}
 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
                 if (len < 0) { return len; }
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(buf4, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
-                if (strcmp(buf, pkt_dev->dst_min) != 0) {
+                buf4[len] = 0;
+                if (strcmp(buf4, pkt_dev->dst_min) != 0) {
                         memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
-                        strncpy(pkt_dev->dst_min, buf, len);
+                        strncpy(pkt_dev->dst_min, buf4, len);
                         pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
                         pkt_dev->cur_daddr = pkt_dev->daddr_min;
                 }
@@ -1091,17 +1092,16 @@ static int proc_if_write(struct file *fi
 		return count;
 	}
 	if (!strcmp(name, "dst_max")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
                 if (len < 0) { return len; }
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(buf4, &user_buffer[i], len))
 			return -EFAULT;
 
-                buf[len] = 0;
-                if (strcmp(buf, pkt_dev->dst_max) != 0) {
+                buf4[len] = 0;
+                if (strcmp(buf4, pkt_dev->dst_max) != 0) {
                         memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
-                        strncpy(pkt_dev->dst_max, buf, len);
+                        strncpy(pkt_dev->dst_max, buf4, len);
                         pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
                         pkt_dev->cur_daddr = pkt_dev->daddr_max;
                 }
@@ -1112,108 +1112,99 @@ static int proc_if_write(struct file *fi
 		return count;
 	}
 	if (!strcmp(name, "dst6")) {
-                char buf[128];
-
 		len = strn_len(&user_buffer[i], 128 - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(buf6, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
+                buf6[len] = 0;
 
-		scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
-		fmt_ip6(buf,  pkt_dev->in6_daddr.s6_addr);
+		scan_ip6(buf6, pkt_dev->in6_daddr.s6_addr);
+		fmt_ip6(buf6,  pkt_dev->in6_daddr.s6_addr);
 
 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
 
                 if(debug) 
-			printk("pktgen: dst6 set to: %s\n", buf);
+			printk("pktgen: dst6 set to: %s\n", buf6);
 
                 i += len;
-		sprintf(pg_result, "OK: dst6=%s", buf);
+		sprintf(pg_result, "OK: dst6=%s", buf6);
 		return count;
 	}
 	if (!strcmp(name, "dst6_min")) {
-                char buf[128];
-
 		len = strn_len(&user_buffer[i], 128 - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(buf6, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
+                buf6[len] = 0;
 
-		scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
-		fmt_ip6(buf,  pkt_dev->min_in6_daddr.s6_addr);
+		scan_ip6(buf6, pkt_dev->min_in6_daddr.s6_addr);
+		fmt_ip6(buf6,  pkt_dev->min_in6_daddr.s6_addr);
 
 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->min_in6_daddr);
                 if(debug) 
-			printk("pktgen: dst6_min set to: %s\n", buf);
+			printk("pktgen: dst6_min set to: %s\n", buf6);
 
                 i += len;
-		sprintf(pg_result, "OK: dst6_min=%s", buf);
+		sprintf(pg_result, "OK: dst6_min=%s", buf6);
 		return count;
 	}
 	if (!strcmp(name, "dst6_max")) {
-                char buf[128];
-
 		len = strn_len(&user_buffer[i], 128 - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(buf6, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
+                buf6[len] = 0;
 
-		scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
-		fmt_ip6(buf,  pkt_dev->max_in6_daddr.s6_addr);
+		scan_ip6(buf6, pkt_dev->max_in6_daddr.s6_addr);
+		fmt_ip6(buf6,  pkt_dev->max_in6_daddr.s6_addr);
 
                 if(debug) 
-			printk("pktgen: dst6_max set to: %s\n", buf);
+			printk("pktgen: dst6_max set to: %s\n", buf6);
 
                 i += len;
-		sprintf(pg_result, "OK: dst6_max=%s", buf);
+		sprintf(pg_result, "OK: dst6_max=%s", buf6);
 		return count;
 	}
 	if (!strcmp(name, "src6")) {
-                char buf[128];
-
 		len = strn_len(&user_buffer[i], 128 - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(buf6, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
+                buf6[len] = 0;
 
-		scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
-		fmt_ip6(buf,  pkt_dev->in6_saddr.s6_addr);
+		scan_ip6(buf6, pkt_dev->in6_saddr.s6_addr);
+		fmt_ip6(buf6,  pkt_dev->in6_saddr.s6_addr);
 
 		ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
 
                 if(debug) 
-			printk("pktgen: src6 set to: %s\n", buf);
+			printk("pktgen: src6 set to: %s\n", buf6);
 		
                 i += len;
-		sprintf(pg_result, "OK: src6=%s", buf);
+		sprintf(pg_result, "OK: src6=%s", buf6);
 		return count;
 	}
 	if (!strcmp(name, "src_min")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
                 if (len < 0) { return len; }
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(buf4, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
-                if (strcmp(buf, pkt_dev->src_min) != 0) {
+                buf4[len] = 0;
+                if (strcmp(buf4, pkt_dev->src_min) != 0) {
                         memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
-                        strncpy(pkt_dev->src_min, buf, len);
+                        strncpy(pkt_dev->src_min, buf4, len);
                         pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
                         pkt_dev->cur_saddr = pkt_dev->saddr_min;
                 }
@@ -1224,15 +1215,14 @@ static int proc_if_write(struct file *fi
 		return count;
 	}
 	if (!strcmp(name, "src_max")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
                 if (len < 0) { return len; }
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(buf4, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
-                if (strcmp(buf, pkt_dev->src_max) != 0) {
+                buf4[len] = 0;
+                if (strcmp(buf4, pkt_dev->src_max) != 0) {
                         memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
-                        strncpy(pkt_dev->src_max, buf, len);
+                        strncpy(pkt_dev->src_max, buf4, len);
                         pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
                         pkt_dev->cur_saddr = pkt_dev->saddr_max;
                 }

---

#1 lore raw

Randy.Dunlap <[email protected]> :

Quoted 2 lines

(resend)
Any comments this time?

Did I miss a simultaneous use of both buffers or could it be possible to save an extra IP_NAME_SZ bytes of data with an evil ugly union ?

Btw, does anyone intend to fix the use of sleepable functions in proc_thread_write() with spinlock ("thread_lock") held or should I allocate it some cycles ?

--
Ueimor

#2 lore raw
Quoted 8 lines · Francois Romieu

Randy.Dunlap <[email protected]> :

Quoted 2 lines

(resend)
Any comments this time?

Did I miss a simultaneous use of both buffers or could it be possible to save an extra IP_NAME_SZ bytes of data with an evil ugly union ?

That's probably doable, I'll double-check it and modify the patch...

Quoted 2 lines

Btw, does anyone intend to fix the use of sleepable functions in proc_thread_write() with spinlock ("thread_lock") held or should I allocate it some cycles ?

Signature · 1 line
~Randy
#3 lore raw
In reply to Francois Romieu
Quoted 8 lines · Francois Romieu

Randy.Dunlap <[email protected]> :

Quoted 2 lines

(resend)
Any comments this time?

Did I miss a simultaneous use of both buffers or could it be possible to save an extra IP_NAME_SZ bytes of data with an evil ugly union ?

I don't see any simultaneous uses of the 2 buffers, so here's the union version of the patch (attached this time), although it only saves 4 bytes... so maybe the compiler had already realized that usage. Either one accomplishes a large stack reduction, but the first one is slightly more readable & maintainable (to me), and less error-prone (or more future-proof) (again, to me).

Thanks,

Signature · 1 line
~Randy

pktgen: proc_if_write: reduce stack usage (on i386) from 776 to 292 bytes by combining/reusing locals.

diffstat:=

 net/core/pktgen.c |   96 ++++++++++++++++++++++++------------------------------
 1 files changed, 44 insertions(+), 52 deletions(-)
net/core/pktgen.c renamed from net/core/pktgen.c~pktgen_stack +44 -52
diff -Naurp ./net/core/pktgen.c~pktgen_stack ./net/core/pktgen.c
@@ -811,6 +811,10 @@ static int proc_if_write(struct file *fi
         struct pktgen_dev *pkt_dev = (struct pktgen_dev*)(data);
         char* pg_result = NULL;
         int tmp = 0;
+	union bufs {
+		char buf4[IP_NAME_SZ];
+		char buf6[128];
+	} bu;
         
         pg_result = &(pkt_dev->result[0]);
         
@@ -1071,16 +1075,15 @@ static int proc_if_write(struct file *fi
 		return count;
 	}
 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
                 if (len < 0) { return len; }
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(bu.buf4, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
-                if (strcmp(buf, pkt_dev->dst_min) != 0) {
+                bu.buf4[len] = 0;
+                if (strcmp(bu.buf4, pkt_dev->dst_min) != 0) {
                         memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
-                        strncpy(pkt_dev->dst_min, buf, len);
+                        strncpy(pkt_dev->dst_min, bu.buf4, len);
                         pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
                         pkt_dev->cur_daddr = pkt_dev->daddr_min;
                 }
@@ -1091,17 +1094,16 @@ static int proc_if_write(struct file *fi
 		return count;
 	}
 	if (!strcmp(name, "dst_max")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
                 if (len < 0) { return len; }
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(bu.buf4, &user_buffer[i], len))
 			return -EFAULT;
 
-                buf[len] = 0;
-                if (strcmp(buf, pkt_dev->dst_max) != 0) {
+                bu.buf4[len] = 0;
+                if (strcmp(bu.buf4, pkt_dev->dst_max) != 0) {
                         memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
-                        strncpy(pkt_dev->dst_max, buf, len);
+                        strncpy(pkt_dev->dst_max, bu.buf4, len);
                         pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
                         pkt_dev->cur_daddr = pkt_dev->daddr_max;
                 }
@@ -1112,108 +1114,99 @@ static int proc_if_write(struct file *fi
 		return count;
 	}
 	if (!strcmp(name, "dst6")) {
-                char buf[128];
-
 		len = strn_len(&user_buffer[i], 128 - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(bu.buf6, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
+                bu.buf6[len] = 0;
 
-		scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
-		fmt_ip6(buf,  pkt_dev->in6_daddr.s6_addr);
+		scan_ip6(bu.buf6, pkt_dev->in6_daddr.s6_addr);
+		fmt_ip6(bu.buf6,  pkt_dev->in6_daddr.s6_addr);
 
 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
 
                 if(debug) 
-			printk("pktgen: dst6 set to: %s\n", buf);
+			printk("pktgen: dst6 set to: %s\n", bu.buf6);
 
                 i += len;
-		sprintf(pg_result, "OK: dst6=%s", buf);
+		sprintf(pg_result, "OK: dst6=%s", bu.buf6);
 		return count;
 	}
 	if (!strcmp(name, "dst6_min")) {
-                char buf[128];
-
 		len = strn_len(&user_buffer[i], 128 - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(bu.buf6, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
+                bu.buf6[len] = 0;
 
-		scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
-		fmt_ip6(buf,  pkt_dev->min_in6_daddr.s6_addr);
+		scan_ip6(bu.buf6, pkt_dev->min_in6_daddr.s6_addr);
+		fmt_ip6(bu.buf6,  pkt_dev->min_in6_daddr.s6_addr);
 
 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->min_in6_daddr);
                 if(debug) 
-			printk("pktgen: dst6_min set to: %s\n", buf);
+			printk("pktgen: dst6_min set to: %s\n", bu.buf6);
 
                 i += len;
-		sprintf(pg_result, "OK: dst6_min=%s", buf);
+		sprintf(pg_result, "OK: dst6_min=%s", bu.buf6);
 		return count;
 	}
 	if (!strcmp(name, "dst6_max")) {
-                char buf[128];
-
 		len = strn_len(&user_buffer[i], 128 - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(bu.buf6, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
+                bu.buf6[len] = 0;
 
-		scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
-		fmt_ip6(buf,  pkt_dev->max_in6_daddr.s6_addr);
+		scan_ip6(bu.buf6, pkt_dev->max_in6_daddr.s6_addr);
+		fmt_ip6(bu.buf6,  pkt_dev->max_in6_daddr.s6_addr);
 
                 if(debug) 
-			printk("pktgen: dst6_max set to: %s\n", buf);
+			printk("pktgen: dst6_max set to: %s\n", bu.buf6);
 
                 i += len;
-		sprintf(pg_result, "OK: dst6_max=%s", buf);
+		sprintf(pg_result, "OK: dst6_max=%s", bu.buf6);
 		return count;
 	}
 	if (!strcmp(name, "src6")) {
-                char buf[128];
-
 		len = strn_len(&user_buffer[i], 128 - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
 
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(bu.buf6, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
+                bu.buf6[len] = 0;
 
-		scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
-		fmt_ip6(buf,  pkt_dev->in6_saddr.s6_addr);
+		scan_ip6(bu.buf6, pkt_dev->in6_saddr.s6_addr);
+		fmt_ip6(bu.buf6,  pkt_dev->in6_saddr.s6_addr);
 
 		ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
 
                 if(debug) 
-			printk("pktgen: src6 set to: %s\n", buf);
+			printk("pktgen: src6 set to: %s\n", bu.buf6);
 		
                 i += len;
-		sprintf(pg_result, "OK: src6=%s", buf);
+		sprintf(pg_result, "OK: src6=%s", bu.buf6);
 		return count;
 	}
 	if (!strcmp(name, "src_min")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
                 if (len < 0) { return len; }
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(bu.buf4, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
-                if (strcmp(buf, pkt_dev->src_min) != 0) {
+                bu.buf4[len] = 0;
+                if (strcmp(bu.buf4, pkt_dev->src_min) != 0) {
                         memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
-                        strncpy(pkt_dev->src_min, buf, len);
+                        strncpy(pkt_dev->src_min, bu.buf4, len);
                         pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
                         pkt_dev->cur_saddr = pkt_dev->saddr_min;
                 }
@@ -1224,15 +1217,14 @@ static int proc_if_write(struct file *fi
 		return count;
 	}
 	if (!strcmp(name, "src_max")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
                 if (len < 0) { return len; }
-                if (copy_from_user(buf, &user_buffer[i], len))
+                if (copy_from_user(bu.buf4, &user_buffer[i], len))
 			return -EFAULT;
-                buf[len] = 0;
-                if (strcmp(buf, pkt_dev->src_max) != 0) {
+                bu.buf4[len] = 0;
+                if (strcmp(bu.buf4, pkt_dev->src_max) != 0) {
                         memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
-                        strncpy(pkt_dev->src_max, buf, len);
+                        strncpy(pkt_dev->src_max, bu.buf4, len);
                         pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
                         pkt_dev->cur_saddr = pkt_dev->saddr_max;
                 }
#4 lore raw
Quoted 4 lines · Randy.Dunlap

I don't see any simultaneous uses of the 2 buffers, so here's the union version of the patch (attached this time), although it only saves 4 bytes... so maybe the compiler had already realized that usage. Either one accomplishes a large stack reduction, but the

Hello!

Here is version with just one buffer. How did you calculate the saving?

					   --ro
core/pktgen.c renamed from core/pktgen.c.050221 +6 -17
@@ -151,7 +151,7 @@
 #include <asm/timex.h>
 
 
-#define VERSION  "pktgen v2.57: Packet Generator for packet performance testing.\n"
+#define VERSION  "pktgen v2.58: Packet Generator for packet performance testing.\n"
 
 /* #define PG_DEBUG(a) a */
 #define PG_DEBUG(a) 
@@ -811,6 +811,7 @@
         struct pktgen_dev *pkt_dev = (struct pktgen_dev*)(data);
         char* pg_result = NULL;
         int tmp = 0;
+	char buf[128];
         
         pg_result = &(pkt_dev->result[0]);
         
@@ -1071,7 +1072,6 @@
 		return count;
 	}
 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
                 if (len < 0) { return len; }
 
@@ -1091,7 +1091,6 @@
 		return count;
 	}
 	if (!strcmp(name, "dst_max")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
                 if (len < 0) { return len; }
 
@@ -1112,9 +1111,7 @@
 		return count;
 	}
 	if (!strcmp(name, "dst6")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1136,9 +1133,7 @@
 		return count;
 	}
 	if (!strcmp(name, "dst6_min")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1159,9 +1154,7 @@
 		return count;
 	}
 	if (!strcmp(name, "dst6_max")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1181,9 +1174,7 @@
 		return count;
 	}
 	if (!strcmp(name, "src6")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1205,7 +1196,6 @@
 		return count;
 	}
 	if (!strcmp(name, "src_min")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
                 if (len < 0) { return len; }
                 if (copy_from_user(buf, &user_buffer[i], len))
@@ -1224,7 +1214,6 @@
 		return count;
 	}
 	if (!strcmp(name, "src_max")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
                 if (len < 0) { return len; }
                 if (copy_from_user(buf, &user_buffer[i], len))
#5 lore raw
Quoted 10 lines · Robert Olsson
Quoted 4 lines · Randy.Dunlap

I don't see any simultaneous uses of the 2 buffers, so here's the union version of the patch (attached this time), although it only saves 4 bytes... so maybe the compiler had already realized that usage. Either one accomplishes a large stack reduction, but the

Hello!

Here is version with just one buffer.

Yep, even better: few changes + using sizeof.

Quoted 1 line

How did you calculate the saving?

objdump the .o file before and after changes. Look at how much temp space is allocated by "sub const,%esp" (on x86). E.g.:

     9e8:	81 ec 24 01 00 00    	sub    $0x124,%esp
Quoted 99 lines
					   --ro
core/pktgen.c renamed from core/pktgen.c.050221 +6 -17
@@ -151,7 +151,7 @@
 #include <asm/timex.h>
 
 
-#define VERSION  "pktgen v2.57: Packet Generator for packet performance testing.\n"
+#define VERSION  "pktgen v2.58: Packet Generator for packet performance testing.\n"
 
 /* #define PG_DEBUG(a) a */
 #define PG_DEBUG(a) 
@@ -811,6 +811,7 @@
         struct pktgen_dev *pkt_dev = (struct pktgen_dev*)(data);
         char* pg_result = NULL;
         int tmp = 0;
+	char buf[128];
 
         pg_result = &(pkt_dev->result[0]);
 
@@ -1071,7 +1072,6 @@
 		return count;
 	}
 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
                 if (len < 0) { return len; }
 
@@ -1091,7 +1091,6 @@
 		return count;
 	}
 	if (!strcmp(name, "dst_max")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
                 if (len < 0) { return len; }
 
@@ -1112,9 +1111,7 @@
 		return count;
 	}
 	if (!strcmp(name, "dst6")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1136,9 +1133,7 @@
 		return count;
 	}
 	if (!strcmp(name, "dst6_min")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1159,9 +1154,7 @@
 		return count;
 	}
 	if (!strcmp(name, "dst6_max")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1181,9 +1174,7 @@
 		return count;
 	}
 	if (!strcmp(name, "src6")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1205,7 +1196,6 @@
 		return count;
 	}
 	if (!strcmp(name, "src_min")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
                 if (len < 0) { return len; }
                 if (copy_from_user(buf, &user_buffer[i], len))
@@ -1224,7 +1214,6 @@
 		return count;
 	}
 	if (!strcmp(name, "src_max")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
                 if (len < 0) { return len; }
                 if (copy_from_user(buf, &user_buffer[i], len))
Signature · 1 line
~Randy
#6 lore raw
Quoted 3 lines · Randy.Dunlap
Quoted 1 line

Here is version with just one buffer.

Yep, even better: few changes + using sizeof.

Hello.

OK! Then we ask DaveM to apply the patch.

				    --ro
core/pktgen.c renamed from core/pktgen.c.050221 +6 -17
@@ -151,7 +151,7 @@
 #include <asm/timex.h>
 
 
-#define VERSION  "pktgen v2.57: Packet Generator for packet performance testing.\n"
+#define VERSION  "pktgen v2.58: Packet Generator for packet performance testing.\n"
 
 /* #define PG_DEBUG(a) a */
 #define PG_DEBUG(a) 
@@ -811,6 +811,7 @@
         struct pktgen_dev *pkt_dev = (struct pktgen_dev*)(data);
         char* pg_result = NULL;
         int tmp = 0;
+	char buf[128];
         
         pg_result = &(pkt_dev->result[0]);
         
@@ -1071,7 +1072,6 @@
 		return count;
 	}
 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
                 if (len < 0) { return len; }
 
@@ -1091,7 +1091,6 @@
 		return count;
 	}
 	if (!strcmp(name, "dst_max")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
                 if (len < 0) { return len; }
 
@@ -1112,9 +1111,7 @@
 		return count;
 	}
 	if (!strcmp(name, "dst6")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1136,9 +1133,7 @@
 		return count;
 	}
 	if (!strcmp(name, "dst6_min")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1159,9 +1154,7 @@
 		return count;
 	}
 	if (!strcmp(name, "dst6_max")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1181,9 +1174,7 @@
 		return count;
 	}
 	if (!strcmp(name, "src6")) {
-                char buf[128];
-
-		len = strn_len(&user_buffer[i], 128 - 1);
+		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
                 if (len < 0) return len; 
 
 		pkt_dev->flags |= F_IPV6;
@@ -1205,7 +1196,6 @@
 		return count;
 	}
 	if (!strcmp(name, "src_min")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
                 if (len < 0) { return len; }
                 if (copy_from_user(buf, &user_buffer[i], len))
@@ -1224,7 +1214,6 @@
 		return count;
 	}
 	if (!strcmp(name, "src_max")) {
-                char buf[IP_NAME_SZ];
 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
                 if (len < 0) { return len; }
                 if (copy_from_user(buf, &user_buffer[i], len))
maintainer Applied
#7 lore raw
Quoted 8 lines · On Tue, 22 Feb 2005 16:41:31 +0100 Robert Olsson <[email protected]>…
Quoted 3 lines · Randy.Dunlap
Quoted 1 line

Here is version with just one buffer.

Yep, even better: few changes + using sizeof.

Hello.

OK! Then we ask DaveM to apply the patch.

Applied, thanks guys.

#8 lore raw
In reply to Randy.Dunlap
Quoted 7 lines · Randy.Dunlap

(resend)
Any comments this time?

pktgen: proc_if_write: reduce stack usage (on i386) from 776 to 296 bytes by combining/reusing locals.

Since these methods are not in the fast path, and the stack usage is not near 4k, does this really matter?

I'm asking more to be educated than to dismiss the changes....

Thanks,
Ben

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com
#9 lore raw
Quoted 10 lines · Ben Greear
Quoted 5 lines · Randy.Dunlap

(resend)
Any comments this time?

pktgen: proc_if_write: reduce stack usage (on i386) from 776 to 296 bytes by combining/reusing locals.

Since these methods are not in the fast path, and the stack usage is not near 4k, does this really matter?

It's not critical or near causing a stack overflow AFAICT. And ideally gcc would recognize this kind of usage and not allocate multiple stack entries for it.

syscall: sys_write (tiny stack usage)

		-> vfs_write (tiny)
		   -> proc_file_write (tiny)
		      -> proc_if_write (pktgen: large)
Quoted 1 line

I'm asking more to be educated than to dismiss the changes....

Signature · 1 line
~Randy