Skip to contentSkip to conversation

scftorture: Use a lock-less list to free memory.

bug report Applied Dan Carpenter 3 participants 6 messages Nov 12, 2024 – Nov 13, 2024 Kernel janitors

lore
Share & tools
lore
#0 lore raw

Hello Sebastian Andrzej Siewior,

Commit 4788c861ad7e ("scftorture: Use a lock-less list to free memory.") from Nov 8, 2024 (linux-next), leads to the following Smatch static checker warning:

kernel/scftorture.c:393 scftorture_invoke_one()
error: we previously assumed 'scfcp' could be null (see line 382)

kernel/scftorture.c

    342 static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp)
    343 {
    344         bool allocfail = false;
    345         uintptr_t cpu;
    346         int ret = 0;
    347         struct scf_check *scfcp = NULL;
    348         struct scf_selector *scfsp = scf_sel_rand(trsp);
    349 
    350         if (scfsp->scfs_prim == SCF_PRIM_SINGLE || scfsp->scfs_wait) {
    351                 scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC);
    352                 if (!scfcp) {
    353                         WARN_ON_ONCE(!IS_ENABLED(CONFIG_KASAN));
    354                         atomic_inc(&n_alloc_errs);
    355                         allocfail = true;
    356                 } else {
    357                         scfcp->scfc_cpu = -1;
    358                         scfcp->scfc_wait = scfsp->scfs_wait;
    359                         scfcp->scfc_out = false;
    360                         scfcp->scfc_rpc = false;
    361                 }
    362         }
    363         if (use_cpus_read_lock)
    364                 cpus_read_lock();
    365         else
    366                 preempt_disable();
    367         switch (scfsp->scfs_prim) {
    368         case SCF_PRIM_RESCHED:
    369                 if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST)) {
    370                         cpu = torture_random(trsp) % nr_cpu_ids;
    371                         scfp->n_resched++;
    372                         resched_cpu(cpu);
    373                         this_cpu_inc(scf_invoked_count);
    374                 }
    375                 break;
    376         case SCF_PRIM_SINGLE:
    377                 cpu = torture_random(trsp) % nr_cpu_ids;
    378                 if (scfsp->scfs_wait)
    379                         scfp->n_single_wait++;
    380                 else
    381                         scfp->n_single++;
    382                 if (scfcp) {

This code assumes that scfcp can be NULL.

    383                         scfcp->scfc_cpu = cpu;
    384                         barrier(); // Prevent race-reduction compiler optimizations.
    385                         scfcp->scfc_in = true;
    386                 }
    387                 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait);
    388                 if (ret) {
    389                         if (scfsp->scfs_wait)
    390                                 scfp->n_single_wait_ofl++;
    391                         else
    392                                 scfp->n_single_ofl++;

--> 393 scf_add_to_free_list(scfcp);

Originally this was a kfree(scfcp) which can accept a NULL, but scf_add_to_free_list() can't handle a NULL parameter.

    394                         scfcp = NULL;
    395                 }
    396                 break;
    397         case SCF_PRIM_SINGLE_RPC:

regards,
dan carpenter

#1 lore raw

PATCH 1/1 scftorture: Handle NULL argument passed to scf_add_to_free_list().

Dan reported that after the rework the newly introduced scf_add_to_free_list() may get a NULL pointer passed. This replaced kfree() which was fine with a NULL pointer but scf_add_to_free_list() isn't.

Let scf_add_to_free_list() handle NULL pointer.

Fixes 4788c861ad7e9 ("scftorture: Use a lock-less list to free memory.")

---

Thank you Dan. I had to look twice, that `scfsp' above looked almost identical.

 kernel/scftorture.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/scftorture.c +2 -0
index eeafd3fc16820..d86d2d9c46248 100644
@@ -155,6 +155,8 @@ static void scf_add_to_free_list(struct scf_check *scfcp)
 	struct llist_head *pool;
 	unsigned int cpu;
 
+	if (!scfcp)
+		return;
 	cpu = raw_smp_processor_id() % nthreads;
 	pool = &per_cpu(scf_free_pool, cpu);
 	llist_add(&scfcp->scf_node, pool);
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH] scftorture: Handle NULL argument passed to scf_add_to_free_list().

Quoted 7 lines · Sebastian Andrzej Siewior

Dan reported that after the rework the newly introduced scf_add_to_free_list() may get a NULL pointer passed. This replaced kfree() which was fine with a NULL pointer but scf_add_to_free_list() isn't.

Let scf_add_to_free_list() handle NULL pointer.

Quoted 8 lines
Fixes 4788c861ad7e9 ("scftorture: Use a lock-less list to free memory.")

---

Thank you Dan. I had to look twice, that `scfsp' above looked almost identical.

