[PATCH 2/2] Remove VLAIS usage from gadget code

From: Behan Webster
Date: Tue Oct 30 2012 - 14:25:13 EST


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 instead calculates offsets into the kmalloc-ed
memory buffer using macros from valign.h.

Signed-off-by: Behan Webster <behanw@xxxxxxxxxxxxxxxxxx>
---
drivers/usb/gadget/f_fs.c | 106 ++++++++++++++++++++++++++-------------------
1 file changed, 61 insertions(+), 45 deletions(-)

diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c
index 64c4ec1..ca6cacc 100644
--- a/drivers/usb/gadget/f_fs.c
+++ b/drivers/usb/gadget/f_fs.c
@@ -22,6 +22,7 @@
#include <linux/pagemap.h>
#include <linux/export.h>
#include <linux/hid.h>
+#include <linux/valign.h>
#include <asm/unaligned.h>

#include <linux/usb/composite.h>
@@ -1875,6 +1876,15 @@ error:
return ret;
}

+static void __ffs_init_stringtabs(struct usb_gadget_strings **stringtabs,
+ struct usb_gadget_strings *t, unsigned len)
+{
+ do {
+ *stringtabs++ = t++;
+ } while (--len);
+ *stringtabs = NULL;
+}
+
static int __ffs_data_got_strings(struct ffs_data *ffs,
char *const _data, size_t len)
{
@@ -1911,31 +1921,26 @@ static int __ffs_data_got_strings(struct ffs_data *ffs,

/* 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);
+ int strtabslen = paddedsize(0, lang_count+1,
+ struct usb_gadget_strings *, struct usb_gadget_strings);
+ int strtablen = paddedsize(strtabslen, lang_count,
+ struct usb_gadget_strings, struct usb_string);
+ int strlen = paddedsize(strtabslen + strtablen,
+ lang_count*(needed_count+1), struct usb_string, int);
+
+ stringtabs = kmalloc(strtabslen + strtablen + strlen,
+ GFP_KERNEL);
if (unlikely(!d)) {
kfree(_data);
return -ENOMEM;
}

- 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;
+ t = paddedstart(stringtabs, strtabslen,
+ struct usb_gadget_strings);
+ s = paddedstart(t, strtablen, struct usb_string);
strings = s;
+
+ __ffs_init_stringtabs(stringtabs, t, lang_count);
}

/* For each language */
@@ -2209,17 +2214,23 @@ static int ffs_func_bind(struct usb_configuration *c,

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;
+ struct ffs_ep *eps;
+ struct usb_descriptor_header **fs_descs;
+ struct usb_descriptor_header **hs_descs;
+ short *inums;
+ char *raw_descs;
+
+ int epslen = paddedsize(0, ffs->eps_count,
+ struct ffs_ep, struct usb_descriptor_header);
+ int fsdlen = paddedsize(epslen, full ? ffs->fs_descs_count + 1 : 0,
+ struct usb_descriptor_header,
+ struct usb_descriptor_header);
+ int hsdlen = paddedsize(fsdlen, high ? ffs->hs_descs_count + 1 : 0,
+ struct usb_descriptor_header, short);
+ int inumlen = paddedsize(hsdlen, ffs->interfaces_count, short, char);
+ int rawlen = paddedsize(inumlen,
+ high ? ffs->raw_descs_length : ffs->raw_fs_descs_length,
+ char, int);

ENTER();

@@ -2227,21 +2238,28 @@ static int ffs_func_bind(struct usb_configuration *c,
if (unlikely(!(full | high)))
return -ENOTSUPP;

- /* Allocate */
- data = kmalloc(sizeof *data, GFP_KERNEL);
+ /* Allocate a single chunk, less management later on */
+ eps = kmalloc(epslen+fsdlen+hsdlen+inumlen+rawlen, GFP_KERNEL);
if (unlikely(!data))
return -ENOMEM;

+ /* Calculate start of each variable in the allocated memory */
+ fs_descs = paddedstart(eps, epslen, struct usb_descriptor_header *);
+ hs_descs = paddedstart(fs_descs, fsdlen,
+ struct usb_descriptor_header *);
+ inums = paddedstart(hs_descs, hsdlen, short);
+ raw_descs = paddedstart(inums, inumlen, char);
+
/* Zero */
- memset(data->eps, 0, sizeof data->eps);
- memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
- memset(data->inums, 0xff, sizeof data->inums);
+ memset(eps, 0, epslen);
+ memcpy(raw_descs, ffs->raw_descs + 16, rawlen);
+ memset(inums, 0xff, inumlen);
for (ret = ffs->eps_count; ret; --ret)
- data->eps[ret].num = -1;
+ eps[ret].num = -1;

/* Save pointers */
- func->eps = data->eps;
- func->interfaces_nums = data->inums;
+ func->eps = eps;
+ func->interfaces_nums = inums;

/*
* Go through all the endpoint descriptors and allocate
@@ -2249,10 +2267,9 @@ static int ffs_func_bind(struct usb_configuration *c,
* numbers without worrying that it may be described later on.
*/
if (likely(full)) {
- func->function.descriptors = data->fs_descs;
+ func->function.descriptors = fs_descs;
ret = ffs_do_descs(ffs->fs_descs_count,
- data->raw_descs,
- sizeof data->raw_descs,
+ raw_descs, rawlen,
__ffs_func_bind_do_descs, func);
if (unlikely(ret < 0))
goto error;
@@ -2261,10 +2278,9 @@ static int ffs_func_bind(struct usb_configuration *c,
}

if (likely(high)) {
- func->function.hs_descriptors = data->hs_descs;
+ func->function.hs_descriptors = hs_descs;
ret = ffs_do_descs(ffs->hs_descs_count,
- data->raw_descs + ret,
- (sizeof data->raw_descs) - ret,
+ raw_descs + ret, rawlen - ret,
__ffs_func_bind_do_descs, func);
}

@@ -2275,7 +2291,7 @@ static int ffs_func_bind(struct usb_configuration *c,
*/
ret = ffs_do_descs(ffs->fs_descs_count +
(high ? ffs->hs_descs_count : 0),
- data->raw_descs, sizeof data->raw_descs,
+ raw_descs, rawlen,
__ffs_func_bind_do_nums, func);
if (unlikely(ret < 0))
goto error;
--
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/