coresight: tmc: allocating memory when needed
A message that is not in the archive (sent off-list or not yet received): 20160402053209.GA15353@mwanda
Quoted 11 lines · Dan Carpenter
Hello Mathieu Poirier,
The patch
06ec4671e5d1: "coresight: tmc: allocating memory when needed" from Dec 9, 2015, leads to the following static checker warning:
drivers/hwtracing/coresight/coresight-tmc-etf.c:125tmc_enable_etf_sink_sysfs()
warn: passingdevm_allocated variable to kfree. 'buf'
drivers/hwtracing/coresight/coresight-tmc-etf.c
109 static inttmc_enable_etf_sink_sysfs(struct coresight_device *csdev, u32 mode)
Quoted 7 lines
110 { 111 u32 val; 112 bool allocated = false; 113 char *buf = NULL; ^^^^^^^^^^This bogus initialization disables GCC's checks for uninitialized memory. Don't disable static analysis for no reason.
Yes, this one doesn't do anything.
Quoted 14 lines
114 unsigned long flags;
115 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
116
117 /* Allocating the memory here while outside of the spinlock */
118 buf = devm_kzalloc(drvdata->dev, drvdata->size, GFP_KERNEL);
119 if (!buf)
120 return -ENOMEM;
121
122 spin_lock_irqsave(&drvdata->spinlock, flags);
123 if (drvdata->reading) {
124 spin_unlock_irqrestore(&drvdata->spinlock, flags);
125 kfree(buf);
^^^^^^^^^^Quoted 5 lines
This is a double free because the buffer is allocated with
devm_kzalloc(). There are lots more double frees. We double free intmc_read_unprepare_etf()as well. It's hard to find all the bugs without knowing the code more than I do. Please review and test unloading.
This is obviously the wrong kind of memory allocator and the same will be true on the ETF side since they were tailored to be as similar as possible. I'll revise this.
Thanks,
Mathieu
Quoted 7 lines
126 return -EBUSY; 127 } 128regards,
dan carpenter