Yeap... Me too. #LowHammingDistance

regards,
dan carpenter

#3 lore raw

Re: [PATCH] scftorture: Handle NULL argument passed to scf_add_to_free_list().

Quoted 8 lines · Dan Carpenter
Quoted 7 lines · Sebastian Andrzej Siewior

Dan reported that after the rework the newly introduced scf_add_to_free_list() may get a NULL pointer passed. This replaced kfree() which was fine with a NULL pointer but scf_add_to_free_list() isn't.

Let scf_add_to_free_list() handle NULL pointer.

Quoted 8 lines
Quoted 8 lines
Fixes 4788c861ad7e9 ("scftorture: Use a lock-less list to free memory.")

---

Thank you Dan. I had to look twice, that `scfsp' above looked almost identical.

Queued and thank you both!

Quoted 1 line

Yeap... Me too. #LowHammingDistance

That could likely be improved, to be sure. I am one of these strange people who wants the pointer to give a hint about its type, something about pointers to rcu_data, rcu_node, and rcu_state pointers all being messed with at the same time.

Maybe s/scfcp/scfchkp/?

Thanx, Paul

#4 lore raw

Re: [PATCH] scftorture: Handle NULL argument passed to scf_add_to_free_list().

Quoted 20 lines · Paul E. McKenney
Quoted 8 lines · Dan Carpenter
Quoted 7 lines · Sebastian Andrzej Siewior

Dan reported that after the rework the newly introduced scf_add_to_free_list() may get a NULL pointer passed. This replaced kfree() which was fine with a NULL pointer but scf_add_to_free_list() isn't.

Let scf_add_to_free_list() handle NULL pointer.

Quoted 8 lines
Quoted 8 lines
Fixes 4788c861ad7e9 ("scftorture: Use a lock-less list to free memory.")

---

Thank you Dan. I had to look twice, that `scfsp' above looked almost identical.

Queued and thank you both!

Quoted 9 lines
Quoted 1 line

Yeap... Me too. #LowHammingDistance

That could likely be improved, to be sure. I am one of these strange people who wants the pointer to give a hint about its type, something about pointers to rcu_data, rcu_node, and rcu_state pointers all being messed with at the same time.

Maybe s/scfcp/scfchkp/?

What about scf_chkp, maybe?

regards,
dan carpenter

#5 lore raw

Re: [PATCH] scftorture: Handle NULL argument passed to scf_add_to_free_list().

Quoted 23 lines · Dan Carpenter
Quoted 22 lines · Paul E. McKenney
Quoted 8 lines · Dan Carpenter
Quoted 7 lines · Sebastian Andrzej Siewior

Dan reported that after the rework the newly introduced scf_add_to_free_list() may get a NULL pointer passed. This replaced kfree() which was fine with a NULL pointer but scf_add_to_free_list() isn't.

Let scf_add_to_free_list() handle NULL pointer.

Quoted 8 lines
Quoted 8 lines
Fixes 4788c861ad7e9 ("scftorture: Use a lock-less list to free memory.")

---

Thank you Dan. I had to look twice, that `scfsp' above looked almost identical.

Queued and thank you both!

Quoted 1 line

Yeap... Me too. #LowHammingDistance

Quoted 8 lines
Quoted 6 lines

That could likely be improved, to be sure. I am one of these strange people who wants the pointer to give a hint about its type, something about pointers to rcu_data, rcu_node, and rcu_state pointers all being messed with at the same time.

Maybe s/scfcp/scfchkp/?

What about scf_chkp, maybe?

I would rather avoid the underscore, myself, but...

Thanx, Paul