usb(4): Stop checking for failures from malloc(M_WAITOK).

Handle the fact that parts of usb(4) can be compiled into the boot
loader, where M_WAITOK does not guarantee a successful allocation.

PR:		240545
Submitted by:	Andrew Reiter <arr@watson.org> (original version)
Reviewed by:	hselasky
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D25706
This commit is contained in:
markj 2020-07-22 14:32:47 +00:00
parent 36ae2ebd15
commit 59b94fa393
12 changed files with 19 additions and 60 deletions

View File

@ -707,8 +707,6 @@ usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe,
* 0xFFFF is a FreeBSD specific magic value.
*/
urb = usb_alloc_urb(0xFFFF, size);
if (urb == NULL)
return (-ENOMEM);
urb->dev = dev;
urb->endpoint = uhe;
@ -1008,16 +1006,14 @@ usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags)
}
urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
if (urb) {
cv_init(&urb->cv_wait, "URBWAIT");
if (iso_packets == 0xFFFF) {
urb->setup_packet = (void *)(urb + 1);
urb->transfer_buffer = (void *)(urb->setup_packet +
sizeof(struct usb_device_request));
} else {
urb->number_of_packets = iso_packets;
}
cv_init(&urb->cv_wait, "URBWAIT");
if (iso_packets == 0xFFFF) {
urb->setup_packet = (void *)(urb + 1);
urb->transfer_buffer = (void *)(urb->setup_packet +
sizeof(struct usb_device_request));
} else {
urb->number_of_packets = iso_packets;
}
return (urb);
}
@ -1722,8 +1718,6 @@ usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe,
return (err);
urb = usb_alloc_urb(0, 0);
if (urb == NULL)
return (-ENOMEM);
usb_fill_bulk_urb(urb, udev, uhe, data, len,
usb_linux_wait_complete, NULL);

View File

