All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert" <linux@treblig.org>
To: Alison Schofield <alison.schofield@intel.com>
Cc: dan.j.williams@intel.com, vishal.l.verma@intel.com,
	dave.jiang@intel.com, ira.weiny@intel.com,
	nvdimm@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 0/2] nvdimm deadcoding
Date: Thu, 20 Feb 2025 21:51:38 +0000	[thread overview]
Message-ID: <Z7ej6lBOfRATYhER@gallifrey> (raw)
In-Reply-To: <Z7eSmfcdNeYr1rWa@aschofie-mobl2.lan>

* Alison Schofield (alison.schofield@intel.com) wrote:
> On Thu, Feb 20, 2025 at 12:45:36AM +0000, linux@treblig.org wrote:
> > From: "Dr. David Alan Gilbert" <linux@treblig.org>
> > 
> > Hi,
> >   A couple of nvdimm dead coding patches; they just
> > remove entirely unused functions.
> 
> I see you've been sending patches for dead code removal
> for several months. What tool are you using for discovery?

Hi Alison,
  Thanks for noticing.

  I'm using my own very very hacky scripts; notes below.
The problem is they generate lots of false-positives
so I've got this big output file from one run of the scripts
and I then have to go through finding the useful ones.
I start with an all-yes-config build on x86, then the
script dumps all the relocations using readelf and finds
symbols that are defined but don't have any apparent
use.
(Actually not quite all-yes-config, but close)

Then for each of those symbols I grep the whole tree
for the symbol and get myself a huge debug file.
That means that for each symbol I'm hopefully left where
it's defined and declared, and if there are actually any
uses that weren't built in my world they show up.

Then I gently work through these, look them up with git log -G
to find when they were added/stopped being used.

On one side there are false positives (stuff only in other
builds, so that's why I grep for the symbol name, also
symbols that are defined but only used locally in a .o
file)
There are lots of false negatives as well.
But then I have to second guess from the git output
  a) If it was recently added don't bother, someone is
probably about to use it.
  b) If it's actually a bug and it *should* be called.
  c) If it's a trivial one liner I don't bother
  d) If it looks like it's really a wrapper of every
firmware call for that device I leave it.
  e) Skip anything __init etc marked, or look magic
(bpf etc)

It's tricky because they're all over, so you fall
into the preferences of each maitainers oddities of
what they care about.

My debug file is alphabetically searched; I'm just near
the end of the n's - a while to go!

I'm hoping once I get to the end, then it'll be a bit
cleaner and I can tidy the scripts up and watch for
new entries in each release.

For reference for the nvdimm symbols my output file looks
like:
---- nd_attach_ndns[^A-Z_a-z] ----
drivers/nvdimm/claim.c:44:bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
drivers/nvdimm/claim.c:59:bool nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
drivers/nvdimm/claim.c:65:  claimed = __nd_attach_ndns(dev, attach, _ndns);
drivers/nvdimm/claim.c:216: if (!__nd_attach_ndns(dev, ndns, _ndns)) { 
drivers/nvdimm/nd-core.h:139:bool nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
drivers/nvdimm/nd-core.h:141:bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
drivers/nvdimm/btt_devs.c:211:  if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
drivers/nvdimm/pfn_devs.c:311:  if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
---- nd_region_conflict[^A-Z_a-z] ----
drivers/nvdimm/nd-core.h:130:int nd_region_conflict(struct nd_region *nd_region, resource_size_t start,
drivers/nvdimm/region_devs.c:1260:int nd_region_conflict(struct nd_region *nd_region, resource_size_t start,


Scripts
  (Which I've not run for a few months, I'm still working through
the first main run)

I start with:
  find . -name \*.o -exec ~/sym/dosyms {} \;
of which dosyms is:
--------
  echo $1
  DIR=$(dirname $1)
  NEWN=$DIR/$(basename -s .o $1).x

  readelf -W -s -r $1 | awk -f ~/sym/relocs.awk |sort|uniq > $NEWN
--------
so that gets me a load of .x files, which I run through
  awk -f ~/sym/collate.awk $(find . -name \*.x) > col
of which collate.awk is:
--------
  { if (($1=="u") || ($1=="U")) {
      use[$2]=use[$2] "," FILENAME
      usecount[$2]++
    } else {
      def[$2]=def[$2] ",:" $1 ":" FILENAME
      defcount[$2]++
    }
  }
  END {
    for (s in def) {
      if (usecount[s] == 0) {
        printf("%s:%d: %s from %s\n", s, usecount[s], use[s], def[s]) 
      }
    }
  }
--------
Then the following magic:
  cut -d' ' -f1 col | sed -e 's/:.*/[^A-Z_a-z]/' | while read SYM; do echo $SYM; ag -s --ignore '*.x' --ignore col --ignore col2 "$SYM" < /dev/null; done > search.out

which gives the output shown above.
(ag is a fast parallel grep, 'the-silver-searcher' )

> Thanks,
> Alison

Have fun,

Dave
> 
> > 
> > Signed-off-by: Dr. David Alan Gilbert <linux@treblig.org>
> > 
> > 
> > Dr. David Alan Gilbert (2):
> >   libnvdimm: Remove unused nd_region_conflict
> >   libnvdimm: Remove unused nd_attach_ndns
> > 
> >  drivers/nvdimm/claim.c       | 11 ----------
> >  drivers/nvdimm/nd-core.h     |  4 ----
> >  drivers/nvdimm/region_devs.c | 41 ------------------------------------
> >  3 files changed, 56 deletions(-)
> > 
> > -- 
> > 2.48.1
> > 
> > 
-- 
 -----Open up your eyes, open up your mind, open up your code -------   
/ Dr. David Alan Gilbert    |       Running GNU/Linux       | Happy  \ 
\        dave @ treblig.org |                               | In Hex /
 \ _________________________|_____ http://www.treblig.org   |_______/

      reply	other threads:[~2025-02-20 21:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-20  0:45 [PATCH 0/2] nvdimm deadcoding linux
2025-02-20  0:45 ` [PATCH 1/2] libnvdimm: Remove unused nd_region_conflict linux
2025-02-20  0:45 ` [PATCH 2/2] libnvdimm: Remove unused nd_attach_ndns linux
2025-02-20 19:10 ` [PATCH 0/2] nvdimm deadcoding Ira Weiny
2025-02-20 20:37 ` Alison Schofield
2025-02-20 21:51   ` Dr. David Alan Gilbert [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z7ej6lBOfRATYhER@gallifrey \
    --to=linux@treblig.org \
    --cc=alison.schofield@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=ira.weiny@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=vishal.l.verma@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.