Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

i3c: master: mipi-i3c-hci: Fix a kernel panic for accessing DAT_data.

The `i3c_master_bus_init` function may attach the I2C devices before the
I3C bus initialization. In this flow, the DAT `alloc_entry`` will be used
before the DAT `init`. Additionally, if the `i3c_master_bus_init` fails,
the DAT `cleanup` will execute before the device is detached, which will
execue DAT `free_entry` function. The above scenario can cause the driver
to use DAT_data when it is NULL.

Signed-off-by: Billy Tsai <billy_tsai@aspeedtech.com>
Link: https://lore.kernel.org/r/20231023080237.560936-1-billy_tsai@aspeedtech.com
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>

authored by

Billy Tsai and committed by
Alexandre Belloni
b53e9758 8911eae9

+18 -9
+18 -9
drivers/i3c/master/mipi-i3c-hci/dat_v1.c
··· 64 64 return -EOPNOTSUPP; 65 65 } 66 66 67 - /* use a bitmap for faster free slot search */ 68 - hci->DAT_data = bitmap_zalloc(hci->DAT_entries, GFP_KERNEL); 69 - if (!hci->DAT_data) 70 - return -ENOMEM; 67 + if (!hci->DAT_data) { 68 + /* use a bitmap for faster free slot search */ 69 + hci->DAT_data = bitmap_zalloc(hci->DAT_entries, GFP_KERNEL); 70 + if (!hci->DAT_data) 71 + return -ENOMEM; 71 72 72 - /* clear them */ 73 - for (dat_idx = 0; dat_idx < hci->DAT_entries; dat_idx++) { 74 - dat_w0_write(dat_idx, 0); 75 - dat_w1_write(dat_idx, 0); 73 + /* clear them */ 74 + for (dat_idx = 0; dat_idx < hci->DAT_entries; dat_idx++) { 75 + dat_w0_write(dat_idx, 0); 76 + dat_w1_write(dat_idx, 0); 77 + } 76 78 } 77 79 78 80 return 0; ··· 89 87 static int hci_dat_v1_alloc_entry(struct i3c_hci *hci) 90 88 { 91 89 unsigned int dat_idx; 90 + int ret; 92 91 92 + if (!hci->DAT_data) { 93 + ret = hci_dat_v1_init(hci); 94 + if (ret) 95 + return ret; 96 + } 93 97 dat_idx = find_first_zero_bit(hci->DAT_data, hci->DAT_entries); 94 98 if (dat_idx >= hci->DAT_entries) 95 99 return -ENOENT; ··· 111 103 { 112 104 dat_w0_write(dat_idx, 0); 113 105 dat_w1_write(dat_idx, 0); 114 - __clear_bit(dat_idx, hci->DAT_data); 106 + if (hci->DAT_data) 107 + __clear_bit(dat_idx, hci->DAT_data); 115 108 } 116 109 117 110 static void hci_dat_v1_set_dynamic_addr(struct i3c_hci *hci,