All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrik Jakobsson <patrik.jakobsson@linux.intel.com>
To: strace-devel@lists.sourceforge.net, gabriel@lse.epita.fr,
	intel-gfx@lists.freedesktop.org, ldv@altlinux.org
Subject: Re: [PATCH v3 2/5] drm: Add private data field to trace control block
Date: Mon, 6 Jul 2015 10:09:05 +0200	[thread overview]
Message-ID: <20150706080905.GA2459@patrik-dev-mach> (raw)
In-Reply-To: <20150703003331.GA29080@altlinux.org>

On Fri, Jul 03, 2015 at 03:33:31AM +0300, Dmitry V. Levin wrote:
> On Wed, Jul 01, 2015 at 02:52:45PM +0200, Patrik Jakobsson wrote:
> [...]
> > --- a/defs.h
> > +++ b/defs.h
> > @@ -266,6 +266,13 @@ struct tcb {
> >  	int u_error;		/* Error code */
> >  	long scno;		/* System call number */
> >  	long u_arg[MAX_ARGS];	/* System call arguments */
> > +
> > +	/*
> > +	 * Private data for the decoding functions of the syscall. TCB core does
> > +	 * _not_ handle allocation / deallocation of this data.
> > +	 */
> > +	void *priv_data;
> > +
> 
> This will result to memory leaks if droptcb() is called before the
> syscall parser that allocated memory had a chance to deallocate it.
> As this data is no longer relevant after leaving trace_syscall_exiting(),
> I suggest to perform deallocation directly from trace_syscall_exiting.
> 
> This API could be made more flexible by adding another pointer -
> the function to be called to deallocate memory, e.g.
> struct tcb {
> 	...
> 	void *priv_data;
> 	void (*free_priv_data)(void *);
> 	...
> };
> ...
> void
> free_priv_data(struct tcb *tcp)
> {
> 	if (tcp->priv_data) {
> 		if (tcp->free_priv_data) {
> 			tcp->free_priv_data(tcp->priv_data);
> 			tcp->free_priv_data = NULL;
> 		}
> 		tcp->priv_data = NULL;
> 	}
> }
> ...
> droptcb(struct tcb *tcp)
> {
> 	...
> 	free_priv_data(tcp);
> 	...
> }
> ...
> trace_syscall_exiting(struct tcb *tcp)
> {
> 	...
>  ret:
> 	free_priv_data(tcp);
> 	...
> }
> 
> [...]
> On Wed, Jul 01, 2015 at 02:52:46PM +0200, Patrik Jakobsson wrote:

Yes, that's more robust. I was afraid it would be too intrusive.

> > * Makefile.am: Add compilation of drm.c
> > * defs.h: Declarations of drm functions
> > * drm.c: Utility functions for drm driver detection
> > * io.c: Dispatch drm ioctls
> > * ioctl.c: Distpatch generic and driver specific ioctls
> 
> This is not quite a GNU style changelog entry.  Please have a look at
> http://www.gnu.org/prep/standards/html_node/Style-of-Change-Logs.html
> and examples in strace.git history.
> 
> [...]

I'll get that sorted out.

> > +#include "defs.h"
> > +
> > +#include <drm.h>
> > +#include <linux/limits.h>
> 
> Please include <sys/param.h> instead of <linux/limits.h>.

Yup

> > +#define DRM_MAX_NAME_LEN 128
> > +
> > +struct drm_ioctl_priv {
> > +	char name[DRM_MAX_NAME_LEN];
> > +};
> > +
> > +inline int drm_is_priv(const unsigned int num)
> > +{
> > +	return (_IOC_NR(num) >= DRM_COMMAND_BASE &&
> > +		_IOC_NR(num) < DRM_COMMAND_END);
> > +}
> > +
> > +static int drm_get_driver_name(struct tcb *tcp, char *name, size_t bufsize)
> > +{
> > +	char path[PATH_MAX];
> > +	char link[PATH_MAX];
> > +	int ret;
> > +
> > +	ret = getfdpath(tcp, tcp->u_arg[0], path, PATH_MAX - 1);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	snprintf(link, PATH_MAX, "/sys/class/drm/%s/device/driver",
> > +		 basename(path));
> > +
> > +	ret = readlink(link, path, PATH_MAX - 1);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	path[ret] = '\0';
> > +	strncpy(name, basename(path), bufsize);
> > +
> > +	return 0;
> > +}
> 
> I think this is getting too complicated.  This function could just return
> strdup(basename(path)) or NULL in case of any error:
> 
> static char *
> drm_get_driver_name(struct tcb *tcp, const char *name)
> {
> 	char path[PATH_MAX];
> 	char link[PATH_MAX];
> 	int ret;
> 
> 	if (getfdpath(tcp, tcp->u_arg[0], path, PATH_MAX - 1) < 0)
> 		return NULL;
> 
> 	if (snprintf(link, PATH_MAX, "/sys/class/drm/%s/device/driver",
> 		     basename(path)) >= PATH_MAX)
> 		return NULL;
> 
> 	ret = readlink(link, path, PATH_MAX - 1);
> 	if (ret < 0)
> 		return NULL;
> 
> 	path[ret] = '\0';
> 	return strdup(basename(path));
> }
> 

That's nicer

> > +
> > +int drm_is_driver(struct tcb *tcp, const char *name)
> > +{
> > +	struct drm_ioctl_priv *priv;
> > +	int ret;
> > +
> > +	/*
> > +	 * If no private data is allocated we are detecting the driver name for
> > +	 * the first time and must resolve it.
> > +	 */
> > +	if (tcp->priv_data == NULL) {
> > +		tcp->priv_data = xcalloc(1, sizeof(struct drm_ioctl_priv));
> 
> xcalloc shouldn't be used if a potential memory allocation error is not
> fatal.  In a parser that performs verbose syscall decoding no memory
> allocation error is fatal.

Ok

> > +		priv = tcp->priv_data;
> > +
> > +		ret = drm_get_driver_name(tcp, priv->name, DRM_MAX_NAME_LEN);
> > +		if (ret)
> > +			return 0;
> > +	}
> > +
> > +	priv = tcp->priv_data;
> > +
> > +	return strncmp(name, priv->name, DRM_MAX_NAME_LEN) == 0;
> 
> Then with priv_data+free_priv_data interface this would looks smth like
> 	...
> 	if (!tcp->priv_data) {
> 		tcp->priv_data = drm_get_driver_name(tcp, name);
> 		if (tcp->priv_data) {
> 			tcp->free_priv_data = free;
> 		} else {
> 			tcp->priv_data = (void *) "";
> 			tcp->free_priv_data = NULL;
> 		}
> 	}
> 	return !strcmp(name, (char *) tcp->priv_data);
> 
> > +}
> > +
> > +int drm_decode_number(struct tcb *tcp, unsigned int arg)
> 
> This is an ioctl request code, let's consistently call it "code" to
> distinguish from its argument.
> 
> [...]
> > --- a/ioctl.c
> > +++ b/ioctl.c
> > @@ -182,7 +182,7 @@ hiddev_decode_number(unsigned int arg)
> >  }
> >  
> >  int
> > -ioctl_decode_command_number(unsigned int arg)
> > +ioctl_decode_command_number(struct tcb *tcp, unsigned int arg)
> 
> I've already changed ioctl_decode_command_number's signature:
> struct tcb * is already there and "arg" is now called "code".
> 
> 
> -- 
> ldv


