svn commit: r292834 - in head/sys/compat/linuxkpi/common: include/linux src

Hans Petter Selasky hselasky at FreeBSD.org
Mon Dec 28 18:20:07 UTC 2015


Author: hselasky
Date: Mon Dec 28 18:20:05 2015
New Revision: 292834
URL: https://svnweb.freebsd.org/changeset/base/292834

Log:
  Reduce memory consumption when allocating kobject strings in the
  LinuxKPI. Compute string length before allocating memory instead of
  using fixed size allocations. Make kobject_set_name_vargs() global
  instead of inline to save some bytes when compiling.
  
  MFC after:	1 week
  Sponsored by:	Mellanox Technologies

Modified:
  head/sys/compat/linuxkpi/common/include/linux/kobject.h
  head/sys/compat/linuxkpi/common/src/linux_compat.c

Modified: head/sys/compat/linuxkpi/common/include/linux/kobject.h
==============================================================================
--- head/sys/compat/linuxkpi/common/include/linux/kobject.h	Mon Dec 28 17:50:31 2015	(r292833)
+++ head/sys/compat/linuxkpi/common/include/linux/kobject.h	Mon Dec 28 18:20:05 2015	(r292834)
@@ -103,29 +103,7 @@ kobject_get(struct kobject *kobj)
 	return kobj;
 }
 
-static inline int
-kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
-{
-	char *old;
-	char *name;
-
-	old = kobj->name;
-
-	if (old && !fmt)
-		return 0;
-
-	name = kzalloc(MAXPATHLEN, GFP_KERNEL);
-	if (!name)
-		return -ENOMEM;
-	vsnprintf(name, MAXPATHLEN, fmt, args);
-	kobj->name = name;
-	kfree(old);
-	for (; *name != '\0'; name++)
-		if (*name == '/')
-			*name = '!';
-	return (0);
-}
-
+int	kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list);
 int	kobject_add(struct kobject *kobj, struct kobject *parent,
 	    const char *fmt, ...);
 

Modified: head/sys/compat/linuxkpi/common/src/linux_compat.c
==============================================================================
--- head/sys/compat/linuxkpi/common/src/linux_compat.c	Mon Dec 28 17:50:31 2015	(r292833)
+++ head/sys/compat/linuxkpi/common/src/linux_compat.c	Mon Dec 28 18:20:05 2015	(r292834)
@@ -94,7 +94,50 @@ panic_cmp(struct rb_node *one, struct rb
 }
 
 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
- 
+
+int
+kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
+{
+	va_list tmp_va;
+	int len;
+	char *old;
+	char *name;
+	char dummy;
+
+	old = kobj->name;
+
+	if (old && fmt == NULL)
+		return (0);
+
+	/* compute length of string */
+	va_copy(tmp_va, args);
+	len = vsnprintf(&dummy, 0, fmt, tmp_va);
+	va_end(tmp_va);
+
+	/* account for zero termination */
+	len++;
+
+	/* check for error */
+	if (len < 1)
+		return (-EINVAL);
+
+	/* allocate memory for string */
+	name = kzalloc(len, GFP_KERNEL);
+	if (name == NULL)
+		return (-ENOMEM);
+	vsnprintf(name, len, fmt, args);
+	kobj->name = name;
+
+	/* free old string */
+	kfree(old);
+
+	/* filter new string */
+	for (; *name != '\0'; name++)
+		if (*name == '/')
+			*name = '!';
+	return (0);
+}
+
 int
 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
 {


More information about the svn-src-all mailing list