[PATCH] Remove VLAIS usage from gadget code - alternate patch

2013-09-25 Thread charlebm
From: Mark Charlebois charl...@gmail.com

The use of variable length arrays in structs (VLAIS) in the Linux Kernel code
precludes the use of compilers which don't implement VLAIS (for instance the
Clang compiler). This alternate patch calculates offsets into the kmalloc-ed
memory buffer using macros. The previous patch required multiple kmalloc and
kfree calls. This version uses group vs struct since it really is not a
struct and is essentially a group of VLA in a common allocated block. This
version also fixes the issues pointed out by Andrzej Pietrasiewicz.

Signed-off-by: Mark Charlebois charl...@gmail.com
Signed-off-by: Behan Webster beh...@converseincode.com
---

--- linux.orig/drivers/usb/gadget/f_fs.c
+++ linux/drivers/usb/gadget/f_fs.c
@@ -30,6 +30,21 @@
 
 #define FUNCTIONFS_MAGIC   0xa647361 /* Chosen by a honest dice roll ;) */
 
+/* Variable Length Array Macros **/
+#define vla_group(groupname) size_t groupname##__##next = 0
+#define vla_group_size(groupname) groupname##__##next
+
+#define vla_item(groupname, type, name, n) \
+   size_t groupname##_##name##__##offset = \
+   (groupname##__##next + __alignof__(type) - 1)  \
+   ~(__alignof__(type) - 1); \
+   size_t groupname##_##name##__##sz = (n) * sizeof(type); \
+   type * groupname##_##name = ({ \
+   groupname##__##next = groupname##_##name##__##offset + \
+   groupname##_##name##__##sz; NULL;})
+
+#define vla_ptr(ptr,groupname,name) groupname##_##name = \
+   (__typeof__(groupname##_##name))ptr[groupname##_##name##__##offset]
 
 /* Debugging /
 
@@ -1909,30 +1924,38 @@
 
/* Allocate everything in one chunk so there's less maintenance. */
{
-   struct {
-   struct usb_gadget_strings *stringtabs[lang_count + 1];
-   struct usb_gadget_strings stringtab[lang_count];
-   struct usb_string strings[lang_count*(needed_count+1)];
-   } *d;
unsigned i = 0;
+   vla_group(d);
+   vla_item(d, struct usb_gadget_strings *, stringtabs,
+   lang_count + 1);
+   vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
+   vla_item(d, struct usb_string, strings,
+   lang_count*(needed_count+1));
+
+   char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
 
-   d = kmalloc(sizeof *d, GFP_KERNEL);
-   if (unlikely(!d)) {
+   if (unlikely(!vlabuf)) {
kfree(_data);
return -ENOMEM;
}
 
-   stringtabs = d-stringtabs;
-   t = d-stringtab;
+   /* Initialize the VLA pointers */
+   vla_ptr(vlabuf, d, stringtabs);
+   vla_ptr(vlabuf, d, stringtab);
+   vla_ptr(vlabuf, d, strings);
+
+   stringtabs = d_stringtabs;
+   t = d_stringtab;
i = lang_count;
do {
*stringtabs++ = t++;
} while (--i);
*stringtabs = NULL;
 
-   stringtabs = d-stringtabs;
-   t = d-stringtab;
-   s = d-strings;
+   /* stringtabs = vlabuf = d_stringtabs for later kfree */
+   stringtabs = d_stringtabs;
+   t = d_stringtab;
+   s = d_strings;
strings = s;
}
 
@@ -2208,16 +2231,15 @@
int ret;
 
/* Make it a single chunk, less management later on */
-   struct {
-   struct ffs_ep eps[ffs-eps_count];
-   struct usb_descriptor_header
-   *fs_descs[full ? ffs-fs_descs_count + 1 : 0];
-   struct usb_descriptor_header
-   *hs_descs[high ? ffs-hs_descs_count + 1 : 0];
-   short inums[ffs-interfaces_count];
-   char raw_descs[high ? ffs-raw_descs_length
-   : ffs-raw_fs_descs_length];
-   } *data;
+   vla_group(d);
+   vla_item(d, struct ffs_ep, eps, ffs-eps_count);
+   vla_item(d, struct usb_descriptor_header *, fs_descs,
+   full ? ffs-fs_descs_count + 1 : 0);
+   vla_item(d, struct usb_descriptor_header *, hs_descs,
+   high ? ffs-hs_descs_count + 1 : 0);
+   vla_item(d, short, inums, ffs-interfaces_count);
+   vla_item(d, char, raw_descs,
+   high ? ffs-raw_descs_length : ffs-raw_fs_descs_length);
 
ENTER();
 
@@ -2225,21 +2247,30 @@
if (unlikely(!(full | high)))
return -ENOTSUPP;
 
-   /* Allocate */
-   data = kmalloc(sizeof *data, GFP_KERNEL);
-   if (unlikely(!data))
+   /* Allocate a single chunk, less management later on */
+   char *vlabuf = 

[PATCH] Remove VLAIS usage from gadget code - alternate patch

2013-09-24 Thread charlebm
From: Mark Charlebois charl...@gmail.com

The use of variable length arrays in structs (VLAIS) in the Linux Kernel code
precludes the use of compilers which don't implement VLAIS (for instance the
Clang compiler). This alternate patch calculates offsets into the kmalloc-ed
memory buffer using macros. The previous patch required multiple kmalloc and
kfree calls.

Signed-off-by: Mark Charlebois charl...@gmail.com
Signed-off-by: Behan Webster beh...@converseincode.com
---

--- linux.orig/drivers/usb/gadget/f_fs.c
+++ linux/drivers/usb/gadget/f_fs.c
@@ -30,6 +30,21 @@
 
 #define FUNCTIONFS_MAGIC   0xa647361 /* Chosen by a honest dice roll ;) */
 
+/* Variable Length Array Macros **/
+#define vla_struct(structname) size_t structname##__##next = 0
+#define vla_struct_size(structname) structname##__##next
+
+#define vla_item(structname, type, name, n) \
+   type * structname##_##name; \
+   size_t structname##_##name##__##offset = \
+   (structname##__##next + __alignof__(type) - 1)  \
+   ~(__alignof__(type) - 1); \
+   size_t structname##_##name##__##sz = n * sizeof(type); \
+   structname##__##next = structname##_##name##__##offset + \
+   structname##_##name##__##sz;
+
+#define vla_ptr(ptr,structname,name) structname##_##name = \
+   (__typeof__(structname##_##name))ptr[structname##_##name##__##offset]
 
 /* Debugging /
 
@@ -1909,30 +1924,38 @@
 
/* Allocate everything in one chunk so there's less maintenance. */
{
-   struct {
-   struct usb_gadget_strings *stringtabs[lang_count + 1];
-   struct usb_gadget_strings stringtab[lang_count];
-   struct usb_string strings[lang_count*(needed_count+1)];
-   } *d;
unsigned i = 0;
+   vla_struct(d);
+   vla_item(d, struct usb_gadget_strings *, stringtabs,
+   lang_count + 1);
+   vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
+   vla_item(d, struct usb_string, strings,
+   lang_count*(needed_count+1));
+
+   char *vlabuf = kmalloc(vla_struct_size(d), GFP_KERNEL);
 
-   d = kmalloc(sizeof *d, GFP_KERNEL);
-   if (unlikely(!d)) {
+   if (unlikely(!vlabuf)) {
kfree(_data);
return -ENOMEM;
}
 
-   stringtabs = d-stringtabs;
-   t = d-stringtab;
+   /* Initialize the VLA pointers */
+   vla_ptr(vlabuf, d, stringtabs);
+   vla_ptr(vlabuf, d, stringtab);
+   vla_ptr(vlabuf, d, strings);
+
+   stringtabs = d_stringtabs;
+   t = d_stringtab;
i = lang_count;
do {
*stringtabs++ = t++;
} while (--i);
*stringtabs = NULL;
 
-   stringtabs = d-stringtabs;
-   t = d-stringtab;
-   s = d-strings;
+   /* stringtabs = vlabuf = d_stringtabs for later free */
+   stringtabs = d_stringtabs;
+   t = d_stringtab;
+   s = d_strings;
strings = s;
}
 
@@ -2208,16 +2231,15 @@
int ret;
 
/* Make it a single chunk, less management later on */
-   struct {
-   struct ffs_ep eps[ffs-eps_count];
-   struct usb_descriptor_header
-   *fs_descs[full ? ffs-fs_descs_count + 1 : 0];
-   struct usb_descriptor_header
-   *hs_descs[high ? ffs-hs_descs_count + 1 : 0];
-   short inums[ffs-interfaces_count];
-   char raw_descs[high ? ffs-raw_descs_length
-   : ffs-raw_fs_descs_length];
-   } *data;
+   vla_struct(d);
+   vla_item(d, struct ffs_ep, eps, ffs-eps_count);
+   vla_item(d, struct usb_descriptor_header *, fs_descs,
+   full ? ffs-fs_descs_count + 1 : 0);
+   vla_item(d, struct usb_descriptor_header *, hs_descs,
+   high ? ffs-hs_descs_count + 1 : 0);
+   vla_item(d, short, inums, ffs-interfaces_count);
+   vla_item(d, char, raw_descs,
+   high ? ffs-raw_descs_length : ffs-raw_fs_descs_length);
 
ENTER();
 
@@ -2225,21 +2247,30 @@
if (unlikely(!(full | high)))
return -ENOTSUPP;
 
-   /* Allocate */
-   data = kmalloc(sizeof *data, GFP_KERNEL);
-   if (unlikely(!data))
+   /* Allocate a single chunk, less management later on */
+   char *vlabuf = kmalloc(vla_struct_size(d), GFP_KERNEL);
+   if (unlikely(!vlabuf))
return -ENOMEM;
 
+   /* Initialize each struct member pointer in the allocated memory */
+   vla_ptr(vlabuf, d, eps);
+ 

[PATCH] usb: LLVMLinux: Remove VLAIS from USB gadget

2013-09-23 Thread charlebm
From: Mark Charlebois charl...@gmail.com


The use of variable length arrays in structs (VLAIS) in the Linux Kernel code
precludes the use of compilers which don't implement VLAIS (for instance the
Clang compiler). This patch removes the use of VLAIS in the gadget driver.

This version has been tested to compile cleanly. 

Signed-off-by: Mark Charlebois charl...@gmail.com
Signed-off-by: Behan Webster beh...@converseincode.com
---
 drivers/usb/gadget/f_fs.c | 128 +++---
 1 file changed, 76 insertions(+), 52 deletions(-)

--- linux.orig/drivers/usb/gadget/f_fs.c
+++ linux/drivers/usb/gadget/f_fs.c
@@ -30,7 +30,6 @@
 
 #define FUNCTIONFS_MAGIC   0xa647361 /* Chosen by a honest dice roll ;) */
 
-
 /* Debugging /
 
 #ifdef VERBOSE_DEBUG
@@ -214,6 +213,8 @@
/* ids in stringtabs are set in functionfs_bind() */
const void  *raw_strings;
struct usb_gadget_strings   **stringtabs;
+   struct usb_gadget_strings   *stringtab;
+   struct usb_string   *strings;
 
/*
 * File system's super block, write once when file system is
@@ -263,7 +264,10 @@
 
struct ffs_ep   *eps;
u8  eps_revmap[16];
+   struct usb_descriptor_header**fs_descs;
+   struct usb_descriptor_header**hs_descs;
short   *interfaces_nums;
+   char*raw_descs;
 
struct usb_function function;
 };
@@ -1345,6 +1349,8 @@
kfree(ffs-raw_descs);
kfree(ffs-raw_strings);
kfree(ffs-stringtabs);
+   kfree(ffs-stringtab);
+   kfree(ffs-strings);
 }
 
 static void ffs_data_reset(struct ffs_data *ffs)
@@ -1357,6 +1363,8 @@
ffs-raw_descs = NULL;
ffs-raw_strings = NULL;
ffs-stringtabs = NULL;
+   ffs-stringtab = NULL;
+   ffs-strings = NULL;
 
ffs-raw_descs_length = 0;
ffs-raw_fs_descs_length = 0;
@@ -1528,12 +1536,10 @@
ffs_data_put(func-ffs);
 
kfree(func-eps);
-   /*
-* eps and interfaces_nums are allocated in the same chunk so
-* only one free is required.  Descriptors are also allocated
-* in the same chunk.
-*/
-
+   kfree(func-fs_descs);
+   kfree(func-hs_descs);
+   kfree(func-interfaces_nums);
+   kfree(func-raw_descs);
kfree(func);
 }
 
@@ -1877,8 +1883,9 @@
  char *const _data, size_t len)
 {
u32 str_count, needed_count, lang_count;
-   struct usb_gadget_strings **stringtabs, *t;
-   struct usb_string *strings, *s;
+   struct usb_gadget_strings *t, **stringtabs = NULL;
+   struct usb_gadget_strings *stringtab = NULL;
+   struct usb_string *s, *strings = NULL;
const char *data = _data;
 
ENTER();
@@ -1907,33 +1914,33 @@
return 0;
}
 
-   /* Allocate everything in one chunk so there's less maintenance. */
{
-   struct {
-   struct usb_gadget_strings *stringtabs[lang_count + 1];
-   struct usb_gadget_strings stringtab[lang_count];
-   struct usb_string strings[lang_count*(needed_count+1)];
-   } *d;
unsigned i = 0;
-
-   d = kmalloc(sizeof *d, GFP_KERNEL);
-   if (unlikely(!d)) {
+   struct usb_gadget_strings **b;
+
+   stringtabs = kmalloc(sizeof(*stringtabs)*(lang_count + 1),
+   GFP_KERNEL);
+   stringtab = kmalloc(sizeof(*stringtab)*(lang_count),
+   GFP_KERNEL);
+   strings = kmalloc(sizeof(*strings)
+   * (lang_count * (needed_count + 1)), GFP_KERNEL);
+   if (unlikely(!stringtabs || !stringtab || !strings)) {
+   kfree(stringtabs);
+   kfree(stringtab);
+   kfree(strings);
kfree(_data);
return -ENOMEM;
}
-
-   stringtabs = d-stringtabs;
-   t = d-stringtab;
+   b = stringtabs;
+   t = stringtab;
i = lang_count;
do {
-   *stringtabs++ = t++;
+   *b++ = t++;
} while (--i);
-   *stringtabs = NULL;
+   *b = NULL;
 
-   stringtabs = d-stringtabs;
-   t = d-stringtab;
-   s = d-strings;
-   strings = s;
+   t = stringtab;
+   s = strings;
}
 
/* For each language */
@@ -1991,12 +1998,16 @@
 
/* Done! */
ffs-stringtabs = stringtabs;
+   ffs-stringtab = stringtab;
+   ffs-strings = strings;
ffs-raw_strings = _data;