@ -4968,10 +4968,6 @@ uaudio_mixer_fill_info(struct uaudio_softc *sc,
iot = malloc(sizeof(struct uaudio_terminal_node) * 256, M_TEMP,
M_WAITOK | M_ZERO);
if (iot == NULL) {
DPRINTF("no memory!\n");
goto done;
}
while ((desc = usb_desc_foreach(cd, desc))) {
dp = desc;

View File

@ -451,10 +451,6 @@ uhid_get_report(struct uhid_softc *sc, uint8_t type,
if (kern_data == NULL) {
kern_data = malloc(len, M_USBDEV, M_WAITOK);
if (kern_data == NULL) {
err = ENOMEM;
goto done;
}
free_data = 1;
}
err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
@ -487,10 +483,6 @@ uhid_set_report(struct uhid_softc *sc, uint8_t type,
if (kern_data == NULL) {
kern_data = malloc(len, M_USBDEV, M_WAITOK);
if (kern_data == NULL) {
err = ENOMEM;
goto done;
}
free_data = 1;
err = copyin(user_data, kern_data, len);
if (err) {

View File

@ -168,10 +168,6 @@ uhid_get_report(struct uhid_snes_softc *sc, uint8_t type,
if (kern_data == NULL) {
kern_data = malloc(len, M_USBDEV, M_WAITOK);
if (kern_data == NULL) {
err = ENOMEM;
goto done;
}
free_data = 1;
}
err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
@ -203,10 +199,6 @@ uhid_set_report(struct uhid_snes_softc *sc, uint8_t type,
if (kern_data == NULL) {
kern_data = malloc(len, M_USBDEV, M_WAITOK);
if (kern_data == NULL) {
err = ENOMEM;
goto done;
}
free_data = 1;
err = copyin(user_data, kern_data, len);
if (err) {

View File

@ -381,10 +381,6 @@ ustorage_fs_attach(device_t dev)
ustorage_fs_ramdisk =
malloc(USTORAGE_FS_RAM_SECT << 9, M_USB,
M_ZERO | M_WAITOK);
if (ustorage_fs_ramdisk == NULL) {
return (ENOMEM);
}
}
sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;

View File

@ -386,13 +386,11 @@ usb_fifo_alloc(struct mtx *mtx)
struct usb_fifo *f;
f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
if (f != NULL) {
cv_init(&f->cv_io, "FIFO-IO");
cv_init(&f->cv_drain, "FIFO-DRAIN");
f->priv_mtx = mtx;
f->refcount = 1;
knlist_init_mtx(&f->selinfo.si_note, mtx);
}
cv_init(&f->cv_io, "FIFO-IO");
cv_init(&f->cv_drain, "FIFO-DRAIN");
f->priv_mtx = mtx;
f->refcount = 1;
knlist_init_mtx(&f->selinfo.si_note, mtx);
return (f);
}
@ -2309,9 +2307,6 @@ usb_alloc_symlink(const char *target)
struct usb_symlink *ps;
ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
if (ps == NULL) {
return (ps);
}
/* XXX no longer needed */
strlcpy(ps->src_path, target, sizeof(ps->src_path));
ps->src_len = strlen(ps->src_path);

View File

@ -1785,9 +1785,11 @@ usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
return (NULL);
}
udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
#if (USB_HAVE_MALLOC_WAITOK == 0)
if (udev == NULL) {
return (NULL);
}
#endif
/* initialise our SX-lock */
sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);

View File

@ -53,6 +53,7 @@
#define USB_HAVE_FIXED_CONFIG 0
#define USB_HAVE_FIXED_PORT 0
#define USB_HAVE_DISABLE_ENUM 1
#define USB_HAVE_MALLOC_WAITOK 1
/* define zero ticks callout value */
#define USB_CALLOUT_ZERO_TICKS 1

View File

@ -53,6 +53,7 @@
#define USB_HAVE_FIXED_CONFIG 0
#define USB_HAVE_FIXED_PORT 0
#define USB_HAVE_DISABLE_ENUM 0
#define USB_HAVE_MALLOC_WAITOK 0
#define USB_CALLOUT_ZERO_TICKS 1

View File

@ -2339,11 +2339,6 @@ ugen_ioctl_post(struct usb_fifo *f, u_long cmd, void *addr, int fflags)
}
f->fs_xfer = malloc(sizeof(f->fs_xfer[0]) *
u.pinit->ep_index_max, M_USB, M_WAITOK | M_ZERO);
if (f->fs_xfer == NULL) {
usb_fifo_free_buffer(f);
error = ENOMEM;
break;
}
f->fs_ep_max = u.pinit->ep_index_max;
f->fs_ep_ptr = u.pinit->pEndpoints;
break;

View File

@ -78,15 +78,10 @@ usb_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq,
alloc_size = (block_size + sizeof(struct usb_mbuf)) * nblocks;
free_ptr = malloc(alloc_size, type, M_WAITOK | M_ZERO);
if (free_ptr == NULL) {
goto done;
}
m_ptr = free_ptr;
data_ptr = (void *)(m_ptr + nblocks);
while (nblocks--) {
m_ptr->cur_data_ptr =
m_ptr->min_data_ptr = data_ptr;
@ -99,6 +94,5 @@ usb_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq,
data_ptr += block_size;
}
}
done:
return (free_ptr);
}

View File

@ -1342,14 +1342,15 @@ usbd_transfer_setup(struct usb_device *udev,
/* allocate zeroed memory */
buf = malloc(parm->size[0], M_USB, M_WAITOK | M_ZERO);
#if (USB_HAVE_MALLOC_WAITOK == 0)
if (buf == NULL) {
parm->err = USB_ERR_NOMEM;
DPRINTFN(0, "cannot allocate memory block for "
"configuration (%d bytes)\n",
parm->size[0]);
goto done;
}
}
#endif
parm->dma_tag_p = USB_ADD_BYTES(buf, parm->size[1]);
parm->dma_page_ptr = USB_ADD_BYTES(buf, parm->size[3]);
parm->dma_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[4]);