• Shang XiaoJing's avatar
    media: max9286: Fix memleak in max9286_v4l2_register() · 8636c5fc
    Shang XiaoJing authored
    There is a kmemleak when testing the media/i2c/max9286.c with bpf mock
    device:
    
    kmemleak: 5 new suspected memory leaks (see /sys/kernel/debug/kmemleak)
    
    unreferenced object 0xffff88810defc400 (size 256):
      comm "python3", pid 278, jiffies 4294737563 (age 31.978s)
      hex dump (first 32 bytes):
        28 06 a7 0a 81 88 ff ff 00 fe 22 12 81 88 ff ff  (.........".....
        10 c4 ef 0d 81 88 ff ff 10 c4 ef 0d 81 88 ff ff  ................
      backtrace:
        [<00000000191de6a7>] __kmalloc_node+0x44/0x1b0
        [<000000002f4912b7>] kvmalloc_node+0x34/0x180
        [<0000000057dc4cae>] v4l2_ctrl_new+0x325/0x10f0 [videodev]
        [<0000000026030272>] v4l2_ctrl_new_std+0x16f/0x210 [videodev]
        [<00000000f0d9ea2f>] max9286_probe+0x76e/0xbff [max9286]
        [<00000000ea8f6455>] i2c_device_probe+0x28d/0x680
        [<0000000087529af3>] really_probe+0x17c/0x3f0
        [<00000000b08be526>] __driver_probe_device+0xe3/0x170
        [<000000004382edea>] driver_probe_device+0x49/0x120
        [<000000007bde528a>] __device_attach_driver+0xf7/0x150
        [<000000009f9c6ab4>] bus_for_each_drv+0x114/0x180
        [<00000000c8aaf588>] __device_attach+0x1e5/0x2d0
        [<0000000041cc06b9>] bus_probe_device+0x126/0x140
        [<000000002309860d>] device_add+0x810/0x1130
        [<000000002827bf98>] i2c_new_client_device+0x359/0x4f0
        [<00000000593bdc85>] of_i2c_register_device+0xf1/0x110
    
    max9286_v4l2_register() calls v4l2_ctrl_new_std(), but won't free the
    created v412_ctrl when fwnode_graph_get_endpoint_by_id() failed, which
    causes the memleak. Call v4l2_ctrl_handler_free() to free the v412_ctrl.
    
    Fixes: 66d8c9d2 ("media: i2c: Add MAX9286 driver")
    Signed-off-by: default avatarShang XiaoJing <shangxiaojing@huawei.com>
    Reviewed-by: default avatarLaurent Pinchart <laurent.pinchart@ideasonboard.com>
    Signed-off-by: default avatarSakari Ailus <sakari.ailus@linux.intel.com>
    Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@kernel.org>
    8636c5fc
max9286.c 42.9 KB