public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Al Viro <viro@ZenIV.linux.org.uk>
To: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [tracepoint] cargo-culting considered harmful...
Date: Fri, 25 Jan 2013 15:32:00 +0000	[thread overview]
Message-ID: <20130125153159.GE4503@ZenIV.linux.org.uk> (raw)
In-Reply-To: <20130125144953.GB20597@Krystal>

On Fri, Jan 25, 2013 at 09:49:53AM -0500, Mathieu Desnoyers wrote:
> static
> void lttng_enumerate_task_fd(struct lttng_session *session,
>                 struct task_struct *p, char *tmp)
> {
>         struct fdtable *fdt;
>         struct file *filp;
>         unsigned int i;
>         const unsigned char *path;
> 
>         task_lock(p);
>         if (!p->files)
>                 goto unlock_task;
>         spin_lock(&p->files->file_lock);
>         fdt = files_fdtable(p->files);
>         for (i = 0; i < fdt->max_fds; i++) {
>                 filp = fcheck_files(p->files, i);
>                 if (!filp)
>                         continue;
>                 path = d_path(&filp->f_path, tmp, PAGE_SIZE);
>                 /* Make sure we give at least some info */
>                 trace_lttng_statedump_file_descriptor(session, p, i,
>                         IS_ERR(path) ?
>                                 filp->f_dentry->d_name.name :
>                                 path);
>         }
>         spin_unlock(&p->files->file_lock);
> unlock_task:
>         task_unlock(p);
> }

*cringe*

a) yes, it needs d_lock for that ->d_name access
b) iterate_fd() is there for purpose; use it, instead of open-coding the
damn loop.  Something like

struct ctx {
	char *page;
	struct lttng_session *session,
	struct task_struct *p;
};
	
static int dump_one(void *p, struct file *file, unsigned fd)
{
	struct ctx *ctx = p;
	const char *s = d_path(&file->f_path, ctx->page, PAGE_SIZE);
	struct dentry *dentry;
	if (!IS_ERR(s)) {
		trace_lttng_statedump_file_descriptor(ctx->session, ctx->p, fd, s);
		return 0;
	}
	/* Make sure we give at least some info */
	dentry = file->f_path.dentry;
	spin_lock(&dentry->d_lock);
	trace_lttng_statedump_file_descriptor(ctx->session, ctx->p, fd,
		dentry->d_name);
	spin_unlock(&dentry->d_lock);
	return 0;
}

...
	task_lock(p);
	iterate_fd(p->files, 0, dump_one, &(struct ctx){tmp, session, p});
	task_unlock(p);

assuming it wouldn't be better to pass tmp/session/p as the single pointer
to struct in the first place - I don't know enough about the callers of
that sucker to tell.  And yes, iterate_fd() will DTRT if given NULL as the
first argument.  The second argument is "which descriptor should I start
from?", callback is called for everything present in the table starting from
that place until it returns non-zero or the end of table is reached...

PS: people really ought to be forced to read their code aloud over the phone -
that would rapidly improve the choice of identifiers ;-)

  reply	other threads:[~2013-01-25 15:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-23 22:55 [tracepoint] cargo-culting considered harmful Al Viro
2013-01-23 23:02 ` Steven Rostedt
2013-01-25 14:38   ` Mathieu Desnoyers
2013-01-25 15:27     ` Steven Rostedt
2013-01-25 16:14       ` Mathieu Desnoyers
2013-01-23 23:51 ` Andrew Morton
2013-01-24  1:48   ` Al Viro
2013-01-25 14:49     ` Mathieu Desnoyers
2013-01-25 15:32       ` Al Viro [this message]
2013-01-25 17:30         ` Mathieu Desnoyers
2013-02-03 19:16 ` [tip:perf/core] tracing: Remove tracepoint sample code tip-bot for Steven Rostedt

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=20130125153159.GE4503@ZenIV.linux.org.uk \
    --to=viro@zeniv.linux.org.uk \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@polymtl.ca \
    --cc=torvalds@linux-foundation.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox