From: "Bence Csókás via B4 Relay" <devnull+bence98.sch.bme.hu@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
"Bence Csókás" <bence98@sch.bme.hu>
Subject: [PATCH v2] USB: core: Use krealloc() in usb_cache_string()
Date: Sun, 15 Mar 2026 12:24:44 +0100 [thread overview]
Message-ID: <20260315-usb-krealloc-v2-1-32f83e090409@sch.bme.hu> (raw)
From: Bence Csókás <bence98@sch.bme.hu>
Instead of "shrinking" the allocation by kmalloc()ing a new, smaller
buffer, utilize krealloc() to shrink the existing allocation. This saves
a memcpy(), as well as eliminates the temporary `smallbuf` allocation,
which guards against allocation failure under extreme memory pressure.
Signed-off-by: Bence Csókás <bence98@sch.bme.hu>
---
Tested on an AthlonII X2 PC running Arch userland with Plasma.
$ sudo lsusb -vvv | grep -P 'i(Product|Manu)'
can't get debug descriptor: Resource temporarily unavailable
iManufacturer 3 Linux 7.0.0-rc3-00355-g94926a68d237 ohci_hcd
iProduct 2 OHCI PCI host controller
iManufacturer 1 Hewlett-Packard Company
iProduct 2 HP USB Smart Card Keyboard
can't get device qualifier: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
iManufacturer 1 PIXART
iProduct 2 USB OPTICAL MOUSE
can't get debug descriptor: Resource temporarily unavailable
can't get device qualifier: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
iManufacturer 3 Linux 7.0.0-rc3-00355-g94926a68d237 ehci_hcd
iProduct 2 EHCI Host Controller
iManufacturer 3 Linux 7.0.0-rc3-00355-g94926a68d237 ehci_hcd
can't get device qualifier: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
iProduct 2 EHCI Host Controller
can't get debug descriptor: Resource temporarily unavailable
iManufacturer 1 USB
iProduct 2 Disk 2.0
iManufacturer 3 Linux 7.0.0-rc3-00355-g94926a68d237 ehci_hcd
iProduct 2 EHCI Host Controller
can't get device qualifier: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
iManufacturer 3 Linux 7.0.0-rc3-00355-g94926a68d237 ohci_hcd
iProduct 2 OHCI PCI host controller
iManufacturer 3 Linux 7.0.0-rc3-00355-g94926a68d237 ohci_hcd
iProduct 2 OHCI PCI host controller
can't get debug descriptor: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
iManufacturer 3 Linux 7.0.0-rc3-00355-g94926a68d237 ohci_hcd
iProduct 2 OHCI PCI host controller
---
Changes in v2:
- Update msg, remove backticks
- Add testing results
- Remove ?:
- Link to v1: https://lore.kernel.org/r/20260312-usb-krealloc-v1-1-f76b92b92402@sch.bme.hu
---
drivers/usb/core/message.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index ea970ddf8879..a0c77709aa36 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1005,7 +1005,7 @@ int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
}
EXPORT_SYMBOL_GPL(usb_string);
-/* one UTF-8-encoded 16-bit character has at most three bytes */
+/* one 16-bit character, when UTF-8-encoded, has at most three bytes */
#define MAX_USB_STRING_SIZE (127 * 3 + 1)
/**
@@ -1026,16 +1026,18 @@ char *usb_cache_string(struct usb_device *udev, int index)
return NULL;
buf = kmalloc(MAX_USB_STRING_SIZE, GFP_NOIO);
- if (buf) {
- len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
- if (len > 0) {
- smallbuf = kmalloc(++len, GFP_NOIO);
- if (!smallbuf)
- return buf;
- memcpy(smallbuf, buf, len);
- }
+ if (!buf)
+ return NULL;
+
+ len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
+ if (len <= 0) {
kfree(buf);
+ return NULL;
}
+
+ smallbuf = krealloc(buf, len + 1, GFP_NOIO);
+ if (unlikely(!smallbuf))
+ return buf;
return smallbuf;
}
EXPORT_SYMBOL_GPL(usb_cache_string);
---
base-commit: 1c9982b4961334c1edb0745a04cabd34bc2de675
change-id: 20260311-usb-krealloc-4e413f239a9f
Best regards,
--
Bence Csókás <bence98@sch.bme.hu>
reply other threads:[~2026-03-15 11:24 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260315-usb-krealloc-v2-1-32f83e090409@sch.bme.hu \
--to=devnull+bence98.sch.bme.hu@kernel.org \
--cc=bence98@sch.bme.hu \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®