[bug report] net: mediatek: mtk_eth_sock: allocate dummy net_device dynamically

Dan Carpenter dan.carpenter at linaro.org
Fri Jul 19 16:52:50 PDT 2024


Hello Breno Leitao,

Commit b209bd6d0bff ("net: mediatek: mtk_eth_sock: allocate dummy
net_device dynamically") from Apr 22, 2024 (linux-next), leads to the
following Smatch static checker warning:

	drivers/net/ethernet/mediatek/mtk_eth_soc.c:5061 mtk_probe()
	error: we previously assumed 'eth->dummy_dev' could be null (see line 5041)

drivers/net/ethernet/mediatek/mtk_eth_soc.c
    5037         /* we run 2 devices on the same DMA ring so we need a dummy device
    5038          * for NAPI to work
    5039          */
    5040         eth->dummy_dev = alloc_netdev_dummy(0);
    5041         if (!eth->dummy_dev) {
                      ^^^^^^^^^^^^^^
->dummy_dev is the last allocation

    5042                 err = -ENOMEM;
    5043                 dev_err(eth->dev, "failed to allocated dummy device\n");
    5044                 goto err_unreg_netdev;
    5045         }
    5046         netif_napi_add(eth->dummy_dev, &eth->tx_napi, mtk_napi_tx);
    5047         netif_napi_add(eth->dummy_dev, &eth->rx_napi, mtk_napi_rx);
    5048 
    5049         platform_set_drvdata(pdev, eth);
    5050         schedule_delayed_work(&eth->reset.monitor_work,
    5051                               MTK_DMA_MONITOR_TIMEOUT);
    5052 
    5053         return 0;
    5054 
    5055 err_unreg_netdev:
    5056         mtk_unreg_dev(eth);
    5057 err_deinit_ppe:
    5058         mtk_ppe_deinit(eth);
    5059         mtk_mdio_cleanup(eth);
    5060 err_free_dev:
--> 5061         mtk_free_dev(eth);
                              ^^^
But it's free here.  (NULL dereference).  I really would suggest moving the
free_netdev() to the release function.  The probe and release function are
easier to read if they're in mirrored order where every allocation has a
matching free.

    5062 err_deinit_hw:
    5063         mtk_hw_deinit(eth);
    5064 err_wed_exit:
    5065         mtk_wed_exit();
    5066 err_destroy_sgmii:
    5067         mtk_sgmii_destroy(eth);
    5068 
    5069         return err;
    5070 }

regards,
dan carpenter



More information about the Linux-mediatek mailing list