| 0/0 |
2026/11/24 09:10 |
flow |
bisect-cause |
30m
Results: map[BisectReasoning:The bug was introduced by commit **`564246fd3ff41afd3234c18ae28bc4ad9d815f79`** (`media: siano: Fix coherent memory allocation failure on arm64`).
### Root Cause Analysis
1. **Reproducer Device & Probe Path:**
The reproducer connects a virtual USB device via `/dev/raw-gadget` with `idVendor = 0x187f` and `idProduct = 0x0100`, matching `SMS1XXX_BOARD_SIANO_STELLAR` in `drivers/media/usb/siano/smsusb.c` (`smsusb_id_table`).
- `smsusb_probe()` calls `smsusb_init_device()`.
- `smsusb_init_device()` calls `smscore_register_device()` (`drivers/media/common/siano/smscoreapi.c`), which allocates a single large `common_buffer` via `kzalloc(dev->common_buffer_size, GFP_KERNEL)` (`USB1_BUFFER_SIZE * MAX_BUFFERS = 0x1000 * 50 = 204800` bytes, an order-6 large kmalloc allocation) and slices it into 50 `struct smscore_buffer_t` entries (`cb->p = common_buffer + i * 0x1000`).
- `smsusb_init_device()` allocates 10 URBs (`dev->surbs[i].urb = usb_alloc_urb(0, GFP_KERNEL)`) and calls `smsusb_start_streaming()` -> `smsusb_submit_urb()`.
- In `smsusb_submit_urb()` (`drivers/media/usb/siano/smsusb.c:171`), each URB's `transfer_buffer` is set to `surb->cb->p`, and **`surb->urb->transfer_flags |= URB_FREE_BUFFER;`** is set.
2. **Disconnect & Invalid `kfree()`:**
When the reproducer exits and closes `/dev/raw-gadget`, the virtual USB device disconnects (`kworker/0:1` running `hub_event` -> `smsusb_disconnect()` -> `smsusb_term_device()`):
- `smsusb_stop_streaming(dev)` kills the URBs and returns `surb->cb` to `coredev->buffers` (leaving `surb->urb->transfer_buffer` pointing into `common_buffer` and `URB_FREE_BUFFER` still set).
- `smscore_unregister_device(dev->coredev)` frees `coredev->common_buffer` via `kfree(coredev->common_buffer)` (`drivers/media/common/siano/smscoreapi.c:1228`), which calls `free_large_kmalloc()`, clears `PageLargeKmalloc`, and returns the order-6 pages to the page allocator.
- `smsusb_term_device()` then loops over `MAX_URBS` and calls `usb_free_urb(dev->surbs[i].urb)` (`drivers/media/usb/siano/smsusb.c:352`).
- `usb_free_urb()` drops the final `kref` and invokes `urb_destroy()` (`drivers/usb/core/urb.c:20`), which checks `if (urb->transfer_flags & URB_FREE_BUFFER) kfree(urb->transfer_buffer);`.
- Calling `kfree()` on `common_buffer` (already freed) and interior pointers `common_buffer + i * 0x1000` (tail pages) enters `free_large_kmalloc()` (`mm/slub.c:6855`) and triggers `WARN_ON_ONCE(!PageLargeKmalloc(page))`.
### Git History & Commit Selection
- **`564246fd3ff4` (`media: siano: Fix coherent memory allocation failure on arm64`) β Primary bug-introducing commit:**
Changed USB `common_buffer` allocation in `smscore_register_device()` from `dma_alloc_coherent()` to `kzalloc()`, added `kfree(coredev->common_buffer)` in `smscore_unregister_device()`, and erroneously added `surb->urb.transfer_flags |= URB_FREE_BUFFER;` in `smsusb_submit_urb()`. Setting `URB_FREE_BUFFER` violates the `URB_FREE_BUFFER` contract because `surb->cb->p` is neither owned by the URB nor an independent `kmalloc()` allocation (for `i > 0` it is an interior pointer into `common_buffer`, and `common_buffer` is already freed by `smscore_unregister_device()`).
- **Related commits (not blamed as primary cause):**
- **`836e2548524d` (`media: usb: siano: Fix allocation of urbs`):** Converted `surb->urb` from an embedded `struct urb` initialized with `usb_init_urb()` (where `usb_free_urb()` was never called, masking the bug) to `usb_alloc_urb()`/`usb_free_urb()` to fix a smatch flexible-array warning. Calling `usb_free_urb()` exposed the pre-existing `URB_FREE_BUFFER` bug from `564246fd3ff4`.
- **`a6687c8ff613` (`slab: Mark large folios for debugging purposes`)** and **`0bdfdd6a05aa` (`slab: Remove folio references from free_large_kmalloc()`):** Added and refactored the `WARN_ON_ONCE(!PageLargeKmalloc(page))` sanity check in `free_large_kmalloc()` that catches this double/invalid free cleanly. Commits:[map[AuthorEmail:tomoki.sekiyama@gmail.com AuthorName:Tomoki Sekiyama Date:2018-03-03T13:20:56-05:00 Hash:564246fd3ff41afd3234c18ae28bc4ad9d815f79 Recipients:[map[Email:mchehab@s-opensource.com Name: To:true] map[Email:tomoki.sekiyama@gmail.com Name: To:true]] Title:media: siano: Fix coherent memory allocation failure on arm64]] IsRelease:false KernelCommit:5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f]
|
| 1/1 |
2026/11/24 09:10 |
action |
use-crash-commit |
0m
Results: map[KernelCommit:5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f]
|
| 2/1 |
2026/11/24 09:10 |
action |
syz-repro-to-c-repro |
0m
Results: map[SimplifiedCRepro:// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/usb/ch9.h>
#ifndef __NR_mmap
#define __NR_mmap 192
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2
static unsigned long long procid;
static void sleep_ms(uint64_t ms)
{
usleep(ms * 1000);
}
#define MAX_FDS 30
#define USB_MAX_IFACE_NUM 4
#define USB_MAX_EP_NUM 32
#define USB_MAX_FDS 6
struct usb_endpoint_index {
struct usb_endpoint_descriptor desc;
int handle;
};
struct usb_iface_index {
struct usb_interface_descriptor* iface;
uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bInterfaceClass;
struct usb_endpoint_index eps[USB_MAX_EP_NUM];
int eps_num;
};
struct usb_device_index {
struct usb_device_descriptor* dev;
struct usb_config_descriptor* config;
uint8_t bDeviceClass;
uint8_t bMaxPower;
int config_length;
struct usb_iface_index ifaces[USB_MAX_IFACE_NUM];
int ifaces_num;
int iface_cur;
};
struct usb_info {
int fd;
struct usb_device_index index;
};
static struct usb_info usb_devices[USB_MAX_FDS];
static struct usb_device_index* lookup_usb_index(int fd)
{
for (int i = 0; i < USB_MAX_FDS; i++) {
if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd)
return &usb_devices[i].index;
}
return NULL;
}
static int usb_devices_num;
static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index)
{
if (length < sizeof(*index->dev) + sizeof(*index->config))
return false;
memset(index, 0, sizeof(*index));
index->dev = (struct usb_device_descriptor*)buffer;
index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev));
index->bDeviceClass = index->dev->bDeviceClass;
index->bMaxPower = index->config->bMaxPower;
index->config_length = length - sizeof(*index->dev);
index->iface_cur = -1;
size_t offset = 0;
while (true) {
if (offset + 1 >= length)
break;
uint8_t desc_length = buffer[offset];
uint8_t desc_type = buffer[offset + 1];
if (desc_length <= 2)
break;
if (offset + desc_length > length)
break;
if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) {
struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset);
index->ifaces[index->ifaces_num].iface = iface;
index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber;
index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting;
index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass;
index->ifaces_num++;
}
if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) {
struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1];
if (iface->eps_num < USB_MAX_EP_NUM) {
memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc));
iface->eps_num++;
}
}
offset += desc_length;
}
return true;
}
static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len)
{
int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED);
if (i >= USB_MAX_FDS)
return NULL;
if (!parse_usb_descriptor(dev, dev_len, &usb_devices[i].index))
return NULL;
__atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE);
return &usb_devices[i].index;
}
struct vusb_connect_string_descriptor {
uint32_t len;
char* str;
} __attribute__((packed));
struct vusb_connect_descriptors {
uint32_t qual_len;
char* qual;
uint32_t bos_len;
char* bos;
uint32_t strs_len;
struct vusb_connect_string_descriptor strs[0];
} __attribute__((packed));
static const char default_string[] = {
8, USB_DT_STRING,
's', 0, 'y', 0, 'z', 0
};
static const char default_lang_id[] = {
4, USB_DT_STRING,
0x09, 0x04
};
static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs,
const struct usb_ctrlrequest* ctrl,
struct usb_qualifier_descriptor* qual,
char** response_data, uint32_t* response_length)
{
struct usb_device_index* index = lookup_usb_index(fd);
uint8_t str_idx;
if (!index)
return false;
switch (ctrl->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (ctrl->bRequest) {
case USB_REQ_GET_DESCRIPTOR:
switch (ctrl->wValue >> 8) {
case USB_DT_DEVICE:
*response_data = (char*)index->dev;
*response_length = sizeof(*index->dev);
return true;
case USB_DT_CONFIG:
*response_data = (char*)index->config;
*response_length = index->config_length;
return true;
case USB_DT_STRING:
str_idx = (uint8_t)ctrl->wValue;
if (descs && str_idx < descs->strs_len) {
*response_data = descs->strs[str_idx].str;
*response_length = descs->strs[str_idx].len;
return true;
}
if (str_idx == 0) {
*response_data = (char*)&default_lang_id[0];
*response_length = default_lang_id[0];
return true;
}
*response_data = (char*)&default_string[0];
*response_length = default_string[0];
return true;
case USB_DT_BOS:
*response_data = descs->bos;
*response_length = descs->bos_len;
return true;
case USB_DT_DEVICE_QUALIFIER:
if (!descs->qual) {
qual->bLength = sizeof(*qual);
qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
qual->bcdUSB = index->dev->bcdUSB;
qual->bDeviceClass = index->dev->bDeviceClass;
qual->bDeviceSubClass = index->dev->bDeviceSubClass;
qual->bDeviceProtocol = index->dev->bDeviceProtocol;
qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0;
qual->bNumConfigurations = index->dev->bNumConfigurations;
qual->bRESERVED = 0;
*response_data = (char*)qual;
*response_length = sizeof(*qual);
return true;
}
*response_data = descs->qual;
*response_length = descs->qual_len;
return true;
default:
break;
}
break;
default:
break;
}
break;
default:
break;
}
return false;
}
typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs,
const struct usb_ctrlrequest* ctrl, bool* done);
static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs,
const struct usb_ctrlrequest* ctrl, bool* done)
{
switch (ctrl->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (ctrl->bRequest) {
case USB_REQ_SET_CONFIGURATION:
*done = true;
return true;
default:
break;
}
break;
}
return false;
}
#define UDC_NAME_LENGTH_MAX 128
struct usb_raw_init {
__u8 driver_name[UDC_NAME_LENGTH_MAX];
__u8 device_name[UDC_NAME_LENGTH_MAX];
__u8 speed;
};
enum usb_raw_event_type {
USB_RAW_EVENT_INVALID = 0,
USB_RAW_EVENT_CONNECT = 1,
USB_RAW_EVENT_CONTROL = 2,
};
struct usb_raw_event {
__u32 type;
__u32 length;
__u8 data[0];
};
struct usb_raw_ep_io {
__u16 ep;
__u16 flags;
__u32 length;
__u8 data[0];
};
#define USB_RAW_EPS_NUM_MAX 30
#define USB_RAW_EP_NAME_MAX 16
#define USB_RAW_EP_ADDR_ANY 0xff
struct usb_raw_ep_caps {
__u32 type_control : 1;
__u32 type_iso : 1;
__u32 type_bulk : 1;
__u32 type_int : 1;
__u32 dir_in : 1;
__u32 dir_out : 1;
};
struct usb_raw_ep_limits {
__u16 maxpacket_limit;
__u16 max_streams;
__u32 reserved;
};
struct usb_raw_ep_info {
__u8 name[USB_RAW_EP_NAME_MAX];
__u32 addr;
struct usb_raw_ep_caps caps;
struct usb_raw_ep_limits limits;
};
struct usb_raw_eps_info {
struct usb_raw_ep_info eps[USB_RAW_EPS_NUM_MAX];
};
#define USB_RAW_IOCTL_INIT _IOW('U', 0, struct usb_raw_init)
#define USB_RAW_IOCTL_RUN _IO('U', 1)
#define USB_RAW_IOCTL_EVENT_FETCH _IOR('U', 2, struct usb_raw_event)
#define USB_RAW_IOCTL_EP0_WRITE _IOW('U', 3, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP0_READ _IOWR('U', 4, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP_ENABLE _IOW('U', 5, struct usb_endpoint_descriptor)
#define USB_RAW_IOCTL_EP_DISABLE _IOW('U', 6, __u32)
#define USB_RAW_IOCTL_EP_WRITE _IOW('U', 7, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP_READ _IOWR('U', 8, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_CONFIGURE _IO('U', 9)
#define USB_RAW_IOCTL_VBUS_DRAW _IOW('U', 10, __u32)
#define USB_RAW_IOCTL_EPS_INFO _IOR('U', 11, struct usb_raw_eps_info)
#define USB_RAW_IOCTL_EP0_STALL _IO('U', 12)
#define USB_RAW_IOCTL_EP_SET_HALT _IOW('U', 13, __u32)
#define USB_RAW_IOCTL_EP_CLEAR_HALT _IOW('U', 14, __u32)
#define USB_RAW_IOCTL_EP_SET_WEDGE _IOW('U', 15, __u32)
static int usb_raw_open()
{
return open("/dev/raw-gadget", O_RDWR);
}
static int usb_raw_init(int fd, uint32_t speed, const char* driver, const char* device)
{
struct usb_raw_init arg;
strncpy((char*)&arg.driver_name[0], driver, sizeof(arg.driver_name));
strncpy((char*)&arg.device_name[0], device, sizeof(arg.device_name));
arg.speed = speed;
return ioctl(fd, USB_RAW_IOCTL_INIT, &arg);
}
static int usb_raw_run(int fd)
{
return ioctl(fd, USB_RAW_IOCTL_RUN, 0);
}
static int usb_raw_configure(int fd)
{
return ioctl(fd, USB_RAW_IOCTL_CONFIGURE, 0);
}
static int usb_raw_vbus_draw(int fd, uint32_t power)
{
return ioctl(fd, USB_RAW_IOCTL_VBUS_DRAW, power);
}
static int usb_raw_ep0_write(int fd, struct usb_raw_ep_io* io)
{
return ioctl(fd, USB_RAW_IOCTL_EP0_WRITE, io);
}
static int usb_raw_ep0_read(int fd, struct usb_raw_ep_io* io)
{
return ioctl(fd, USB_RAW_IOCTL_EP0_READ, io);
}
static int usb_raw_event_fetch(int fd, struct usb_raw_event* event)
{
return ioctl(fd, USB_RAW_IOCTL_EVENT_FETCH, event);
}
static int usb_raw_ep_enable(int fd, struct usb_endpoint_descriptor* desc)
{
return ioctl(fd, USB_RAW_IOCTL_EP_ENABLE, desc);
}
static int usb_raw_ep_disable(int fd, int ep)
{
return ioctl(fd, USB_RAW_IOCTL_EP_DISABLE, ep);
}
static int usb_raw_ep0_stall(int fd)
{
return ioctl(fd, USB_RAW_IOCTL_EP0_STALL, 0);
}
#define USB_MAX_PACKET_SIZE 4096
struct usb_raw_control_event {
struct usb_raw_event inner;
struct usb_ctrlrequest ctrl;
char data[USB_MAX_PACKET_SIZE];
};
struct usb_raw_ep_io_data {
struct usb_raw_ep_io inner;
char data[USB_MAX_PACKET_SIZE];
};
static void set_interface(int fd, int n)
{
struct usb_device_index* index = lookup_usb_index(fd);
if (!index)
return;
if (index->iface_cur >= 0 && index->iface_cur < index->ifaces_num) {
for (int ep = 0; ep < index->ifaces[index->iface_cur].eps_num; ep++) {
int rv = usb_raw_ep_disable(fd, index->ifaces[index->iface_cur].eps[ep].handle);
if (rv < 0) {
} else {
}
}
}
if (n >= 0 && n < index->ifaces_num) {
for (int ep = 0; ep < index->ifaces[n].eps_num; ep++) {
int rv = usb_raw_ep_enable(fd, &index->ifaces[n].eps[ep].desc);
if (rv < 0) {
} else {
index->ifaces[n].eps[ep].handle = rv;
}
}
index->iface_cur = n;
}
}
static int configure_device(int fd)
{
struct usb_device_index* index = lookup_usb_index(fd);
if (!index)
return -1;
int rv = usb_raw_vbus_draw(fd, index->bMaxPower);
if (rv < 0) {
return rv;
}
rv = usb_raw_configure(fd);
if (rv < 0) {
return rv;
}
set_interface(fd, 0);
return 0;
}
static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev,
const struct vusb_connect_descriptors* descs,
lookup_connect_out_response_t lookup_connect_response_out)
{
if (!dev) {
return -1;
}
int fd = usb_raw_open();
if (fd < 0) {
return fd;
}
if (fd >= MAX_FDS) {
close(fd);
return -1;
}
struct usb_device_index* index = add_usb_index(fd, dev, dev_len);
if (!index) {
return -1;
}
char device[32];
sprintf(&device[0], "dummy_udc.%llu", procid);
int rv = usb_raw_init(fd, speed, "dummy_udc", &device[0]);
if (rv < 0) {
return rv;
}
rv = usb_raw_run(fd);
if (rv < 0) {
return rv;
}
bool done = false;
while (!done) {
struct usb_raw_control_event event;
event.inner.type = 0;
event.inner.length = sizeof(event.ctrl);
rv = usb_raw_event_fetch(fd, (struct usb_raw_event*)&event);
if (rv < 0) {
return rv;
}
if (event.inner.type != USB_RAW_EVENT_CONTROL)
continue;
char* response_data = NULL;
uint32_t response_length = 0;
struct usb_qualifier_descriptor qual;
if (event.ctrl.bRequestType & USB_DIR_IN) {
if (!lookup_connect_response_in(fd, descs, &event.ctrl, &qual, &response_data, &response_length)) {
usb_raw_ep0_stall(fd);
continue;
}
} else {
if (!lookup_connect_response_out(fd, descs, &event.ctrl, &done)) {
usb_raw_ep0_stall(fd);
continue;
}
response_data = NULL;
response_length = event.ctrl.wLength;
}
if ((event.ctrl.bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD &&
event.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) {
rv = configure_device(fd);
if (rv < 0) {
return rv;
}
}
struct usb_raw_ep_io_data response;
response.inner.ep = 0;
response.inner.flags = 0;
if (response_length > sizeof(response.data))
response_length = 0;
if (event.ctrl.wLength < response_length)
response_length = event.ctrl.wLength;
response.inner.length = response_length;
if (response_data)
memcpy(&response.data[0], response_data, response_length);
else
memset(&response.data[0], 0, response_length);
if (event.ctrl.bRequestType & USB_DIR_IN) {
rv = usb_raw_ep0_write(fd, (struct usb_raw_ep_io*)&response);
} else {
rv = usb_raw_ep0_read(fd, (struct usb_raw_ep_io*)&response);
}
if (rv < 0) {
return rv;
}
}
sleep_ms(200);
return fd;
}
static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3)
{
uint64_t speed = a0;
uint64_t dev_len = a1;
const char* dev = (const char*)a2;
const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3;
return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic);
}
int main(void)
{
syscall(__NR_mmap, /*addr=*/0x7ffff000, /*len=*/0x1000, /*prot=*/0, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
syscall(__NR_mmap, /*addr=*/0x80000000, /*len=*/0x1000000, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/7, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
syscall(__NR_mmap, /*addr=*/0x81000000, /*len=*/0x1000, /*prot=*/0, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
const char* reason;
(void)reason;
if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {}
// syz_usb_connect arguments: [
// speed: usb_device_speed = 0x0 (4 bytes)
// dev_len: len = 0x32 (4 bytes)
// dev: ptr[inout, array[ANYUNION]] {
// array[ANYUNION] {
// union ANYUNION {
// ANYBLOB: buffer: {12 01 00 02 00 00 00 40 7f 18 00 01 00 00 00 00 00 01 09 02 20 00 01 01 00 80 32 09 04 00 00 02 ff 00 00 00 07 05 81 02 40 00 00 07 05 02 02 40 00 00} (length 0x32)
// }
// }
// }
// conn_descs: nil
// ]
// returns fd_usb
memcpy((void*)0x80000000, "... [truncated large byte array] ...", 50);
syz_usb_connect(/*speed=*/0, /*dev_len=*/0x32, /*dev=*/0x80000000, /*conn_descs=*/0);
return 0;
}
]
|
| 3/1 |
2026/11/24 09:10 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/3a4e75985f00e9bb085f2be31a057981944100d3/data]
|
| 4/1 |
2026/11/24 09:11 |
action |
kernel-builder |
14m
Results: map[KernelObj:/app/workdir/cache/build/1365ab491a819d4d2a1b18143908ab401f2a708c/data]
|
| 5/1 |
2026/11/24 09:26 |
action |
codesearch-prepare |
11m
Results: map[Index:codesearch-index]
|
| 6/1 |
2026/11/24 09:37 |
agent |
cause-finder |
3m
Model:
Results: map[CandidateCommits:[564246fd3ff41afd3234c18ae28bc4ad9d815f79]]
Instruction:
You are an experienced Linux kernel developer tasked with identifying the commit
that introduced a kernel bug (cause bisection).
Your investigation strategy:
1. Establish the root cause first.
Analyze the crash report and the code it points to (stack traces, allocation/free stacks,
the involved functions and structs) until you understand the exact invariant that is
violated and the exact execution sequence that leads to the crash.
Identify the specific functions, structs, invariants, or lines of code where the buggy
logic resides.
2. Use git tools (git-blame, git-log, git-show) to trace the history
of the affected functions and lines.
3. Trace past pure refactorings, code movements, formatting changes, or variable renames
to find the commit that actually introduced the buggy logic or broke an existing invariant.
4. Do NOT blame commits that merely added debugging or sanitizer checks (WARN_ON, BUG_ON,
KASAN/KMSAN/LOCKDEP annotations) that exposed an older bug, unless the check itself is
incorrect and is the bug.
5. Do NOT return a merge commit unless the bug was introduced by the merge conflict
resolution itself; otherwise identify the underlying non-merge commit.
6. Verify your hypothesis by inspecting the full commit diff with git-show and
confirming how the change introduced the exact condition described in the root cause.
Output rules:
- Returning a SINGLE commit in CandidateCommits (a 1-element list) is the expected norm
and is strongly preferred. A single commit gives maintainers a clear, directly actionable
result, whereas returning extra commits dilutes the report and notifies additional commit
authors unnecessarily.
- Once you identify the primary commit that introduced (or fixed) the bug, return ONLY that
commit in CandidateCommits. Do NOT include secondary or "contributing" commits such as
earlier background/foundation commits, preparatory or follow-up commits in the same
series, later refactorings, incomplete fix attempts, or commits that merely exposed or
enabled the code path (e.g. new callers, default config changes, or sanitizer/debug checks).
Mention those related commits in your text reply for context instead.
- Returning more than one commit (at most 3) is the rare exception, not the norm. Only return
multiple commits when:
1. There is genuine, unresolvable ambiguity between 2-3 equally plausible candidates after
thorough investigation, OR
2. The bug was strictly and inseparably introduced (or fixed) by 2-3 commits together, with
no single primary commit.
In those rare cases, order CandidateCommits by relevance with the most important/likely
commit FIRST.
- If you cannot determine the commit with confidence, return an empty CandidateCommits list
rather than guessing.
- Your text reply must provide a clear bisection log explaining the root cause of the bug,
the git history you traced, the commits you examined (with their hashes and titles), why
you ruled out other commits, and how you reached your conclusion.
Don't make assumptions about the kernel source code (it may be different from what you assume it is).
Extensively use the provided code access tools (codesearch-*, git-*, grepper, etc)
to examine the actual source code, and confirm any assumptions.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
During execution, you may receive periodic guidance in <supervisor_advice> tags;
use it to refine your investigation or wrap up once you have sufficient evidence.
Prompt:
The crash is:
------------[ cut here ]------------
!PageLargeKmalloc(page)
WARNING: mm/slub.c:6855 at free_large_kmalloc+0x97/0x110 mm/slub.c:6855, CPU#0: kworker/0:1/10
The following C code is a draft of the syscall sequence that triggers the kernel crash. Keep in mind
that it may lack the precise threading, sandboxing, and some arguments of a working reproducer:
// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/usb/ch9.h>
#ifndef __NR_mmap
#define __NR_mmap 192
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2
static unsigned long long procid;
static void sleep_ms(uint64_t ms)
{
usleep(ms * 1000);
}
#define MAX_FDS 30
#define USB_MAX_IFACE_NUM 4
#define USB_MAX_EP_NUM 32
#define USB_MAX_FDS 6
struct usb_endpoint_index {
struct usb_endpoint_descriptor desc;
int handle;
};
struct usb_iface_index {
struct usb_interface_descriptor* iface;
uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bInterfaceClass;
struct usb_endpoint_index eps[USB_MAX_EP_NUM];
int eps_num;
};
struct usb_device_index {
struct usb_device_descriptor* dev;
struct usb_config_descriptor* config;
uint8_t bDeviceClass;
uint8_t bMaxPower;
int config_length;
struct usb_iface_index ifaces[USB_MAX_IFACE_NUM];
int ifaces_num;
int iface_cur;
};
struct usb_info {
int fd;
struct usb_device_index index;
};
static struct usb_info usb_devices[USB_MAX_FDS];
static struct usb_device_index* lookup_usb_index(int fd)
{
for (int i = 0; i < USB_MAX_FDS; i++) {
if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd)
return &usb_devices[i].index;
}
return NULL;
}
static int usb_devices_num;
static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index)
{
if (length < sizeof(*index->dev) + sizeof(*index->config))
return false;
memset(index, 0, sizeof(*index));
index->dev = (struct usb_device_descriptor*)buffer;
index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev));
index->bDeviceClass = index->dev->bDeviceClass;
index->bMaxPower = index->config->bMaxPower;
index->config_length = length - sizeof(*index->dev);
index->iface_cur = -1;
size_t offset = 0;
while (true) {
if (offset + 1 >= length)
break;
uint8_t desc_length = buffer[offset];
uint8_t desc_type = buffer[offset + 1];
if (desc_length <= 2)
break;
if (offset + desc_length > length)
break;
if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) {
struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset);
index->ifaces[index->ifaces_num].iface = iface;
index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber;
index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting;
index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass;
index->ifaces_num++;
}
if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) {
struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1];
if (iface->eps_num < USB_MAX_EP_NUM) {
memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc));
iface->eps_num++;
}
}
offset += desc_length;
}
return true;
}
static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len)
{
int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED);
if (i >= USB_MAX_FDS)
return NULL;
if (!parse_usb_descriptor(dev, dev_len, &usb_devices[i].index))
return NULL;
__atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE);
return &usb_devices[i].index;
}
struct vusb_connect_string_descriptor {
uint32_t len;
char* str;
} __attribute__((packed));
struct vusb_connect_descriptors {
uint32_t qual_len;
char* qual;
uint32_t bos_len;
char* bos;
uint32_t strs_len;
struct vusb_connect_string_descriptor strs[0];
} __attribute__((packed));
static const char default_string[] = {
8, USB_DT_STRING,
's', 0, 'y', 0, 'z', 0
};
static const char default_lang_id[] = {
4, USB_DT_STRING,
0x09, 0x04
};
static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs,
const struct usb_ctrlrequest* ctrl,
struct usb_qualifier_descriptor* qual,
char** response_data, uint32_t* response_length)
{
struct usb_device_index* index = lookup_usb_index(fd);
uint8_t str_idx;
if (!index)
return false;
switch (ctrl->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (ctrl->bRequest) {
case USB_REQ_GET_DESCRIPTOR:
switch (ctrl->wValue >> 8) {
case USB_DT_DEVICE:
*response_data = (char*)index->dev;
*response_length = sizeof(*index->dev);
return true;
case USB_DT_CONFIG:
*response_data = (char*)index->config;
*response_length = index->config_length;
return true;
case USB_DT_STRING:
str_idx = (uint8_t)ctrl->wValue;
if (descs && str_idx < descs->strs_len) {
*response_data = descs->strs[str_idx].str;
*response_length = descs->strs[str_idx].len;
return true;
}
if (str_idx == 0) {
*response_data = (char*)&default_lang_id[0];
*response_length = default_lang_id[0];
return true;
}
*response_data = (char*)&default_string[0];
*response_length = default_string[0];
return true;
case USB_DT_BOS:
*response_data = descs->bos;
*response_length = descs->bos_len;
return true;
case USB_DT_DEVICE_QUALIFIER:
if (!descs->qual) {
qual->bLength = sizeof(*qual);
qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
qual->bcdUSB = index->dev->bcdUSB;
qual->bDeviceClass = index->dev->bDeviceClass;
qual->bDeviceSubClass = index->dev->bDeviceSubClass;
qual->bDeviceProtocol = index->dev->bDeviceProtocol;
qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0;
qual->bNumConfigurations = index->dev->bNumConfigurations;
qual->bRESERVED = 0;
*response_data = (char*)qual;
*response_length = sizeof(*qual);
return true;
}
*response_data = descs->qual;
*response_length = descs->qual_len;
return true;
default:
break;
}
break;
default:
break;
}
break;
default:
break;
}
return false;
}
typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs,
const struct usb_ctrlrequest* ctrl, bool* done);
static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs,
const struct usb_ctrlrequest* ctrl, bool* done)
{
switch (ctrl->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (ctrl->bRequest) {
case USB_REQ_SET_CONFIGURATION:
*done = true;
return true;
default:
break;
}
break;
}
return false;
}
#define UDC_NAME_LENGTH_MAX 128
struct usb_raw_init {
__u8 driver_name[UDC_NAME_LENGTH_MAX];
__u8 device_name[UDC_NAME_LENGTH_MAX];
__u8 speed;
};
enum usb_raw_event_type {
USB_RAW_EVENT_INVALID = 0,
USB_RAW_EVENT_CONNECT = 1,
USB_RAW_EVENT_CONTROL = 2,
};
struct usb_raw_event {
__u32 type;
__u32 length;
__u8 data[0];
};
struct usb_raw_ep_io {
__u16 ep;
__u16 flags;
__u32 length;
__u8 data[0];
};
#define USB_RAW_EPS_NUM_MAX 30
#define USB_RAW_EP_NAME_MAX 16
#define USB_RAW_EP_ADDR_ANY 0xff
struct usb_raw_ep_caps {
__u32 type_control : 1;
__u32 type_iso : 1;
__u32 type_bulk : 1;
__u32 type_int : 1;
__u32 dir_in : 1;
__u32 dir_out : 1;
};
struct usb_raw_ep_limits {
__u16 maxpacket_limit;
__u16 max_streams;
__u32 reserved;
};
struct usb_raw_ep_info {
__u8 name[USB_RAW_EP_NAME_MAX];
__u32 addr;
struct usb_raw_ep_caps caps;
struct usb_raw_ep_limits limits;
};
struct usb_raw_eps_info {
struct usb_raw_ep_info eps[USB_RAW_EPS_NUM_MAX];
};
#define USB_RAW_IOCTL_INIT _IOW('U', 0, struct usb_raw_init)
#define USB_RAW_IOCTL_RUN _IO('U', 1)
#define USB_RAW_IOCTL_EVENT_FETCH _IOR('U', 2, struct usb_raw_event)
#define USB_RAW_IOCTL_EP0_WRITE _IOW('U', 3, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP0_READ _IOWR('U', 4, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP_ENABLE _IOW('U', 5, struct usb_endpoint_descriptor)
#define USB_RAW_IOCTL_EP_DISABLE _IOW('U', 6, __u32)
#define USB_RAW_IOCTL_EP_WRITE _IOW('U', 7, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP_READ _IOWR('U', 8, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_CONFIGURE _IO('U', 9)
#define USB_RAW_IOCTL_VBUS_DRAW _IOW('U', 10, __u32)
#define USB_RAW_IOCTL_EPS_INFO _IOR('U', 11, struct usb_raw_eps_info)
#define USB_RAW_IOCTL_EP0_STALL _IO('U', 12)
#define USB_RAW_IOCTL_EP_SET_HALT _IOW('U', 13, __u32)
#define USB_RAW_IOCTL_EP_CLEAR_HALT _IOW('U', 14, __u32)
#define USB_RAW_IOCTL_EP_SET_WEDGE _IOW('U', 15, __u32)
static int usb_raw_open()
{
return open("/dev/raw-gadget", O_RDWR);
}
static int usb_raw_init(int fd, uint32_t speed, const char* driver, const char* device)
{
struct usb_raw_init arg;
strncpy((char*)&arg.driver_name[0], driver, sizeof(arg.driver_name));
strncpy((char*)&arg.device_name[0], device, sizeof(arg.device_name));
arg.speed = speed;
return ioctl(fd, USB_RAW_IOCTL_INIT, &arg);
}
static int usb_raw_run(int fd)
{
return ioctl(fd, USB_RAW_IOCTL_RUN, 0);
}
static int usb_raw_configure(int fd)
{
return ioctl(fd, USB_RAW_IOCTL_CONFIGURE, 0);
}
static int usb_raw_vbus_draw(int fd, uint32_t power)
{
return ioctl(fd, USB_RAW_IOCTL_VBUS_DRAW, power);
}
static int usb_raw_ep0_write(int fd, struct usb_raw_ep_io* io)
{
return ioctl(fd, USB_RAW_IOCTL_EP0_WRITE, io);
}
static int usb_raw_ep0_read(int fd, struct usb_raw_ep_io* io)
{
return ioctl(fd, USB_RAW_IOCTL_EP0_READ, io);
}
static int usb_raw_event_fetch(int fd, struct usb_raw_event* event)
{
return ioctl(fd, USB_RAW_IOCTL_EVENT_FETCH, event);
}
static int usb_raw_ep_enable(int fd, struct usb_endpoint_descriptor* desc)
{
return ioctl(fd, USB_RAW_IOCTL_EP_ENABLE, desc);
}
static int usb_raw_ep_disable(int fd, int ep)
{
return ioctl(fd, USB_RAW_IOCTL_EP_DISABLE, ep);
}
static int usb_raw_ep0_stall(int fd)
{
return ioctl(fd, USB_RAW_IOCTL_EP0_STALL, 0);
}
#define USB_MAX_PACKET_SIZE 4096
struct usb_raw_control_event {
struct usb_raw_event inner;
struct usb_ctrlrequest ctrl;
char data[USB_MAX_PACKET_SIZE];
};
struct usb_raw_ep_io_data {
struct usb_raw_ep_io inner;
char data[USB_MAX_PACKET_SIZE];
};
static void set_interface(int fd, int n)
{
struct usb_device_index* index = lookup_usb_index(fd);
if (!index)
return;
if (index->iface_cur >= 0 && index->iface_cur < index->ifaces_num) {
for (int ep = 0; ep < index->ifaces[index->iface_cur].eps_num; ep++) {
int rv = usb_raw_ep_disable(fd, index->ifaces[index->iface_cur].eps[ep].handle);
if (rv < 0) {
} else {
}
}
}
if (n >= 0 && n < index->ifaces_num) {
for (int ep = 0; ep < index->ifaces[n].eps_num; ep++) {
int rv = usb_raw_ep_enable(fd, &index->ifaces[n].eps[ep].desc);
if (rv < 0) {
} else {
index->ifaces[n].eps[ep].handle = rv;
}
}
index->iface_cur = n;
}
}
static int configure_device(int fd)
{
struct usb_device_index* index = lookup_usb_index(fd);
if (!index)
return -1;
int rv = usb_raw_vbus_draw(fd, index->bMaxPower);
if (rv < 0) {
return rv;
}
rv = usb_raw_configure(fd);
if (rv < 0) {
return rv;
}
set_interface(fd, 0);
return 0;
}
static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev,
const struct vusb_connect_descriptors* descs,
lookup_connect_out_response_t lookup_connect_response_out)
{
if (!dev) {
return -1;
}
int fd = usb_raw_open();
if (fd < 0) {
return fd;
}
if (fd >= MAX_FDS) {
close(fd);
return -1;
}
struct usb_device_index* index = add_usb_index(fd, dev, dev_len);
if (!index) {
return -1;
}
char device[32];
sprintf(&device[0], "dummy_udc.%llu", procid);
int rv = usb_raw_init(fd, speed, "dummy_udc", &device[0]);
if (rv < 0) {
return rv;
}
rv = usb_raw_run(fd);
if (rv < 0) {
return rv;
}
bool done = false;
while (!done) {
struct usb_raw_control_event event;
event.inner.type = 0;
event.inner.length = sizeof(event.ctrl);
rv = usb_raw_event_fetch(fd, (struct usb_raw_event*)&event);
if (rv < 0) {
return rv;
}
if (event.inner.type != USB_RAW_EVENT_CONTROL)
continue;
char* response_data = NULL;
uint32_t response_length = 0;
struct usb_qualifier_descriptor qual;
if (event.ctrl.bRequestType & USB_DIR_IN) {
if (!lookup_connect_response_in(fd, descs, &event.ctrl, &qual, &response_data, &response_length)) {
usb_raw_ep0_stall(fd);
continue;
}
} else {
if (!lookup_connect_response_out(fd, descs, &event.ctrl, &done)) {
usb_raw_ep0_stall(fd);
continue;
}
response_data = NULL;
response_length = event.ctrl.wLength;
}
if ((event.ctrl.bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD &&
event.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) {
rv = configure_device(fd);
if (rv < 0) {
return rv;
}
}
struct usb_raw_ep_io_data response;
response.inner.ep = 0;
response.inner.flags = 0;
if (response_length > sizeof(response.data))
response_length = 0;
if (event.ctrl.wLength < response_length)
response_length = event.ctrl.wLength;
response.inner.length = response_length;
if (response_data)
memcpy(&response.data[0], response_data, response_length);
else
memset(&response.data[0], 0, response_length);
if (event.ctrl.bRequestType & USB_DIR_IN) {
rv = usb_raw_ep0_write(fd, (struct usb_raw_ep_io*)&response);
} else {
rv = usb_raw_ep0_read(fd, (struct usb_raw_ep_io*)&response);
}
if (rv < 0) {
return rv;
}
}
sleep_ms(200);
return fd;
}
static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3)
{
uint64_t speed = a0;
uint64_t dev_len = a1;
const char* dev = (const char*)a2;
const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3;
return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic);
}
int main(void)
{
syscall(__NR_mmap, /*addr=*/0x7ffff000, /*len=*/0x1000, /*prot=*/0, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
syscall(__NR_mmap, /*addr=*/0x80000000, /*len=*/0x1000000, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/7, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
syscall(__NR_mmap, /*addr=*/0x81000000, /*len=*/0x1000, /*prot=*/0, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
const char* reason;
(void)reason;
if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {}
// syz_usb_connect arguments: [
// speed: usb_device_speed = 0x0 (4 bytes)
// dev_len: len = 0x32 (4 bytes)
// dev: ptr[inout, array[ANYUNION]] {
// array[ANYUNION] {
// union ANYUNION {
// ANYBLOB: buffer: {12 01 00 02 00 00 00 40 7f 18 00 01 00 00 00 00 00 01 09 02 20 00 01 01 00 80 32 09 04 00 00 02 ff 00 00 00 07 05 81 02 40 00 00 07 05 02 02 40 00 00} (length 0x32)
// }
// }
// }
// conn_descs: nil
// ]
// returns fd_usb
memcpy((void*)0x80000000, "... [truncated large byte array] ...", 50);
syz_usb_connect(/*speed=*/0, /*dev_len=*/0x32, /*dev=*/0x80000000, /*conn_descs=*/0);
return 0;
}
Search for the commit that introduced this bug.
Reply: The bug was introduced by commit **`564246fd3ff41afd3234c18ae28bc4ad9d815f79`** (`media: siano: Fix coherent memory allocation failure on arm64`).
### Root Cause Analysis
1. **Reproducer Device & Probe Path:**
The reproducer connects a virtual USB device via `/dev/raw-gadget` with `idVendor = 0x187f` and `idProduct = 0x0100`, matching `SMS1XXX_BOARD_SIANO_STELLAR` in `drivers/media/usb/siano/smsusb.c` (`smsusb_id_table`).
- `smsusb_probe()` calls `smsusb_init_device()`.
- `smsusb_init_device()` calls `smscore_register_device()` (`drivers/media/common/siano/smscoreapi.c`), which allocates a single large `common_buffer` via `kzalloc(dev->common_buffer_size, GFP_KERNEL)` (`USB1_BUFFER_SIZE * MAX_BUFFERS = 0x1000 * 50 = 204800` bytes, an order-6 large kmalloc allocation) and slices it into 50 `struct smscore_buffer_t` entries (`cb->p = common_buffer + i * 0x1000`).
- `smsusb_init_device()` allocates 10 URBs (`dev->surbs[i].urb = usb_alloc_urb(0, GFP_KERNEL)`) and calls `smsusb_start_streaming()` -> `smsusb_submit_urb()`.
- In `smsusb_submit_urb()` (`drivers/media/usb/siano/smsusb.c:171`), each URB's `transfer_buffer` is set to `surb->cb->p`, and **`surb->urb->transfer_flags |= URB_FREE_BUFFER;`** is set.
2. **Disconnect & Invalid `kfree()`:**
When the reproducer exits and closes `/dev/raw-gadget`, the virtual USB device disconnects (`kworker/0:1` running `hub_event` -> `smsusb_disconnect()` -> `smsusb_term_device()`):
- `smsusb_stop_streaming(dev)` kills the URBs and returns `surb->cb` to `coredev->buffers` (leaving `surb->urb->transfer_buffer` pointing into `common_buffer` and `URB_FREE_BUFFER` still set).
- `smscore_unregister_device(dev->coredev)` frees `coredev->common_buffer` via `kfree(coredev->common_buffer)` (`drivers/media/common/siano/smscoreapi.c:1228`), which calls `free_large_kmalloc()`, clears `PageLargeKmalloc`, and returns the order-6 pages to the page allocator.
- `smsusb_term_device()` then loops over `MAX_URBS` and calls `usb_free_urb(dev->surbs[i].urb)` (`drivers/media/usb/siano/smsusb.c:352`).
- `usb_free_urb()` drops the final `kref` and invokes `urb_destroy()` (`drivers/usb/core/urb.c:20`), which checks `if (urb->transfer_flags & URB_FREE_BUFFER) kfree(urb->transfer_buffer);`.
- Calling `kfree()` on `common_buffer` (already freed) and interior pointers `common_buffer + i * 0x1000` (tail pages) enters `free_large_kmalloc()` (`mm/slub.c:6855`) and triggers `WARN_ON_ONCE(!PageLargeKmalloc(page))`.
### Git History & Commit Selection
- **`564246fd3ff4` (`media: siano: Fix coherent memory allocation failure on arm64`) β Primary bug-introducing commit:**
Changed USB `common_buffer` allocation in `smscore_register_device()` from `dma_alloc_coherent()` to `kzalloc()`, added `kfree(coredev->common_buffer)` in `smscore_unregister_device()`, and erroneously added `surb->urb.transfer_flags |= URB_FREE_BUFFER;` in `smsusb_submit_urb()`. Setting `URB_FREE_BUFFER` violates the `URB_FREE_BUFFER` contract because `surb->cb->p` is neither owned by the URB nor an independent `kmalloc()` allocation (for `i > 0` it is an interior pointer into `common_buffer`, and `common_buffer` is already freed by `smscore_unregister_device()`).
- **Related commits (not blamed as primary cause):**
- **`836e2548524d` (`media: usb: siano: Fix allocation of urbs`):** Converted `surb->urb` from an embedded `struct urb` initialized with `usb_init_urb()` (where `usb_free_urb()` was never called, masking the bug) to `usb_alloc_urb()`/`usb_free_urb()` to fix a smatch flexible-array warning. Calling `usb_free_urb()` exposed the pre-existing `URB_FREE_BUFFER` bug from `564246fd3ff4`.
- **`a6687c8ff613` (`slab: Mark large folios for debugging purposes`)** and **`0bdfdd6a05aa` (`slab: Remove folio references from free_large_kmalloc()`):** Added and refactored the `WARN_ON_ONCE(!PageLargeKmalloc(page))` sanity check in `free_large_kmalloc()` that catches this double/invalid free cleanly.
|
| 7/2 |
2026/11/24 09:37 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 10889
output: 81
thoughts: 535
Thoughts: **Investigating the Kernel Crash and USB Descriptor**
My immediate thought upon seeing this crash report, specifically the `!PageLargeKmalloc(page)` warning originating from `mm/slub.c:6855` in `free_large_kmalloc`, is that we're dealing with a memory management issue related to large kmalloc allocations, and it's occurring during a free operation. The context provided by the reproducer, `syz_usb_connect` along with a specific USB device descriptor, strongly suggests that this memory corruption is tied to USB device handling.
So, the first thing I'm doing is meticulously examining both the crash report and the reproducer. The USB descriptor itself is a crucial piece of information. I'm breaking it down byte by byte to fully understand the properties of the emulated USB device.
Specifically, I'm decoding:
* The `bLength` (0x12) and `bDescriptorType` (0x01) to confirm it's a standard device descriptor.
* The USB specification version (`bcdUSB` = 0x0200).
* The device class, subclass, and protocol (`bDeviceClass` = 0x00, `bDeviceSubClass` = 0x00, `bDeviceProtocol` = 0x00) indicating a generic or vendor-specific device.
* The endpoint 0 maximum packet size (`bMaxPacketSize0` = 0x40, which is 64 bytes).
* The Vendor ID (`idVendor` = 0x187f) and Product ID (`idProduct` = 0x0100). It's important to note the little-endian representation and convert them correctly. I'm also making a mental note of the Vendor ID β "Siano Mobile Silicon!" β as this might provide a clue about the kernel module or driver involved.
* The device release number (`bcdDevice` = 0x0000).
* The string descriptor indices (`iManufacturer`, `iProduct`, `iSerialNumber` all 0) and the number of configurations (`bNumConfigurations` = 1).
My next logical step, armed with this decoded descriptor, is to dive deep into the kernel source code. I'll be focusing on `mm/slub.c` at line 6855, understanding the `free_large_kmalloc` function's logic. More importantly, I need to find where in the kernel code this specific USB Vendor ID (`0x187f`) and Product ID (`0x0100`) are being processed, particularly in relation to USB device connection and descriptor parsing. This will likely lead me to the relevant USB driver code where the memory allocation or deallocation that's causing the `!PageLargeKmalloc` issue is occurring.
|
| 8/2 |
2026/11/24 09:37 |
tool |
codesearch-definition-source |
0m
Args: map[Name:free_large_kmalloc]
Results: map[SourceCode:6851: static void free_large_kmalloc(struct page *page, void *object)
6852: {
6853: unsigned int order = compound_order(page);
6854:
6855: if (WARN_ON_ONCE(!PageLargeKmalloc(page))) {
6856: dump_page(page, "Not a kmalloc allocation");
6857: return;
6858: }
6859:
6860: if (WARN_ON_ONCE(order == 0))
6861: pr_warn_once("object pointer: 0x%p\n", object);
6862:
6863: kmemleak_free(object);
6864: kasan_kfree_large(object);
6865: kmsan_kfree_large(object);
6866:
6867: mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
6868: -(PAGE_SIZE << order));
6869: __ClearPageLargeKmalloc(page);
6870: free_frozen_pages(page, order);
6871: }
SourceFile:mm/slub.c]
|
| 9/2 |
2026/11/24 09:37 |
tool |
grepper |
0m
Args:
Results: map[Output:crypto/streebog_generic.c=29=static const struct streebog_uint512 C[12] = {
--
crypto/streebog_generic.c-84- cpu_to_le64(0xcffaa6b71c9ab7b4ULL),
crypto/streebog_generic.c:85: cpu_to_le64(0x187f9ab49af08ec6ULL),
crypto/streebog_generic.c-86- cpu_to_le64(0x2d66c4f95142a46cULL),
--
drivers/gpu/drm/amd/include/asic_reg/dce/dce_12_0_offset.h-10029-#define mmDIG0_DIG_FE_CNTL_BASE_IDX 2
drivers/gpu/drm/amd/include/asic_reg/dce/dce_12_0_offset.h:10030:#define mmDIG0_DIG_OUTPUT_CRC_CNTL 0x187f
drivers/gpu/drm/amd/include/asic_reg/dce/dce_12_0_offset.h-10031-#define mmDIG0_DIG_OUTPUT_CRC_CNTL_BASE_IDX 2
--
drivers/gpu/drm/amd/include/asic_reg/gc/gc_10_1_0_offset.h-3940-#define mmGCEA_IO_RD_CLI2GRP_MAP1_BASE_IDX 0
drivers/gpu/drm/amd/include/asic_reg/gc/gc_10_1_0_offset.h:3941:#define mmGCEA_IO_WR_CLI2GRP_MAP0 0x187f
drivers/gpu/drm/amd/include/asic_reg/gc/gc_10_1_0_offset.h-3942-#define mmGCEA_IO_WR_CLI2GRP_MAP0_BASE_IDX 0
--
drivers/gpu/drm/amd/include/asic_reg/gc/gc_10_3_0_offset.h-3751-#define mmGCEA_IO_RD_CLI2GRP_MAP1_BASE_IDX 0
drivers/gpu/drm/amd/include/asic_reg/gc/gc_10_3_0_offset.h:3752:#define mmGCEA_IO_WR_CLI2GRP_MAP0 0x187f
drivers/gpu/drm/amd/include/asic_reg/gc/gc_10_3_0_offset.h-3753-#define mmGCEA_IO_WR_CLI2GRP_MAP0_BASE_IDX 0
--
drivers/gpu/drm/amd/include/asic_reg/gc/gc_11_0_0_offset.h-2481-#define regGCEA_IO_RD_CLI2GRP_MAP1_BASE_IDX 0
drivers/gpu/drm/amd/include/asic_reg/gc/gc_11_0_0_offset.h:2482:#define regGCEA_IO_WR_CLI2GRP_MAP0 0x187f
drivers/gpu/drm/amd/include/asic_reg/gc/gc_11_0_0_offset.h-2483-#define regGCEA_IO_WR_CLI2GRP_MAP0_BASE_IDX 0
--
drivers/gpu/drm/amd/include/asic_reg/gc/gc_11_0_3_offset.h-2589-#define regGCEA_IO_RD_CLI2GRP_MAP1_BASE_IDX 0
drivers/gpu/drm/amd/include/asic_reg/gc/gc_11_0_3_offset.h:2590:#define regGCEA_IO_WR_CLI2GRP_MAP0 0x187f
drivers/gpu/drm/amd/include/asic_reg/gc/gc_11_0_3_offset.h-2591-#define regGCEA_IO_WR_CLI2GRP_MAP0_BASE_IDX 0
--
drivers/gpu/drm/amd/include/asic_reg/gc/gc_11_5_0_offset.h-1598-#define regGCEA_IO_RD_CLI2GRP_MAP1_BASE_IDX 0
drivers/gpu/drm/amd/include/asic_reg/gc/gc_11_5_0_offset.h:1599:#define regGCEA_IO_WR_CLI2GRP_MAP0 0x187f
drivers/gpu/drm/amd/include/asic_reg/gc/gc_11_5_0_offset.h-1600-#define regGCEA_IO_WR_CLI2GRP_MAP0_BASE_IDX 0
--
drivers/media/i2c/hi556.c=125=static const struct hi556_reg mipi_data_rate_874mbps[] = {
--
drivers/media/i2c/hi556.c-255- {0x3000, 0x60f8},
drivers/media/i2c/hi556.c:256: {0x3002, 0x187f},
drivers/media/i2c/hi556.c-257- {0x3004, 0x7060},
--
drivers/media/i2c/hi556.c-265- {0x3014, 0x60f8},
drivers/media/i2c/hi556.c:266: {0x3016, 0x187f},
drivers/media/i2c/hi556.c-267- {0x3018, 0x7060},
--
drivers/media/usb/siano/smsusb.c=629=static const struct usb_device_id smsusb_id_table[] = {
drivers/media/usb/siano/smsusb.c-630- /* This device is only present before firmware load */
drivers/media/usb/siano/smsusb.c:631: { USB_DEVICE(0x187f, 0x0010),
drivers/media/usb/siano/smsusb.c-632- .driver_info = SMS1XXX_BOARD_SIANO_STELLAR_ROM },
drivers/media/usb/siano/smsusb.c-633- /* This device pops up after firmware load */
drivers/media/usb/siano/smsusb.c:634: { USB_DEVICE(0x187f, 0x0100),
drivers/media/usb/siano/smsusb.c-635- .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
drivers/media/usb/siano/smsusb.c-636-
drivers/media/usb/siano/smsusb.c:637: { USB_DEVICE(0x187f, 0x0200),
drivers/media/usb/siano/smsusb.c-638- .driver_info = SMS1XXX_BOARD_SIANO_NOVA_A },
drivers/media/usb/siano/smsusb.c:639: { USB_DEVICE(0x187f, 0x0201),
drivers/media/usb/siano/smsusb.c-640- .driver_info = SMS1XXX_BOARD_SIANO_NOVA_B },
drivers/media/usb/siano/smsusb.c:641: { USB_DEVICE(0x187f, 0x0300),
drivers/media/usb/siano/smsusb.c-642- .driver_info = SMS1XXX_BOARD_SIANO_VEGA },
--
drivers/media/usb/siano/smsusb.c-692- .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
drivers/media/usb/siano/smsusb.c:693: { USB_DEVICE(0x187f, 0x0202),
drivers/media/usb/siano/smsusb.c-694- .driver_info = SMS1XXX_BOARD_SIANO_NICE },
drivers/media/usb/siano/smsusb.c:695: { USB_DEVICE(0x187f, 0x0301),
drivers/media/usb/siano/smsusb.c-696- .driver_info = SMS1XXX_BOARD_SIANO_VENICE },
drivers/media/usb/siano/smsusb.c:697: { USB_DEVICE(0x187f, 0x0302),
drivers/media/usb/siano/smsusb.c-698- .driver_info = SMS1XXX_BOARD_SIANO_VENICE },
drivers/media/usb/siano/smsusb.c:699: { USB_DEVICE(0x187f, 0x0310),
drivers/media/usb/siano/smsusb.c-700- .driver_info = SMS1XXX_BOARD_SIANO_MING },
drivers/media/usb/siano/smsusb.c:701: { USB_DEVICE(0x187f, 0x0500),
drivers/media/usb/siano/smsusb.c-702- .driver_info = SMS1XXX_BOARD_SIANO_PELE },
drivers/media/usb/siano/smsusb.c:703: { USB_DEVICE(0x187f, 0x0600),
drivers/media/usb/siano/smsusb.c-704- .driver_info = SMS1XXX_BOARD_SIANO_RIO },
drivers/media/usb/siano/smsusb.c:705: { USB_DEVICE(0x187f, 0x0700),
drivers/media/usb/siano/smsusb.c-706- .driver_info = SMS1XXX_BOARD_SIANO_DENVER_2160 },
drivers/media/usb/siano/smsusb.c:707: { USB_DEVICE(0x187f, 0x0800),
drivers/media/usb/siano/smsusb.c-708- .driver_info = SMS1XXX_BOARD_SIANO_DENVER_1530 },
--
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h=871=extern const u32 dmae_reg_go_c[];
--
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h-2496- */
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h:2497:#define NIG_REG_P0_TLLH_PTP_PARAM_MASK 0x187f0
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h-2498-/* [RW 14] Mask register for the rules used in detecting PTP packets. Set
--
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h-2506- */
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h:2507:#define NIG_REG_P0_TLLH_PTP_RULE_MASK 0x187f4
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h-2508-/* [RW 19] Packet TimeSync information that is buffered in 1-deep FIFOs for
--
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h-2532- */
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h:2533:#define NIG_REG_P1_TLLH_PTP_PARAM_MASK 0x187f8
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h-2534-/* [RW 14] Mask register for the rules used in detecting PTP packets. Set
--
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h-2542- */
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h:2543:#define NIG_REG_P1_TLLH_PTP_RULE_MASK 0x187fc
drivers/net/ethernet/broadcom/bnx2x/bnx2x_reg.h-2544-/* [RW 32] Specify which of the credit registers the client is to be mapped
]
|
| 10/2 |
2026/11/24 09:37 |
tool |
git-log |
0m
Args: map[Count:10 PathPrefix:mm/slub.c]
Results: map[Output:d15d7703166d mm/slub: initialize the free pointer of the bootstrap kmem_cache_node
af2fb3ee6679 mm/slab: do not wake up kswapd in __kfree_rcu_sheaf()
1b87ea06a34b mm/slub: refill prefilled sheaves from the barn
6932405b8154 mm/slub: make the case handling in __slab_free() easier to follow
0710b29e51ef mm/slub: unify slub stat definitions to simplify metric additions
4a724bcf5d70 mm/slab: take n->list_lock in __slab_try_return_freelist() to avoid race
5541d8975841 mm/slab: disallow kfree_rcu_sheaf() on PREEMPT_RT again
874d785e89a6 mm/slab: reject unsupported kmalloc() sizes
a6172cca157f mm/slab, kfence, memcg: completely remove obj_ext for kfence objects
d4404b0f5b8b mm/slab: stop allocating objcg pointers when unnecessary
]
|
| 11/2 |
2026/11/24 09:37 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 15404
output: 159
thoughts: 168
Thoughts: **The Deep Dive into the Siano USB Driver Anomaly**
My thought process began with a jolt of recognition: I'd found a specific USB device entry, `USB_DEVICE(0x187f, 0x0100)`, within `drivers/media/usb/siano/smsusb.c`, explicitly identified as `SMS1XXX_BOARD_SIANO_STELLAR`. This was a key piece of the puzzle.
Immediately following this identification, a critical question arose, a true point of concern: why did the `free_large_kmalloc` function trigger a `WARN_ON_ONCE(!PageLargeKmalloc(page))` warning? This wasn't just a minor blip; it signaled a potential memory management issue, something that demands a thorough investigation given its implications for system stability and resource utilization.
My immediate next step was to cast a wider net and then progressively narrow the focus. I decided to explore the entire `drivers/media/usb/siano/` directory. This would allow me to understand the context of the `smsusb.c` file and its interactions with other components within the Siano USB driver. Simultaneously, I planned to perform a `git-log` on this directory. This historical perspective is invaluable for understanding how changes have been made over time, potentially revealing the origin of the problematic behavior.
In parallel, I expanded my scope to include `drivers/media/common/siano/`. This suggests a belief that the issue might not be isolated to the USB-specific driver but could stem from a common Siano media component. Examining this broader area would help me identify any shared code or dependencies that might be contributing to the warning.
Finally, my investigation would culminate in a focused reading of `drivers/media/usb/siano/smsusb.c` itself. This is where the core logic resides, and I would be scrutinizing it for any patterns or anomalies that could explain the `free_large_kmalloc` warning. Concurrently, I would be looking at recent commits specifically related to the `siano` codebase, or even more broadly, to `kfree` and `free_large_kmalloc` in general. This targeted search in the commit history is crucial. It's highly probable that a recent change, perhaps a seemingly innocuous modification to memory handling or a driver update, is the root cause of this warning. I need to pinpoint that specific commit to understand what changed and why it's now leading to this unexpected behavior.
|
| 12/2 |
2026/11/24 09:37 |
tool |
codesearch-dir-index |
0m
Args: map[Dir:drivers/media/usb/siano]
Results: map[Files:[smsusb.c] Subdirs:<nil>]
|
| 13/2 |
2026/11/24 09:38 |
tool |
codesearch-dir-index |
0m
Args: map[Dir:drivers/media/common/siano]
Results: map[Files:[sms-cards.c sms-cards.h smscoreapi.c smscoreapi.h smsdvb-debugfs.c smsdvb-main.c smsdvb.h smsendian.c smsendian.h smsir.c smsir.h] Subdirs:<nil>]
|
| 14/2 |
2026/11/24 09:38 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:drivers/media/usb/siano]
Results: map[Output:bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
836e2548524d media: usb: siano: Fix allocation of urbs
6a57a21943da Normalise "name (ad@dr)" MODULE_AUTHORs to "name <ad@dr>"
16fae979ed6b media: usb: siano: Use kmemdup to simplify kmalloc and memcpy logic
b9c7141f3840 drivers: usb: smsusb: fix error handling code in smsusb_init_device
6f489a966fbe media: usb: siano: Fix warning due to null work_func_t function pointer
ebad8e731c1c media: usb: siano: Fix use after free bugs caused by do_submit_urb
051d3b5437af media: siano: remove duplicate USB device IDs
1771e9fb67e2 media: Use fallthrough pseudo-keyword
a47686636d84 media: smsusb: better handle optional alignment
45457c01171f media: usb: siano: Fix false-positive "uninitialized variable" warning
1ccea77e2a26 treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 13
ec8f24b7faaf treewide: Add SPDX license identifier - Makefile/Kconfig
31e0456de5be media: usb: siano: Fix general protection fault in smsusb
14340de506c9 media: prefix header search paths with $(srctree)/
b60a5b8dcf49 media: Kconfig files: use the right help coding style
3e4d8f48b9bf media: usb: fix several typos
0f4bb10857e2 media: siano: Use kmemdup instead of duplicating its function
782b9d201112 media: siano: use GFP_DMA only for smssdio
]
|
| 15/2 |
2026/11/24 09:38 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:drivers/media/common/siano]
Results: map[Output:dccc0c3ddf8f media: rc: fix race between unregister and urb/irq callbacks
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
97e8e73777ab media: siano: Remove redundant ternary operators
734ac57e47b3 media: siano: Fix error handling in smsdvb_module_init()
2c21fd53a1a0 media: siano: remove redundant null pointer checks in cec_devnode_init()
e7a9c987381a media: siano: Remove unused declarations
e7311aa47a6a media: siano: Simplify smscore_load_firmware_from_file
364ae46413f6 media: siano: Use flex arrays for sms_firmware
b657179a11d9 media: siano: Remove unused structures
6c69a73adf50 media: siano: Refactor struct sms_msg_data
6a57a21943da Normalise "name (ad@dr)" MODULE_AUTHORs to "name <ad@dr>"
8cd08e365153 media: siano: Drop unnecessary error check for debugfs_create_dir/file()
ff7ff3656465 drivers/media/common/siano/smsendian.c : eliminate unnecessary type conversions
2f7d0c94396e media: siano: Convert to use sysfs_emit_at() API
39ad5b4a5ae7 media: siano: use DEFINE_MUTEX() for mutex lock
5368b1ee2939 media: siano: fix device register error path
13dfead49db0 media: siano: Fix out-of-bounds warnings in smscore_load_firmware_family2()
ca8519ddc4d7 media: siano: use DEFINE_MUTEX() for mutex lock and LIST_HEAD for list head
c277077bab0d media: media/siano: Fix transmission parameters reporting for ISDB-T
]
|
| 16/2 |
2026/11/24 09:38 |
tool |
git-blame |
0m
Args: map[End:6871 File:mm/slub.c Start:6851]
Results: map[Output:0bdfdd6a05aa5 6851) static void free_large_kmalloc(struct page *page, void *object)
b774d3e326d30 6852) {
0bdfdd6a05aa5 6853) unsigned int order = compound_order(page);
b774d3e326d30 6854)
0bdfdd6a05aa5 6855) if (WARN_ON_ONCE(!PageLargeKmalloc(page))) {
0bdfdd6a05aa5 6856) dump_page(page, "Not a kmalloc allocation");
a6687c8ff613f 6857) return;
a6687c8ff613f 6858) }
a6687c8ff613f 6859)
b774d3e326d30 6860) if (WARN_ON_ONCE(order == 0))
b774d3e326d30 6861) pr_warn_once("object pointer: 0x%p\n", object);
b774d3e326d30 6862)
b774d3e326d30 6863) kmemleak_free(object);
b774d3e326d30 6864) kasan_kfree_large(object);
b774d3e326d30 6865) kmsan_kfree_large(object);
b774d3e326d30 6866)
0bdfdd6a05aa5 6867) mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
b774d3e326d30 6868) -(PAGE_SIZE << order));
0bdfdd6a05aa5 6869) __ClearPageLargeKmalloc(page);
0bdfdd6a05aa5 6870) free_frozen_pages(page, order);
b774d3e326d30 6871) }
]
|
| 17/2 |
2026/11/24 09:38 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 17545
output: 249
thoughts: 131
Thoughts: **Immediate Reflection on Siano USB Driver URB Allocation Fixes**
My immediate reaction is one of urgent curiosity triggered by a set of commits related to URB (USB Request Block) allocation in the `drivers/media/usb/siano/` directory. Specifically, commit `836e2548524d` titled "media: usb: siano: Fix allocation of urbs" has caught my eye. This is immediately followed by commits `69050f8d6d07` ("treewide: Replace kmalloc with kmalloc_obj for non-scalar types") and `bf4afc53b77a`. The conjunction of these changes, especially the explicit mention of fixing URB allocation alongside a broader change to `kmalloc_obj` for non-scalar types, strongly suggests a potential issue and subsequent improvement in how these crucial data structures are being managed.
Therefore, my next step is to dive directly into the code, starting with `drivers/media/usb/siano/smsusb.c`. I want to examine the specific code modifications introduced by `836e2548524d` and understand how the URB allocation was problematic. Concurrently, I'll be looking at the `smsusb.c` file in the vicinity of probe, init, free, and URB handling functions. This will allow me to trace the lifecycle of URBs from their acquisition to their release and see how the changes address any identified race conditions, potential memory leaks, or incorrect sizing. The broader commit regarding `kmalloc_obj` also makes me want to ensure that the URBs themselves are indeed non-scalar types, and if so, that this new allocation method is being correctly applied and is contributing to a more robust and safer allocation pattern. I'm anticipating seeing a cleaner, more reliable way of managing URBs, potentially resolving subtle bugs that might have existed previously.
|
| 18/2 |
2026/11/24 09:38 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 836e2548524d2dfcb5acaf3be78f203b6b4bde6f
Author: Ricardo Ribalda <ribalda@chromium.org>
Date: Mon Apr 15 12:48:13 2024 +0000
media: usb: siano: Fix allocation of urbs
USB urbs must be allocated with usb_alloc_urb. Quoting the manual
Only use this function (usb_init_urb) if you _really_ understand what you
are doing.
Fix the following smatch error:
drivers/media/usb/siano/smsusb.c:53:38: warning: array of flexible structures
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
diff --git a/drivers/media/usb/siano/smsusb.c b/drivers/media/usb/siano/smsusb.c
index 723510520d09..2c8179a84991 100644
--- a/drivers/media/usb/siano/smsusb.c
+++ b/drivers/media/usb/siano/smsusb.c
@@ -40,7 +40,7 @@ struct smsusb_urb_t {
struct smscore_buffer_t *cb;
struct smsusb_device_t *dev;
- struct urb urb;
+ struct urb *urb;
/* For the bottom half */
struct work_struct wq;
@@ -160,7 +160,7 @@ static int smsusb_submit_urb(struct smsusb_device_t *dev,
}
usb_fill_bulk_urb(
- &surb->urb,
+ surb->urb,
dev->udev,
usb_rcvbulkpipe(dev->udev, dev->in_ep),
surb->cb->p,
@@ -168,9 +168,9 @@ static int smsusb_submit_urb(struct smsusb_device_t *dev,
smsusb_onresponse,
surb
);
- surb->urb.transfer_flags |= URB_FREE_BUFFER;
+ surb->urb->transfer_flags |= URB_FREE_BUFFER;
- return usb_submit_urb(&surb->urb, GFP_ATOMIC);
+ return usb_submit_urb(surb->urb, GFP_ATOMIC);
}
static void smsusb_stop_streaming(struct smsusb_device_t *dev)
@@ -178,7 +178,7 @@ static void smsusb_stop_streaming(struct smsusb_device_t *dev)
int i;
for (i = 0; i < MAX_URBS; i++) {
- usb_kill_urb(&dev->surbs[i].urb);
+ usb_kill_urb(dev->surbs[i].urb);
if (dev->surbs[i].wq.func)
cancel_work_sync(&dev->surbs[i].wq);
@@ -338,6 +338,8 @@ static void smsusb_term_device(struct usb_interface *intf)
struct smsusb_device_t *dev = usb_get_intfdata(intf);
if (dev) {
+ int i;
+
dev->state = SMSUSB_DISCONNECTED;
smsusb_stop_streaming(dev);
@@ -346,6 +348,9 @@ static void smsusb_term_device(struct usb_interface *intf)
if (dev->coredev)
smscore_unregister_device(dev->coredev);
+ for (i = 0; i < MAX_URBS; i++)
+ usb_free_urb(dev->surbs[i].urb);
+
pr_debug("device 0x%p destroyed\n", dev);
kfree(dev);
}
@@ -463,7 +468,9 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)
/* initialize urbs */
for (i = 0; i < MAX_URBS; i++) {
dev->surbs[i].dev = dev;
- usb_init_urb(&dev->surbs[i].urb);
+ dev->surbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
+ if (!dev->surbs[i].urb)
+ goto err_unregister_device;
}
pr_debug("smsusb_start_streaming(...).\n");
@@ -486,6 +493,7 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)
return rc;
err_unregister_device:
+ /* smsusb_term_device() frees any allocated urb. */
smsusb_term_device(intf);
#ifdef CONFIG_MEDIA_CONTROLLER_DVB
media_device_unregister(mdev);
]
|
| 19/2 |
2026/11/24 09:38 |
tool |
git-show |
0m
Args: map[Commit:69050f8d6d07 File:drivers/media/usb/siano]
Results: map[Output:commit 69050f8d6d075dc01af7a5f2f550a8067510366f
Author: Kees Cook <kees@kernel.org>
Date: Fri Feb 20 23:49:23 2026 -0800
treewide: Replace kmalloc with kmalloc_obj for non-scalar types
This is the result of running the Coccinelle script from
scripts/coccinelle/api/kmalloc_objs.cocci. The script is designed to
avoid scalar types (which need careful case-by-case checking), and
instead replace kmalloc-family calls that allocate struct or union
object instances:
Single allocations: kmalloc(sizeof(TYPE), ...)
are replaced with: kmalloc_obj(TYPE, ...)
Array allocations: kmalloc_array(COUNT, sizeof(TYPE), ...)
are replaced with: kmalloc_objs(TYPE, COUNT, ...)
Flex array allocations: kmalloc(struct_size(PTR, FAM, COUNT), ...)
are replaced with: kmalloc_flex(*PTR, FAM, COUNT, ...)
(where TYPE may also be *VAR)
The resulting allocations no longer return "void *", instead returning
"TYPE *".
Signed-off-by: Kees Cook <kees@kernel.org>
diff --git a/drivers/media/usb/siano/smsusb.c b/drivers/media/usb/siano/smsusb.c
index 2c8179a84991..4d9cb4184ca2 100644
--- a/drivers/media/usb/siano/smsusb.c
+++ b/drivers/media/usb/siano/smsusb.c
@@ -367,7 +367,7 @@ static void *siano_media_device_register(struct smsusb_device_t *dev,
struct sms_board *board = sms_get_board(board_id);
int ret;
- mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
+ mdev = kzalloc_obj(*mdev, GFP_KERNEL);
if (!mdev)
return NULL;
@@ -397,7 +397,7 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)
int align = 0;
/* create device object */
- dev = kzalloc(sizeof(struct smsusb_device_t), GFP_KERNEL);
+ dev = kzalloc_obj(struct smsusb_device_t, GFP_KERNEL);
if (!dev)
return -ENOMEM;
]
|
| 20/2 |
2026/11/24 09:38 |
tool |
read-file |
0m
Args: map[File:drivers/media/usb/siano/smsusb.c FirstLine:1 LineCount:100]
Results: map[Contents: 1: // SPDX-License-Identifier: GPL-2.0-or-later
2: /****************************************************************
3:
4: Siano Mobile Silicon, Inc.
5: MDTV receiver kernel modules.
6: Copyright (C) 2005-2009, Uri Shkolnik, Anatoly Greenblat
7:
8:
9: ****************************************************************/
10:
11: #include "smscoreapi.h"
12:
13: #include <linux/kernel.h>
14: #include <linux/init.h>
15: #include <linux/usb.h>
16: #include <linux/firmware.h>
17: #include <linux/slab.h>
18: #include <linux/module.h>
19: #include <media/media-device.h>
20:
21: #include "sms-cards.h"
22: #include "smsendian.h"
23:
24: #define USB1_BUFFER_SIZE 0x1000
25: #define USB2_BUFFER_SIZE 0x2000
26:
27: #define MAX_BUFFERS 50
28: #define MAX_URBS 10
29:
30: struct smsusb_device_t;
31:
32: enum smsusb_state {
33: SMSUSB_DISCONNECTED,
34: SMSUSB_SUSPENDED,
35: SMSUSB_ACTIVE
36: };
37:
38: struct smsusb_urb_t {
39: struct list_head entry;
40: struct smscore_buffer_t *cb;
41: struct smsusb_device_t *dev;
42:
43: struct urb *urb;
44:
45: /* For the bottom half */
46: struct work_struct wq;
47: };
48:
49: struct smsusb_device_t {
50: struct usb_device *udev;
51: struct smscore_device_t *coredev;
52:
53: struct smsusb_urb_t surbs[MAX_URBS];
54:
55: int response_alignment;
56: int buffer_size;
57:
58: unsigned char in_ep;
59: unsigned char out_ep;
60: enum smsusb_state state;
61: };
62:
63: static int smsusb_submit_urb(struct smsusb_device_t *dev,
64: struct smsusb_urb_t *surb);
65:
66: /*
67: * Completing URB's callback handler - bottom half (process context)
68: * submits the URB prepared on smsusb_onresponse()
69: */
70: static void do_submit_urb(struct work_struct *work)
71: {
72: struct smsusb_urb_t *surb = container_of(work, struct smsusb_urb_t, wq);
73: struct smsusb_device_t *dev = surb->dev;
74:
75: smsusb_submit_urb(dev, surb);
76: }
77:
78: /*
79: * Completing URB's callback handler - top half (interrupt context)
80: * adds completing sms urb to the global surbs list and activtes the worker
81: * thread the surb
82: * IMPORTANT - blocking functions must not be called from here !!!
83:
84: * @param urb pointer to a completing urb object
85: */
86: static void smsusb_onresponse(struct urb *urb)
87: {
88: struct smsusb_urb_t *surb = (struct smsusb_urb_t *) urb->context;
89: struct smsusb_device_t *dev = surb->dev;
90:
91: if (urb->status == -ESHUTDOWN) {
92: pr_err("error, urb status %d (-ESHUTDOWN), %d bytes\n",
93: urb->status, urb->actual_length);
94: return;
95: }
96:
97: if ((urb->actual_length > 0) && (urb->status == 0)) {
98: struct sms_msg_hdr *phdr = (struct sms_msg_hdr *)surb->cb->p;
99:
100: smsendian_handle_message_header(phdr);
]
|
| 21/2 |
2026/11/24 09:38 |
tool |
read-file |
0m
Args: map[File:drivers/media/usb/siano/smsusb.c FirstLine:101 LineCount:100]
Results: map[Contents: 101: if (urb->actual_length >= phdr->msg_length) {
102: surb->cb->size = phdr->msg_length;
103:
104: if (dev->response_alignment &&
105: (phdr->msg_flags & MSG_HDR_FLAG_SPLIT_MSG)) {
106:
107: surb->cb->offset =
108: dev->response_alignment +
109: ((phdr->msg_flags >> 8) & 3);
110:
111: /* sanity check */
112: if (((int) phdr->msg_length +
113: surb->cb->offset) > urb->actual_length) {
114: pr_err("invalid response msglen %d offset %d size %d\n",
115: phdr->msg_length,
116: surb->cb->offset,
117: urb->actual_length);
118: goto exit_and_resubmit;
119: }
120:
121: /* move buffer pointer and
122: * copy header to its new location */
123: memcpy((char *) phdr + surb->cb->offset,
124: phdr, sizeof(struct sms_msg_hdr));
125: } else
126: surb->cb->offset = 0;
127:
128: pr_debug("received %s(%d) size: %d\n",
129: smscore_translate_msg(phdr->msg_type),
130: phdr->msg_type, phdr->msg_length);
131:
132: smsendian_handle_rx_message((struct sms_msg_data *) phdr);
133:
134: smscore_onresponse(dev->coredev, surb->cb);
135: surb->cb = NULL;
136: } else {
137: pr_err("invalid response msglen %d actual %d\n",
138: phdr->msg_length, urb->actual_length);
139: }
140: } else
141: pr_err("error, urb status %d, %d bytes\n",
142: urb->status, urb->actual_length);
143:
144:
145: exit_and_resubmit:
146: INIT_WORK(&surb->wq, do_submit_urb);
147: schedule_work(&surb->wq);
148: }
149:
150: static int smsusb_submit_urb(struct smsusb_device_t *dev,
151: struct smsusb_urb_t *surb)
152: {
153: if (!surb->cb) {
154: /* This function can sleep */
155: surb->cb = smscore_getbuffer(dev->coredev);
156: if (!surb->cb) {
157: pr_err("smscore_getbuffer(...) returned NULL\n");
158: return -ENOMEM;
159: }
160: }
161:
162: usb_fill_bulk_urb(
163: surb->urb,
164: dev->udev,
165: usb_rcvbulkpipe(dev->udev, dev->in_ep),
166: surb->cb->p,
167: dev->buffer_size,
168: smsusb_onresponse,
169: surb
170: );
171: surb->urb->transfer_flags |= URB_FREE_BUFFER;
172:
173: return usb_submit_urb(surb->urb, GFP_ATOMIC);
174: }
175:
176: static void smsusb_stop_streaming(struct smsusb_device_t *dev)
177: {
178: int i;
179:
180: for (i = 0; i < MAX_URBS; i++) {
181: usb_kill_urb(dev->surbs[i].urb);
182: if (dev->surbs[i].wq.func)
183: cancel_work_sync(&dev->surbs[i].wq);
184:
185: if (dev->surbs[i].cb) {
186: smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
187: dev->surbs[i].cb = NULL;
188: }
189: }
190: }
191:
192: static int smsusb_start_streaming(struct smsusb_device_t *dev)
193: {
194: int i, rc;
195:
196: for (i = 0; i < MAX_URBS; i++) {
197: rc = smsusb_submit_urb(dev, &dev->surbs[i]);
198: if (rc < 0) {
199: pr_err("smsusb_submit_urb(...) failed\n");
200: smsusb_stop_streaming(dev);
]
|
| 22/2 |
2026/11/24 09:38 |
tool |
read-file |
0m
Args: map[File:drivers/media/usb/siano/smsusb.c FirstLine:201 LineCount:100]
Results: map[Contents: 201: break;
202: }
203: }
204:
205: return rc;
206: }
207:
208: static int smsusb_sendrequest(void *context, void *buffer, size_t size)
209: {
210: struct smsusb_device_t *dev = (struct smsusb_device_t *) context;
211: struct sms_msg_hdr *phdr;
212: int dummy, ret;
213:
214: if (dev->state != SMSUSB_ACTIVE) {
215: pr_debug("Device not active yet\n");
216: return -ENOENT;
217: }
218:
219: phdr = kmemdup(buffer, size, GFP_KERNEL);
220: if (!phdr)
221: return -ENOMEM;
222:
223: pr_debug("sending %s(%d) size: %d\n",
224: smscore_translate_msg(phdr->msg_type), phdr->msg_type,
225: phdr->msg_length);
226:
227: smsendian_handle_tx_message((struct sms_msg_data *) phdr);
228: smsendian_handle_message_header((struct sms_msg_hdr *)phdr);
229: ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
230: phdr, size, &dummy, 1000);
231:
232: kfree(phdr);
233: return ret;
234: }
235:
236: static char *smsusb1_fw_lkup[] = {
237: "dvbt_stellar_usb.inp",
238: "dvbh_stellar_usb.inp",
239: "tdmb_stellar_usb.inp",
240: "none",
241: "dvbt_bda_stellar_usb.inp",
242: };
243:
244: static inline char *sms_get_fw_name(int mode, int board_id)
245: {
246: char **fw = sms_get_board(board_id)->fw;
247: return (fw && fw[mode]) ? fw[mode] : smsusb1_fw_lkup[mode];
248: }
249:
250: static int smsusb1_load_firmware(struct usb_device *udev, int id, int board_id)
251: {
252: const struct firmware *fw;
253: u8 *fw_buffer;
254: int rc, dummy;
255: char *fw_filename;
256:
257: if (id < 0)
258: id = sms_get_board(board_id)->default_mode;
259:
260: if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) {
261: pr_err("invalid firmware id specified %d\n", id);
262: return -EINVAL;
263: }
264:
265: fw_filename = sms_get_fw_name(id, board_id);
266:
267: rc = request_firmware(&fw, fw_filename, &udev->dev);
268: if (rc < 0) {
269: pr_warn("failed to open '%s' mode %d, trying again with default firmware\n",
270: fw_filename, id);
271:
272: fw_filename = smsusb1_fw_lkup[id];
273: rc = request_firmware(&fw, fw_filename, &udev->dev);
274: if (rc < 0) {
275: pr_warn("failed to open '%s' mode %d\n",
276: fw_filename, id);
277:
278: return rc;
279: }
280: }
281:
282: fw_buffer = kmemdup(fw->data, fw->size, GFP_KERNEL);
283: if (fw_buffer) {
284: rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2),
285: fw_buffer, fw->size, &dummy, 1000);
286:
287: pr_debug("sent %zu(%d) bytes, rc %d\n", fw->size, dummy, rc);
288:
289: kfree(fw_buffer);
290: } else {
291: pr_err("failed to allocate firmware buffer\n");
292: rc = -ENOMEM;
293: }
294: pr_debug("read FW %s, size=%zu\n", fw_filename, fw->size);
295:
296: release_firmware(fw);
297:
298: return rc;
299: }
300:
]
|
| 23/2 |
2026/11/24 09:38 |
tool |
read-file |
0m
Args: map[File:drivers/media/usb/siano/smsusb.c FirstLine:301 LineCount:100]
Results: map[Contents: 301: static void smsusb1_detectmode(void *context, int *mode)
302: {
303: char *product_string =
304: ((struct smsusb_device_t *) context)->udev->product;
305:
306: *mode = DEVICE_MODE_NONE;
307:
308: if (!product_string) {
309: product_string = "none";
310: pr_err("product string not found\n");
311: } else if (strstr(product_string, "DVBH"))
312: *mode = 1;
313: else if (strstr(product_string, "BDA"))
314: *mode = 4;
315: else if (strstr(product_string, "DVBT"))
316: *mode = 0;
317: else if (strstr(product_string, "TDMB"))
318: *mode = 2;
319:
320: pr_debug("%d \"%s\"\n", *mode, product_string);
321: }
322:
323: static int smsusb1_setmode(void *context, int mode)
324: {
325: struct sms_msg_hdr msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK,
326: sizeof(struct sms_msg_hdr), 0 };
327:
328: if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
329: pr_err("invalid firmware id specified %d\n", mode);
330: return -EINVAL;
331: }
332:
333: return smsusb_sendrequest(context, &msg, sizeof(msg));
334: }
335:
336: static void smsusb_term_device(struct usb_interface *intf)
337: {
338: struct smsusb_device_t *dev = usb_get_intfdata(intf);
339:
340: if (dev) {
341: int i;
342:
343: dev->state = SMSUSB_DISCONNECTED;
344:
345: smsusb_stop_streaming(dev);
346:
347: /* unregister from smscore */
348: if (dev->coredev)
349: smscore_unregister_device(dev->coredev);
350:
351: for (i = 0; i < MAX_URBS; i++)
352: usb_free_urb(dev->surbs[i].urb);
353:
354: pr_debug("device 0x%p destroyed\n", dev);
355: kfree(dev);
356: }
357:
358: usb_set_intfdata(intf, NULL);
359: }
360:
361: static void *siano_media_device_register(struct smsusb_device_t *dev,
362: int board_id)
363: {
364: #ifdef CONFIG_MEDIA_CONTROLLER_DVB
365: struct media_device *mdev;
366: struct usb_device *udev = dev->udev;
367: struct sms_board *board = sms_get_board(board_id);
368: int ret;
369:
370: mdev = kzalloc_obj(*mdev);
371: if (!mdev)
372: return NULL;
373:
374: media_device_usb_init(mdev, udev, board->name);
375:
376: ret = media_device_register(mdev);
377: if (ret) {
378: media_device_cleanup(mdev);
379: kfree(mdev);
380: return NULL;
381: }
382:
383: pr_info("media controller created\n");
384:
385: return mdev;
386: #else
387: return NULL;
388: #endif
389: }
390:
391: static int smsusb_init_device(struct usb_interface *intf, int board_id)
392: {
393: struct smsdevice_params_t params;
394: struct smsusb_device_t *dev;
395: void *mdev;
396: int i, rc;
397: int align = 0;
398:
399: /* create device object */
400: dev = kzalloc_obj(struct smsusb_device_t);
]
|
| 24/2 |
2026/11/24 09:38 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 25670
output: 168
thoughts: 219
Thoughts: **Wait a Second, I've Found Something Odd**
I was reviewing the code, and then it hit me β I need to re-examine this section. Specifically, I'm looking at line 171 of `drivers/media/usb/siano/smsusb.c`. The `usb_fill_bulk_urb` call there uses `surb->cb->p` as the buffer pointer.
And then I saw it: `surb->urb->transfer_flags |= URB_FREE_BUFFER;`. This is what really threw me off. Why are we setting `URB_FREE_BUFFER` here? It implies that the memory pointed to by `surb->cb->p` should be freed by the USB core after the transfer.
This raises a crucial question for me: where exactly is `surb->cb->p` allocated? What is its origin? To answer that, I need to trace back to `smscore_getbuffer` and dive into `smscoreapi.c`. I'm going to focus my attention on the `smscore_getbuffer` function and its related `smscore_create_buffer` in `drivers/media/common/siano/smscoreapi.c`. This seems to be the key to understanding how this buffer is managed and why `URB_FREE_BUFFER` is being applied.
|
| 25/2 |
2026/11/24 09:38 |
tool |
codesearch-definition-source |
0m
Args: map[Name:smscore_getbuffer]
Results: map[SourceCode:1637: struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev)
1638: {
1639: struct smscore_buffer_t *cb = NULL;
1640:
1641: wait_event(coredev->buffer_mng_waitq, (cb = get_entry(coredev)));
1642:
1643: return cb;
1644: }
SourceFile:drivers/media/common/siano/smscoreapi.c]
|
| 26/2 |
2026/11/24 09:38 |
tool |
codesearch-definition-source |
0m
Args: map[Name:smscore_createbuffer]
Results: map[SourceCode: 614: static struct
615: smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
616: dma_addr_t common_buffer_phys)
617: {
618: struct smscore_buffer_t *cb;
619:
620: cb = kzalloc_obj(*cb);
621: if (!cb)
622: return NULL;
623:
624: cb->p = buffer;
625: cb->offset_in_common = buffer - (u8 *) common_buffer;
626: if (common_buffer_phys)
627: cb->phys = common_buffer_phys + cb->offset_in_common;
628:
629: return cb;
630: }
SourceFile:drivers/media/common/siano/smscoreapi.c]
|
| 27/2 |
2026/11/24 09:38 |
tool |
codesearch-definition-source |
0m
Args: map[Name:smscore_register_device]
Results: map[SourceCode: 632: /*
633: * creates coredev object for a device, prepares buffers,
634: * creates buffer mappings, notifies registered hotplugs about new device.
635: *
636: * @param params device pointer to struct with device specific parameters
637: * and handlers
638: * @param coredev pointer to a value that receives created coredev object
639: *
640: * return: 0 on success, <0 on error.
641: */
642: int smscore_register_device(struct smsdevice_params_t *params,
643: struct smscore_device_t **coredev,
644: gfp_t gfp_buf_flags,
645: void *mdev)
646: {
647: struct smscore_device_t *dev;
648: u8 *buffer;
649:
650: dev = kzalloc_obj(*dev);
651: if (!dev)
652: return -ENOMEM;
653:
654: #ifdef CONFIG_MEDIA_CONTROLLER_DVB
655: dev->media_dev = mdev;
656: #endif
657: dev->gfp_buf_flags = gfp_buf_flags;
658:
659: /* init list entry so it could be safe in smscore_unregister_device */
660: INIT_LIST_HEAD(&dev->entry);
661:
662: /* init queues */
663: INIT_LIST_HEAD(&dev->clients);
664: INIT_LIST_HEAD(&dev->buffers);
665:
666: /* init locks */
667: spin_lock_init(&dev->clientslock);
668: spin_lock_init(&dev->bufferslock);
669:
670: /* init completion events */
671: init_completion(&dev->version_ex_done);
672: init_completion(&dev->data_download_done);
673: init_completion(&dev->data_validity_done);
674: init_completion(&dev->trigger_done);
675: init_completion(&dev->init_device_done);
676: init_completion(&dev->reload_start_done);
677: init_completion(&dev->resume_done);
678: init_completion(&dev->gpio_configuration_done);
679: init_completion(&dev->gpio_set_level_done);
680: init_completion(&dev->gpio_get_level_done);
681: init_completion(&dev->ir_init_done);
682:
683: /* Buffer management */
684: init_waitqueue_head(&dev->buffer_mng_waitq);
685:
686: /* alloc common buffer */
687: dev->common_buffer_size = params->buffer_size * params->num_buffers;
688: if (params->usb_device)
689: buffer = kzalloc(dev->common_buffer_size, GFP_KERNEL);
690: else
691: buffer = dma_alloc_coherent(params->device,
692: dev->common_buffer_size,
693: &dev->common_buffer_phys,
694: GFP_KERNEL | dev->gfp_buf_flags);
695: if (!buffer) {
696: smscore_unregister_device(dev);
697: return -ENOMEM;
698: }
699: dev->common_buffer = buffer;
700:
701: /* prepare dma buffers */
702: for (; dev->num_buffers < params->num_buffers;
703: dev->num_buffers++, buffer += params->buffer_size) {
704: struct smscore_buffer_t *cb;
705:
706: cb = smscore_createbuffer(buffer, dev->common_buffer,
707: dev->common_buffer_phys);
708: if (!cb) {
709: smscore_unregister_device(dev);
710: return -ENOMEM;
711: }
712:
713: smscore_putbuffer(dev, cb);
714: }
715:
716: pr_debug("allocated %d buffers\n", dev->num_buffers);
717:
718: dev->mode = DEVICE_MODE_NONE;
719: dev->board_id = SMS_BOARD_UNKNOWN;
720: dev->context = params->context;
721: dev->device = params->device;
722: dev->usb_device = params->usb_device;
723: dev->setmode_handler = params->setmode_handler;
724: dev->detectmode_handler = params->detectmode_handler;
725: dev->sendrequest_handler = params->sendrequest_handler;
726: dev->preload_handler = params->preload_handler;
727: dev->postload_handler = params->postload_handler;
728:
729: dev->device_flags = params->flags;
730: strscpy(dev->devpath, params->devpath, sizeof(dev->devpath));
731:
732: smscore_registry_settype(dev->devpath, params->device_type);
733:
734: /* add device to devices list */
735: mutex_lock(&g_smscore_deviceslock);
736: list_add(&dev->entry, &g_smscore_devices);
737: mutex_unlock(&g_smscore_deviceslock);
738:
739: *coredev = dev;
740:
741: pr_debug("device %p created\n", dev);
742:
743: return 0;
744: }
SourceFile:drivers/media/common/siano/smscoreapi.c]
|
| 28/2 |
2026/11/24 09:38 |
tool |
read-file |
0m
Args: map[File:drivers/media/usb/siano/smsusb.c FirstLine:401 LineCount:100]
Results: map[Contents: 401: if (!dev)
402: return -ENOMEM;
403:
404: memset(¶ms, 0, sizeof(params));
405: usb_set_intfdata(intf, dev);
406: dev->udev = interface_to_usbdev(intf);
407: dev->state = SMSUSB_DISCONNECTED;
408:
409: for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
410: struct usb_endpoint_descriptor *desc =
411: &intf->cur_altsetting->endpoint[i].desc;
412:
413: if (desc->bEndpointAddress & USB_DIR_IN) {
414: dev->in_ep = desc->bEndpointAddress;
415: align = usb_endpoint_maxp(desc) - sizeof(struct sms_msg_hdr);
416: } else {
417: dev->out_ep = desc->bEndpointAddress;
418: }
419: }
420:
421: pr_debug("in_ep = %02x, out_ep = %02x\n", dev->in_ep, dev->out_ep);
422: if (!dev->in_ep || !dev->out_ep || align < 0) { /* Missing endpoints? */
423: smsusb_term_device(intf);
424: return -ENODEV;
425: }
426:
427: params.device_type = sms_get_board(board_id)->type;
428:
429: switch (params.device_type) {
430: case SMS_STELLAR:
431: dev->buffer_size = USB1_BUFFER_SIZE;
432:
433: params.setmode_handler = smsusb1_setmode;
434: params.detectmode_handler = smsusb1_detectmode;
435: break;
436: case SMS_UNKNOWN_TYPE:
437: pr_err("Unspecified sms device type!\n");
438: fallthrough;
439: default:
440: dev->buffer_size = USB2_BUFFER_SIZE;
441: dev->response_alignment = align;
442:
443: params.flags |= SMS_DEVICE_FAMILY2;
444: break;
445: }
446:
447: params.device = &dev->udev->dev;
448: params.usb_device = dev->udev;
449: params.buffer_size = dev->buffer_size;
450: params.num_buffers = MAX_BUFFERS;
451: params.sendrequest_handler = smsusb_sendrequest;
452: params.context = dev;
453: usb_make_path(dev->udev, params.devpath, sizeof(params.devpath));
454:
455: mdev = siano_media_device_register(dev, board_id);
456:
457: /* register in smscore */
458: rc = smscore_register_device(¶ms, &dev->coredev, 0, mdev);
459: if (rc < 0) {
460: pr_err("smscore_register_device(...) failed, rc %d\n", rc);
461: goto err_unregister_device;
462: }
463:
464: smscore_set_board_id(dev->coredev, board_id);
465:
466: dev->coredev->is_usb_device = true;
467:
468: /* initialize urbs */
469: for (i = 0; i < MAX_URBS; i++) {
470: dev->surbs[i].dev = dev;
471: dev->surbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
472: if (!dev->surbs[i].urb)
473: goto err_unregister_device;
474: }
475:
476: pr_debug("smsusb_start_streaming(...).\n");
477: rc = smsusb_start_streaming(dev);
478: if (rc < 0) {
479: pr_err("smsusb_start_streaming(...) failed\n");
480: goto err_unregister_device;
481: }
482:
483: dev->state = SMSUSB_ACTIVE;
484:
485: rc = smscore_start_device(dev->coredev);
486: if (rc < 0) {
487: pr_err("smscore_start_device(...) failed\n");
488: goto err_unregister_device;
489: }
490:
491: pr_debug("device 0x%p created\n", dev);
492:
493: return rc;
494:
495: err_unregister_device:
496: /* smsusb_term_device() frees any allocated urb. */
497: smsusb_term_device(intf);
498: #ifdef CONFIG_MEDIA_CONTROLLER_DVB
499: media_device_unregister(mdev);
500: #endif
]
|
| 29/2 |
2026/11/24 09:38 |
tool |
read-file |
0m
Args: map[File:drivers/media/usb/siano/smsusb.c FirstLine:501 LineCount:130]
Results: map[Contents: 501: kfree(mdev);
502: return rc;
503: }
504:
505: static int smsusb_probe(struct usb_interface *intf,
506: const struct usb_device_id *id)
507: {
508: struct usb_device *udev = interface_to_usbdev(intf);
509: char devpath[32];
510: int i, rc;
511:
512: pr_info("board id=%lu, interface number %d\n",
513: id->driver_info,
514: intf->cur_altsetting->desc.bInterfaceNumber);
515:
516: if (sms_get_board(id->driver_info)->intf_num !=
517: intf->cur_altsetting->desc.bInterfaceNumber) {
518: pr_debug("interface %d won't be used. Expecting interface %d to popup\n",
519: intf->cur_altsetting->desc.bInterfaceNumber,
520: sms_get_board(id->driver_info)->intf_num);
521: return -ENODEV;
522: }
523:
524: if (intf->num_altsetting > 1) {
525: rc = usb_set_interface(udev,
526: intf->cur_altsetting->desc.bInterfaceNumber,
527: 0);
528: if (rc < 0) {
529: pr_err("usb_set_interface failed, rc %d\n", rc);
530: return rc;
531: }
532: }
533:
534: pr_debug("smsusb_probe %d\n",
535: intf->cur_altsetting->desc.bInterfaceNumber);
536: for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
537: pr_debug("endpoint %d %02x %02x %d\n", i,
538: intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
539: intf->cur_altsetting->endpoint[i].desc.bmAttributes,
540: intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
541: if (intf->cur_altsetting->endpoint[i].desc.bEndpointAddress &
542: USB_DIR_IN)
543: rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev,
544: intf->cur_altsetting->endpoint[i].desc.bEndpointAddress));
545: else
546: rc = usb_clear_halt(udev, usb_sndbulkpipe(udev,
547: intf->cur_altsetting->endpoint[i].desc.bEndpointAddress));
548: }
549: if ((udev->actconfig->desc.bNumInterfaces == 2) &&
550: (intf->cur_altsetting->desc.bInterfaceNumber == 0)) {
551: pr_debug("rom interface 0 is not used\n");
552: return -ENODEV;
553: }
554:
555: if (id->driver_info == SMS1XXX_BOARD_SIANO_STELLAR_ROM) {
556: /* Detected a Siano Stellar uninitialized */
557:
558: snprintf(devpath, sizeof(devpath), "usb\\%d-%s",
559: udev->bus->busnum, udev->devpath);
560: pr_info("stellar device in cold state was found at %s.\n",
561: devpath);
562: rc = smsusb1_load_firmware(
563: udev, smscore_registry_getmode(devpath),
564: id->driver_info);
565:
566: /* This device will reset and gain another USB ID */
567: if (!rc)
568: pr_info("stellar device now in warm state\n");
569: else
570: pr_err("Failed to put stellar in warm state. Error: %d\n",
571: rc);
572:
573: return rc;
574: } else {
575: rc = smsusb_init_device(intf, id->driver_info);
576: }
577:
578: pr_info("Device initialized with return code %d\n", rc);
579: sms_board_load_modules(id->driver_info);
580: return rc;
581: }
582:
583: static void smsusb_disconnect(struct usb_interface *intf)
584: {
585: smsusb_term_device(intf);
586: }
587:
588: static int smsusb_suspend(struct usb_interface *intf, pm_message_t msg)
589: {
590: struct smsusb_device_t *dev = usb_get_intfdata(intf);
591: printk(KERN_INFO "%s Entering status %d.\n", __func__, msg.event);
592: dev->state = SMSUSB_SUSPENDED;
593: /*smscore_set_power_mode(dev, SMS_POWER_MODE_SUSPENDED);*/
594: smsusb_stop_streaming(dev);
595: return 0;
596: }
597:
598: static int smsusb_resume(struct usb_interface *intf)
599: {
600: int rc, i;
]
|
| 30/2 |
2026/11/24 09:38 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 31431
output: 153
thoughts: 902
Thoughts: ** Eureka! A Critical Memory Management Flaw Identified! **
I've just had a "BINGO!" moment, staring at the `smscore_register_device` function. It's allocating a substantial chunk of memory, `dev->common_buffer_size = 0x1000 * 50 = 204800` bytes, which is a hefty ~200 KB, a significant allocation that registers as an order 6 in kernel terms. What's more, this single `kzalloc` is intended to serve multiple buffers.
Then, I see this loop:
for (; dev->num_buffers < params->num_buffers;
dev->num_buffers++, buffer += params->buffer_size) {
struct smscore_buffer_t *cb;
This is where it gets *really* interesting. For buffer index 0, `cb->p` is indeed `dev->common_buffer`. However, for all subsequent buffers (indices 1 through 49), `cb->p` is an offset *within* that initial `common_buffer`, like `dev->common_buffer + i * 0x1000`. These are not separate allocations; they are interior pointers into the one large, `kzalloc`'d block. Crucially, I noticed that even buffer 0, the very start of this allocated block, is slated for freeing in `smscore_unregister_device`.
Now, I'm looking at `smsusb_submit_urb` and a specific line:
usb_fill_bulk_urb(
surb->urb,
dev->udev,
usb_rcvbulkpipe(dev->udev, dev->in_ep),
surb->cb->p, // THIS IS THE KEY!
dev->buffer_size,
smsusb_onresponse,
surb
);
surb->urb->transfer_flags |= URB_FREE_BUFFER;
The problem emerges when `URB_FREE_BUFFER` is set. When the USB core eventually calls `usb_free_urb` (which happens when the `usb_put_urb` refcount hits zero, perhaps during `smsusb_term_device` or after the USB HCD gives back the URB), it checks this flag. If set, it attempts to `kfree(urb->transfer_buffer)`.
My concern is what happens here. Let's trace it: When `surb->cb` points to buffer index 1 (meaning `surb->cb->p` is `dev->common_buffer + 0x1000`), the USB core will attempt to `kfree` a pointer *into the middle* of the large compound page allocated by `kzalloc`.
I need to confirm the exact behavior of `kfree` when given an interior pointer to a compound allocation. The `virt_to_folio` call within `kfree` should, in theory, retrieve the head page of the compound page. However, if the first buffer (index 0) has already been processed and its `smscore_putbuffer` or subsequent freeing action has already cleared the `PageLargeKmalloc` flag on the head page (via `__ClearPageLargeKmalloc`), then subsequent calls to `kfree` on offsets within that same compound page might fail to recognize it as part of a large allocation.
This leads to a potential double-free or, more accurately, an attempt to free memory that has already been freed or is being managed by another part of the system. The sequence is critical: if buffer 0 is freed *first*, its `kfree` might correctly identify and free the entire compound page. But then, when buffer 1 (or any subsequent buffer) is processed and its `kfree` is called on `dev->common_buffer + 0x1000`, the `PageLargeKmalloc` flag on the head page might already be cleared. `kfree` would then likely treat this as a regular, non-compound allocation and attempt to free it, leading to a crash or corrupted memory.
The critical question is: did this bug manifest in the past, or is it a latent issue that could be triggered under specific timing conditions? I need to dive into `git blame` for `URB_FREE_BUFFER` in `smsusb.c` and meticulously examine `urb_destroy` and `kfree`'s internal logic regarding compound page management. This is a serious memory corruption vulnerability.
|
| 31/2 |
2026/11/24 09:38 |
tool |
git-blame |
0m
Args: map[End:175 File:drivers/media/usb/siano/smsusb.c Start:150]
Results: map[Output:0c071f374f66f drivers/media/dvb/siano/smsusb.c 150) static int smsusb_submit_urb(struct smsusb_device_t *dev,
0c071f374f66f drivers/media/dvb/siano/smsusb.c 151) struct smsusb_urb_t *surb)
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 152) {
822374165d6b1 drivers/media/dvb/siano/smsusb.c 153) if (!surb->cb) {
dd47fbd40e6ea drivers/media/usb/siano/smsusb.c 154) /* This function can sleep */
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 155) surb->cb = smscore_getbuffer(dev->coredev);
822374165d6b1 drivers/media/dvb/siano/smsusb.c 156) if (!surb->cb) {
5ed0a2c7eca32 drivers/media/usb/siano/smsusb.c 157) pr_err("smscore_getbuffer(...) returned NULL\n");
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 158) return -ENOMEM;
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 159) }
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 160) }
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 161)
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 162) usb_fill_bulk_urb(
836e2548524d2 drivers/media/usb/siano/smsusb.c 163) surb->urb,
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 164) dev->udev,
05f0ffbc48751 drivers/media/usb/siano/smsusb.c 165) usb_rcvbulkpipe(dev->udev, dev->in_ep),
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 166) surb->cb->p,
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 167) dev->buffer_size,
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 168) smsusb_onresponse,
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 169) surb
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 170) );
836e2548524d2 drivers/media/usb/siano/smsusb.c 171) surb->urb->transfer_flags |= URB_FREE_BUFFER;
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 172)
836e2548524d2 drivers/media/usb/siano/smsusb.c 173) return usb_submit_urb(surb->urb, GFP_ATOMIC);
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 174) }
2e5c1ec8865ab drivers/media/mdtv/smsusb.c 175)
]
|
| 32/2 |
2026/11/24 09:38 |
tool |
git-log |
0m
Error:
Args: map[PathPrefix:drivers/media/usb/siano/smsusb.c SourcePath:drivers/media/usb/siano/smsusb.c SymbolName:smsusb_submit_urb]
Results:
|
| 33/2 |
2026/11/24 09:38 |
tool |
codesearch-definition-source |
0m
Args:
Results: map[SourceCode: 20: static void urb_destroy(struct kref *kref)
21: {
22: struct urb *urb = to_urb(kref);
23:
24: if (urb->transfer_flags & URB_FREE_BUFFER)
25: kfree(urb->transfer_buffer);
26:
27: kfree(urb);
28: }
SourceFile:drivers/usb/core/urb.c]
|
| 34/2 |
2026/11/24 09:38 |
tool |
codesearch-definition-source |
0m
Args:
Results: map[SourceCode:6896: /**
6897: * kfree - free previously allocated memory
6898: * @object: pointer returned by kmalloc(), kmalloc_nolock(), or kmem_cache_alloc()
6899: *
6900: * If @object is NULL, no operation is performed.
6901: */
6902: void kfree(const void *object)
6903: {
6904: struct page *page;
6905: struct slab *slab;
6906: struct kmem_cache *s;
6907: void *x = (void *)object;
6908:
6909: trace_kfree(_RET_IP_, object);
6910:
6911: if (unlikely(ZERO_OR_NULL_PTR(object)))
6912: return;
6913:
6914: page = virt_to_page(object);
6915: slab = page_slab(page);
6916: if (!slab) {
6917: /* kmalloc_nolock() doesn't support large kmalloc */
6918: free_large_kmalloc(page, (void *)object);
6919: return;
6920: }
6921:
6922: s = slab->slab_cache;
6923: slab_free(s, slab, x, _RET_IP_);
6924: }
SourceFile:mm/slub.c]
|
| 35/2 |
2026/11/24 09:38 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 34085
output: 135
thoughts: 961
Thoughts: **A Deep Dive into URB Management and Memory Issues in the Siano Driver**
WHOOOOOOOOOOOOOA! I've just stumbled upon a rather alarming situation within the Siano USB driver, and I need to trace this carefully, examining `git-blame`, the `urb_destroy` function, `kfree`, and specifically commit `836e2548524d`. This is a significant memory leak and potential corruption waiting to happen.
Let's go back in time, before commit `836e2548524d` ("media: usb: siano: Fix allocation of urbs"). In that era, `surb->urb` was directly embedded within `struct smsusb_urb_t` as `struct urb urb;`, and `usb_init_urb(&dev->surbs[i].urb)` was used for initialization. Now, I need to understand exactly what `usb_init_urb()` and its counterpart, `usb_free_urb()`, do.
Upon investigation, specifically within `smsusb_term_device`, it becomes horrifyingly clear: before commit `836e2548524d`, `usb_free_urb` was *never* called on `&dev->surbs[i].urb`! This is a critical oversight.
Here's the kicker regarding `kref` counts: When `usb_init_urb` was called, it set the `kref` of `surb->urb` to 1. Then, `usb_submit_urb` would call `usb_get_urb`, incrementing the `kref` to 2. When the URB completed or was killed, `usb_put_urb` would decrement it back to 1. This means that the `urb_destroy` function, which is responsible for final cleanup when the `kref` reaches zero, was *never* called before commit `836e2548524d`. The URBs were never truly freed.
But it gets even worse. Commit `836e2548524d` actually changed the structure: it moved from `struct urb urb;` to `struct urb *urb;`. This commit introduced the following changes:
1. **Allocation:** `dev->surbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);` was added in `smsusb_init_device()`. This is the correct way to allocate a URB dynamically.
2. **Deallocation:** Crucially, `usb_free_urb(dev->surbs[i].urb);` was *added* to `smsusb_term_device()`. This is the essential cleanup step that was missing.
3. **Buffer Flag:** `surb->urb->transfer_flags |= URB_FREE_BUFFER;` was retained in `smsusb_submit_urb()`. This flag is important for indicating that the buffer associated with the URB should also be freed upon completion.
Now, let's scrutinize `kfree()` within `mm/slub.c` and its interaction with the `common_buffer`. The `kfree` implementation gets the `struct page` using `virt_to_page(object)`. This function returns the `struct page` for that *exact* 4KB page, not necessarily the head page of a compound page.
When `surb->cb->p` for `i = 1` points to `dev->common_buffer + 0x1000`, `page` becomes the *second* page (a tail page) of the compound page. A tail page, by definition, does *not* have `PageLargeKmalloc(page)` set. Only the head page of a compound allocation typically has this flag. This raises questions about how `kfree` handles tail pages and whether `usb_free_urb` (or the underlying `kfree` it calls) is correctly deallocating the entire compound buffer when a tail page is involved. I need to delve deeper into what `PageLargeKmalloc` signifies, when it was introduced (checking commit `0bdfdd6a05aa5`), and how it interacts with `usb_free_urb` and `usb_init_urb`.
And there's another critical issue, even independent of the compound page problem! Look at `urb_destroy`. For `i = 0`, it calls `kfree(dev->common_buffer)`. This frees the entire `common_buffer`. However, *after* this, `smscore_unregister_device` *also* calls `kfree(dev->common_buffer)`! This is a double-free waiting to happen, as `smscore_unregister_device` is called *before* `usb_free_urb` in `smsusb_term_device`.
Let's examine the order of operations in `smsusb_term_device` once more:
345: smsusb_stop_streaming(dev);
346:
347: /* unregister from smscore */
348: if (dev->coredev)
349: smscore_unregister_device(dev->coredev);
350:
351: for (i = 0; i < MAX_URBS; i++)
352: usb_free_urb(dev->surbs[i].urb);
This order confirms my suspicion. `smscore_unregister_device` happens, potentially freeing `common_buffer`, and then the loop iterates through `usb_free_urb`. If `urb_destroy` (called indirectly by `usb_free_urb`) for `i=0` also frees `common_buffer`, we have a serious problem. I must meticulously check the implementation of `smscore_unregister_device` and the exact logic within `urb_destroy` and `usb_free_urb`, paying close attention to commit `0bdfdd6a05aa5` and its implications for memory management of compound pages and the `common_buffer`. This is a tangled web of memory management that needs immediate untangling.
|
| 36/2 |
2026/11/24 09:38 |
tool |
codesearch-definition-source |
0m
Args: map[Name:smscore_unregister_device]
Results: map[SourceCode:1177: /*
1178: * notifies all clients registered with the device, notifies hotplugs,
1179: * frees all buffers and coredev object
1180: *
1181: * @param coredev pointer to a coredev object returned by
1182: * smscore_register_device
1183: *
1184: * return: 0 on success, <0 on error.
1185: */
1186: void smscore_unregister_device(struct smscore_device_t *coredev)
1187: {
1188: struct smscore_buffer_t *cb;
1189: int num_buffers = 0;
1190: int retry = 0;
1191:
1192: mutex_lock(&g_smscore_deviceslock);
1193:
1194: /* Release input device (IR) resources */
1195: sms_ir_exit(coredev);
1196:
1197: smscore_notify_clients(coredev);
1198: smscore_notify_callbacks(coredev, NULL, 0);
1199:
1200: /* at this point all buffers should be back
1201: * onresponse must no longer be called */
1202:
1203: while (1) {
1204: while (!list_empty(&coredev->buffers)) {
1205: cb = (struct smscore_buffer_t *) coredev->buffers.next;
1206: list_del(&cb->entry);
1207: kfree(cb);
1208: num_buffers++;
1209: }
1210: if (num_buffers == coredev->num_buffers)
1211: break;
1212: if (++retry > 10) {
1213: pr_info("exiting although not all buffers released.\n");
1214: break;
1215: }
1216:
1217: pr_debug("waiting for %d buffer(s)\n",
1218: coredev->num_buffers - num_buffers);
1219: mutex_unlock(&g_smscore_deviceslock);
1220: msleep(100);
1221: mutex_lock(&g_smscore_deviceslock);
1222: }
1223:
1224: pr_debug("freed %d buffers\n", num_buffers);
1225:
1226: if (coredev->common_buffer) {
1227: if (coredev->usb_device)
1228: kfree(coredev->common_buffer);
1229: else
1230: dma_free_coherent(coredev->device,
1231: coredev->common_buffer_size,
1232: coredev->common_buffer,
1233: coredev->common_buffer_phys);
1234: }
1235: kfree(coredev->fw_buf);
1236:
1237: list_del(&coredev->entry);
1238: kfree(coredev);
1239:
1240: mutex_unlock(&g_smscore_deviceslock);
1241:
1242: pr_debug("device %p destroyed\n", coredev);
1243: }
SourceFile:drivers/media/common/siano/smscoreapi.c]
|
| 37/2 |
2026/11/24 09:38 |
tool |
codesearch-definition-source |
0m
Args:
Results: map[SourceCode: 83: /**
84: * usb_free_urb - frees the memory used by a urb when all users of it are finished
85: * @urb: pointer to the urb to free, may be NULL
86: *
87: * Must be called when a user of a urb is finished with it. When the last user
88: * of the urb calls this function, the memory of the urb is freed.
89: *
90: * Note: The transfer buffer associated with the urb is not freed unless the
91: * URB_FREE_BUFFER transfer flag is set.
92: */
93: void usb_free_urb(struct urb *urb)
94: {
95: if (urb)
96: kref_put(&urb->kref, urb_destroy);
97: }
SourceFile:drivers/usb/core/urb.c]
|
| 38/2 |
2026/11/24 09:38 |
tool |
codesearch-definition-source |
0m
Args:
Results: map[SourceCode: 30: /**
31: * usb_init_urb - initializes a urb so that it can be used by a USB driver
32: * @urb: pointer to the urb to initialize
33: *
34: * Initializes a urb so that the USB subsystem can use it properly.
35: *
36: * If a urb is created with a call to usb_alloc_urb() it is not
37: * necessary to call this function. Only use this if you allocate the
38: * space for a struct urb on your own. If you call this function, be
39: * careful when freeing the memory for your urb that it is no longer in
40: * use by the USB core.
41: *
42: * Only use this function if you _really_ understand what you are doing.
43: */
44: void usb_init_urb(struct urb *urb)
45: {
46: if (urb) {
47: memset(urb, 0, sizeof(*urb));
48: kref_init(&urb->kref);
49: INIT_LIST_HEAD(&urb->urb_list);
50: INIT_LIST_HEAD(&urb->anchor_list);
51: }
52: }
SourceFile:drivers/usb/core/urb.c]
|
| 39/2 |
2026/11/24 09:38 |
tool |
git-show |
0m
Args: map[Commit:0bdfdd6a05aa5]
Results: map[Output:commit 0bdfdd6a05aa51fa66bae15af79dba977eeaffe9
Author: Matthew Wilcox (Oracle) <willy@infradead.org>
Date: Thu Nov 13 00:09:20 2025 +0000
slab: Remove folio references from free_large_kmalloc()
There's no need to use folio APIs here; just use a page directly.
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Harry Yoo <harry.yoo@oracle.com>
Link: https://patch.msgid.link/20251113000932.1589073-7-willy@infradead.org
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
diff --git a/mm/slub.c b/mm/slub.c
index 458e58e37480..79b71ee47f63 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -6742,12 +6742,12 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
}
EXPORT_SYMBOL(kmem_cache_free);
-static void free_large_kmalloc(struct folio *folio, void *object)
+static void free_large_kmalloc(struct page *page, void *object)
{
- unsigned int order = folio_order(folio);
+ unsigned int order = compound_order(page);
- if (WARN_ON_ONCE(!folio_test_large_kmalloc(folio))) {
- dump_page(&folio->page, "Not a kmalloc allocation");
+ if (WARN_ON_ONCE(!PageLargeKmalloc(page))) {
+ dump_page(page, "Not a kmalloc allocation");
return;
}
@@ -6758,10 +6758,10 @@ static void free_large_kmalloc(struct folio *folio, void *object)
kasan_kfree_large(object);
kmsan_kfree_large(object);
- lruvec_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B,
+ mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
-(PAGE_SIZE << order));
- __folio_clear_large_kmalloc(folio);
- free_frozen_pages(&folio->page, order);
+ __ClearPageLargeKmalloc(page);
+ free_frozen_pages(page, order);
}
/*
@@ -6789,7 +6789,7 @@ void kvfree_rcu_cb(struct rcu_head *head)
* consider folio order
*/
obj = (void *) PAGE_ALIGN_DOWN((unsigned long)obj);
- free_large_kmalloc(folio, obj);
+ free_large_kmalloc(&folio->page, obj);
return;
}
@@ -6829,7 +6829,7 @@ void kfree(const void *object)
folio = virt_to_folio(object);
if (unlikely(!folio_test_slab(folio))) {
- free_large_kmalloc(folio, (void *)object);
+ free_large_kmalloc(&folio->page, (void *)object);
return;
}
@@ -7253,7 +7253,7 @@ int build_detached_freelist(struct kmem_cache *s, size_t size,
if (!s) {
/* Handle kalloc'ed objects */
if (unlikely(!folio_test_slab(folio))) {
- free_large_kmalloc(folio, object);
+ free_large_kmalloc(&folio->page, object);
df->slab = NULL;
return size;
}
]
|
| 40/2 |
2026/11/24 09:38 |
tool |
git-show |
0m
Args: map[Commit:a6687c8ff613f]
Results: map[Output:commit a6687c8ff613fc13a71ce1390593ba8d27c52db9
Author: Matthew Wilcox (Oracle) <willy@infradead.org>
Date: Mon Mar 3 17:28:05 2025 +0000
slab: Mark large folios for debugging purposes
If a user calls p = kmalloc(1024); kfree(p); kfree(p); and 'p' was the
only object in the slab, we may free the slab after the first call to
kfree(). If we do, we clear PGTY_slab and the second call to kfree()
will call free_large_kmalloc(). That will leave a trace in the logs
("object pointer: 0x%p"), but otherwise proceed to free the memory,
which is likely to corrupt the page allocator's metadata.
Allocate a new page type for large kmalloc and mark the memory with it
while it's allocated. That lets us detect this double-free and return
without harming any data structures.
Reported-by: Hannes Reinecke <hare@suse.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Harry Yoo <harry.yoo@oracle.com>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index 36d283552f80..df9234e5f478 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -925,14 +925,15 @@ FOLIO_FLAG_FALSE(has_hwpoisoned)
enum pagetype {
/* 0x00-0x7f are positive numbers, ie mapcount */
/* Reserve 0x80-0xef for mapcount overflow. */
- PGTY_buddy = 0xf0,
- PGTY_offline = 0xf1,
- PGTY_table = 0xf2,
- PGTY_guard = 0xf3,
- PGTY_hugetlb = 0xf4,
- PGTY_slab = 0xf5,
- PGTY_zsmalloc = 0xf6,
- PGTY_unaccepted = 0xf7,
+ PGTY_buddy = 0xf0,
+ PGTY_offline = 0xf1,
+ PGTY_table = 0xf2,
+ PGTY_guard = 0xf3,
+ PGTY_hugetlb = 0xf4,
+ PGTY_slab = 0xf5,
+ PGTY_zsmalloc = 0xf6,
+ PGTY_unaccepted = 0xf7,
+ PGTY_large_kmalloc = 0xf8,
PGTY_mapcount_underflow = 0xff
};
@@ -1075,6 +1076,7 @@ PAGE_TYPE_OPS(Zsmalloc, zsmalloc, zsmalloc)
* Serialized with zone lock.
*/
PAGE_TYPE_OPS(Unaccepted, unaccepted, unaccepted)
+FOLIO_TYPE_OPS(large_kmalloc, large_kmalloc)
/**
* PageHuge - Determine if the page belongs to hugetlbfs
diff --git a/mm/slub.c b/mm/slub.c
index d94af020b305..3e6ab4986f8f 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -4255,6 +4255,7 @@ static void *___kmalloc_large_node(size_t size, gfp_t flags, int node)
ptr = folio_address(folio);
lruvec_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B,
PAGE_SIZE << order);
+ __folio_set_large_kmalloc(folio);
}
ptr = kasan_kmalloc_large(ptr, size, flags);
@@ -4730,6 +4731,11 @@ static void free_large_kmalloc(struct folio *folio, void *object)
{
unsigned int order = folio_order(folio);
+ if (WARN_ON_ONCE(!folio_test_large_kmalloc(folio))) {
+ dump_page(&folio->page, "Not a kmalloc allocation");
+ return;
+ }
+
if (WARN_ON_ONCE(order == 0))
pr_warn_once("object pointer: 0x%p\n", object);
@@ -4739,6 +4745,7 @@ static void free_large_kmalloc(struct folio *folio, void *object)
lruvec_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B,
-(PAGE_SIZE << order));
+ __folio_clear_large_kmalloc(folio);
folio_put(folio);
}
]
|
| 41/2 |
2026/11/24 09:38 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 39210
output: 142
thoughts: 1966
Thoughts: **Deconstructing the USB Driver Disconnect Bug**
My thought process here is to meticulously trace the lifecycle of a USB device connection and disconnection within this specific driver, focusing on how resources are managed and, crucially, where the double-free is occurring.
1. **Reproducer Setup:** First, I'm mentally walking through the reproducer's actions: it's simulating a USB device connection. This device is identified by `idVendor=0x187f` and `idProduct=0x0100`, which the driver recognizes as an `SMS1XXX_BOARD_SIANO_STELLAR`. It has a single interface with two bulk endpoints, one for IN and one for OUT.
2. **Driver Probe (`smsusb_probe`):**
* The driver identifies the board type, confirming it's the `SMS1XXX_BOARD_SIANO_STELLAR`. I'm double-checking the expected interface number (`intf_num`) and device type (`type`) to ensure no misidentification.
* Then, `smsusb_init_device` kicks in. It allocates a `struct smsusb_device_t` (let's call it `dev`).
* Crucially, it sets up parameters for the core driver: `params.buffer_size` is set to either 4096 or 8192 bytes, and `params.num_buffers` to 50.
* The call to `smscore_register_device` is key here. It allocates a substantial chunk of memory for a "common buffer" (`dev->common_buffer_size` is 4096 * 50 = 204800 bytes). This allocation is a large kmalloc page (order-6, 256KB). It then carves this large allocation into 50 individual `smscore_buffer_t` objects, each pointing to a 4096-byte segment within this common buffer. These buffers are added to the `coredev->buffers` list.
* Next, 10 URBs (USB Request Blocks) are allocated using `usb_alloc_urb`.
* The `smsusb_start_streaming` function is called. Inside this, `smsusb_submit_urb` is invoked for each of the 10 URBs. Each submission:
* Retrieves a buffer from `dev->coredev` (so, `surb[0]` gets the first 4096 bytes of the common buffer, `surb[1]` the next, and so on).
* Fills the URB with this buffer, setting `urb->transfer_flags` to `URB_FREE_BUFFER`. This flag tells the USB core to `kfree` the `transfer_buffer` when the URB is completed and freed.
* Finally, it submits the URB to the hardware using `usb_submit_urb` with `GFP_ATOMIC`.
* `smscore_start_device` is called to finalize the core device initialization.
3. **The Trigger and the Warning:** Now, I'm looking at the reproducer's behavior and the kernel warning:
* The reproducer connects the fake USB device, interacts with `/dev/raw-gadget` for a short period (200ms), and then *exits*.
* When `main()` exits, the file descriptor for `/dev/raw-gadget` is closed. This action triggers a disconnection event for the virtual USB device.
* The `dummy_hcd` (the USB gadget driver for `/dev/raw-gadget`) calls `usb_gadget_disconnect`. This, in turn, schedules a work item on the `dummy_hub` workqueue, which runs under `kworker/0:1`. This is why the warning mentions `kworker/0:1/10` and a CPU (`CPU#0`).
* The `usb_disconnect` path leads to `smsusb_disconnect(intf)`.
4. **The Disconnection (`smsusb_disconnect` and `smsusb_term_device`):** This is where the problem unfolds.
* `smsusb_term_device` is called. It first sets the device state to `SMSUSB_DISCONNECTED`.
* Then, `smsusb_stop_streaming(dev)` is invoked. This function's job is to cancel and kill the submitted URBs (`usb_kill_urb`). Importantly, after killing the URBs, it returns the associated `surb->cb` (the `smscore_buffer_t`) back to the `coredev->buffers` pool.
* **Critical Point 1:** At this stage, even though the URBs are killed, `dev->surbs[i].urb->transfer_buffer` *still points* to its original buffer (`common_buffer + i * 4096`), and `dev->surbs[i].urb->transfer_flags` *still has `URB_FREE_BUFFER` set*.
* Next, `smscore_unregister_device(dev->coredev)` is called. This is where the *first* free of the large memory block occurs. It calls `kfree(coredev->common_buffer)`. This `kfree` operation, because `coredev->common_buffer` was a large kmalloc, invokes `free_large_kmalloc`. This function not only frees the memory but also clears the `PageLargeKmalloc` flag on the associated page and returns it to the page allocator.
* **Critical Point 2:** Now, the loop in `smsusb_term_device` begins to execute: `for (i = 0; i < MAX_URBS; i++) usb_free_urb(dev->surbs[i].urb);`.
* When `i = 0`, `usb_free_urb` checks `urb->transfer_flags & URB_FREE_BUFFER`. Since it's set, it calls `kfree(urb->transfer_buffer)`. Here, `urb->transfer_buffer` points to the original `common_buffer`.
* **The Double Free:** But `common_buffer` was *already freed* in the previous step by `smscore_unregister_device`!
* The `kfree(urb->transfer_buffer)` call now attempts to free a page that has already been returned to the page allocator and has had its `PageLargeKmalloc` flag cleared. The `free_large_kmalloc` routine, when called for a page that *doesn't* have `PageLargeKmalloc` set (which is now the case), hits a `WARN_ON_ONCE(!PageLargeKmalloc(page))`. This is precisely the warning we see: `free_large_kmalloc+0x97/0x110` complaining about `!PageLargeKmalloc(page)`.
* **Further Issues:** Even if the `common_buffer` hadn't been freed yet, subsequent iterations (`i = 1` through `9`) would attempt to `kfree` interior pointers (`common_buffer + i * 4096`) within the large kmalloc allocation. This is also invalid. The initial `kfree(common_buffer)` from `smscore_unregister_device` correctly frees the whole block. The `kfree` of interior pointers from `usb_free_urb` would either be a double-free (if `i=0` and `common_buffer` was already freed) or an attempt to free non-page-aligned addresses (for `i=1` to `9`).
5. **Historical Context (Commit Analysis):** My immediate next step is to understand why this bug *wasn't* present before. This involves digging into the commit history.
* I need to examine commit `836e2548524d` ("media: usb: siano: Fix allocation of urbs") to see what it changed that might have exposed or fixed this.
* More importantly, I need to look at the commit that *introduced* the `URB_FREE_BUFFER` flag into the URB filling logic in the first place. This is likely commit `2e5c1ec8865ab`.
* I'd also check commits around the introduction of `URB_FREE_BUFFER` or changes to `smsusb_term_device` and `smsusb_stop_streaming` using `git log` to trace the evolution of these functions and their interaction with URBs and memory management. The goal is to pinpoint the exact sequence of changes that led to this conflicting memory ownership.
In essence, the driver incorrectly assumes that when it stops streaming and frees the URBs, the URBs' `transfer_buffer`s can be freed independently. However, the core driver's `smscore_unregister_device` has already taken ownership and freed the entire `common_buffer` allocation. The subsequent `kfree` via `URB_FREE_BUFFER` on already freed memory, or on interior pointers, leads directly to the crash and the warning.
|
| 42/2 |
2026/11/24 09:39 |
tool |
git-show |
0m
Args: map[Commit:2e5c1ec8865ab]
Results: map[Output:commit 2e5c1ec8865abd81e24a394918c7ba315e0b7b70
Author: Michael Krufky <mkrufky@linuxtv.org>
Date: Mon May 19 18:56:13 2008 -0300
V4L/DVB (8258): add support for SMS1010 and SMS1150 based digital television devices
initial driver drop, provided by Siano Mobile Silicon, Inc.
Signed-off-by: Michael Krufky <mkrufky@linuxtv.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
diff --git a/drivers/media/Kconfig b/drivers/media/Kconfig
index 7a7803b5d497..e6a5879ea5f6 100644
--- a/drivers/media/Kconfig
+++ b/drivers/media/Kconfig
@@ -115,6 +115,20 @@ source "drivers/media/radio/Kconfig"
source "drivers/media/dvb/Kconfig"
+#
+# Mobile Digital TV devices (DVB-H, T-DMB, etc.)
+#
+menuconfig MDTV_ADAPTERS
+ bool "Mobile Digital TV adapter"
+ default y
+
+if MDTV_ADAPTERS
+
+source "drivers/media/mdtv/Kconfig"
+
+endif # MDTV_ADAPTERS
+
+
config DAB
boolean "DAB adapters"
---help---
diff --git a/drivers/media/Makefile b/drivers/media/Makefile
index 09a829d8a7e7..ec2102bcb282 100644
--- a/drivers/media/Makefile
+++ b/drivers/media/Makefile
@@ -6,3 +6,4 @@ obj-y += common/ video/
obj-$(CONFIG_VIDEO_DEV) += radio/
obj-$(CONFIG_DVB_CORE) += dvb/
+obj-$(CONFIG_MDTV_ADAPTERS) += mdtv/
\ No newline at end of file
diff --git a/drivers/media/mdtv/Kconfig b/drivers/media/mdtv/Kconfig
new file mode 100644
index 000000000000..f3bae45000d1
--- /dev/null
+++ b/drivers/media/mdtv/Kconfig
@@ -0,0 +1,36 @@
+#
+# Mobile Digital TV device configuration
+#
+
+config MDTV_SIANO_STELLAR_COMMON
+ tristate "Siano SMS10xx adapter"
+ default m
+ ---help---
+ Choose Y here if you have SMS10xx chipset.
+
+ In order to control the SMS10xx chipset you will need SMS Host Control library.
+
+ Further documentation on this driver can be found on the WWW at
+ <http://www.siano-ms.com/>.
+
+ To compile this driver as a module, choose M here: the
+ modules will be called smschar and smsnet.
+
+config MDTV_SIANO_STELLAR_USB
+ tristate "Siano SMS10xx USB dongle support"
+ depends on MDTV_SIANO_STELLAR_COMMON
+ default m
+ ---help---
+ Choose Y here if you have USB dongle with SMS10xx chipset.
+
+ In order to control the SMS10xx chipset you will need SMS Host Control library.
+
+ Further documentation on this driver can be found on the WWW at
+ <http://www.siano-ms.com/>.
+
+ To compile this driver as a module, choose M here: the
+ module will be called smsusb.
+
+
+
+
diff --git a/drivers/media/mdtv/Makefile b/drivers/media/mdtv/Makefile
new file mode 100644
index 000000000000..1e54d8f796dc
--- /dev/null
+++ b/drivers/media/mdtv/Makefile
@@ -0,0 +1,8 @@
+#
+# Makefile for the kernel MDTV driver
+#
+
+obj-$(CONFIG_MDTV_SIANO_STELLAR_COMMON) += smschar.o smsnet.o
+obj-$(CONFIG_MDTV_SIANO_STELLAR_USB) += smsusb.o
+
+EXTRA_CFLAGS +=
diff --git a/drivers/media/mdtv/smschar.c b/drivers/media/mdtv/smschar.c
new file mode 100644
index 000000000000..0477ad0ed6d6
--- /dev/null
+++ b/drivers/media/mdtv/smschar.c
@@ -0,0 +1,575 @@
+/*!
+
+ \file smschar.c
+
+ \brief Implementation of smscore client for cdev based access
+
+ \par Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
+
+ \par This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 3 as
+ published by the Free Software Foundation;
+
+ Software distributed under the License is distributed on an "AS
+ IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+ implied.
+
+ \author Anatoly Greenblat
+
+*/
+
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/init.h>
+
+#include <linux/kernel.h> /* printk() */
+#include <linux/fs.h> /* everything... */
+#include <linux/types.h> /* size_t */
+#include <linux/cdev.h>
+#include <linux/sched.h>
+#include <asm/system.h> /* cli(), *_flags */
+#include <asm/uaccess.h> /* copy_*_user */
+
+#include "smskdefs.h" // page, scatterlist, kmutex
+#include "smscoreapi.h"
+#include "smstypes.h"
+
+#include "smscharioctl.h"
+
+#define SMS_CHR_MAX_Q_LEN 10 // max number of packets allowed to be pending on queue
+#define SMSCHAR_NR_DEVS 7
+
+typedef struct _smschar_device
+{
+ struct cdev cdev; //!< Char device structure - kernel's device model representation
+
+ wait_queue_head_t waitq; /* Processes waiting */
+ spinlock_t lock; //!< critical section
+ int pending_count;
+ struct list_head pending_data; //!< list of pending data
+
+ smscore_buffer_t *currentcb;
+
+ int device_index;
+
+ smscore_device_t *coredev;
+ smscore_client_t *smsclient;
+} smschar_device_t;
+
+//! Holds the major number of the device node. may be changed at load time.
+int smschar_major = 251;
+
+//! Holds the first minor number of the device node. may be changed at load time.
+int smschar_minor = 0;
+
+// macros that allow the load time parameters change
+module_param ( smschar_major, int, S_IRUGO );
+module_param ( smschar_minor, int, S_IRUGO );
+
+#ifdef SMSCHAR_DEBUG
+
+ #undef PERROR
+# define PERROR(fmt, args...) printk( KERN_INFO "smschar error: line %d- %s(): " fmt,__LINE__, __FUNCTION__, ## args)
+ #undef PWARNING
+# define PWARNING(fmt, args...) printk( KERN_INFO "smschar warning: line %d- %s(): " fmt,__LINE__, __FUNCTION__, ## args)
+ #undef PDEBUG /* undef it, just in case */
+# define PDEBUG(fmt, args...) printk( KERN_INFO "smschar - %s(): " fmt, __FUNCTION__, ## args)
+
+#else /* not debugging: nothing */
+
+ #define PDEBUG(fmt, args...)
+ #define PERROR(fmt, args...)
+ #define PWARNING(fmt, args...)
+
+#endif
+
+smschar_device_t smschar_devices[SMSCHAR_NR_DEVS];
+static int g_smschar_inuse = 0;
+
+/**
+ * unregisters sms client and returns all queued buffers
+ *
+ * @param dev pointer to the client context (smschar parameters block)
+ *
+ */
+void smschar_unregister_client(smschar_device_t* dev)
+{
+ unsigned long flags;
+
+ if (dev->coredev && dev->smsclient)
+ {
+ wake_up_interruptible(&dev->waitq);
+
+ spin_lock_irqsave(&dev->lock, flags);
+
+ while (!list_empty(&dev->pending_data))
+ {
+ smscore_buffer_t *cb = (smscore_buffer_t *) dev->pending_data.next;
+ list_del(&cb->entry);
+
+ smscore_putbuffer(dev->coredev, cb);
+
+ dev->pending_count --;
+ }
+
+ if (dev->currentcb)
+ {
+ smscore_putbuffer(dev->coredev, dev->currentcb);
+ dev->currentcb = NULL;
+ dev->pending_count --;
+ }
+
+ smscore_unregister_client(dev->smsclient);
+ dev->smsclient = NULL;
+
+ spin_unlock_irqrestore(&dev->lock, flags);
+ }
+}
+
+/**
+ * queues incoming buffers into buffers queue
+ *
+ * @param context pointer to the client context (smschar parameters block)
+ * @param cb pointer to incoming buffer descriptor
+ *
+ * @return 0 on success, <0 on queue overflow.
+ */
+int smschar_onresponse(void *context, smscore_buffer_t *cb)
+{
+ smschar_device_t *dev = context;
+ unsigned long flags;
+
+ spin_lock_irqsave(&dev->lock, flags);
+
+ if (dev->pending_count > SMS_CHR_MAX_Q_LEN)
+ {
+ spin_unlock_irqrestore(&dev->lock, flags);
+ return -EBUSY;
+ }
+
+ dev->pending_count ++;
+
+ // if data channel, remove header
+ if (dev->device_index)
+ {
+ cb->size -= sizeof(SmsMsgHdr_ST);
+ cb->offset += sizeof(SmsMsgHdr_ST);
+ }
+
+ list_add_tail(&cb->entry, &dev->pending_data);
+ spin_unlock_irqrestore(&dev->lock, flags);
+
+ if (waitqueue_active(&dev->waitq))
+ wake_up_interruptible(&dev->waitq);
+
+ return 0;
+}
+
+/**
+ * handles device removal event
+ *
+ * @param context pointer to the client context (smschar parameters block)
+ *
+ */
+void smschar_onremove(void *context)
+{
+ smschar_device_t *dev = (smschar_device_t *) context;
+
+ smschar_unregister_client(dev);
+ dev->coredev = NULL;
+}
+
+/**
+ * registers client associated with the node
+ *
+ * @param inode Inode concerned.
+ * @param file File concerned.
+ *
+ * @return 0 on success, <0 on error.
+ */
+int smschar_open (struct inode *inode, struct file *file)
+{
+ smschar_device_t *dev = container_of(inode->i_cdev, smschar_device_t, cdev);
+ int rc = -ENODEV;
+
+ PDEBUG("entering index %d\n", dev->device_index);
+
+ if (dev->coredev)
+ {
+ smsclient_params_t params;
+
+ params.initial_id = dev->device_index ? dev->device_index : SMS_HOST_LIB;
+ params.data_type = dev->device_index ? MSG_SMS_DAB_CHANNEL : 0;
+ params.onresponse_handler = smschar_onresponse;
+ params.onremove_handler = smschar_onremove;
+ params.context = dev;
+
+ rc = smscore_register_client(dev->coredev, ¶ms, &dev->smsclient);
+ if (!rc)
+ {
+ file->private_data = dev;
+ }
+ }
+
+ PDEBUG("exiting, rc %d\n", rc);
+
+ return rc;
+}
+
+/**
+ * unregisters client associated with the node
+ *
+ * @param inode Inode concerned.
+ * @param file File concerned.
+ *
+ */
+int smschar_release(struct inode *inode, struct file *file)
+{
+ smschar_unregister_client(file->private_data);
+
+ PDEBUG("exiting\n");
+
+ return 0;
+}
+
+/**
+ * copies data from buffers in incoming queue into a user buffer
+ *
+ * @param file File structure.
+ * @param buf Source buffer.
+ * @param count Size of source buffer.
+ * @param f_pos Position in file (ignored).
+ *
+ * @return Number of bytes read, or <0 on error.
+ */
+ssize_t smschar_read ( struct file * file, char __user * buf, size_t count, loff_t * f_pos )
+{
+ smschar_device_t *dev = file->private_data;
+ unsigned long flags;
+ int copied = 0;
+
+ if (!dev->coredev || !dev->smsclient)
+ {
+ PERROR("no client\n");
+ return -ENODEV;
+ }
+
+ while (copied != count)
+ {
+ if (0 > wait_event_interruptible(dev->waitq, !list_empty(&dev->pending_data)))
+ {
+ PERROR("wait_event_interruptible error\n");
+ return -ENODEV;
+ }
+
+ if (!dev->smsclient)
+ {
+ PERROR("no client\n");
+ return -ENODEV;
+ }
+
+ spin_lock_irqsave(&dev->lock, flags);
+
+ while (!list_empty(&dev->pending_data) && (copied != count))
+ {
+ smscore_buffer_t *cb = (smscore_buffer_t *) dev->pending_data.next;
+ int actual_size = min(((int) count - copied), cb->size);
+
+ copy_to_user(&buf[copied], &((char*)cb->p)[cb->offset], actual_size);
+
+ copied += actual_size;
+ cb->offset += actual_size;
+ cb->size -= actual_size;
+
+ if (!cb->size)
+ {
+ list_del(&cb->entry);
+ smscore_putbuffer(dev->coredev, cb);
+
+ dev->pending_count --;
+ }
+ }
+
+ spin_unlock_irqrestore(&dev->lock, flags);
+ }
+
+ return copied;
+}
+
+/**
+ * sends the buffer to the associated device
+ *
+ * @param file File structure.
+ * @param buf Source buffer.
+ * @param count Size of source buffer.
+ * @param f_pos Position in file (ignored).
+ *
+ * @return Number of bytes read, or <0 on error.
+ */
+ssize_t smschar_write(struct file *file, const char __user *buf, size_t count, loff_t *f_pos)
+{
+ smschar_device_t *dev = file->private_data;
+ void *buffer;
+
+ if (!dev->smsclient)
+ {
+ PERROR("no client\n");
+ return -ENODEV;
+ }
+
+ buffer = kmalloc(ALIGN(count, SMS_ALLOC_ALIGNMENT) + SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
+ if (buffer)
+ {
+ void *msg_buffer = (void*) SMS_ALIGN_ADDRESS(buffer);
+
+ if (!copy_from_user(msg_buffer, buf, count))
+ smsclient_sendrequest(dev->smsclient, msg_buffer, count);
+ else
+ count = 0;
+
+ kfree(buffer);
+ }
+
+ return count;
+}
+
+int smschar_mmap(struct file *file, struct vm_area_struct *vma)
+{
+ smschar_device_t *dev = file->private_data;
+ return smscore_map_common_buffer(dev->coredev, vma);
+}
+
+/**
+ * waits until buffer inserted into a queue. when inserted buffer offset are reported
+ * to the calling process. previously reported buffer is returned to smscore pool
+ *
+ * @param dev pointer to smschar parameters block
+ * @param touser pointer to a structure that receives incoming buffer offsets
+ *
+ * @return 0 on success, <0 on error.
+ */
+int smschar_wait_get_buffer(smschar_device_t* dev, smschar_buffer_t* touser)
+{
+ unsigned long flags;
+ int rc;
+
+ spin_lock_irqsave(&dev->lock, flags);
+
+ if (dev->currentcb)
+ {
+ smscore_putbuffer(dev->coredev, dev->currentcb);
+ dev->currentcb = NULL;
+ dev->pending_count --;
+ }
+
+ spin_unlock_irqrestore(&dev->lock, flags);
+
+ rc = wait_event_interruptible(dev->waitq, !list_empty(&dev->pending_data));
+ if (rc < 0)
+ {
+ PERROR("wait_event_interruptible error\n");
+ return rc;
+ }
+
+ if (!dev->smsclient)
+ {
+ PERROR("no client\n");
+ return -ENODEV;
+ }
+
+ spin_lock_irqsave(&dev->lock, flags);
+
+ if (!list_empty(&dev->pending_data))
+ {
+ smscore_buffer_t *cb = (smscore_buffer_t *) dev->pending_data.next;
+
+ touser->offset = cb->offset_in_common + cb->offset;
+ touser->size = cb->size;
+
+ list_del(&cb->entry);
+
+ dev->currentcb = cb;
+ }
+ else
+ {
+ touser->offset = 0;
+ touser->size = 0;
+ }
+
+ spin_unlock_irqrestore(&dev->lock, flags);
+
+ return 0;
+}
+
+int smschar_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
+{
+ smschar_device_t *dev = file->private_data;
+ void __user *up = (void __user *) arg;
+
+ if (!dev->coredev || !dev->smsclient)
+ {
+ PERROR("no client\n");
+ return -ENODEV;
+ }
+
+ switch(cmd)
+ {
+ case SMSCHAR_SET_DEVICE_MODE:
+ return smscore_set_device_mode(dev->coredev, (int) arg);
+
+ case SMSCHAR_GET_DEVICE_MODE:
+ {
+ if (put_user(smscore_get_device_mode(dev->coredev), (int*) up))
+ return -EFAULT;
+
+ break;
+ }
+
+ case SMSCHAR_GET_BUFFER_SIZE:
+ {
+ if (put_user(smscore_get_common_buffer_size(dev->coredev), (int*) up))
+ return -EFAULT;
+
+ break;
+ }
+
+ case SMSCHAR_WAIT_GET_BUFFER:
+ {
+ smschar_buffer_t touser;
+ int rc;
+
+ rc = smschar_wait_get_buffer(dev, &touser);
+ if (rc < 0)
+ return rc;
+
+ if (copy_to_user(up, &touser, sizeof(smschar_buffer_t)))
+ return -EFAULT;
+
+ break;
+ }
+
+ default:
+ return -ENOIOCTLCMD;
+ }
+
+ return 0;
+}
+
+struct file_operations smschar_fops =
+{
+ .owner = THIS_MODULE,
+ .read = smschar_read,
+ .write = smschar_write,
+ .open = smschar_open,
+ .release = smschar_release,
+ .mmap = smschar_mmap,
+ .ioctl = smschar_ioctl,
+};
+
+static int smschar_setup_cdev ( smschar_device_t *dev, int index )
+{
+ int rc, devno = MKDEV ( smschar_major, smschar_minor + index );
+
+ cdev_init ( &dev->cdev, &smschar_fops );
+
+ dev->cdev.owner = THIS_MODULE;
+ dev->cdev.ops = &smschar_fops;
+
+ kobject_set_name(&dev->cdev.kobj, "Siano_sms%d", index);
+
+ rc = cdev_add ( &dev->cdev, devno, 1 );
+
+ PDEBUG("exiting %p %d, rc %d\n", dev, index, rc);
+
+ return rc;
+}
+
+/**
+ * smschar callback that called when device plugged in/out. the function
+ * register or unregisters char device interface according to plug in/out
+ *
+ * @param coredev pointer to device that is being plugged in/out
+ * @param device pointer to system device object
+ * @param arrival 1 on plug-on, 0 othewise
+ *
+ * @return 0 on success, <0 on error.
+ */
+int smschar_hotplug(smscore_device_t* coredev, struct device* device, int arrival)
+{
+ int rc = 0, i;
+
+ PDEBUG("entering %d\n", arrival);
+
+ if (arrival)
+ {
+ // currently only 1 instance supported
+ if (!g_smschar_inuse)
+ {
+ /* data notification callbacks assignment */
+ memset ( smschar_devices, 0, SMSCHAR_NR_DEVS * sizeof ( smschar_device_t ) );
+
+ /* Initialize each device. */
+ for (i = 0; i < SMSCHAR_NR_DEVS; i++)
+ {
+ smschar_setup_cdev ( &smschar_devices[i], i );
+
+ INIT_LIST_HEAD(&smschar_devices[i].pending_data);
+ spin_lock_init(&smschar_devices[i].lock);
+ init_waitqueue_head(&smschar_devices[i].waitq);
+
+ smschar_devices[i].coredev = coredev;
+ smschar_devices[i].device_index = i;
+ }
+
+ g_smschar_inuse = 1;
+ }
+ }
+ else
+ {
+ // currently only 1 instance supported
+ if (g_smschar_inuse)
+ {
+ /* Get rid of our char dev entries */
+ for(i = 0; i < SMSCHAR_NR_DEVS; i++)
+ cdev_del(&smschar_devices[i].cdev);
+
+ g_smschar_inuse = 0;
+ }
+ }
+
+ PDEBUG("exiting, rc %d\n", rc);
+
+ return rc; /* succeed */
+}
+
+int smschar_initialize(void)
+{
+ dev_t devno = MKDEV ( smschar_major, smschar_minor );
+ int rc;
+
+ if(smschar_major)
+ {
+ rc = register_chrdev_region ( devno, SMSCHAR_NR_DEVS, "smschar" );
+ }
+ else
+ {
+ rc = alloc_chrdev_region ( &devno, smschar_minor, SMSCHAR_NR_DEVS, "smschar" );
+ smschar_major = MAJOR ( devno );
+ }
+
+ if (rc < 0)
+ {
+ PWARNING ( "smschar: can't get major %d\n", smschar_major );
+ return rc;
+ }
+
+ return smscore_register_hotplug(smschar_hotplug);
+}
+
+void smschar_terminate(void)
+{
+ dev_t devno = MKDEV ( smschar_major, smschar_minor );
+
+ unregister_chrdev_region(devno, SMSCHAR_NR_DEVS);
+ smscore_unregister_hotplug(smschar_hotplug);
+}
diff --git a/drivers/media/mdtv/smschar.h b/drivers/media/mdtv/smschar.h
new file mode 100644
index 000000000000..1cd2f32a7f71
--- /dev/null
+++ b/drivers/media/mdtv/smschar.h
@@ -0,0 +1,7 @@
+#ifndef __smschar_h__
+#define __smschar_h__
+
+extern int smschar_initialize(void);
+extern void smschar_terminate(void);
+
+#endif // __smschar_h__
diff --git a/drivers/media/mdtv/smscharioctl.h b/drivers/media/mdtv/smscharioctl.h
new file mode 100644
index 000000000000..e57b89efc499
--- /dev/null
+++ b/drivers/media/mdtv/smscharioctl.h
@@ -0,0 +1,17 @@
+#ifndef __smscharioctl_h__
+#define __smscharioctl_h__
+
+#include <linux/ioctl.h>
+
+typedef struct _smschar_buffer_t
+{
+ unsigned long offset; // offset in common buffer (mapped to user space)
+ int size;
+} smschar_buffer_t;
+
+#define SMSCHAR_SET_DEVICE_MODE _IOW('K', 0, int)
+#define SMSCHAR_GET_DEVICE_MODE _IOR('K', 1, int)
+#define SMSCHAR_GET_BUFFER_SIZE _IOR('K', 2, int)
+#define SMSCHAR_WAIT_GET_BUFFER _IOR('K', 3, smschar_buffer_t)
+
+#endif // __smscharioctl_h__
diff --git a/drivers/media/mdtv/smscoreapi.c b/drivers/media/mdtv/smscoreapi.c
new file mode 100644
index 000000000000..a354912391a0
--- /dev/null
+++ b/drivers/media/mdtv/smscoreapi.c
@@ -0,0 +1,1170 @@
+/*!
+
+ \file smscoreapi.c
+
+ \brief Siano core API module
+ This file contains implementation for the interface to sms core component
+
+ \par Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
+
+ \par This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 3 as
+ published by the Free Software Foundation;
+
+ Software distributed under the License is distributed on an "AS
+ IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+ implied.
+
+ \author Anatoly Greenblat
+
+*/
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/dma-mapping.h>
+#include <linux/delay.h>
+#include <asm/io.h>
+
+#include "smskdefs.h" // device, page, scatterlist, kmutex
+
+#include <linux/firmware.h>
+
+#include "smscoreapi.h"
+#include "smstypes.h"
+
+#include "smschar.h"
+
+typedef struct _smscore_device_notifyee
+{
+ struct list_head entry;
+ hotplug_t hotplug;
+} smscore_device_notifyee_t;
+
+typedef struct _smscore_client
+{
+ struct list_head entry;
+ smscore_device_t *coredev;
+
+ void *context;
+
+ int data_type;
+
+ onresponse_t onresponse_handler;
+ onremove_t onremove_handler;
+} *psmscore_client_t;
+
+typedef struct _smscore_subclient
+{
+ struct list_head entry;
+ smscore_client_t *client;
+
+ int id;
+} smscore_subclient_t;
+
+typedef struct _smscore_device
+{
+ struct list_head entry;
+
+ struct list_head clients;
+ struct list_head subclients;
+ spinlock_t clientslock;
+
+ struct list_head buffers;
+ spinlock_t bufferslock;
+ int num_buffers;
+
+ void *common_buffer;
+ int common_buffer_size;
+ dma_addr_t common_buffer_phys;
+
+ void *context;
+ struct device *device;
+
+ char devpath[32];
+ unsigned long device_flags;
+
+ setmode_t setmode_handler;
+ detectmode_t detectmode_handler;
+ sendrequest_t sendrequest_handler;
+ preload_t preload_handler;
+ postload_t postload_handler;
+
+ int mode, modes_supported;
+
+ struct completion version_ex_done, data_download_done, trigger_done;
+ struct completion init_device_done, reload_start_done, resume_done;
+} *psmscore_device_t;
+
+typedef struct _smscore_registry_entry
+{
+ struct list_head entry;
+ char devpath[32];
+ int mode;
+} smscore_registry_entry_t;
+
+struct list_head g_smscore_notifyees;
+struct list_head g_smscore_devices;
+kmutex_t g_smscore_deviceslock;
+
+struct list_head g_smscore_registry;
+kmutex_t g_smscore_registrylock;
+
+static int default_mode = 1;
+module_param(default_mode, int, 0644);
+MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");
+
+int smscore_registry_getmode(char* devpath)
+{
+ smscore_registry_entry_t *entry;
+ struct list_head *next;
+
+ kmutex_lock(&g_smscore_registrylock);
+
+ for (next = g_smscore_registry.next; next != &g_smscore_registry; next = next->next)
+ {
+ entry = (smscore_registry_entry_t *) next;
+
+ if (!strcmp(entry->devpath, devpath))
+ {
+ kmutex_unlock(&g_smscore_registrylock);
+ return entry->mode;
+ }
+ }
+
+ entry = (smscore_registry_entry_t *) kmalloc(sizeof(smscore_registry_entry_t), GFP_KERNEL);
+ if (entry)
+ {
+ entry->mode = default_mode;
+ strcpy(entry->devpath, devpath);
+
+ list_add(&entry->entry, &g_smscore_registry);
+ }
+
+ kmutex_unlock(&g_smscore_registrylock);
+
+ return default_mode;
+}
+
+void smscore_registry_setmode(char* devpath, int mode)
+{
+ smscore_registry_entry_t *entry;
+ struct list_head *next;
+
+ kmutex_lock(&g_smscore_registrylock);
+
+ for (next = g_smscore_registry.next; next != &g_smscore_registry; next = next->next)
+ {
+ entry = (smscore_registry_entry_t *) next;
+
+ if (!strcmp(entry->devpath, devpath))
+ {
+ entry->mode = mode;
+ break;
+ }
+ }
+
+ kmutex_unlock(&g_smscore_registrylock);
+}
+
+
+void list_add_locked(struct list_head *new, struct list_head *head, spinlock_t* lock)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(lock, flags);
+
+ list_add(new, head);
+
+ spin_unlock_irqrestore(lock, flags);
+}
+
+/**
+ * register a client callback that called when device plugged in/unplugged
+ * NOTE: if devices exist callback is called immediately for each device
+ *
+ * @param hotplug callback
+ *
+ * @return 0 on success, <0 on error.
+ */
+int smscore_register_hotplug(hotplug_t hotplug)
+{
+ smscore_device_notifyee_t *notifyee;
+ struct list_head *next, *first;
+ int rc = 0;
+
+ kmutex_lock(&g_smscore_deviceslock);
+
+ notifyee = kmalloc(sizeof(smscore_device_notifyee_t), GFP_KERNEL);
+ if (notifyee)
+ {
+ // now notify callback about existing devices
+ first = &g_smscore_devices;
+ for (next = first->next; next != first && !rc; next = next->next)
+ {
+ smscore_device_t *coredev = (smscore_device_t *) next;
+ rc = hotplug(coredev, coredev->device, 1);
+ }
+
+ if (rc >= 0)
+ {
+ notifyee->hotplug = hotplug;
+ list_add(¬ifyee->entry, &g_smscore_notifyees);
+ }
+ else
+ kfree(notifyee);
+ }
+ else
+ rc = -ENOMEM;
+
+ kmutex_unlock(&g_smscore_deviceslock);
+
+ return rc;
+}
+
+/**
+ * unregister a client callback that called when device plugged in/unplugged
+ *
+ * @param hotplug callback
+ *
+ */
+void smscore_unregister_hotplug(hotplug_t hotplug)
+{
+ struct list_head *next, *first;
+
+ kmutex_lock(&g_smscore_deviceslock);
+
+ first = &g_smscore_notifyees;
+
+ for (next = first->next; next != first;)
+ {
+ smscore_device_notifyee_t *notifyee = (smscore_device_notifyee_t *) next;
+ next = next->next;
+
+ if (notifyee->hotplug == hotplug)
+ {
+ list_del(¬ifyee->entry);
+ kfree(notifyee);
+ }
+ }
+
+ kmutex_unlock(&g_smscore_deviceslock);
+}
+
+void smscore_notify_clients(smscore_device_t *coredev)
+{
+ smscore_client_t* client;
+
+ // the client must call smscore_unregister_client from remove handler
+ while (!list_empty(&coredev->clients))
+ {
+ client = (smscore_client_t *) coredev->clients.next;
+ client->onremove_handler(client->context);
+ }
+}
+
+int smscore_notify_callbacks(smscore_device_t *coredev, struct device *device, int arrival)
+{
+ struct list_head *next, *first;
+ int rc = 0;
+
+ // note: must be called under g_deviceslock
+
+ first = &g_smscore_notifyees;
+
[Output truncated: showing 1000 of 3727 lines]]
|
| 43/2 |
2026/11/24 09:39 |
tool |
git-log |
0m
Args: map[CodeRegexp:URB_FREE_BUFFER PathPrefix:drivers/media/usb/siano]
Results: map[Output:836e2548524d media: usb: siano: Fix allocation of urbs
564246fd3ff4 media: siano: Fix coherent memory allocation failure on arm64
]
|
| 44/2 |
2026/11/24 09:39 |
tool |
git-log |
0m
Args: map[CodeRegexp:usb_free_urb PathPrefix:drivers/media/usb/siano]
Results: map[Output:836e2548524d media: usb: siano: Fix allocation of urbs
]
|
| 45/2 |
2026/11/24 09:39 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:drivers/media/usb/siano/smsusb.c]
Results: map[Output:bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
836e2548524d media: usb: siano: Fix allocation of urbs
6a57a21943da Normalise "name (ad@dr)" MODULE_AUTHORs to "name <ad@dr>"
16fae979ed6b media: usb: siano: Use kmemdup to simplify kmalloc and memcpy logic
b9c7141f3840 drivers: usb: smsusb: fix error handling code in smsusb_init_device
6f489a966fbe media: usb: siano: Fix warning due to null work_func_t function pointer
ebad8e731c1c media: usb: siano: Fix use after free bugs caused by do_submit_urb
051d3b5437af media: siano: remove duplicate USB device IDs
1771e9fb67e2 media: Use fallthrough pseudo-keyword
a47686636d84 media: smsusb: better handle optional alignment
45457c01171f media: usb: siano: Fix false-positive "uninitialized variable" warning
1ccea77e2a26 treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 13
31e0456de5be media: usb: siano: Fix general protection fault in smsusb
3e4d8f48b9bf media: usb: fix several typos
0f4bb10857e2 media: siano: Use kmemdup instead of duplicating its function
782b9d201112 media: siano: use GFP_DMA only for smssdio
564246fd3ff4 media: siano: Fix coherent memory allocation failure on arm64
6e6a8b5a38cb media: replace all <spaces><tab> occurrences
929b99ed9b31 media: siano: get rid of documentation warnings
]
|
| 46/2 |
2026/11/24 09:39 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 51430
output: 75
thoughts: 123
Thoughts: ** Eureka! I've stumbled upon something significant while digging through the `git-log` related to `URB_FREE_BUFFER` within the `drivers/media/usb/siano` module.**
Initially, my attention was drawn to commit `836e2548524d`, which addressed the allocation of URBs. However, as I delved deeper, I noticed a preceding commit, `564246fd3ff4`, also pertaining to `URB_FREE_BUFFER`. This sparked a crucial thought: I need to thoroughly examine what commit `564246fd3ff4` actually accomplished. It's imperative to understand its precise impact, especially given its role in fixing coherent memory allocation failures on arm64. The relationship between these two commits is key, and I suspect there's more to uncover by understanding the earlier fix.
|
| 47/2 |
2026/11/24 09:39 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 564246fd3ff41afd3234c18ae28bc4ad9d815f79
Author: Tomoki Sekiyama <tomoki.sekiyama@gmail.com>
Date: Sat Mar 3 13:20:56 2018 -0500
media: siano: Fix coherent memory allocation failure on arm64
On some architectures such as arm64, siano chip based TV-tuner
USB devices are not recognized correctly due to coherent memory
allocation failure with the following error:
[ 663.556135] usbcore: deregistering interface driver smsusb
[ 683.624809] smsusb:smsusb_probe: board id=18, interface number 0
[ 683.633530] smsusb:smsusb_init_device: smscore_register_device(...) failed, rc -12
[ 683.641501] smsusb:smsusb_probe: Device initialized with return code -12
[ 683.652978] smsusb: probe of 1-1:1.0 failed with error -12
This is caused by dma_alloc_coherent(NULL, ...) returning NULL in
smscoreapi.c.
To fix this error, allocate the buffer memory for the USB devices
via kmalloc() and let the USB core do the DMA mapping and free.
Signed-off-by: Tomoki Sekiyama <tomoki.sekiyama@gmail.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
diff --git a/drivers/media/common/siano/smscoreapi.c b/drivers/media/common/siano/smscoreapi.c
index c5c827e11b64..b5dcc6d1fe90 100644
--- a/drivers/media/common/siano/smscoreapi.c
+++ b/drivers/media/common/siano/smscoreapi.c
@@ -631,7 +631,8 @@ smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
cb->p = buffer;
cb->offset_in_common = buffer - (u8 *) common_buffer;
- cb->phys = common_buffer_phys + cb->offset_in_common;
+ if (common_buffer_phys)
+ cb->phys = common_buffer_phys + cb->offset_in_common;
return cb;
}
@@ -690,17 +691,21 @@ int smscore_register_device(struct smsdevice_params_t *params,
/* alloc common buffer */
dev->common_buffer_size = params->buffer_size * params->num_buffers;
- dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
- &dev->common_buffer_phys,
- GFP_KERNEL | GFP_DMA);
- if (!dev->common_buffer) {
+ if (params->usb_device)
+ buffer = kzalloc(dev->common_buffer_size, GFP_KERNEL);
+ else
+ buffer = dma_alloc_coherent(params->device,
+ dev->common_buffer_size,
+ &dev->common_buffer_phys,
+ GFP_KERNEL | GFP_DMA);
+ if (!buffer) {
smscore_unregister_device(dev);
return -ENOMEM;
}
+ dev->common_buffer = buffer;
/* prepare dma buffers */
- for (buffer = dev->common_buffer;
- dev->num_buffers < params->num_buffers;
+ for (; dev->num_buffers < params->num_buffers;
dev->num_buffers++, buffer += params->buffer_size) {
struct smscore_buffer_t *cb;
@@ -720,6 +725,7 @@ int smscore_register_device(struct smsdevice_params_t *params,
dev->board_id = SMS_BOARD_UNKNOWN;
dev->context = params->context;
dev->device = params->device;
+ dev->usb_device = params->usb_device;
dev->setmode_handler = params->setmode_handler;
dev->detectmode_handler = params->detectmode_handler;
dev->sendrequest_handler = params->sendrequest_handler;
@@ -1231,10 +1237,15 @@ void smscore_unregister_device(struct smscore_device_t *coredev)
pr_debug("freed %d buffers\n", num_buffers);
- if (coredev->common_buffer)
- dma_free_coherent(NULL, coredev->common_buffer_size,
- coredev->common_buffer, coredev->common_buffer_phys);
-
+ if (coredev->common_buffer) {
+ if (coredev->usb_device)
+ kfree(coredev->common_buffer);
+ else
+ dma_free_coherent(coredev->device,
+ coredev->common_buffer_size,
+ coredev->common_buffer,
+ coredev->common_buffer_phys);
+ }
kfree(coredev->fw_buf);
list_del(&coredev->entry);
diff --git a/drivers/media/common/siano/smscoreapi.h b/drivers/media/common/siano/smscoreapi.h
index 4cc39e4a8318..134c69f7ea7b 100644
--- a/drivers/media/common/siano/smscoreapi.h
+++ b/drivers/media/common/siano/smscoreapi.h
@@ -134,6 +134,7 @@ struct smscore_buffer_t {
struct smsdevice_params_t {
struct device *device;
+ struct usb_device *usb_device;
int buffer_size;
int num_buffers;
@@ -176,6 +177,7 @@ struct smscore_device_t {
void *context;
struct device *device;
+ struct usb_device *usb_device;
char devpath[32];
unsigned long device_flags;
diff --git a/drivers/media/usb/siano/smsusb.c b/drivers/media/usb/siano/smsusb.c
index f13e4b01b5a5..6d436e9e454f 100644
--- a/drivers/media/usb/siano/smsusb.c
+++ b/drivers/media/usb/siano/smsusb.c
@@ -179,8 +179,7 @@ static int smsusb_submit_urb(struct smsusb_device_t *dev,
smsusb_onresponse,
surb
);
- surb->urb.transfer_dma = surb->cb->phys;
- surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+ surb->urb.transfer_flags |= URB_FREE_BUFFER;
return usb_submit_urb(&surb->urb, GFP_ATOMIC);
}
@@ -446,6 +445,7 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)
dev->in_ep, dev->out_ep);
params.device = &dev->udev->dev;
+ params.usb_device = dev->udev;
params.buffer_size = dev->buffer_size;
params.num_buffers = MAX_BUFFERS;
params.sendrequest_handler = smsusb_sendrequest;
]
|
| 48/2 |
2026/11/24 09:39 |
tool |
git-show |
0m
Args: map[Commit:2e5c1ec8865ab File:drivers/media/mdtv/smsusb.c]
Results: map[Output:commit 2e5c1ec8865abd81e24a394918c7ba315e0b7b70
Author: Michael Krufky <mkrufky@linuxtv.org>
Date: Mon May 19 18:56:13 2008 -0300
V4L/DVB (8258): add support for SMS1010 and SMS1150 based digital television devices
initial driver drop, provided by Siano Mobile Silicon, Inc.
Signed-off-by: Michael Krufky <mkrufky@linuxtv.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
diff --git a/drivers/media/mdtv/smsusb.c b/drivers/media/mdtv/smsusb.c
new file mode 100644
index 000000000000..43af826b3832
--- /dev/null
+++ b/drivers/media/mdtv/smsusb.c
@@ -0,0 +1,428 @@
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/usb.h>
+#include <linux/firmware.h>
+
+#include "smskdefs.h" // page, scatterlist, kmutex
+#include "smscoreapi.h"
+#include "smstypes.h"
+
+#define USB_VID_SIANO 0x187f
+#define USB_PID_0010 0x0010
+#define USB_PID_0100 0x0100
+#define USB_PID_0200 0x0200
+
+#define USB1_BUFFER_SIZE 0x1000
+#define USB2_BUFFER_SIZE 0x4000
+
+#define MAX_BUFFERS 50
+#define MAX_URBS 10
+
+typedef struct _smsusb_device smsusb_device_t;
+
+typedef struct _smsusb_urb
+{
+ smscore_buffer_t *cb;
+ smsusb_device_t *dev;
+
+ struct urb urb;
+} smsusb_urb_t;
+
+typedef struct _smsusb_device
+{
+ struct usb_device* udev;
+ smscore_device_t *coredev;
+
+ smsusb_urb_t surbs[MAX_URBS];
+
+ int response_alignment;
+ int buffer_size;
+} *psmsusb_device_t;
+
+static struct usb_device_id smsusb_id_table [] = {
+ { USB_DEVICE(USB_VID_SIANO, USB_PID_0010) },
+ { USB_DEVICE(USB_VID_SIANO, USB_PID_0100) },
+ { USB_DEVICE(USB_VID_SIANO, USB_PID_0200) },
+ { } /* Terminating entry */
+};
+MODULE_DEVICE_TABLE (usb, smsusb_id_table);
+
+int smsusb_submit_urb(smsusb_device_t* dev, smsusb_urb_t* surb);
+
+void smsusb_onresponse(struct urb *urb)
+{
+ smsusb_urb_t *surb = (smsusb_urb_t *) urb->context;
+ smsusb_device_t *dev = surb->dev;
+
+ if (urb->status < 0)
+ {
+ printk(KERN_INFO "%s error, urb status %d, %d bytes\n", __FUNCTION__, urb->status, urb->actual_length);
+ return;
+ }
+
+ if (urb->actual_length > 0)
+ {
+ SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *) surb->cb->p;
+
+ if (urb->actual_length >= phdr->msgLength)
+ {
+ surb->cb->size = phdr->msgLength;
+
+ if (dev->response_alignment && (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG))
+ {
+ surb->cb->offset = dev->response_alignment + ((phdr->msgFlags >> 8) & 3);
+
+ // sanity check
+ if (((int) phdr->msgLength + surb->cb->offset) > urb->actual_length)
+ {
+ printk("%s: invalid response msglen %d offset %d size %d\n", __FUNCTION__, phdr->msgLength, surb->cb->offset, urb->actual_length);
+ goto exit_and_resubmit;
+ }
+
+ // move buffer pointer and copy header to its new location
+ memcpy((char*) phdr + surb->cb->offset, phdr, sizeof(SmsMsgHdr_ST));
+ }
+ else
+ surb->cb->offset = 0;
+
+ smscore_onresponse(dev->coredev, surb->cb);
+ surb->cb = NULL;
+ }
+ else
+ {
+ printk("%s invalid response msglen %d actual %d\n", __FUNCTION__, phdr->msgLength, urb->actual_length);
+ }
+ }
+
+exit_and_resubmit:
+ smsusb_submit_urb(dev, surb);
+}
+
+int smsusb_submit_urb(smsusb_device_t* dev, smsusb_urb_t* surb)
+{
+ if (!surb->cb)
+ {
+ surb->cb = smscore_getbuffer(dev->coredev);
+ if (!surb->cb)
+ {
+ printk(KERN_INFO "%s smscore_getbuffer(...) returned NULL\n", __FUNCTION__);
+ return -ENOMEM;
+ }
+ }
+
+ usb_fill_bulk_urb(
+ &surb->urb,
+ dev->udev,
+ usb_rcvbulkpipe(dev->udev, 0x81),
+ surb->cb->p,
+ dev->buffer_size,
+ smsusb_onresponse,
+ surb
+ );
+ surb->urb.transfer_dma = surb->cb->phys;
+ surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+
+ return usb_submit_urb(&surb->urb, GFP_ATOMIC);
+}
+
+void smsusb_stop_streaming(smsusb_device_t* dev)
+{
+ int i;
+
+ for (i = 0; i < MAX_URBS; i ++)
+ {
+ usb_kill_urb(&dev->surbs[i].urb);
+
+ if (dev->surbs[i].cb)
+ {
+ smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
+ dev->surbs[i].cb = NULL;
+ }
+ }
+}
+
+int smsusb_start_streaming(smsusb_device_t* dev)
+{
+ int i, rc;
+
+ for (i = 0; i < MAX_URBS; i ++)
+ {
+ rc = smsusb_submit_urb(dev, &dev->surbs[i]);
+ if (rc < 0)
+ {
+ printk(KERN_INFO "%s smsusb_submit_urb(...) failed\n", __FUNCTION__);
+ smsusb_stop_streaming(dev);
+ break;
+ }
+ }
+
+ return rc;
+}
+
+int smsusb_sendrequest(void *context, void *buffer, size_t size)
+{
+ smsusb_device_t* dev = (smsusb_device_t*) context;
+ int dummy;
+
+ return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2), buffer, size, &dummy, 1000);
+}
+
+char *smsusb1_fw_lkup[] =
+{
+ "dvbt_stellar_usb.inp",
+ "dvbh_stellar_usb.inp",
+ "tdmb_stellar_usb.inp",
+ "none",
+ "dvbt_bda_stellar_usb.inp",
+};
+
+int smsusb1_load_firmware(struct usb_device *udev, int id)
+{
+ const struct firmware *fw;
+ u8* fw_buffer;
+ int rc, dummy;
+
+ if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA)
+ {
+ printk(KERN_INFO "%s invalid firmware id specified %d\n", __FUNCTION__, id);
+ return -EINVAL;
+ }
+
+ rc = request_firmware(&fw, smsusb1_fw_lkup[id], &udev->dev);
+ if (rc < 0)
+ {
+ printk(KERN_INFO "%s failed to open \"%s\" mode %d\n", __FUNCTION__, smsusb1_fw_lkup[id], id);
+ return rc;
+ }
+
+ fw_buffer = kmalloc(fw->size, GFP_KERNEL);
+ if (fw_buffer)
+ {
+ memcpy(fw_buffer, fw->data, fw->size);
+
+ rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2), fw_buffer, fw->size, &dummy, 1000);
+
+ printk(KERN_INFO "%s: sent %d(%d) bytes, rc %d\n", __FUNCTION__, fw->size, dummy, rc);
+
+ kfree(fw_buffer);
+ }
+ else
+ {
+ printk(KERN_INFO "failed to allocate firmware buffer\n");
+ rc = -ENOMEM;
+ }
+
+ release_firmware(fw);
+
+ return rc;
+}
+
+void smsusb1_detectmode(void *context, int *mode)
+{
+ char *product_string = ((smsusb_device_t *) context)->udev->product;
+
+ *mode = DEVICE_MODE_NONE;
+
+ if (!product_string)
+ {
+ product_string = "none";
+ printk("%s product string not found\n", __FUNCTION__);
+ }
+ else
+ {
+ if (strstr(product_string, "DVBH"))
+ *mode = 1;
+ else if (strstr(product_string, "BDA"))
+ *mode = 4;
+ else if (strstr(product_string, "DVBT"))
+ *mode = 0;
+ else if (strstr(product_string, "TDMB"))
+ *mode = 2;
+ }
+
+ printk("%s: %d \"%s\"\n", __FUNCTION__, *mode, product_string);
+}
+
+int smsusb1_setmode(void *context, int mode)
+{
+ SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK, sizeof(SmsMsgHdr_ST), 0 };
+
+ if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA)
+ {
+ printk(KERN_INFO "%s invalid firmware id specified %d\n", __FUNCTION__, mode);
+ return -EINVAL;
+ }
+
+ return smsusb_sendrequest(context, &Msg, sizeof(Msg));
+}
+
+void smsusb_term_device(struct usb_interface *intf)
+{
+ smsusb_device_t *dev = (smsusb_device_t*) usb_get_intfdata(intf);
+
+ if (dev)
+ {
+ smsusb_stop_streaming(dev);
+
+ // unregister from smscore
+ if (dev->coredev)
+ smscore_unregister_device(dev->coredev);
+
+ kfree(dev);
+
+ printk(KERN_INFO "%s device %p destroyed\n", __FUNCTION__, dev);
+ }
+
+ usb_set_intfdata(intf, NULL);
+}
+
+int smsusb_init_device(struct usb_interface *intf)
+{
+ smsdevice_params_t params;
+ smsusb_device_t* dev;
+ int i, rc;
+
+ // create device object
+ dev = kzalloc(sizeof(smsusb_device_t), GFP_KERNEL);
+ if (!dev)
+ {
+ printk(KERN_INFO "%s kzalloc(sizeof(smsusb_device_t) failed\n", __FUNCTION__);
+ return -ENOMEM;
+ }
+
+ memset(¶ms, 0, sizeof(params));
+ usb_set_intfdata(intf, dev);
+ dev->udev = interface_to_usbdev(intf);
+
+ switch (dev->udev->descriptor.idProduct)
+ {
+ case USB_PID_0100:
+ dev->buffer_size = USB1_BUFFER_SIZE;
+
+ params.setmode_handler = smsusb1_setmode;
+ params.detectmode_handler = smsusb1_detectmode;
+ break;
+
+ default:
+ dev->buffer_size = USB2_BUFFER_SIZE;
+ dev->response_alignment = dev->udev->ep_in[1]->desc.wMaxPacketSize - sizeof(SmsMsgHdr_ST);
+
+ params.flags |= SMS_DEVICE_FAMILY2;
+ break;
+ }
+
+ params.device = &dev->udev->dev;
+ params.buffer_size = dev->buffer_size;
+ params.num_buffers = MAX_BUFFERS;
+ params.sendrequest_handler = smsusb_sendrequest;
+ params.context = dev;
+ snprintf(params.devpath, sizeof(params.devpath), "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath);
+
+ // register in smscore
+ rc = smscore_register_device(¶ms, &dev->coredev);
+ if (rc < 0)
+ {
+ printk(KERN_INFO "%s smscore_register_device(...) failed, rc %d\n", __FUNCTION__, rc);
+ smsusb_term_device(intf);
+ return rc;
+ }
+
+ // initialize urbs
+ for (i = 0; i < MAX_URBS; i ++)
+ {
+ dev->surbs[i].dev = dev;
+ usb_init_urb(&dev->surbs[i].urb);
+ }
+
+ rc = smsusb_start_streaming(dev);
+ if (rc < 0)
+ {
+ printk(KERN_INFO "%s smsusb_start_streaming(...) failed\n", __FUNCTION__);
+ smsusb_term_device(intf);
+ return rc;
+ }
+
+ rc = smscore_start_device(dev->coredev);
+ if (rc < 0)
+ {
+ printk(KERN_INFO "%s smscore_start_device(...) failed\n", __FUNCTION__);
+ smsusb_term_device(intf);
+ return rc;
+ }
+
+ printk(KERN_INFO "%s device %p created\n", __FUNCTION__, dev);
+
+ return rc;
+}
+
+int smsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
+{
+ struct usb_device *udev = interface_to_usbdev(intf);
+ char devpath[32];
+ int i, rc;
+
+ if (intf->num_altsetting > 0)
+ {
+ rc = usb_set_interface(udev, intf->cur_altsetting->desc.bInterfaceNumber, 0);
+ if (rc < 0)
+ {
+ printk(KERN_INFO "%s usb_set_interface failed, rc %d\n", __FUNCTION__, rc);
+ return rc;
+ }
+ }
+
+ printk(KERN_INFO "smsusb_probe %d\n", intf->cur_altsetting->desc.bInterfaceNumber);
+ for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i ++)
+ printk(KERN_INFO "endpoint %d %02x %02x %d\n", i, intf->cur_altsetting->endpoint[i].desc.bEndpointAddress, intf->cur_altsetting->endpoint[i].desc.bmAttributes, intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
+
+ if (udev->actconfig->desc.bNumInterfaces == 2 && intf->cur_altsetting->desc.bInterfaceNumber == 0)
+ {
+ printk(KERN_INFO "rom interface 0 is not used\n");
+ return -ENODEV;
+ }
+
+ if (intf->cur_altsetting->desc.bInterfaceNumber == 1)
+ {
+ snprintf(devpath, 32, "%d:%s", udev->bus->busnum, udev->devpath);
+ return smsusb1_load_firmware(udev, smscore_registry_getmode(devpath));
+ }
+
+ return smsusb_init_device(intf);
+}
+
+void smsusb_disconnect(struct usb_interface *intf)
+{
+ smsusb_term_device(intf);
+}
+
+static struct usb_driver smsusb_driver = {
+ .name = "smsusb",
+ .probe = smsusb_probe,
+ .disconnect = smsusb_disconnect,
+ .id_table = smsusb_id_table,
+};
+
+int smsusb_module_init(void)
+{
+ int rc = usb_register(&smsusb_driver);
+ if (rc)
+ printk(KERN_INFO "usb_register failed. Error number %d\n", rc);
+
+ printk(KERN_INFO "%s\n", __FUNCTION__);
+
+ return rc;
+}
+
+void smsusb_module_exit(void)
+{
+ usb_deregister(&smsusb_driver);
+ printk(KERN_INFO "%s\n", __FUNCTION__);
+}
+
+module_init(smsusb_module_init);
+module_exit(smsusb_module_exit);
+
+MODULE_DESCRIPTION("smsusb");
+MODULE_AUTHOR("Anatoly Greenblatt,,, (anatolyg@siano-ms.com)");
+MODULE_LICENSE("GPL");
]
|
| 49/2 |
2026/11/24 09:39 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 58342
output: 35
thoughts: 590
Thoughts: **Initial Reassessment of a Kernel Commit**
Hold on a minute, I need to re-examine these commits. Looking at `564246fd3ff4` from 2018, titled "media: siano: Fix coherent memory allocation failure on arm64," I'm seeing some concerning changes. It modified `smscoreapi.c` to replace `dma_alloc_coherent` with `kzalloc` for `dev->common_buffer` for USB devices, and then `kfree` it. That seems like a change in memory management, perhaps to address a specific allocation issue on arm64.
But then, in `smsusb.c`, it *removed* `URB_NO_TRANSFER_DMA_MAP` and, critically, *added* `surb->urb.transfer_flags |= URB_FREE_BUFFER;`. This addition is what's really making me pause. Why would the author introduce `URB_FREE_BUFFER` here? My initial thought is that they likely misunderstood its purpose, perhaps believing it meant "instruct the USB core to handle the freeing and unmapping of DMA memory."
Let me dig deeper into the context of that commit. Back in 2018, when `564246fd3ff4` was introduced, `surb->urb` was embedded within `struct smsusb_urb_t`. Crucially, it was initialized with `usb_init_urb(&dev->surbs[i].urb)`, but `usb_free_urb` was *never* called on this embedded `urb` structure. This means that from 2018 all the way to 2024, `urb_destroy()`, which is triggered when an URB's reference count drops to zero, was never actually executed for these `dev->surbs[i].urb` instances.
This raises another question: could `urb_destroy()` have ever been called on these URBs *before* the commit `836e2548524d`? Let's trace the lifecycle. Before `836e2548524d`, `usb_init_urb` sets the `kref` count to 1. When `usb_submit_urb` is called, it increments the count to 2 via `usb_get_urb`. Upon completion or `usb_kill_urb`, the count is decremented back to 1 with `usb_put_urb`. The `smsusb_term_device` function, which is supposed to clean up, only calls `kfree(dev)` and never explicitly calls `usb_free_urb` or `usb_put_urb` on the embedded `&dev->surbs[i].urb`. Because the `kref` count never reaches zero, `urb_destroy()` is consequently never invoked.
Given this, I need to investigate further. Does anything else within the USB core subsystem check or react to `URB_FREE_BUFFER` in a way that might be relevant to this situation? I should perform a grep across `drivers/usb/` to see where else `URB_FREE_BUFFER` is utilized.
|
| 50/2 |
2026/11/24 09:39 |
tool |
grepper |
0m
Args: map[Expression:URB_FREE_BUFFER PathPrefixes:[drivers/usb/]]
Results: map[Output:drivers/usb/class/usblp.c=721=static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length)
--
drivers/usb/class/usblp.c-738- writebuf, transfer_length, usblp_bulk_write, usblp);
drivers/usb/class/usblp.c:739: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/usb/class/usblp.c-740-
--
drivers/usb/class/usbtmc.c=756=static struct urb *usbtmc_create_urb(void)
--
drivers/usb/class/usbtmc.c-772- urb->transfer_buffer_length = bufsize;
drivers/usb/class/usbtmc.c:773: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/usb/class/usbtmc.c-774- return urb;
--
drivers/usb/core/urb.c=20=static void urb_destroy(struct kref *kref)
--
drivers/usb/core/urb.c-23-
drivers/usb/core/urb.c:24: if (urb->transfer_flags & URB_FREE_BUFFER)
drivers/usb/core/urb.c-25- kfree(urb->transfer_buffer);
--
drivers/usb/core/urb.c=81=EXPORT_SYMBOL_GPL(usb_alloc_urb);
--
drivers/usb/core/urb.c-90- * Note: The transfer buffer associated with the urb is not freed unless the
drivers/usb/core/urb.c:91: * URB_FREE_BUFFER transfer flag is set.
drivers/usb/core/urb.c-92- */
--
drivers/usb/core/urb.c=367=int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
--
drivers/usb/core/urb.c-512- allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT | URB_DIR_MASK |
drivers/usb/core/urb.c:513: URB_FREE_BUFFER);
drivers/usb/core/urb.c-514- switch (xfertype) {
--
drivers/usb/storage/uas.c=472=static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
--
drivers/usb/storage/uas.c-490- urb->stream_id = cmdinfo->uas_tag;
drivers/usb/storage/uas.c:491: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/usb/storage/uas.c-492- out:
--
drivers/usb/storage/uas.c=499=static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
--
drivers/usb/storage/uas.c-528- uas_cmd_cmplt, NULL);
drivers/usb/storage/uas.c:529: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/usb/storage/uas.c-530- out:
--
drivers/usb/usbip/stub_rx.c=399=static void masking_bogus_flags(struct urb *urb)
--
drivers/usb/usbip/stub_rx.c-432- allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT |
drivers/usb/usbip/stub_rx.c:433: URB_DIR_MASK | URB_FREE_BUFFER);
drivers/usb/usbip/stub_rx.c-434- switch (xfertype) {
--
drivers/usb/usbip/usbip_common.c=387=static const struct urb_to_usbip_flags flag_map[NUM_USBIP_FLAGS] = {
--
drivers/usb/usbip/usbip_common.c-392- {URB_NO_INTERRUPT, USBIP_URB_NO_INTERRUPT},
drivers/usb/usbip/usbip_common.c:393: {URB_FREE_BUFFER, USBIP_URB_FREE_BUFFER},
drivers/usb/usbip/usbip_common.c-394- {URB_DIR_IN, USBIP_URB_DIR_IN},
]
|
| 51/2 |
2026/11/24 09:39 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 60077
output: 87
thoughts: 4155
Thoughts: **Analysis of the Bug-Introducing Commit**
I've been carefully dissecting the commit history to pinpoint the exact commit that introduced this bug, and it's a bit nuanced. My primary focus is on two candidates: `564246fd3ff4` and `836e2548524d`.
Let's break down what each one did:
1. **`564246fd3ff4` ("media: siano: Fix coherent memory allocation failure on arm64", 2018)**:
* This commit altered `smscore_register_device` to use `kzalloc` for `dev->common_buffer` instead of `dma_alloc_coherent`. It also introduced `surb->urb.transfer_flags |= URB_FREE_BUFFER;` within `smsusb_submit_urb()`.
* The core issue here is setting `URB_FREE_BUFFER`. This flag tells the USB core to `kfree(urb->transfer_buffer)` when the URB is freed via `usb_free_urb()`. However, `surb->urb.transfer_buffer` pointed to `surb->cb->p`, which is a slice within `dev->common_buffer`. Crucially, `dev->common_buffer` itself is freed by `smscore_unregister_device()`. This creates a fundamental violation: `URB_FREE_BUFFER` expects `transfer_buffer` to be an independently `kmalloc`'d buffer owned by the URB, not a sub-section of a buffer already managed and freed elsewhere.
* Here's the twist for this commit: While the *logic* of setting `URB_FREE_BUFFER` was incorrect even in 2018, the *bug* didn't manifest. This is because, at that time, `surb->urb` was initialized using `usb_init_urb()`, and `usb_free_urb()` was never called on these URBs. Without `usb_free_urb()` being called, the `urb_destroy()` function, which processes the `URB_FREE_BUFFER` flag, was never executed. So, `564246fd3ff4` laid the groundwork for a bug, but it was latent. The commit message itself indicates a misunderstanding of `URB_FREE_BUFFER`, possibly thinking it related to DMA mapping rather than buffer ownership.
2. **`836e2548524d` ("media: usb: siano: Fix allocation of urbs", 2024)**:
* This commit made a significant change: it switched from embedded `struct urb` instances to dynamically allocated URBs using `usb_alloc_urb()` and subsequently freeing them with `usb_free_urb()` in `smsusb_term_device()`.
* This is where the latent bug from `564246fd3ff4` finally detonated. By calling `usb_free_urb()`, the `urb_destroy()` function was now executed. This function checks `urb->transfer_flags & URB_FREE_BUFFER`.
* When `urb_destroy()` found `URB_FREE_BUFFER` set (thanks to commit `564246fd3ff4`), it attempted to `kfree(urb->transfer_buffer)`.
* This `kfree()` call happened *after* `smscore_unregister_device()` had already freed `coredev->common_buffer`. This resulted in:
* A **double-free** for the first URB (where `urb->transfer_buffer` was `common_buffer`).
* Attempts to free **interior pointers** (`common_buffer + i * buffer_size`) of the now-freed `common_buffer` for all subsequent URBs.
* This clearly leads to page allocator corruption, double-free errors, and the observed crashes.
**Determining the "Bug-Introducing" Commit**
Now, the critical question: which commit is the *bug-introducing* one according to the user's rules?
* Rule 3 states: "Trace past pure refactorings, code movements, formatting changes, or variable renames to find the commit that actually introduced the buggy logic or broke an existing invariant."
* Rule 4 states: "Do NOT blame commits that merely added debugging or sanitizer checks... that exposed an older bug..."
Let's evaluate based on these rules:
* **`564246fd3ff4`**: This commit *introduced the buggy logic*. It set `URB_FREE_BUFFER` incorrectly, violating the contract for how `transfer_buffer` should be managed. The commit message itself shows a misunderstanding. However, this bug did *not* manifest as a crash or memory corruption at the time because the code path that would trigger it (`usb_free_urb()` leading to `urb_destroy()`) didn't exist. It was a "ticking time bomb."
* **`836e2548524d`**: This commit *exposed* the latent bug. It didn't introduce the faulty logic itself (the `URB_FREE_BUFFER` flag was already there and wrongly set). Instead, it created the *conditions* under which the faulty logic would execute and cause a crash. Before `836e2548524d`, the `URB_FREE_BUFFER` flag was effectively a no-op. After `836e2548524d`, it became lethal.
* **Commits like `a6687c8ff613f` (2025)**: These are explicitly excluded by Rule 4. They added debugging checks (`WARN_ON_ONCE(!PageLargeKmalloc(page))`) that caught the invalid `kfree()` operation by detecting page allocator corruption. They didn't *introduce* the bug; they merely made its detection more robust.
Considering the rules, especially Rule 3 focusing on the "buggy logic" or "broken invariant," I believe `564246fd3ff4` is the commit that *introduced* the flawed logic. While `836e2548524d` is the commit that made the bug observable and crash, it did so by activating already incorrect logic. If `564246fd3ff4` hadn't set `URB_FREE_BUFFER` incorrectly, `836e2548524d` would have been a completely benign refactoring.
Therefore, my conclusion is that **`564246fd3ff4`** is the commit that introduced the buggy logic, even though `836e2548524d` is the commit that caused the observable crash. I will adhere to Rule 3 and identify the one that put the faulty logic in place.
|
| 52/2 |
2026/11/24 09:39 |
tool |
git-show |
0m
Args:
Results: map[Output:commit b9c7141f384097fa4fa67d2f72e5731d628aef7c
Author: Dongliang Mu <dzm91@hust.edu.cn>
Date: Mon Feb 27 18:24:08 2023 +0800
drivers: usb: smsusb: fix error handling code in smsusb_init_device
The previous commit 4b208f8b561f ("[media] siano: register media controller
earlier")moves siano_media_device_register before smscore_register_device,
and adds corresponding error handling code if smscore_register_device
fails. However, it misses the following error handling code of
smsusb_init_device.
Fix this by moving error handling code at the end of smsusb_init_device
and adding a goto statement in the following error handling parts.
Fixes: 4b208f8b561f ("[media] siano: register media controller earlier")
Signed-off-by: Dongliang Mu <dzm91@hust.edu.cn>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
diff --git a/drivers/media/usb/siano/smsusb.c b/drivers/media/usb/siano/smsusb.c
index 640737d3b8ae..8a39cac76c58 100644
--- a/drivers/media/usb/siano/smsusb.c
+++ b/drivers/media/usb/siano/smsusb.c
@@ -455,12 +455,7 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)
rc = smscore_register_device(¶ms, &dev->coredev, 0, mdev);
if (rc < 0) {
pr_err("smscore_register_device(...) failed, rc %d\n", rc);
- smsusb_term_device(intf);
-#ifdef CONFIG_MEDIA_CONTROLLER_DVB
- media_device_unregister(mdev);
-#endif
- kfree(mdev);
- return rc;
+ goto err_unregister_device;
}
smscore_set_board_id(dev->coredev, board_id);
@@ -477,8 +472,7 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)
rc = smsusb_start_streaming(dev);
if (rc < 0) {
pr_err("smsusb_start_streaming(...) failed\n");
- smsusb_term_device(intf);
- return rc;
+ goto err_unregister_device;
}
dev->state = SMSUSB_ACTIVE;
@@ -486,13 +480,20 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)
rc = smscore_start_device(dev->coredev);
if (rc < 0) {
pr_err("smscore_start_device(...) failed\n");
- smsusb_term_device(intf);
- return rc;
+ goto err_unregister_device;
}
pr_debug("device 0x%p created\n", dev);
return rc;
+
+err_unregister_device:
+ smsusb_term_device(intf);
+#ifdef CONFIG_MEDIA_CONTROLLER_DVB
+ media_device_unregister(mdev);
+#endif
+ kfree(mdev);
+ return rc;
}
static int smsusb_probe(struct usb_interface *intf,
]
|
| 53/2 |
2026/11/24 09:39 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 6f489a966fbeb0da63d45c2c66a8957eab604bf6
Author: Duoming Zhou <duoming@zju.edu.cn>
Date: Tue May 23 07:59:32 2023 +0800
media: usb: siano: Fix warning due to null work_func_t function pointer
The previous commit ebad8e731c1c ("media: usb: siano: Fix use after
free bugs caused by do_submit_urb") adds cancel_work_sync() in
smsusb_stop_streaming(). But smsusb_stop_streaming() may be called,
even if the work_struct surb->wq has not been initialized. As a result,
the warning will occur. One of the processes that could lead to warning
is shown below:
smsusb_probe()
smsusb_init_device()
if (!dev->in_ep || !dev->out_ep || align < 0) {
smsusb_term_device(intf);
smsusb_stop_streaming()
cancel_work_sync(&dev->surbs[i].wq);
__cancel_work_timer()
__flush_work()
if (WARN_ON(!work->func)) // work->func is null
The log reported by syzbot is shown below:
WARNING: CPU: 0 PID: 897 at kernel/workqueue.c:3066 __flush_work+0x798/0xa80 kernel/workqueue.c:3063
Modules linked in:
CPU: 0 PID: 897 Comm: kworker/0:2 Not tainted 6.2.0-rc1-syzkaller #0
RIP: 0010:__flush_work+0x798/0xa80 kernel/workqueue.c:3066
...
RSP: 0018:ffffc9000464ebf8 EFLAGS: 00010246
RAX: 1ffff11002dbb420 RBX: 0000000000000021 RCX: 1ffffffff204fa4e
RDX: dffffc0000000000 RSI: 0000000000000001 RDI: ffff888016dda0e8
RBP: ffffc9000464ed98 R08: 0000000000000001 R09: ffffffff90253b2f
R10: 0000000000000001 R11: 0000000000000000 R12: ffff888016dda0e8
R13: ffff888016dda0e8 R14: ffff888016dda100 R15: 0000000000000001
FS: 0000000000000000(0000) GS:ffff8880b9a00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007ffd4331efe8 CR3: 000000000b48e000 CR4: 00000000003506f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
<TASK>
__cancel_work_timer+0x315/0x460 kernel/workqueue.c:3160
smsusb_stop_streaming drivers/media/usb/siano/smsusb.c:182 [inline]
smsusb_term_device+0xda/0x2d0 drivers/media/usb/siano/smsusb.c:344
smsusb_init_device+0x400/0x9ce drivers/media/usb/siano/smsusb.c:419
smsusb_probe+0xbbd/0xc55 drivers/media/usb/siano/smsusb.c:567
...
This patch adds check before cancel_work_sync(). If surb->wq has not
been initialized, the cancel_work_sync() will not be executed.
Reported-by: syzbot+27b0b464864741b18b99@syzkaller.appspotmail.com
Fixes: ebad8e731c1c ("media: usb: siano: Fix use after free bugs caused by do_submit_urb")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
diff --git a/drivers/media/usb/siano/smsusb.c b/drivers/media/usb/siano/smsusb.c
index 6f443c542c6d..640737d3b8ae 100644
--- a/drivers/media/usb/siano/smsusb.c
+++ b/drivers/media/usb/siano/smsusb.c
@@ -179,7 +179,8 @@ static void smsusb_stop_streaming(struct smsusb_device_t *dev)
for (i = 0; i < MAX_URBS; i++) {
usb_kill_urb(&dev->surbs[i].urb);
- cancel_work_sync(&dev->surbs[i].wq);
+ if (dev->surbs[i].wq.func)
+ cancel_work_sync(&dev->surbs[i].wq);
if (dev->surbs[i].cb) {
smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
]
|
| 54/2 |
2026/11/24 09:39 |
tool |
git-show |
0m
Args:
Results: map[Output:commit ebad8e731c1c06adf04621d6fd327b860c0861b5
Author: Duoming Zhou <duoming@zju.edu.cn>
Date: Mon Jan 23 03:04:38 2023 +0100
media: usb: siano: Fix use after free bugs caused by do_submit_urb
There are UAF bugs caused by do_submit_urb(). One of the KASan reports
is shown below:
[ 36.403605] BUG: KASAN: use-after-free in worker_thread+0x4a2/0x890
[ 36.406105] Read of size 8 at addr ffff8880059600e8 by task kworker/0:2/49
[ 36.408316]
[ 36.408867] CPU: 0 PID: 49 Comm: kworker/0:2 Not tainted 6.2.0-rc3-15798-g5a41237ad1d4-dir8
[ 36.411696] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g15584
[ 36.416157] Workqueue: 0x0 (events)
[ 36.417654] Call Trace:
[ 36.418546] <TASK>
[ 36.419320] dump_stack_lvl+0x96/0xd0
[ 36.420522] print_address_description+0x75/0x350
[ 36.421992] print_report+0x11b/0x250
[ 36.423174] ? _raw_spin_lock_irqsave+0x87/0xd0
[ 36.424806] ? __virt_addr_valid+0xcf/0x170
[ 36.426069] ? worker_thread+0x4a2/0x890
[ 36.427355] kasan_report+0x131/0x160
[ 36.428556] ? worker_thread+0x4a2/0x890
[ 36.430053] worker_thread+0x4a2/0x890
[ 36.431297] ? worker_clr_flags+0x90/0x90
[ 36.432479] kthread+0x166/0x190
[ 36.433493] ? kthread_blkcg+0x50/0x50
[ 36.434669] ret_from_fork+0x22/0x30
[ 36.435923] </TASK>
[ 36.436684]
[ 36.437215] Allocated by task 24:
[ 36.438289] kasan_set_track+0x50/0x80
[ 36.439436] __kasan_kmalloc+0x89/0xa0
[ 36.440566] smsusb_probe+0x374/0xc90
[ 36.441920] usb_probe_interface+0x2d1/0x4c0
[ 36.443253] really_probe+0x1d5/0x580
[ 36.444539] __driver_probe_device+0xe3/0x130
[ 36.446085] driver_probe_device+0x49/0x220
[ 36.447423] __device_attach_driver+0x19e/0x1b0
[ 36.448931] bus_for_each_drv+0xcb/0x110
[ 36.450217] __device_attach+0x132/0x1f0
[ 36.451470] bus_probe_device+0x59/0xf0
[ 36.452563] device_add+0x4ec/0x7b0
[ 36.453830] usb_set_configuration+0xc63/0xe10
[ 36.455230] usb_generic_driver_probe+0x3b/0x80
[ 36.456166] printk: console [ttyGS0] disabled
[ 36.456569] usb_probe_device+0x90/0x110
[ 36.459523] really_probe+0x1d5/0x580
[ 36.461027] __driver_probe_device+0xe3/0x130
[ 36.462465] driver_probe_device+0x49/0x220
[ 36.463847] __device_attach_driver+0x19e/0x1b0
[ 36.465229] bus_for_each_drv+0xcb/0x110
[ 36.466466] __device_attach+0x132/0x1f0
[ 36.467799] bus_probe_device+0x59/0xf0
[ 36.469010] device_add+0x4ec/0x7b0
[ 36.470125] usb_new_device+0x863/0xa00
[ 36.471374] hub_event+0x18c7/0x2220
[ 36.472746] process_one_work+0x34c/0x5b0
[ 36.474041] worker_thread+0x4b7/0x890
[ 36.475216] kthread+0x166/0x190
[ 36.476267] ret_from_fork+0x22/0x30
[ 36.477447]
[ 36.478160] Freed by task 24:
[ 36.479239] kasan_set_track+0x50/0x80
[ 36.480512] kasan_save_free_info+0x2b/0x40
[ 36.481808] ____kasan_slab_free+0x122/0x1a0
[ 36.483173] __kmem_cache_free+0xc4/0x200
[ 36.484563] smsusb_term_device+0xcd/0xf0
[ 36.485896] smsusb_probe+0xc85/0xc90
[ 36.486976] usb_probe_interface+0x2d1/0x4c0
[ 36.488303] really_probe+0x1d5/0x580
[ 36.489498] __driver_probe_device+0xe3/0x130
[ 36.491140] driver_probe_device+0x49/0x220
[ 36.492475] __device_attach_driver+0x19e/0x1b0
[ 36.493988] bus_for_each_drv+0xcb/0x110
[ 36.495171] __device_attach+0x132/0x1f0
[ 36.496617] bus_probe_device+0x59/0xf0
[ 36.497875] device_add+0x4ec/0x7b0
[ 36.498972] usb_set_configuration+0xc63/0xe10
[ 36.500264] usb_generic_driver_probe+0x3b/0x80
[ 36.501740] usb_probe_device+0x90/0x110
[ 36.503084] really_probe+0x1d5/0x580
[ 36.504241] __driver_probe_device+0xe3/0x130
[ 36.505548] driver_probe_device+0x49/0x220
[ 36.506766] __device_attach_driver+0x19e/0x1b0
[ 36.508368] bus_for_each_drv+0xcb/0x110
[ 36.509646] __device_attach+0x132/0x1f0
[ 36.510911] bus_probe_device+0x59/0xf0
[ 36.512103] device_add+0x4ec/0x7b0
[ 36.513215] usb_new_device+0x863/0xa00
[ 36.514736] hub_event+0x18c7/0x2220
[ 36.516130] process_one_work+0x34c/0x5b0
[ 36.517396] worker_thread+0x4b7/0x890
[ 36.518591] kthread+0x166/0x190
[ 36.519599] ret_from_fork+0x22/0x30
[ 36.520851]
[ 36.521405] Last potentially related work creation:
[ 36.523143] kasan_save_stack+0x3f/0x60
[ 36.524275] kasan_record_aux_stack_noalloc+0x9d/0xb0
[ 36.525831] insert_work+0x25/0x130
[ 36.527039] __queue_work+0x4d4/0x620
[ 36.528236] queue_work_on+0x72/0xb0
[ 36.529344] __usb_hcd_giveback_urb+0x13f/0x1b0
[ 36.530819] dummy_timer+0x350/0x1a40
[ 36.532149] call_timer_fn+0x2c/0x190
[ 36.533567] expire_timers+0x69/0x1f0
[ 36.534736] __run_timers+0x289/0x2d0
[ 36.535841] run_timer_softirq+0x2d/0x60
[ 36.537110] __do_softirq+0x116/0x380
[ 36.538377]
[ 36.538950] Second to last potentially related work creation:
[ 36.540855] kasan_save_stack+0x3f/0x60
[ 36.542084] kasan_record_aux_stack_noalloc+0x9d/0xb0
[ 36.543592] insert_work+0x25/0x130
[ 36.544891] __queue_work+0x4d4/0x620
[ 36.546168] queue_work_on+0x72/0xb0
[ 36.547328] __usb_hcd_giveback_urb+0x13f/0x1b0
[ 36.548805] dummy_timer+0x350/0x1a40
[ 36.550116] call_timer_fn+0x2c/0x190
[ 36.551570] expire_timers+0x69/0x1f0
[ 36.552762] __run_timers+0x289/0x2d0
[ 36.553916] run_timer_softirq+0x2d/0x60
[ 36.555118] __do_softirq+0x116/0x380
[ 36.556239]
[ 36.556807] The buggy address belongs to the object at ffff888005960000
[ 36.556807] which belongs to the cache kmalloc-4k of size 4096
[ 36.560652] The buggy address is located 232 bytes inside of
[ 36.560652] 4096-byte region [ffff888005960000, ffff888005961000)
[ 36.564791]
[ 36.565355] The buggy address belongs to the physical page:
[ 36.567212] page:000000004f0a0731 refcount:1 mapcount:0 mapping:0000000000000000 index:0x00
[ 36.570534] head:000000004f0a0731 order:3 compound_mapcount:0 subpages_mapcount:0 compound0
[ 36.573717] flags: 0x100000000010200(slab|head|node=0|zone=1)
[ 36.575481] raw: 0100000000010200 ffff888001042140 dead000000000122 0000000000000000
[ 36.577842] raw: 0000000000000000 0000000000040004 00000001ffffffff 0000000000000000
[ 36.580175] page dumped because: kasan: bad access detected
[ 36.581994]
[ 36.582548] Memory state around the buggy address:
[ 36.583983] ffff88800595ff80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
[ 36.586240] ffff888005960000: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 36.588884] >ffff888005960080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 36.591071] ^
[ 36.593295] ffff888005960100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 36.595705] ffff888005960180: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 36.598026] ==================================================================
[ 36.600224] Disabling lock debugging due to kernel taint
[ 36.602681] general protection fault, probably for non-canonical address 0x43600a000000060I
[ 36.607129] CPU: 0 PID: 49 Comm: kworker/0:2 Tainted: G B 6.2.0-rc3-15798-8
[ 36.611115] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g15584
[ 36.615026] Workqueue: events do_submit_urb
[ 36.616290] RIP: 0010:_raw_spin_lock_irqsave+0x8a/0xd0
[ 36.618107] Code: 24 00 00 00 00 48 89 df be 04 00 00 00 e8 9e b5 c6 fe 48 89 ef be 04 00 5
[ 36.623522] RSP: 0018:ffff888004b6fcf0 EFLAGS: 00010046
[ 36.625072] RAX: 0000000000000000 RBX: 043600a000000060 RCX: ffffffff9fc0e0d7
[ 36.627206] RDX: 0000000000000000 RSI: dffffc0000000000 RDI: ffff888004b6fcf0
[ 36.629813] RBP: ffff888004b6fcf0 R08: dffffc0000000000 R09: ffffed100096df9f
[ 36.631974] R10: dfffe9100096dfa0 R11: 1ffff1100096df9e R12: ffff888005960020
[ 36.634285] R13: ffff8880059600f0 R14: 0000000000000246 R15: 0000000000000001
[ 36.636438] FS: 0000000000000000(0000) GS:ffff88806d600000(0000) knlGS:0000000000000000
[ 36.639092] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 36.640951] CR2: 00007f07476819a3 CR3: 0000000004a34000 CR4: 00000000000006f0
[ 36.643411] Call Trace:
[ 36.644215] <TASK>
[ 36.644902] smscore_getbuffer+0x3e/0x1e0
[ 36.646147] do_submit_urb+0x4f/0x190
[ 36.647449] process_one_work+0x34c/0x5b0
[ 36.648777] worker_thread+0x4b7/0x890
[ 36.649984] ? worker_clr_flags+0x90/0x90
[ 36.651166] kthread+0x166/0x190
[ 36.652151] ? kthread_blkcg+0x50/0x50
[ 36.653547] ret_from_fork+0x22/0x30
[ 36.655051] </TASK>
[ 36.655733] Modules linked in:
[ 36.656787] ---[ end trace 0000000000000000 ]---
[ 36.658328] RIP: 0010:_raw_spin_lock_irqsave+0x8a/0xd0
[ 36.660045] Code: 24 00 00 00 00 48 89 df be 04 00 00 00 e8 9e b5 c6 fe 48 89 ef be 04 00 5
[ 36.665730] RSP: 0018:ffff888004b6fcf0 EFLAGS: 00010046
[ 36.667448] RAX: 0000000000000000 RBX: 043600a000000060 RCX: ffffffff9fc0e0d7
[ 36.669675] RDX: 0000000000000000 RSI: dffffc0000000000 RDI: ffff888004b6fcf0
[ 36.672645] RBP: ffff888004b6fcf0 R08: dffffc0000000000 R09: ffffed100096df9f
[ 36.674921] R10: dfffe9100096dfa0 R11: 1ffff1100096df9e R12: ffff888005960020
[ 36.677034] R13: ffff8880059600f0 R14: 0000000000000246 R15: 0000000000000001
[ 36.679184] FS: 0000000000000000(0000) GS:ffff88806d600000(0000) knlGS:0000000000000000
[ 36.681655] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 36.683383] CR2: 00007f07476819a3 CR3: 0000000004a34000 CR4: 00000000000006f0
[ 36.685733] Kernel panic - not syncing: Fatal exception
[ 36.688585] Kernel Offset: 0x1d400000 from 0xffffffff81000000 (relocation range: 0xfffffff)
[ 36.692199] ---[ end Kernel panic - not syncing: Fatal exception ]---
When the siano device is plugged in, it may call the following functions
to initialize the device.
smsusb_probe()-->smsusb_init_device()-->smscore_start_device().
When smscore_start_device() gets failed, the function smsusb_term_device()
will be called and smsusb_device_t will be deallocated. Although we use
usb_kill_urb() in smsusb_stop_streaming() to cancel transfer requests
and wait for them to finish, the worker threads that are scheduled by
smsusb_onresponse() may be still running. As a result, the UAF bugs
could happen.
We add cancel_work_sync() in smsusb_stop_streaming() in order that the
worker threads could finish before the smsusb_device_t is deallocated.
Fixes: dd47fbd40e6e ("[media] smsusb: don't sleep while atomic")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
diff --git a/drivers/media/usb/siano/smsusb.c b/drivers/media/usb/siano/smsusb.c
index fe9c7b3a950e..6f443c542c6d 100644
--- a/drivers/media/usb/siano/smsusb.c
+++ b/drivers/media/usb/siano/smsusb.c
@@ -179,6 +179,7 @@ static void smsusb_stop_streaming(struct smsusb_device_t *dev)
for (i = 0; i < MAX_URBS; i++) {
usb_kill_urb(&dev->surbs[i].urb);
+ cancel_work_sync(&dev->surbs[i].wq);
if (dev->surbs[i].cb) {
smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
]
|
| 55/2 |
2026/11/24 09:39 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 74016
output: 38
thoughts: 191
Thoughts: **Initial Diff Examination and Hypothesis Formation**
Alright, my immediate thought is to meticulously scrutinize the differences between these two commits: `836e2548524d` and `564246fd3ff4`. I'm specifically zeroing in on the commit from 2018, `564246fd3ff4`, titled "media: siano: Fix coherent memory allocation failure on arm64."
My focus is drawn to a particular change introduced in `smsusb_submit_urb()`: the addition of `surb->urb.transfer_flags |= URB_FREE_BUFFER;`. This line immediately sparks a question in my mind: why is setting `URB_FREE_BUFFER` within `smsusb_submit_urb()` considered a bug? It seems counterintuitive, as the flag's name suggests it's meant for freeing buffers. This leads me to suspect that its placement or interaction in this specific context is problematic.
To understand this potential issue fully, I need to consult the definitive source. I'll be diving into `include/linux/usb.h`, specifically looking at the documentation surrounding `URB_FREE_BUFFER`. My goal is to understand the precise semantics of this flag and how it's intended to be used, which will then help me pinpoint why its inclusion in `smsusb_submit_urb()` by the 2018 commit might be flawed.
|
| 56/2 |
2026/11/24 09:39 |
tool |
grepper |
0m
Args: map[Expression:URB_FREE_BUFFER PathPrefixes:[include/linux/usb.h]]
Results: map[Output:include/linux/usb.h=1381=extern int usb_disabled(void);
--
include/linux/usb.h-1400- * needed */
include/linux/usb.h:1401:#define URB_FREE_BUFFER 0x0100 /* Free transfer buffer with the URB */
include/linux/usb.h-1402-
]
|
| 57/2 |
2026/11/24 09:39 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 74353
output: 25
thoughts: 2130
Thoughts: **My Investigation into the URB Buffer Freeing Bug**
My investigation focuses on a critical bug related to the freeing of USB transfer buffers within the Linux kernel's USB subsystem, specifically concerning the Siano driver. The core issue lies in the interaction between `URB_FREE_BUFFER` flag and how transfer buffers are managed.
Initially, I examined the definition of `URB_FREE_BUFFER` in `include/linux/usb.h` and its usage within `drivers/usb/core/urb.c`'s `urb_destroy` function and `usb_free_urb`. The `URB_FREE_BUFFER` flag signals that the URB's `transfer_buffer` should be freed along with the URB itself when `urb_destroy` is invoked.
My attention was drawn to commit `564246fd3ff4` by Tomoki Sekiyama. This commit introduced the `URB_FREE_BUFFER` flag with the stated intent of having the USB core handle DMA mapping and freeing of allocated buffer memory. However, my analysis reveals that this commit also inadvertently introduced a bug. The underlying issue is that `564246fd3ff4` applied `URB_FREE_BUFFER` to an URB whose `transfer_buffer` was not an independently allocated buffer via `kmalloc`. Instead, it was a sub-buffer (`cb->p`) carved out from a larger buffer (`coredev->common_buffer`) that was managed and freed separately by `smscore_unregister_device()`. This directly violates the fundamental invariant of `URB_FREE_BUFFER`, which requires the `transfer_buffer` to be an independently owned `kmalloc` buffer.
A crucial question arose: why didn't this bug manifest and cause a crash at the time of `564246fd3ff4`? The reason, I discovered, is that the `smsusb.c` driver at that time did *not* use `usb_alloc_urb()` and `usb_free_urb()`. Instead, it used `usb_init_urb()` and handled freeing the main device structure (`dev`) with `kfree(dev)`. Consequently, `urb_destroy()` was *never* called, and thus the problematic `kfree(urb->transfer_buffer)` was never executed.
Fast forward to 2024, commit `836e2548524d` ("media: usb: siano: Fix allocation of urbs") modified `smsusb.c`. This commit changed the URB allocation mechanism to use `usb_alloc_urb(0, GFP_KERNEL)` and the freeing mechanism to use `usb_free_urb(dev->surbs[i].urb)` within `smsusb_term_device()`. The introduction of `usb_free_urb()` meant that `urb_destroy()` was now being called on these URBs. This is where the latent bug from `564246fd3ff4` finally surfaced. When `urb_destroy()` was called, it attempted to `kfree(urb->transfer_buffer)` on pointers that were no longer validβthey pointed into `coredev->common_buffer` which had already been freed by `smscore_unregister_device()`. This led to the observed crashes due to double-freeing or freeing invalid memory.
Now, the critical decision is which commit should be considered the root cause. Based on the investigation guidelines:
1. Commit `564246fd3ff4` is the one that *introduced the buggy logic* by setting `URB_FREE_BUFFER` on an inappropriate buffer. It broke an existing invariant.
2. Commit `836e2548524d` is a commit that *exposed or enabled the code path* that triggered the bug. It didn't introduce the buggy logic itself, but rather made the previously dormant bug active by properly calling `usb_free_urb()`.
Therefore, I conclude that `564246fd3ff4` is the primary commit that introduced the bug. The fix for this bug would be to *remove* the line `surb->urb->transfer_flags |= URB_FREE_BUFFER;` from `smsusb_submit_urb()`, as the `transfer_buffer` is managed by the core device logic and not by the URB's lifecycle.
|
| 58/2 |
2026/11/24 09:40 |
tool |
grepper |
0m
Args: map[Expression:URB_FREE_BUFFER]
Results: map[Output:
Full output is too long, showing 200 out of 221 lines.
Use more precise expression or PathPrefixes if possible.
Number of matching lines per file (22 files in total):
drivers/bluetooth/bpa10x.c:2
drivers/bluetooth/btmtk.c:2
drivers/bluetooth/btusb_main.c:4
drivers/media/usb/em28xx/em28xx-core.c:1
drivers/media/usb/siano/smsusb.c:1
drivers/net/can/usb/f81604.c:3
drivers/net/can/usb/gs_usb.c:2
drivers/net/can/usb/peak_usb/pcan_usb_core.c:2
drivers/net/usb/lan78xx.c:1
drivers/net/usb/usbnet.c:2
drivers/net/wireless/ath/carl9170/usb.c:3
drivers/net/wireless/intersil/p54/p54usb.c:2
drivers/net/wireless/zydas/zd1211rw/zd_usb.c:1
drivers/nfc/nfcmrvl/usb.c:1
drivers/usb/class/usblp.c:1
drivers/usb/class/usbtmc.c:1
drivers/usb/core/urb.c:3
drivers/usb/storage/uas.c:2
drivers/usb/usbip/stub_rx.c:1
drivers/usb/usbip/usbip_common.c:1
include/linux/usb.h:1
include/uapi/linux/usbip.h:1
drivers/bluetooth/bpa10x.c=121=static inline int bpa10x_submit_intr_urb(struct hci_dev *hdev)
--
drivers/bluetooth/bpa10x.c-145-
drivers/bluetooth/bpa10x.c:146: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/bluetooth/bpa10x.c-147-
--
drivers/bluetooth/bpa10x.c=161=static inline int bpa10x_submit_bulk_urb(struct hci_dev *hdev)
--
drivers/bluetooth/bpa10x.c-185-
drivers/bluetooth/bpa10x.c:186: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/bluetooth/bpa10x.c-187-
--
drivers/bluetooth/btmtk.c=650=static int btmtk_usb_submit_wmt_recv_urb(struct hci_dev *hdev)
--
drivers/bluetooth/btmtk.c-686-
drivers/bluetooth/btmtk.c:687: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/bluetooth/btmtk.c-688-
--
drivers/bluetooth/btmtk.c=1658=static int btmtk_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
--
drivers/bluetooth/btmtk.c-1688-
drivers/bluetooth/btmtk.c:1689: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/bluetooth/btmtk.c-1690-
--
drivers/bluetooth/btusb_main.c=1650=static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
--
drivers/bluetooth/btusb_main.c-1687-
drivers/bluetooth/btusb_main.c:1688: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/bluetooth/btusb_main.c-1689-
--
drivers/bluetooth/btusb_main.c=1772=static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
--
drivers/bluetooth/btusb_main.c-1799-
drivers/bluetooth/btusb_main.c:1800: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/bluetooth/btusb_main.c-1801-
--
drivers/bluetooth/btusb_main.c=1932=static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
--
drivers/bluetooth/btusb_main.c-1962-
drivers/bluetooth/btusb_main.c:1963: urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
drivers/bluetooth/btusb_main.c-1964-
--
drivers/bluetooth/btusb_main.c=2024=static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
--
drivers/bluetooth/btusb_main.c-2051-
drivers/bluetooth/btusb_main.c:2052: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/bluetooth/btusb_main.c-2053-
--
drivers/media/usb/em28xx/em28xx-core.c=1008=int em28xx_alloc_urbs(struct em28xx *dev, enum em28xx_mode mode, int xfer_bulk,
--
drivers/media/usb/em28xx/em28xx-core.c-1089-
drivers/media/usb/em28xx/em28xx-core.c:1090: urb->transfer_flags = URB_FREE_BUFFER;
drivers/media/usb/em28xx/em28xx-core.c-1091-
--
drivers/media/usb/siano/smsusb.c=150=static int smsusb_submit_urb(struct smsusb_device_t *dev,
--
drivers/media/usb/siano/smsusb.c-170- );
drivers/media/usb/siano/smsusb.c:171: surb->urb->transfer_flags |= URB_FREE_BUFFER;
drivers/media/usb/siano/smsusb.c-172-
--
drivers/net/can/usb/f81604.c=679=static int f81604_register_urbs(struct f81604_port_priv *priv)
--
drivers/net/can/usb/f81604.c-709-
drivers/net/can/usb/f81604.c:710: rx_urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/can/usb/f81604.c-711- usb_anchor_urb(rx_urb, &priv->urbs_anchor);
--
drivers/net/can/usb/f81604.c-748-
drivers/net/can/usb/f81604.c:749: int_urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/can/usb/f81604.c-750- usb_anchor_urb(int_urb, &priv->urbs_anchor);
--
drivers/net/can/usb/f81604.c=938=static netdev_tx_t f81604_start_xmit(struct sk_buff *skb,
--
drivers/net/can/usb/f81604.c-966-
drivers/net/can/usb/f81604.c:967: write_urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/can/usb/f81604.c-968-
--
drivers/net/can/usb/gs_usb.c=838=static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
--
drivers/net/can/usb/gs_usb.c-905-
drivers/net/can/usb/gs_usb.c:906: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/can/usb/gs_usb.c-907- usb_anchor_urb(urb, &dev->tx_submitted);
--
drivers/net/can/usb/gs_usb.c=951=static int gs_can_open(struct net_device *netdev)
--
drivers/net/can/usb/gs_usb.c-1011- gs_usb_receive_bulk_callback, parent);
drivers/net/can/usb/gs_usb.c:1012: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/can/usb/gs_usb.c-1013-
--
drivers/net/can/usb/peak_usb/pcan_usb_core.c=435=static int peak_usb_start(struct peak_usb_device *dev)
--
drivers/net/can/usb/peak_usb/pcan_usb_core.c-463- /* ask last usb_free_urb() to also kfree() transfer_buffer */
drivers/net/can/usb/peak_usb/pcan_usb_core.c:464: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/can/usb/peak_usb/pcan_usb_core.c-465- usb_anchor_urb(urb, &dev->rx_submitted);
--
drivers/net/can/usb/peak_usb/pcan_usb_core.c-520- /* ask last usb_free_urb() to also kfree() transfer_buffer */
drivers/net/can/usb/peak_usb/pcan_usb_core.c:521: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/can/usb/peak_usb/pcan_usb_core.c-522- }
--
drivers/net/usb/lan78xx.c=4659=static int lan78xx_probe(struct usb_interface *intf,
--
drivers/net/usb/lan78xx.c-4776- intr_complete, dev, period);
drivers/net/usb/lan78xx.c:4777: dev->urb_intr->transfer_flags |= URB_FREE_BUFFER;
drivers/net/usb/lan78xx.c-4778-
--
drivers/net/usb/usbnet.c=223=static int init_status(struct usbnet *dev, struct usb_interface *intf)
--
drivers/net/usb/usbnet.c-248- buf, maxp, intr_complete, dev, period);
drivers/net/usb/usbnet.c:249: dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
drivers/net/usb/usbnet.c-250- dev_dbg(&intf->dev,
--
drivers/net/usb/usbnet.c=2213=int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
--
drivers/net/usb/usbnet.c-2251- usbnet_async_cmd_cb, req);
drivers/net/usb/usbnet.c:2252: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/usb/usbnet.c-2253-
--
drivers/net/wireless/ath/carl9170/usb.c=466=static struct urb *carl9170_usb_alloc_rx_urb(struct ar9170 *ar, gfp_t gfp)
--
drivers/net/wireless/ath/carl9170/usb.c-484-
drivers/net/wireless/ath/carl9170/usb.c:485: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/wireless/ath/carl9170/usb.c-486-
--
drivers/net/wireless/ath/carl9170/usb.c=490=static int carl9170_usb_send_rx_irq_urb(struct ar9170 *ar)
--
drivers/net/wireless/ath/carl9170/usb.c-507-
drivers/net/wireless/ath/carl9170/usb.c:508: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/wireless/ath/carl9170/usb.c-509-
--
drivers/net/wireless/ath/carl9170/usb.c=611=int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd,
--
drivers/net/wireless/ath/carl9170/usb.c-644- if (free_buf)
drivers/net/wireless/ath/carl9170/usb.c:645: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/wireless/ath/carl9170/usb.c-646-
--
drivers/net/wireless/intersil/p54/p54usb.c=323=static void p54u_tx_net2280(struct ieee80211_hw *dev, struct sk_buff *skb)
--
drivers/net/wireless/intersil/p54/p54usb.c-355- /*
drivers/net/wireless/intersil/p54/p54usb.c:356: * URB_FREE_BUFFER triggers a code path in the USB subsystem that will
drivers/net/wireless/intersil/p54/p54usb.c-357- * free what is inside the transfer_buffer after the last reference to
--
drivers/net/wireless/intersil/p54/p54usb.c-359- */
drivers/net/wireless/intersil/p54/p54usb.c:360: int_urb->transfer_flags |= URB_FREE_BUFFER | URB_ZERO_PACKET;
drivers/net/wireless/intersil/p54/p54usb.c-361- reg = NULL;
--
drivers/net/wireless/zydas/zd1211rw/zd_usb.c=1868=int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
--
drivers/net/wireless/zydas/zd1211rw/zd_usb.c-1924-
drivers/net/wireless/zydas/zd1211rw/zd_usb.c:1925: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/net/wireless/zydas/zd1211rw/zd_usb.c-1926-
--
drivers/nfc/nfcmrvl/usb.c=102=nfcmrvl_submit_bulk_urb(struct nfcmrvl_usb_drv_data *drv_data, gfp_t mem_flags)
--
drivers/nfc/nfcmrvl/usb.c-127-
drivers/nfc/nfcmrvl/usb.c:128: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/nfc/nfcmrvl/usb.c-129-
--
drivers/usb/class/usblp.c=721=static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length)
--
drivers/usb/class/usblp.c-738- writebuf, transfer_length, usblp_bulk_write, usblp);
drivers/usb/class/usblp.c:739: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/usb/class/usblp.c-740-
--
drivers/usb/class/usbtmc.c=756=static struct urb *usbtmc_create_urb(void)
--
drivers/usb/class/usbtmc.c-772- urb->transfer_buffer_length = bufsize;
drivers/usb/class/usbtmc.c:773: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/usb/class/usbtmc.c-774- return urb;
--
drivers/usb/core/urb.c=20=static void urb_destroy(struct kref *kref)
--
drivers/usb/core/urb.c-23-
drivers/usb/core/urb.c:24: if (urb->transfer_flags & URB_FREE_BUFFER)
drivers/usb/core/urb.c-25- kfree(urb->transfer_buffer);
--
drivers/usb/core/urb.c=81=EXPORT_SYMBOL_GPL(usb_alloc_urb);
--
drivers/usb/core/urb.c-90- * Note: The transfer buffer associated with the urb is not freed unless the
drivers/usb/core/urb.c:91: * URB_FREE_BUFFER transfer flag is set.
drivers/usb/core/urb.c-92- */
--
drivers/usb/core/urb.c=367=int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
--
drivers/usb/core/urb.c-512- allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT | URB_DIR_MASK |
drivers/usb/core/urb.c:513: URB_FREE_BUFFER);
drivers/usb/core/urb.c-514- switch (xfertype) {
--
drivers/usb/storage/uas.c=472=static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
--
drivers/usb/storage/uas.c-490- urb->stream_id = cmdinfo->uas_tag;
drivers/usb/storage/uas.c:491: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/usb/storage/uas.c-492- out:
--
drivers/usb/storage/uas.c=499=static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
--
drivers/usb/storage/uas.c-528- uas_cmd_cmplt, NULL);
drivers/usb/storage/uas.c:529: urb->transfer_flags |= URB_FREE_BUFFER;
drivers/usb/storage/uas.c-530- out:
--
drivers/usb/usbip/stub_rx.c=399=static void masking_bogus_flags(struct urb *urb)
--
]
|
| 59/2 |
2026/11/24 09:40 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 81225
output: 42
thoughts: 4176
Thoughts: **The Crucial Moment: Unraveling the `URB_FREE_BUFFER` Enigma**
My immediate thought upon examining the code was a flash of insight: "Aha! Look at that!" It struck me that `URB_FREE_BUFFER` is a very peculiar flag. After a thorough scan, I discovered that it's *exclusively* used by `urb_destroy()` in `drivers/usb/core/urb.c`. Nowhere else in the entire kernel does anything directly manipulate this flag, except for the allowed flags mask in `usb_submit_urb`. This isolation immediately raised a red flag in my mind.
Then came the critical task of discerning which of the two commits, `564246fd3ff4` or `836e2548524d`, was the true culprit. I needed to dissect them carefully, re-reading each commit message and the provided instructions with an expert's rigor.
First, I focused on `564246fd3ff4`, titled "media: siano: Fix coherent memory allocation failure on arm64". My analysis revealed two key points:
1. This commit introduced `surb->urb.transfer_flags |= URB_FREE_BUFFER;` into `smsusb_submit_urb()`. This was problematic because `surb->cb->p` is, in reality, a sub-slice of `coredev->common_buffer`. This `common_buffer` is managed and freed by `smscore_unregister_device()`, not by the URB itself.
2. The fundamental flaw in `564246fd3ff4` was setting `URB_FREE_BUFFER` on an URB whose `transfer_buffer` was part of `common_buffer`. This creates a direct conflict with the buffer's lifecycle, as it would be freed twice β once by `smscore_unregister_device()` and again by `urb_destroy()`.
The question then became: why didn't this manifest as a crash until commit `836e2548524d`? I realized that, prior to `836e2548524d`, `smsusb.c` used `usb_init_urb()` for its embedded `struct urb`. Crucially, `usb_free_urb()` was never called, meaning `urb_destroy()` was never executed. Consequently, the buggy `URB_FREE_BUFFER` flag remained dormant.
Commit `836e2548524d`, titled "media: usb: siano: Fix allocation of urbs", changed this dynamic. It switched from `usb_init_urb()` to the standard `usb_alloc_urb()` and `usb_free_urb()` pair. This change wasn't intended to alter the buffer ownership model or how `cb->p` was freed; it was a necessary API compliance step. However, by introducing calls to `usb_free_urb()`, it inadvertently triggered `urb_destroy()`, which then finally executed the problematic `kfree(urb->transfer_buffer)` logic, exposing the bug introduced by `564246fd3ff4`.
However, I had to consider the alternative: could `836e2548524d` itself be the commit that *introduced* the bug? I revisited the instructions, particularly the one stating: "Trace past pure refactorings, code movements, formatting changes, or variable renames to find the commit that actually introduced the buggy logic or broke an existing invariant." Was `836e2548524d` a mere refactoring?
It did change `struct urb urb;` to `struct urb *urb;` and introduced `usb_alloc_urb()`/`usb_free_urb()` to address a smatch warning and align with USB core practices. But the critical point remained: *before* `836e2548524d`, `smsusb.c` did not call `usb_free_urb()`, rendering `URB_FREE_BUFFER` effectively inert.
This led me to simulate the behavior of `git bisect` with the provided syzkaller reproducer, which targets the `free_large_kmalloc` / double-free / invalid `kfree()` scenario. If I were to bisect, I'd need to determine when the invalid `kfree()` (the actual memory corruption bug) first became possible.
Before `836e2548524d`, the `kfree(urb->transfer_buffer)` within `urb_destroy()` could *never* be reached. On `836e2548524d`, however, `kfree(urb->transfer_buffer)` is invoked on *every* `smsusb_term_device()` call.
Testing this hypothesis against the reproducer:
* On `836e2548524d^` (the state before), *no bug* occurs at runtime because `urb_destroy()` isn't called.
* On `836e2548524d`, the situation changes drastically. `smsusb_term_device()` proceeds to double-free `common_buffer` and, more critically, frees nine interior pointers into `common_buffer`. This is where the KASAN/DEBUG_VM sanitizer would flag the invalid `kfree()` or page refcount corruption.
My mind then shifted to the "cause bisection" principle in kernel development, specifically regarding `Fixes:` tags. What is the *purpose* of cause bisection? To pinpoint the commit that *introduced the bug*.
I considered the common patterns in Linux kernel history: Commit X introduces a latent bug (e.g., a misused flag), and Commit Y enables a caller or a proper cleanup mechanism, making the bug manifest.
Then, the output rules for my task became paramount: "Do NOT include secondary or 'contributing' commits such as earlier background/foundation commits, preparatory or follow-up commits in the same series, later refactorings, incomplete fix attempts, or **commits that merely exposed or enabled the code path (e.g. new callers, default config changes, or sanitizer/debug checks)**."
This rule was the key.
* `564246fd3ff4` is the commit that **actually introduced the buggy logic**. It added `surb->urb.transfer_flags |= URB_FREE_BUFFER;` despite `surb->cb->p` being a sub-buffer of `common_buffer`, which is managed by `smscore_unregister_device()`. This was fundamentally incorrect from its inception.
* `836e2548524d` is the commit that **merely exposed or enabled the `urb_destroy()` code path**. By switching to `usb_alloc_urb()`/`usb_free_urb()`, it made the `if (urb->transfer_flags & URB_FREE_BUFFER) kfree(urb->transfer_buffer)` conditional branch runnable.
* `a6687c8ff613f` is a later commit that added a sanitizer/debug check, catching the invalid `kfree()` more cleanly.
I then thoroughly examined the opposing argument: could `836e2548524d` be primary, with `564246fd3ff4` as an "earlier background commit"? I rejected this. An "earlier background commit" typically introduces benign code that is later misused. Was the `URB_FREE_BUFFER` flag addition in `564246fd3ff4` benign on its own? Absolutely not. It was *wrong from the start*.
Let's re-verify the specifics of `564246fd3ff4`:
1. `URB_FREE_BUFFER` means `kfree(urb->transfer_buffer)` in `urb_destroy()`.
2. `surb->cb->p` should *never* be freed by the URB. It's reused, it's an interior pointer to `common_buffer`, and `common_buffer` is already freed by `smscore_unregister_device()`. Passing an interior pointer to `kfree()` is a cardinal sin.
3. The commit message of `564246fd3ff4` explicitly states the intent was for the USB core to handle freeing the buffer via `URB_FREE_BUFFER`. This intention was based on a misunderstanding of the buffer ownership model, compounded by the fact that the buggy flag was dormant until `836e2548524d`.
And what about `836e2548524d`?
1. It did *not* introduce `URB_FREE_BUFFER`. It only changed `.` to `->` for pointer dereferencing.
2. It did *not* introduce the `kfree(coredev->common_buffer)` in `smscore_unregister_device()`.
3. Its sole purpose was to switch to `usb_alloc_urb()`/`usb_free_urb()`.
4. This switch was the direct cause of `urb_destroy()` being called on URBs with the `URB_FREE_BUFFER` flag set, thus *triggering* the double-free bug for the first time.
The critical insight, then, came from considering `git bisect` again. If a KASAN/DEBUG_VM reproducer is used, `git bisect` would indeed land on `836e2548524d`. This is because the bug *manifests* at that point. However, the *root cause* β the introduction of the logically flawed flag β lies with `564246fd3ff4`. The instruction to find the commit that "actually introduced the buggy logic or broke an existing invariant" strongly points to the latter.
While `836e2548524d` is where the bug *starts to crash*, it did not introduce the faulty logic itself. It merely activated a dormant, fundamentally flawed mechanism. Therefore, `564246fd3ff4` is the commit that *introduced* the bug's logic.
I also took a moment to verify the device specifics mentioned in the reproducer (vendor/product IDs, configuration, interface, and endpoints) against the `sms_get_board(SMS1XXX_BOARD_SIANO_STELLAR)` function in `drivers/media/common/siano/sms-cards.c` to ensure I was examining the correct code path, which I confirmed. The device indeed matches the `SMS1XXX_BOARD_SIANO_STELLAR` configuration.
|
| 60/2 |
2026/11/24 09:40 |
tool |
grepper |
0m
Args: map[Expression:SMS1XXX_BOARD_SIANO_STELLAR PathPrefixes:[drivers/media/]]
Results: map[Output:drivers/media/common/siano/sms-cards.c=12=static struct sms_board sms_boards[] = {
--
drivers/media/common/siano/sms-cards.c-17- },
drivers/media/common/siano/sms-cards.c:18: [SMS1XXX_BOARD_SIANO_STELLAR] = {
drivers/media/common/siano/sms-cards.c-19- .name = "Siano Stellar Digital Receiver",
--
drivers/media/common/siano/sms-cards.c-97- },
drivers/media/common/siano/sms-cards.c:98: [SMS1XXX_BOARD_SIANO_STELLAR_ROM] = {
drivers/media/common/siano/sms-cards.c-99- .name = "Siano Stellar Digital Receiver ROM",
--
drivers/media/common/siano/sms-cards.h-16-#define SMS_BOARD_UNKNOWN 0
drivers/media/common/siano/sms-cards.h:17:#define SMS1XXX_BOARD_SIANO_STELLAR 1
drivers/media/common/siano/sms-cards.h-18-#define SMS1XXX_BOARD_SIANO_NOVA_A 2
--
drivers/media/common/siano/sms-cards.h-28-#define SMS1XXX_BOARD_SIANO_VENICE 12
drivers/media/common/siano/sms-cards.h:29:#define SMS1XXX_BOARD_SIANO_STELLAR_ROM 13
drivers/media/common/siano/sms-cards.h-30-#define SMS1XXX_BOARD_ZTE_DVB_DATA_CARD 14
--
drivers/media/mmc/siano/smssdio.c=50=static const struct sdio_device_id smssdio_ids[] = {
drivers/media/mmc/siano/smssdio.c-51- {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_STELLAR),
drivers/media/mmc/siano/smssdio.c:52: .driver_data = SMS1XXX_BOARD_SIANO_STELLAR},
drivers/media/mmc/siano/smssdio.c-53- {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_A0),
--
drivers/media/usb/siano/smsusb.c=505=static int smsusb_probe(struct usb_interface *intf,
--
drivers/media/usb/siano/smsusb.c-554-
drivers/media/usb/siano/smsusb.c:555: if (id->driver_info == SMS1XXX_BOARD_SIANO_STELLAR_ROM) {
drivers/media/usb/siano/smsusb.c-556- /* Detected a Siano Stellar uninitialized */
--
drivers/media/usb/siano/smsusb.c=629=static const struct usb_device_id smsusb_id_table[] = {
--
drivers/media/usb/siano/smsusb.c-631- { USB_DEVICE(0x187f, 0x0010),
drivers/media/usb/siano/smsusb.c:632: .driver_info = SMS1XXX_BOARD_SIANO_STELLAR_ROM },
drivers/media/usb/siano/smsusb.c-633- /* This device pops up after firmware load */
drivers/media/usb/siano/smsusb.c-634- { USB_DEVICE(0x187f, 0x0100),
drivers/media/usb/siano/smsusb.c:635: .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
drivers/media/usb/siano/smsusb.c-636-
]
|
| 61/2 |
2026/11/24 09:40 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 86430
output: 47
thoughts: 28
Thoughts: **My Thought Process on `drivers/media/common/siano/sms-cards.c` Lines 18-30**
Alright, so I'm looking at lines 18 through 30 of the `drivers/media/common/siano/sms-cards.c` file. My immediate focus is to understand what's happening in this particular section of the code, given my expertise in this domain. I'm not just skimming; I'm dissecting it to grasp the underlying logic and its implications within the broader context of the Siano driver.
My first thought is to identify the primary function or purpose of these lines. Are they setting up device structures, handling configuration parameters, initializing hardware components, or perhaps defining some sort of data structures? I'm scanning for keywords and common patterns in driver development. I'm looking for function calls, variable declarations, and assignments that give me clues about the operational flow.
As I read, I'm trying to anticipate potential issues or areas of interest. For instance, if I see memory allocation, I'm thinking about error handling. If there's hardware interaction, I'm considering concurrency and potential race conditions. If it's related to device probing or identification, I'm mentally flagging it for potential conflicts or incompatibilities.
I'm also paying close attention to the context provided by the surrounding code. While I'm specifically looking at these lines, their true meaning is often revealed by what comes before and after. What state is the driver in before these lines execute? What actions are triggered as a consequence of their execution? This macro-level understanding is crucial for a truly "fulsome" summary.
Furthermore, I'm considering the implications for device performance and stability. Are these lines contributing to efficient resource utilization? Are they introducing any potential bottlenecks or points of failure? My goal is to not just understand *what* the code does, but *why* it's done that way and what the downstream effects might be.
Ultimately, I'm aiming to build a mental model of this code snippet's role within the Siano driver's architecture, ensuring I have a comprehensive grasp of its functionality, dependencies, and potential impact, all from the perspective of someone who understands the intricacies of this field.
|
| 62/2 |
2026/11/24 09:41 |
tool |
read-file |
0m
Args: map[File:drivers/media/common/siano/sms-cards.c FirstLine:18 LineCount:20]
Results: map[Contents: 18: [SMS1XXX_BOARD_SIANO_STELLAR] = {
19: .name = "Siano Stellar Digital Receiver",
20: .type = SMS_STELLAR,
21: .default_mode = DEVICE_MODE_DVBT_BDA,
22: },
23: [SMS1XXX_BOARD_SIANO_NOVA_A] = {
24: .name = "Siano Nova A Digital Receiver",
25: .type = SMS_NOVA_A0,
26: .default_mode = DEVICE_MODE_DVBT_BDA,
27: },
28: [SMS1XXX_BOARD_SIANO_NOVA_B] = {
29: .name = "Siano Nova B Digital Receiver",
30: .type = SMS_NOVA_B0,
31: .default_mode = DEVICE_MODE_DVBT_BDA,
32: },
33: [SMS1XXX_BOARD_SIANO_VEGA] = {
34: .name = "Siano Vega Digital Receiver",
35: .type = SMS_VEGA,
36: .default_mode = DEVICE_MODE_CMMB,
37: },
]
|
| 63/2 |
2026/11/24 09:41 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 86850
output: 55
thoughts: 2658
Thoughts: **My Thought Process on the Siano USB Buffer Deallocation Bug**
Alright, let's break this down. I'm looking at this reproducer, and the pieces just click together perfectly. It's really satisfying when you see how everything aligns.
First, I trace the device setup: `SMS1XXX_BOARD_SIANO_STELLAR` is configured with `intf_num = 0` and `type = SMS_STELLAR`. This triggers `smsusb_probe()`, which correctly identifies interface 0, clears halts on endpoints 0x81 and 0x02, and then calls `smsusb_init_device()`.
Inside `smsusb_init_device()`:
* The buffer size is set to `USB1_BUFFER_SIZE` (4096 bytes).
* The number of buffers is `MAX_BUFFERS` (50).
* Then, `smscore_register_device()` comes into play. This is where the large `common_buffer` is allocated using `kzalloc(204800, GFP_KERNEL)`, which is a significant order-6 allocation. This large buffer is then divided into 50 smaller `smscore_buffer_t` entries, each pointing to a 4096-byte chunk within `common_buffer`.
* `smsusb_init_device()` also allocates 10 URBs using `usb_alloc_urb()`.
* Next, `smsusb_start_streaming()` submits these 10 URBs. Crucially, the `transfer_buffer` for each URB is set to point to one of the `cb->p` pointers (i.e., a 4096-byte slice of `common_buffer`). And here's a key point: `surb->urb->transfer_flags` is set to include `URB_FREE_BUFFER`.
* With streaming started, `smscore_start_device()` succeeds.
The simulation proceeds, the device is implicitly closed, and then the `dummy_hcd` or `hub_event` causes the USB device to disconnect. This leads to `smsusb_disconnect()` and then `smsusb_term_device()`.
Now, the cleanup phase begins, and this is where the problem emerges:
* `smsusb_stop_streaming()` is called. This function cancels the URBs and returns the `surb->cb` structures to the `coredev->buffers` pool.
* Then `smscore_unregister_device()` is called. This function frees the `coredev->common_buffer` using `kfree()`, which in turn calls `free_large_kmalloc()`. This successfully frees the order-6 pages.
* **Here's the critical failure:** The code then iterates through the allocated URBs (`dev->surbs[i].urb`) and calls `usb_free_urb()` for each. The `urb_destroy()` function, which is implicitly called by `usb_free_urb()`, checks the `urb->transfer_flags`. Because `URB_FREE_BUFFER` was set earlier, it attempts to `kfree(urb->transfer_buffer)`.
* The problem is that `urb->transfer_buffer` points to `common_buffer` or one of its interior chunks. Since `common_buffer` was *already freed* by `smscore_unregister_device()`, calling `kfree()` again on these pointers leads to a double-free. For URBs after the first one (i.e., `i = 1..9`), `urb->transfer_buffer` points to internal pages of the large allocation. This triggers the `WARN_ON_ONCE(!PageLargeKmalloc(page))` in `mm/slub.c`, indicating a serious memory corruption.
Now, I need to pinpoint the exact commit responsible. I'm comparing `564246fd3ff4` and `836e2548524d`.
Let's consider `564246fd3ff4` ("media: siano: Fix coherent memory allocation failure on arm64"). This commit is where the buggy logic was *introduced*. Specifically, it added the `surb->urb->transfer_flags |= URB_FREE_BUFFER;` line in `smsusb_submit_urb()`. At that exact time, `common_buffer` was being allocated and then later freed by `smscore_unregister_device()`. Adding `URB_FREE_BUFFER` here was a fundamental mistake because:
1. `urb->transfer_buffer` (`cb->p`) was not independently allocated; it was a slice of `common_buffer`.
2. These buffers were reused across URB submissions.
3. The buffer was already freed by `smscore_unregister_device()`.
Now, why isn't `836e2548524d` ("media: usb: siano: Fix allocation of urbs") the primary commit? This commit's purpose was to switch from an embedded URB struct (`usb_init_urb()`) to dynamically allocated URBs (`usb_alloc_urb()` / `usb_free_urb()`) to resolve a smatch warning. Before this change, `smsusb.c` never actually called `usb_free_urb()`, so the `urb_destroy()` path (and thus the `URB_FREE_BUFFER` check) was never executed. Therefore, `836e2548524d` *exposed* the bug introduced by `564246fd3ff4` by enabling the `urb_destroy()` function to be called. It merely made the existing bug manifest.
The rules are very clear: I need to identify the commit that *introduced* the buggy logic, not the one that merely exposed or enabled it. `564246fd3ff4` introduced the incorrect `URB_FREE_BUFFER` flag. `836e2548524d` just switched to a standard URB lifecycle that subsequently triggered that flag. The fix for this issue is precisely to remove that `URB_FREE_BUFFER` flag, which was added in `564246fd3ff4`.
Therefore, `564246fd3ff41afd3234c18ae28bc4ad9d815f79` is unequivocally the primary commit responsible for introducing this bug. I will provide this single commit as the candidate and explain the roles of the other related commits, like `836e2548524d2dfcb5acaf3be78f203b6b4bde6f` and `a6687c8ff613fc13a71ce1390593ba8d27c52db9` (which added the sanitizer check), in the accompanying text. This adheres perfectly to the "single commit" and "primary introducer" guidelines.
|
| 64/2 |
2026/11/24 09:41 |
tool |
set-results |
0m
Args: map[CandidateCommits:[564246fd3ff41afd3234c18ae28bc4ad9d815f79]]
Results: map[CandidateCommits:[564246fd3ff41afd3234c18ae28bc4ad9d815f79]]
|
| 65/2 |
2026/11/24 09:41 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 89614
output: 1303
thoughts: 37
Thoughts: **Bisection Report Summary**
Alright, the `set-results` command has run successfully. My primary objective now is to distill this information into a crystal-clear, concise bisection report. I'll be meticulous in adhering to only the directly observed facts, ensuring that the report includes only verifiable details such as precise file paths, the exact symbol names involved, and the specific commit hashes that mark the critical points. No speculation, no extraneous information β just the pure, unadulterated data derived from the bisection process.
|
| 66/1 |
2026/11/24 09:41 |
action |
format-bisect |
0m
Results: map[Commits:[map[AuthorEmail:tomoki.sekiyama@gmail.com AuthorName:Tomoki Sekiyama Date:2018-03-03T13:20:56-05:00 Hash:564246fd3ff41afd3234c18ae28bc4ad9d815f79 Recipients:[map[Email:mchehab@s-opensource.com Name: To:true] map[Email:tomoki.sekiyama@gmail.com Name: To:true]] Title:media: siano: Fix coherent memory allocation failure on arm64]] IsRelease:false]
|