_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

  reply	other threads:[~2015-07-06  8:09 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-01 12:52 [PATCH v3 0/5] drm: Add decoding for DRM/KMS and i915 ioctls Patrik Jakobsson
     [not found] ` <1435755168-16207-1-git-send-email-patrik.jakobsson-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2015-07-01 12:52   ` [PATCH v3 1/5] drm: Add config for detecting libdrm Patrik Jakobsson
     [not found]     ` <1435755168-16207-2-git-send-email-patrik.jakobsson-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2015-07-23  9:48       ` Mike Frysinger
2015-07-23 10:44         ` Dmitry V. Levin
     [not found]           ` <20150723104417.GA21575-u2l5PoMzF/Vg9hUCZPvPmw@public.gmane.org>
2015-07-23 11:02             ` Mike Frysinger
2015-07-30 13:30         ` Patrik Jakobsson
2015-07-30 14:04           ` Mike Frysinger
2015-07-31  9:09             ` Patrik Jakobsson
2015-08-01 18:22               ` Dmitry V. Levin
2015-08-02 14:03                 ` Patrik Jakobsson
2015-07-01 12:52   ` [PATCH v3 2/5] drm: Add private data field to trace control block Patrik Jakobsson
2015-07-03  0:33     ` Dmitry V. Levin
2015-07-06  8:09       ` Patrik Jakobsson [this message]
2015-07-01 12:52   ` [PATCH v3 3/5] drm: Add dispatcher and driver identification for DRM Patrik Jakobsson
2015-07-01 12:52 ` [PATCH v3 4/5] drm: Add decoding of i915 ioctls Patrik Jakobsson
2015-07-03  0:36   ` Dmitry V. Levin
     [not found]     ` <20150703003609.GB29080-u2l5PoMzF/Vg9hUCZPvPmw@public.gmane.org>
2015-07-06 10:35       ` Patrik Jakobsson
2015-07-06 14:40         ` Gabriel Laskar
2015-07-08  0:11           ` Dmitry V. Levin
2015-07-10 12:36             ` Patrik Jakobsson
2015-07-10 12:57               ` Dmitry V. Levin
2015-07-01 12:52 ` [PATCH v3 5/5] drm: Add decoding of DRM and KMS ioctls Patrik Jakobsson

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=20150706080905.GA2459@patrik-dev-mach \
    --to=patrik.jakobsson@linux.intel.com \
    --cc=gabriel@lse.epita.fr \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=ldv@altlinux.org \
    --cc=strace-devel@lists.sourceforge.net \
    /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.