[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[MiNT] [PATCH][4/5] Fix allocation in stack space.



Commit message:

Fix allocation of memory in stack space to be cache line size aligned,
this is important just in case those buffers are involved in DMA
operations.
Contributed by David Galvez.
diff -r a02994a6acc1 -r 0700a79489cc sys/sockets/xif/asix.c
--- a/sys/sockets/xif/asix.c	Thu Nov 13 14:01:08 2014 +0100
+++ b/sys/sockets/xif/asix.c	Tue Nov 18 18:54:36 2014 +0100
@@ -249,7 +249,7 @@
 
 static int asix_mdio_read(struct ueth_data *dev, int phy_id, int loc)
 {
-	__u16 res[1];
+	ALLOC_CACHE_ALIGN_BUFFER(__u16, res, 1);
 
 	asix_set_sw_mii(dev);
 	asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, res);
@@ -264,7 +264,7 @@
 static void
 asix_mdio_write(struct ueth_data *dev, int phy_id, int loc, int val)
 {
-	__u16 res[1];
+	ALLOC_CACHE_ALIGN_BUFFER(__u16, res, 1);
 	*res = cpu2le16(val);
 
 	DEBUG(("asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
@@ -292,7 +292,7 @@
 
 static inline int asix_get_phy_addr(struct ueth_data *dev)
 {
-	u8 buf[2];
+	ALLOC_CACHE_ALIGN_BUFFER(u8, buf, 2);
 
 	int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
 
@@ -325,7 +325,7 @@
 
 static u16 asix_read_rx_ctl(struct ueth_data *dev)
 {
-	__u16 v[1];
+	ALLOC_CACHE_ALIGN_BUFFER(__u16, v, 1);
 
 	int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, v);
 
@@ -396,7 +396,7 @@
 	struct ueth_data *dev = (struct ueth_data *)eth->data;
 	struct asix_private *priv = (struct asix_private *)dev->dev_priv;
 	int i;
-	unsigned char buf[ETH_ALEN];
+	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
 
 	if (priv->flags & FLAG_EEPROM_MAC) {
 		for (i = 0; i < (ETH_ALEN >> 1); i++) {
@@ -569,7 +569,7 @@
 	u32 packet_len;
 	long actual_len = 0;
 	long size;
-	unsigned char msg[length + sizeof(packet_len) + 3 /* pad */];
+	unsigned char *msg;
 
 	DEBUG(("** %s(), len %d\n", __func__, length));
 	if (dev->pusb_dev == 0) {
@@ -579,6 +579,12 @@
 	packet_len = ((length ^ 0x0000ffff) << 16) + length;
 	packet_len = cpu2le32(packet_len);
 
+	msg = (unsigned char *)kmalloc(length + sizeof(packet_len) + 3 /* pad */);
+	if (!msg) {
+		DEBUG(("Out of memory"));
+		return ENOMEM;
+	}
+
 	memcpy(msg, &packet_len, sizeof(packet_len));
 	memcpy(msg + sizeof(packet_len), (void *)packet, length);
 	if (length & 1)
@@ -592,7 +598,8 @@
 				size,
 				&actual_len,
 				USB_BULK_SEND_TIMEOUT, 0);
-	if (err != 0)
+	kfree(msg);
+	if (err != 0) 
 		return ENOMEM;
 	else 
 		return E_OK;
@@ -600,7 +607,7 @@
 
 static int asix_recv(struct eth_device *eth)
 {
-	unsigned char recv_buf[AX_RX_URB_SIZE];
+	unsigned char *recv_buf;
 	struct ueth_data *dev = (struct ueth_data *)eth->data;
 	unsigned char *buf_ptr;
 	long err;
@@ -608,6 +615,12 @@
 	u32 packet_len;
 	BUF *buf;
 
+	recv_buf = (unsigned char *)kmalloc(AX_RX_URB_SIZE);
+	if (!recv_buf) {
+		DEBUG(("Out of memory"));
+		return ENOMEM;
+	}
+
 	if (dev->pusb_dev == 0) {
 		err = -1;
 		goto out;
@@ -694,7 +707,7 @@
 #else
 	addroottimeout(10, asix_poll, 0);
 #endif
-
+	kfree(recv_buf);
 	return err;
 }
 
diff -r a02994a6acc1 -r 0700a79489cc sys/usb/src.km/hub.c
--- a/sys/usb/src.km/hub.c	Thu Nov 13 14:01:08 2014 +0100
+++ b/sys/usb/src.km/hub.c	Tue Nov 18 18:54:36 2014 +0100
@@ -350,7 +350,7 @@
 struct usb_hub_device *
 usb_hub_configure(struct usb_device *dev)
 {
-	unsigned char buffer[USB_BUFSIZ];
+	unsigned char *buffer;
 	struct usb_hub_descriptor *descriptor;
 	struct usb_hub_status *hubsts;
 	long i;
@@ -362,10 +362,16 @@
 		return NULL;
 
 	/* Get the the hub descriptor */
+	buffer = (unsigned char *)kmalloc(USB_BUFSIZ);
+	if (!buffer) {
+		DEBUG(("Out of memory"));
+		return NULL;
+	}
 	if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
 		DEBUG(("usb_hub_configure: failed to get hub " \
 				   "descriptor, giving up %lx", dev->status));
-		return NULL;
+		hub = NULL;
+		goto errout;
 	}
 	descriptor = (struct usb_hub_descriptor *)buffer;
 
@@ -375,13 +381,15 @@
 		DEBUG(("usb_hub_configure: failed to get hub " \
 				"descriptor - too long: %d",
 				descriptor->bLength));
-		return NULL;
+		hub = NULL;
+		goto errout;
 	}
 
 	if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
 		DEBUG(("usb_hub_configure: failed to get hub " \
 				"descriptor 2nd giving up %lx", dev->status));
-		return NULL;
+		hub = NULL;
+		goto errout;
 	}
 	memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
 	/* adjust 16bit values */
@@ -447,13 +455,15 @@
 	if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
 		DEBUG(("usb_hub_configure: failed to get Status - " \
 				"too long: %d", descriptor->bLength));
-		return NULL;
+		hub = NULL;
+		goto errout;
 	}
 
 	if (usb_get_hub_status(dev, buffer) < 0) {
 		DEBUG(("usb_hub_configure: failed to get Status %lx",
 				dev->status));
-		return NULL;
+		hub = NULL;
+		goto errout;
 	}
 
 	hubsts = (struct usb_hub_status *)buffer;
@@ -471,8 +481,10 @@
 	usb_hub_power_on(dev, hub->desc.bPwrOn2PwrGood * 2);
 
 	hub->pusb_dev = dev;
-	
-	return hub; 
+
+errout:
+	kfree(buffer);
+	return hub;
 }
 
 long 
diff -r a02994a6acc1 -r 0700a79489cc sys/usb/src.km/udd/storage/usb_storage.c
--- a/sys/usb/src.km/udd/storage/usb_storage.c	Thu Nov 13 14:01:08 2014 +0100
+++ b/sys/usb/src.km/udd/storage/usb_storage.c	Tue Nov 18 18:54:36 2014 +0100
@@ -300,7 +300,7 @@
 static unsigned long usb_get_max_lun(struct us_data *us)
 {
 	int len;
-	unsigned char result[1];
+	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
 	len = usb_control_msg(us->pusb_dev,
 			      usb_rcvctrlpipe(us->pusb_dev, 0),
 			      US_BBB_GET_MAX_LUN,
@@ -909,7 +909,7 @@
 	long actlen;
 	long dir_in;
 	unsigned long pipe;
-	umass_bbb_cbw_t cbw;
+	umass_bbb_cbw_t *cbw;
 	dir_in = US_DIRECTION(srb->cmd[0]);
 	DEBUG(("usb_stor_BBB_comdat: dir_in: %ld",dir_in));
 #ifdef BBB_COMDAT_TRACE
@@ -930,6 +930,13 @@
 		DEBUG((buf));
 	}
 #endif
+
+	cbw = (umass_bbb_cbw_t *)kmalloc(sizeof(umass_bbb_cbw_t));
+	if (!cbw)
+	{
+		DEBUG(("Out of memory"));
+		return ENOMEM;
+	}
 	/* sanity checks */
 	if(!(srb->cmdlen <= CBWCDBLENGTH))
 	{
@@ -938,21 +945,22 @@
 	}
 	/* always OUT to the ep */
 	pipe = usb_sndbulkpipe(us->pusb_dev, (long)us->ep_out);
-	cbw.dCBWSignature = cpu2le32(CBWSIGNATURE);
-	cbw.dCBWTag = cpu2le32(CBWTag++);
-	cbw.dCBWDataTransferLength = cpu2le32(srb->datalen);
-	cbw.bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
-	cbw.bCBWLUN = srb->lun;
-	cbw.bCDBLength = srb->cmdlen;
+	cbw->dCBWSignature = cpu2le32(CBWSIGNATURE);
+	cbw->dCBWTag = cpu2le32(CBWTag++);
+	cbw->dCBWDataTransferLength = cpu2le32(srb->datalen);
+	cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
+	cbw->bCBWLUN = srb->lun;
+	cbw->bCDBLength = srb->cmdlen;
 	/* copy the command data into the CBW command data buffer */
 	/* DST SRC LEN!!! */
-	memcpy(&cbw.CBWCDB, srb->cmd, srb->cmdlen);
+	memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
 
-	result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE, &actlen, USB_CNTL_TIMEOUT * 5, 0);
+	result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE, &actlen, USB_CNTL_TIMEOUT * 5, 0);
 	if(result < 0)
 	{
 		DEBUG(("usb_stor_BBB_comdat:usb_bulk_msg error"));
 	}
+	kfree(cbw);
 	return result;
 }
 
@@ -1072,7 +1080,7 @@
 	unsigned char *ptr;
 	long idx;
 #endif
-	umass_bbb_csw_t csw;
+	umass_bbb_csw_t *csw;
 	dir_in = US_DIRECTION(srb->cmd[0]);
 	/* COMMAND phase */
 	DEBUG(("COMMAND phase"));
@@ -1131,6 +1139,12 @@
 	retry = 0;
 again:
 	DEBUG(("STATUS phase"));
+	csw = (umass_bbb_csw_t *)kmalloc(sizeof(umass_bbb_csw_t));
+	if (!csw)
+	{
+		DEBUG(("Out of memory"));
+		return USB_STOR_TRANSPORT_FAILED;
+	}
 	result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE, &actlen, USB_CNTL_TIMEOUT*5, 0);
 	/* special handling of STALL in STATUS phase */
 
@@ -1148,13 +1162,14 @@
 	{
 		DEBUG(("usb_bulk_msg error status %ld", us->pusb_dev->status));
 		usb_stor_BBB_reset(us);
-		return USB_STOR_TRANSPORT_FAILED;
+		result = USB_STOR_TRANSPORT_FAILED;
+		goto out;
 	}
 #ifdef BBB_XPORT_TRACE
 	unsigned char buf2[UMASS_BBB_CSW_SIZE * 16];
 
 	sprintf(buf2, sizeof(buf2),"\0");
-	ptr = (unsigned char *)&csw;
+	ptr = (unsigned char *)csw;
 	for(idx = 0; idx < UMASS_BBB_CSW_SIZE; idx++)
 	{
 		sprintf(build_str, sizeof(build_str), "ptr[%ld] 0x%x ", idx, ptr[idx]);
@@ -1163,43 +1178,51 @@
 	DEBUG((buf2));
 #endif
 	/* misuse pipe to get the residue */
-	pipe = le2cpu32(csw.dCSWDataResidue);
+	pipe = le2cpu32(csw->dCSWDataResidue);
 	if(pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
 		pipe = srb->datalen - data_actlen;
-	if(CSWSIGNATURE != le2cpu32(csw.dCSWSignature))
+	if(CSWSIGNATURE != le2cpu32(csw->dCSWSignature))
 	{
 		DEBUG(("!CSWSIGNATURE"));
 		usb_stor_BBB_reset(us);
-		return USB_STOR_TRANSPORT_FAILED;
+		result = USB_STOR_TRANSPORT_FAILED;
+		goto out;
 	}
-	else if((CBWTag - 1) != le2cpu32(csw.dCSWTag))
+	else if((CBWTag - 1) != le2cpu32(csw->dCSWTag))
 	{
 		DEBUG(("!Tag"));
 		usb_stor_BBB_reset(us);
-		return USB_STOR_TRANSPORT_FAILED;
+		result = USB_STOR_TRANSPORT_FAILED;
+		goto out;
 	}
-	else if(csw.bCSWStatus > CSWSTATUS_PHASE)
+	else if(csw->bCSWStatus > CSWSTATUS_PHASE)
 	{
 		DEBUG((">PHASE"));
 		usb_stor_BBB_reset(us);
-		return USB_STOR_TRANSPORT_FAILED;
+		result = USB_STOR_TRANSPORT_FAILED;
+		goto out;
 	}
-	else if(csw.bCSWStatus == CSWSTATUS_PHASE)
+	else if(csw->bCSWStatus == CSWSTATUS_PHASE)
 	{
 		DEBUG(("=PHASE"));
 		usb_stor_BBB_reset(us);
-		return USB_STOR_TRANSPORT_FAILED;
+		result = USB_STOR_TRANSPORT_FAILED;
+		goto out;
 	}
 	else if(data_actlen > srb->datalen)
 	{
 		DEBUG(("transferred %dB instead of %ldB", data_actlen, srb->datalen));
-		return USB_STOR_TRANSPORT_FAILED;
+		result = USB_STOR_TRANSPORT_FAILED;
+		goto out;
 	}
-	else if(csw.bCSWStatus == CSWSTATUS_FAILED)
+	else if(csw->bCSWStatus == CSWSTATUS_FAILED)
 	{
 		DEBUG(("FAILED"));
-		return USB_STOR_TRANSPORT_FAILED;
+		result = USB_STOR_TRANSPORT_FAILED;
+		goto out;
 	}
+out:
+	kfree(csw);
 	return result;
 }
 
@@ -1722,8 +1745,8 @@
 usb_stor_get_info(struct usb_device *dev, struct us_data *ss, block_dev_desc_t *dev_desc)
 {
 	unsigned char perq, modi;
-	unsigned long cap[2];
-	unsigned char usb_stor_buf[36];
+	ALLOC_CACHE_ALIGN_BUFFER(unsigned long, cap, 2);
+	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, usb_stor_buf, 36);
 	unsigned long *capacity, *blksz;
 	ccb pccb;
 	DEBUG(("usb_stor_get_info()"));
diff -r a02994a6acc1 -r 0700a79489cc sys/usb/src.km/usb.c
--- a/sys/usb/src.km/usb.c	Thu Nov 13 14:01:08 2014 +0100
+++ b/sys/usb/src.km/usb.c	Tue Nov 18 18:54:36 2014 +0100
@@ -149,7 +149,7 @@
 			void *data, unsigned short size, long timeout)
 {
 	struct control_msg arg;
-	struct devrequest setup_packet;
+	struct devrequest *setup_packet;
 	long r;
 	struct ucdif *ucd = dev->controller;
 	(void) r;
@@ -159,12 +159,18 @@
 		return -1;
 	}
 
+	setup_packet = (struct devrequest *)kmalloc(sizeof(struct devrequest));
+	if (!setup_packet) {
+		DEBUG(("Out of memory"));
+		return ENOMEM;
+	}
+
 	/* set setup command */
-	setup_packet.requesttype = requesttype;
-	setup_packet.request = request;
-	setup_packet.value = cpu2le16(value);
-	setup_packet.index = cpu2le16(index);
-	setup_packet.length = cpu2le16(size);
+	setup_packet->requesttype = requesttype;
+	setup_packet->request = request;
+	setup_packet->value = cpu2le16(value);
+	setup_packet->index = cpu2le16(index);
+	setup_packet->length = cpu2le16(size);
 	DEBUG(("usb_control_msg: request: 0x%x, requesttype: 0x%x, value 0x%x idx 0x%x length 0x%x",
 		   request, requesttype, value, index, size));
 	dev->status = USB_ST_NOT_PROC; /* not yet processed */
@@ -173,10 +179,12 @@
 	arg.pipe = pipe;
 	arg.data = data;
 	arg.size = size;
-	arg.setup = &setup_packet;
+	arg.setup = setup_packet;
 
 	r = (*ucd->ioctl)(ucd, SUBMIT_CONTROL_MSG, (long)&arg);
 
+	kfree(setup_packet);
+
 	if (timeout == 0)
 	{
 		DEBUG(("size %d \r", size));
@@ -197,9 +205,8 @@
 #endif
 
 	if (dev->status)
-	{
 		return -1;
-	}
+
 
 	return dev->act_len;
 }
@@ -711,15 +718,19 @@
  */
 long usb_string(struct usb_device *dev, long index, char *buf, long size)
 {
-	unsigned char mybuf[USB_BUFSIZ];
 	unsigned char *tbuf;
 	long err;
 	unsigned long u, idx;
 
 	if (size <= 0 || !buf || !index)
 		return -1;
+
+	tbuf = (unsigned char *)kmalloc(USB_BUFSIZ);
+	if (!tbuf) {
+		DEBUG(("Out of memory"));
+		return ENOMEM;
+	}
 	buf[0] = 0;
-	tbuf = &mybuf[0];
 
 	/* get langid for strings if it's not yet known */
 	if (!dev->have_langid) {
@@ -727,10 +738,11 @@
 		if (err < 0) {
 			DEBUG(("error getting string descriptor 0 " \
 				   "(error=%lx)", dev->status));
-			return -1;
+			goto errout;
 		} else if (tbuf[0] < 4) {
 			DEBUG(("string descriptor 0 too short"));
-			return -1;
+			err = -1;
+			goto errout;
 		} else {
 			dev->have_langid = -1;
 			dev->string_langid = tbuf[2] | (tbuf[3] << 8);
@@ -743,7 +755,7 @@
 
 	err = usb_string_sub(dev, dev->string_langid, index, tbuf);
 	if (err < 0)
-		return err;
+		goto errout;
 
 	size--;		/* leave room for trailing NULL char in output buffer */
 	for (idx = 0, u = 2; u < err; u += 2) {
@@ -756,6 +768,8 @@
 	}
 	buf[idx] = 0;
 	err = idx;
+errout:
+	kfree(tbuf);
 	return err;
 }