linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Remove VLAIS usage from gadget code - alternate patch
@ 2013-09-24 17:56 charlebm
  2013-09-24 21:12 ` Behan Webster
  2013-09-25 14:35 ` Andrzej Pietrasiewicz
  0 siblings, 2 replies; 4+ messages in thread
From: charlebm @ 2013-09-24 17:56 UTC (permalink / raw)
  To: balbi
  Cc: Mark Charlebois, Behan Webster, Linus Torvalds,
	Greg Kroah-Hartman, USB list, Linux Kernel Mailing List,
	Andrzej Pietrasiewicz

From: Mark Charlebois <charlebm@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 <charlebm@gmail.com>
Signed-off-by: Behan Webster <behanw@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);
+	vla_ptr(vlabuf, d, fs_descs);
+	vla_ptr(vlabuf, d, hs_descs);
+	vla_ptr(vlabuf, d, inums);
+	vla_ptr(vlabuf, d, raw_descs);
+
 	/* 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(d_eps, 0, d_eps__sz);
+	memcpy(d_raw_descs, ffs->raw_descs + 16, d_raw_descs__sz);
+	memset(d_inums, 0xff, d_inums__sz);
 	for (ret = ffs->eps_count; ret; --ret)
-		data->eps[ret].num = -1;
+		d_eps[ret].num = -1;
 
-	/* Save pointers */
-	func->eps             = data->eps;
-	func->interfaces_nums = data->inums;
+	/* Save pointers
+	 * d_eps == vlabuf, func->eps used to free vlabuf later
+         */
+	func->eps             = d_eps;
+	func->interfaces_nums = d_inums;
 
 	/*
 	 * Go through all the endpoint descriptors and allocate
@@ -2247,10 +2278,10 @@
 	 * numbers without worrying that it may be described later on.
 	 */
 	if (likely(full)) {
-		func->function.fs_descriptors = data->fs_descs;
+		func->function.fs_descriptors = d_fs_descs;
 		ret = ffs_do_descs(ffs->fs_descs_count,
-				   data->raw_descs,
-				   sizeof data->raw_descs,
+				   d_raw_descs,
+				   d_raw_descs__sz,
 				   __ffs_func_bind_do_descs, func);
 		if (unlikely(ret < 0))
 			goto error;
@@ -2259,10 +2290,10 @@
 	}
 
 	if (likely(high)) {
-		func->function.hs_descriptors = data->hs_descs;
+		func->function.hs_descriptors = d_hs_descs;
 		ret = ffs_do_descs(ffs->hs_descs_count,
-				   data->raw_descs + ret,
-				   (sizeof data->raw_descs) - ret,
+				   d_raw_descs + ret,
+				   d_raw_descs__sz - ret,
 				   __ffs_func_bind_do_descs, func);
 	}
 
@@ -2273,7 +2304,7 @@
 	 */
 	ret = ffs_do_descs(ffs->fs_descs_count +
 			   (high ? ffs->hs_descs_count : 0),
-			   data->raw_descs, sizeof data->raw_descs,
+			   d_raw_descs, d_raw_descs__sz,
 			   __ffs_func_bind_do_nums, func);
 	if (unlikely(ret < 0))
 		goto error;

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Remove VLAIS usage from gadget code - alternate patch
  2013-09-24 17:56 [PATCH] Remove VLAIS usage from gadget code - alternate patch charlebm
@ 2013-09-24 21:12 ` Behan Webster
  2013-09-25 14:35 ` Andrzej Pietrasiewicz
  1 sibling, 0 replies; 4+ messages in thread
From: Behan Webster @ 2013-09-24 21:12 UTC (permalink / raw)
  To: balbi
  Cc: charlebm, Linus Torvalds, Greg Kroah-Hartman, USB list,
	Linux Kernel Mailing List, Andrzej Pietrasiewicz

On 09/24/13 13:56, charlebm@gmail.com wrote:
> From: Mark Charlebois <charlebm@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 <charlebm@gmail.com>
> Signed-off-by: Behan Webster <behanw@converseincode.com>
This one uses macros and carves up a single piece of memory from one 
kmalloc.

Behan

-- 
Behan Webster
behanw@converseincode.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Remove VLAIS usage from gadget code - alternate patch
  2013-09-24 17:56 [PATCH] Remove VLAIS usage from gadget code - alternate patch charlebm
  2013-09-24 21:12 ` Behan Webster
@ 2013-09-25 14:35 ` Andrzej Pietrasiewicz
  1 sibling, 0 replies; 4+ messages in thread
From: Andrzej Pietrasiewicz @ 2013-09-25 14:35 UTC (permalink / raw)
  To: charlebm
  Cc: balbi, Behan Webster, Linus Torvalds, Greg Kroah-Hartman,
	USB list, Linux Kernel Mailing List

Hi Mark,

Nice to hear from you again; on Saturday LOT's dreamliner was not
grounded and I have safely returned home ;)

Please see my comments inline.

W dniu 24.09.2013 19:56, charlebm@gmail.com pisze:
> From: Mark Charlebois <charlebm@gmail.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); \

