public inbox for smatch@vger.kernel.org
 help / color / mirror / Atom feed
* apparent bug about check_free_strict
@ 2024-11-18 11:55 Toomas Soome
  2024-11-18 12:52 ` Dan Carpenter
  0 siblings, 1 reply; 17+ messages in thread
From: Toomas Soome @ 2024-11-18 11:55 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: smatch

hi!

I did enable illumos kernel memory allocation/free checks (kmem_alloc/kmem_free) and apparently I did find something interesting.

The warning is:
/code/illumos-gate/usr/src/tools/proto/root_i386-nd/opt/onbld/bin/i386/smatch: ../../common/os/devcfg.c:8583 e_ddi_retire_device() warn: passing freed memory 'pdip'
/code/illumos-gate/usr/src/tools/proto/root_i386-nd/opt/onbld/bin/i386/smatch: ../../common/os/devcfg.c:8612 e_ddi_retire_device() warn: passing freed memory 'dip'
/code/illumos-gate/usr/src/tools/proto/root_i386-nd/opt/onbld/bin/i386/smatch: ../../common/os/devcfg.c:8621 e_ddi_retire_device() warn: passing freed memory ‘dip'

The code for first error about pdip is:

  8572          pdip = ddi_get_parent(dip);
  8573          ndi_hold_devi(pdip);
  8574     8575          /*
  8576           * Run devfs_clean() in case dip has no constraints and is
  8577           * not in use, so is retireable but there are dv_nodes holding
  8578           * ref-count on the dip. Note that devfs_clean() always returns
  8579           * success.
  8580           */
  8581          devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
  8582          (void) ddi_deviname(dip, devnm);
  8583          (void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
  8584          kmem_free(devnm, MAXNAMELEN + 1);
  8585     8586          ndi_devi_enter(pdip);

We get this error about pdip with devfs_clean(), but apparently the ‘freed’ state is set with ndi_hold_devi(pdip) call; of course the call itself is not the quilty one, but the construct is — as soon as I either comment the ndi_hold_devi() out *or* if I move it down before devfs_clean(), then the error disappears.

Therefore, it appears that code segment such as:

var = f();
g(var);

is causing state of var to be set ‘freed’ and check_free_strict.c is ending up spitting out the warning about passing freed memory with next function call.


now the next warning is about code:

  8609          constraint = 1; /* assume constraints allow retire */
  8610          (void) e_ddi_retire_notify(dip, &constraint);
  8611          if (!is_leaf_node(dip)) {
  8612                  ndi_devi_enter(dip);
  8613                  ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
  8614                      &constraint);
  8615                  ndi_devi_exit(dip);
  8616          }
  8617
  8618          /*
  8619           * Now finalize the retire
  8620           */
  8621          (void) e_ddi_retire_finalize(dip, &constraint);
  8622          if (!is_leaf_node(dip)) {
  8623                  ndi_devi_enter(dip);
  8624                  ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
  8625                      &constraint);
  8626                  ndi_devi_exit(dip);
  8627          }

Here we do get warning about ndi_devi_enter(), but if I replace dip in is_leaf_node() by NULL, we do not get any more warnings about ‘dip’.

PS: the line number differences with git is because my branch has other change fixing memory leak discovered by smatch:D

rgds,
toomas

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

end of thread, other threads:[~2025-11-26 15:13 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-18 11:55 apparent bug about check_free_strict Toomas Soome
2024-11-18 12:52 ` Dan Carpenter
2024-11-18 13:28   ` Toomas Soome
2024-11-18 15:27     ` Dan Carpenter
     [not found]       ` <ADB0555A-8DE4-49D1-B769-A02EB82690A9@me.com>
2025-11-21 18:01         ` Toomas Soome
2025-11-24 14:46           ` Dan Carpenter
2025-11-24 15:30             ` Toomas Soome
2025-11-25 13:38               ` Toomas Soome
2025-11-25 14:28                 ` Toomas Soome
2025-11-25 14:50                   ` Dan Carpenter
2025-11-25 15:04                     ` Toomas Soome
2025-11-25 15:34                       ` Oleg Drokin
2025-11-26 12:14                         ` Dan Carpenter
2025-11-25 17:12                       ` Dan Carpenter
     [not found]                     ` <32FD91B6-32B3-45FC-A6E5-EA39439466E3@me.com>
2025-11-26 15:12                       ` Dan Carpenter
     [not found]               ` <45D1224C-6C4C-4745-9FA6-F07BB1792831@me.com>
2025-11-25 13:50                 ` Dan Carpenter
2025-11-25 13:40         ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox