dmaengine: dmatest: Check list for emptiness before access its last entry
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Tue, 22 Sep 2020 11:58:46 +0000 (14:58 +0300)
committerVinod Koul <vkoul@kernel.org>
Tue, 22 Sep 2020 14:48:41 +0000 (20:18 +0530)
After writing a garbage to the channel we get an Oops in dmatest_chan_set()
due to access to last entry in the empty list.

[  212.670672] BUG: unable to handle page fault for address: fffffff000000020
[  212.677562] #PF: supervisor read access in kernel mode
[  212.682702] #PF: error_code(0x0000) - not-present page
...
[  212.710074] RIP: 0010:dmatest_chan_set+0x149/0x2d0 [dmatest]
[  212.715739] Code: e8 cc f9 ff ff 48 8b 1d 0d 55 00 00 48 83 7b 10 00 0f 84 63 01 00 00 48 c7 c7 d0 65 4d c0 e8 ee 4a f5 e1 48 89 c6 48 8b 43 10 <48> 8b 40 20 48 8b 78 58 48 85 ff 0f 84 f5 00 00 00 e8 b1 41 f5 e1

Fix this by checking list for emptiness before accessing its last entry.

Fixes: d53513d5dc28 ("dmaengine: dmatest: Add support for multi channel testing")
Cc: Vladimir Murzin <vladimir.murzin@arm.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Tested-by: Peter Ujfalusi <peter.ujfalusi@ti.com>
Link: https://lore.kernel.org/r/20200922115847.30100-2-andriy.shevchenko@linux.intel.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
drivers/dma/dmatest.c

index 5dc3db216602869d467fbfd8fd70e2bd32bfc677..00f288bd377c3013e1eb66bac8969a44be432c0c 100644 (file)
@@ -1255,15 +1255,14 @@ static int dmatest_chan_set(const char *val, const struct kernel_param *kp)
        add_threaded_test(info);
 
        /* Check if channel was added successfully */
-       dtc = list_last_entry(&info->channels, struct dmatest_chan, node);
-
-       if (dtc->chan) {
+       if (!list_empty(&info->channels)) {
                /*
                 * if new channel was not successfully added, revert the
                 * "test_channel" string to the name of the last successfully
                 * added channel. exception for when users issues empty string
                 * to channel parameter.
                 */
+               dtc = list_last_entry(&info->channels, struct dmatest_chan, node);
                if ((strcmp(dma_chan_name(dtc->chan), strim(test_channel)) != 0)
                    && (strcmp("", strim(test_channel)) != 0)) {
                        ret = -EINVAL;