All of lore.kernel.org
 help / color / mirror / Atom feed
* [bug report] powerpc/perf: Add nest IMC PMU support
@ 2018-10-18  9:33 Dan Carpenter
  2018-10-24  7:06 ` Anju T Sudhakar
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2018-10-18  9:33 UTC (permalink / raw)
  To: anju; +Cc: linuxppc-dev

Hello Anju T Sudhakar,

The patch 885dcd709ba9: "powerpc/perf: Add nest IMC PMU support" from
Jul 19, 2017, leads to the following static checker warning:

	arch/powerpc/perf/imc-pmu.c:506 nest_imc_event_init()
	warn: 'pcni' can't be NULL.

arch/powerpc/perf/imc-pmu.c
   485          if (event->cpu < 0)
   486                  return -EINVAL;
   487  
   488          pmu = imc_event_to_pmu(event);
   489  
   490          /* Sanity check for config (event offset) */
   491          if ((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size)
   492                  return -EINVAL;
   493  
   494          /*
   495           * Nest HW counter memory resides in a per-chip reserve-memory (HOMER).
   496           * Get the base memory addresss for this cpu.
   497           */
   498          chip_id = cpu_to_chip_id(event->cpu);
   499          pcni = pmu->mem_info;
                ^^^^^^^^^^^^^^^^^^^^
   500          do {
   501                  if (pcni->id == chip_id) {
   502                          flag = true;
   503                          break;
   504                  }
   505                  pcni++;
                        ^^^^^^
   506          } while (pcni);
                         ^^^^
This will loop until we crash.  I'm not sure what was intended.

   507  
   508          if (!flag)
   509                  return -ENODEV;
   510  

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 5+ messages in thread
* [bug report] powerpc/perf: Add nest IMC PMU support
@ 2018-01-31 15:25 Dan Carpenter
  2018-02-01 11:27 ` Madhavan Srinivasan
  2018-02-08  6:31 ` Anju T Sudhakar
  0 siblings, 2 replies; 5+ messages in thread
From: Dan Carpenter @ 2018-01-31 15:25 UTC (permalink / raw)
  To: Anju T Sudhakar; +Cc: linuxppc-dev

Hello Anju T Sudhakar,

The patch 885dcd709ba9: "powerpc/perf: Add nest IMC PMU support" from
Jul 19, 2017, leads to the following static checker warning:

	arch/powerpc/perf/imc-pmu.c:1393 init_imc_pmu()
	warn: 'pmu_ptr' was already freed.

arch/powerpc/perf/imc-pmu.c
  1317  int init_imc_pmu(struct device_node *parent, struct imc_pmu *pmu_ptr, int pmu_idx)
  1318  {
  1319          int ret;
  1320  
  1321          ret = imc_mem_init(pmu_ptr, parent, pmu_idx);
  1322          if (ret) {
  1323                  imc_common_mem_free(pmu_ptr);
  1324                  return ret;
  1325          }

Change this to:

		if (ret)
			goto err_free_mpu_ptr;

Or something instead of a direct return.  That's more normal kernel
style.

  1326  
  1327          switch (pmu_ptr->domain) {
  1328          case IMC_DOMAIN_NEST:
  1329                  /*
  1330                  * Nest imc pmu need only one cpu per chip, we initialize the
  1331                  * cpumask for the first nest imc pmu and use the same for the
  1332                  * rest. To handle the cpuhotplug callback unregister, we track
  1333                  * the number of nest pmus in "nest_pmus".
  1334                  */
  1335                  mutex_lock(&nest_init_lock);
  1336                  if (nest_pmus == 0) {
  1337                          ret = init_nest_pmu_ref();
  1338                          if (ret) {
  1339                                  mutex_unlock(&nest_init_lock);
  1340                                  goto err_free;
  1341                          }
  1342                          /* Register for cpu hotplug notification. */
  1343                          ret = nest_pmu_cpumask_init();
  1344                          if (ret) {
  1345                                  mutex_unlock(&nest_init_lock);
  1346                                  kfree(nest_imc_refc);
  1347                                  kfree(per_nest_pmu_arr);
  1348                                  goto err_free;
  1349                          }
  1350                  }
  1351                  nest_pmus++;
  1352                  mutex_unlock(&nest_init_lock);
  1353                  break;
  1354          case IMC_DOMAIN_CORE:
  1355                  ret = core_imc_pmu_cpumask_init();
  1356                  if (ret) {
  1357                          cleanup_all_core_imc_memory();
  1358                          return ret;

These direct returns don't look correct...

  1359                  }
  1360  
  1361                  break;
  1362          case IMC_DOMAIN_THREAD:
  1363                  ret = thread_imc_cpu_init();
  1364                  if (ret) {
  1365                          cleanup_all_thread_imc_memory();
  1366                          return ret;
  1367                  }
  1368  
  1369                  break;
  1370          default:
  1371                  return  -1;     /* Unknown domain */

This one certainly looks like a memory leak.  Plus -1 is -EPERM which is
probably not the correct error code.


  1372          }
  1373  
  1374          ret = update_events_in_group(parent, pmu_ptr);
  1375          if (ret)
  1376                  goto err_free;
  1377  
  1378          ret = update_pmu_ops(pmu_ptr);
  1379          if (ret)
  1380                  goto err_free;
  1381  
  1382          ret = perf_pmu_register(&pmu_ptr->pmu, pmu_ptr->pmu.name, -1);
  1383          if (ret)
  1384                  goto err_free;
  1385  
  1386          pr_info("%s performance monitor hardware support registered\n",
  1387                                                          pmu_ptr->pmu.name);
  1388  
  1389          return 0;
  1390  
  1391  err_free:
  1392          imc_common_mem_free(pmu_ptr);
  1393          imc_common_cpuhp_mem_free(pmu_ptr);
                                          ^^^^^^^
This is a use after free, it should be in the reverse order.

err_free_cpuhp:
	imc_common_cpuhp_mem_free(pmu_ptr);
err_free_pmu_ptr:
	imc_common_mem_free(pmu_ptr);

  1394          return ret;
  1395  }

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-10-24  7:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-18  9:33 [bug report] powerpc/perf: Add nest IMC PMU support Dan Carpenter
2018-10-24  7:06 ` Anju T Sudhakar
  -- strict thread matches above, loose matches on Subject: below --
2018-01-31 15:25 Dan Carpenter
2018-02-01 11:27 ` Madhavan Srinivasan
2018-02-08  6:31 ` Anju T Sudhakar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.