most likely this shoud read:

+	size_t structname##_##name##__##sz = (n) * sizeof(type); \

otherwise vla_item(....., lang_count + 1); will expand to:

size_t d_stringtabs__sz = lang_count + \
	1 * sizeof(struct usb_gadget_strings *);

> +	structname##__##next = structname##_##name##__##offset + \
> +		structname##_##name##__##sz;
> +
> +#define vla_ptr(ptr,structname,name) structname##_##name = \
> +	(__typeof__(structname##_##name))&ptr[structname##_##name##__##offset]

<snip>

>   		unsigned i = 0;
> +		vla_struct(d);
> +		vla_item(d, struct usb_gadget_strings *, stringtabs,
> +			lang_count + 1);

Can you somehow avoid mixing code and declarations? The last thing in
the  expansion of this vla_item(.......) is an assignment, and

> +		vla_item(d, struct usb_gadget_strings, stringtab, lang_count);

the first thing in expansion of the next vla_item(.......) is a
declaration. GCC most likely will complain (issue a warning).

One solution I can think of here (a bit hackish) is to use a braced
group as an expression: define vla_item() in such a way that first
it declares e.g. d_stringtabs__offset, then d_stringtabs__sz, and
then

struct usb_gadget_strings **d_stringtabs =
	({d__next = d_stringtabs__offset + d_stringtabs__sz; NULL;});

I am not a fan of this kind of style, but can't think of any better way
now. And I don't know what Clang thinks of it :O

Thanks,

AP

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] Remove VLAIS usage from gadget code - alternate patch
@ 2013-09-25 21:23 charlebm
  0 siblings, 0 replies; 4+ messages in thread
From: charlebm @ 2013-09-25 21:23 UTC (permalink / raw)
  To: balbi
  Cc: Mark Charlebois, Behan Webster, Linus Torvalds,
	Greg Kroah-Hartman, USB list, Linux Kernel Mailing List,
	Andrzej Pietrasiewicz

From: Mark Charlebois <charlebm@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 <charlebm@gmail.com>
Signed-off-by: Behan Webster <behanw@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 = kmalloc(vla_group_size(d), GFP_KERNEL);
+	if (unlikely(!vlabuf))
 		return -ENOMEM;
 
+	/* Initialize each struct member pointer in the allocated memory */
+	vla_ptr(vlabuf, d, eps);
+	vla_ptr(vlabuf, d, fs_descs);
+	vla_ptr(vlabuf, d, hs_descs);
+	vla_ptr(vlabuf, d, inums);
+	vla_ptr(vlabuf, d, raw_descs);
+
 	/* 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(d_eps, 0, d_eps__sz);
+	memcpy(d_raw_descs, ffs->raw_descs + 16, d_raw_descs__sz);
+	memset(d_inums, 0xff, d_inums__sz);
 	for (ret = ffs->eps_count; ret; --ret)
-		data->eps[ret].num = -1;
+		d_eps[ret].num = -1;
 
-	/* Save pointers */
-	func->eps             = data->eps;
-	func->interfaces_nums = data->inums;
+	/* Save pointers
+	 * d_eps == vlabuf, func->eps used to kfree vlabuf later
+	*/
+	func->eps             = d_eps;
+	func->interfaces_nums = d_inums;
 
 	/*
 	 * Go through all the endpoint descriptors and allocate
@@ -2247,10 +2278,10 @@
 	 * numbers without worrying that it may be described later on.
 	 */
 	if (likely(full)) {
-		func->function.fs_descriptors = data->fs_descs;
+		func->function.fs_descriptors = d_fs_descs;
 		ret = ffs_do_descs(ffs->fs_descs_count,
-				   data->raw_descs,
-				   sizeof data->raw_descs,
+				   d_raw_descs,
+				   d_raw_descs__sz,
 				   __ffs_func_bind_do_descs, func);
 		if (unlikely(ret < 0))
 			goto error;
@@ -2259,10 +2290,10 @@
 	}
 
 	if (likely(high)) {
-		func->function.hs_descriptors = data->hs_descs;
+		func->function.hs_descriptors = d_hs_descs;
 		ret = ffs_do_descs(ffs->hs_descs_count,
-				   data->raw_descs + ret,
-				   (sizeof data->raw_descs) - ret,
+				   d_raw_descs + ret,
+				   d_raw_descs__sz - ret,
 				   __ffs_func_bind_do_descs, func);
 	}
 
@@ -2273,7 +2304,7 @@
 	 */
 	ret = ffs_do_descs(ffs->fs_descs_count +
 			   (high ? ffs->hs_descs_count : 0),
-			   data->raw_descs, sizeof data->raw_descs,
+			   d_raw_descs, d_raw_descs__sz,
 			   __ffs_func_bind_do_nums, func);
 	if (unlikely(ret < 0))
 		goto error;

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-09-25 21:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-24 17:56 [PATCH] Remove VLAIS usage from gadget code - alternate patch charlebm
2013-09-24 21:12 ` Behan Webster
2013-09-25 14:35 ` Andrzej Pietrasiewicz
2013-09-25 21:23 charlebm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).