mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Richard Cochran <richardcochran@gmail.com>
Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org,
	netdev@vger.kernel.org, Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Arnd Bergmann <arnd@arndb.de>, Christoph Lameter <cl@linux.com>,
	David Miller <davem@davemloft.net>,
	John Stultz <johnstul@us.ibm.com>,
	Krzysztof Halasa <khc@pm.waw.pl>,
	Peter Zijlstra <peterz@infradead.org>,
	Rodolfo Giometti <giometti@linux.it>
Subject: Re: [PATCH V7 3/8] posix clocks: introduce dynamic clocks
Date: Thu, 16 Dec 2010 21:56:38 +0100 (CET)	[thread overview]
Message-ID: <alpine.LFD.2.00.1012161939340.12146@localhost6.localdomain6> (raw)
In-Reply-To: <8debe4d48d9a6484b7fbd35d8888524155fed977.1292512461.git.richard.cochran@omicron.at>

On Thu, 16 Dec 2010, Richard Cochran wrote:

> --- /dev/null
> +++ b/include/linux/posix-clock.h
> +/**
> + * struct posix_clock_fops - character device operations
> + *
> + * Every posix clock is represented by a character device. Drivers may
> + * optionally offer extended capabilities by implementing these
> + * functions. The character device file operations are first handled
> + * by the clock device layer, then passed on to the driver by calling
> + * these functions.
> + *
> + * The clock device layer already uses fp->private_data, so drivers
> + * are provided their private data via the 'priv' paramenter.
> + */
> +void *posix_clock_private(struct file *fp);

Leftover ? There is neither a caller nor an implementation

> +struct posix_clock_fops {
> +	int (*fasync)  (void *priv, int fd, struct file *file, int on);
> +	int (*mmap)    (void *priv, struct vm_area_struct *vma);
> +	int (*open)    (void *priv, fmode_t f_mode);
> +	int (*release) (void *priv);
> +	long (*ioctl)  (void *priv, unsigned int cmd, unsigned long arg);
> +	long (*compat_ioctl) (void *priv, unsigned int cmd, unsigned long arg);

Do we really need a compat_ioctl ?

> +	ssize_t (*read) (void *priv, uint flags, char __user *buf, size_t cnt);
> +	unsigned int (*poll) (void *priv, struct file *file, poll_table *wait);

> --- a/kernel/time/Makefile
> +++ b/kernel/time/Makefile
> @@ -1,4 +1,5 @@
> -obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.o
> +obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o \
> +timecompare.o timeconv.o posix-clock.o

Please start a new obj-y += line instead
  
> --- /dev/null
> +++ b/kernel/time/posix-clock.c
> +
> +#define MAX_CLKDEV BITS_PER_LONG

Please put some more space between the MAX_CLKDEV and BITS_PER_LONG. I
had to look three times to not read it as a single word.

> +static DECLARE_BITMAP(clocks_map, MAX_CLKDEV);
> +static DEFINE_MUTEX(clocks_mutex); /* protects 'clocks_map' */

Please avoid tail comments

> +struct posix_clock {
> +	struct posix_clock_operations *ops;
> +	struct cdev cdev;
> +	struct kref kref;
> +	struct mutex mux;
> +	void *priv;

You can get away with that private pointer and all the void *
arguments to the various posix_clock_operations, if you mandate that
the posix_clock_operations are embedded into a clock specific data
structure.

So void *priv would become struct posix_clock_operations *clkops and
you can get your private data in the clock implementation with
container_of().

> +	int index;
> +	bool zombie;

Ths field is only set, but nowhere else used. What's the purpose ?
Leftover ?

> +};
> +

> +static void delete_clock(struct kref *kref);
> +
> +
> +static int posix_clock_open(struct inode *inode, struct file *fp)
> +{
> +	struct posix_clock *clk =
> +		container_of(inode->i_cdev, struct posix_clock, cdev);
> +
> +	kref_get(&clk->kref);
> +	fp->private_data = clk;

fp->private_data should only be set on success. And this will leak a
ref count when the clock open function fails.

What's that kref protecting here ?

> +
> +	if (clk->ops->fops.open)
> +		return clk->ops->fops.open(clk->priv, fp->f_mode);
> +	else
> +		return 0;
> +}
> +
> +static int posix_clock_release(struct inode *inode, struct file *fp)
> +{
> +	struct posix_clock *clk = fp->private_data;
> +	int err = 0;

fp->private_data should be set to NULL in the release function.

> +	if (clk->ops->fops.release)
> +		err = clk->ops->fops.release(clk->priv);
> +
> +	kref_put(&clk->kref, delete_clock);

> +struct posix_clock *posix_clock_create(struct posix_clock_operations *cops,
> +				       dev_t devid, void *priv)
> +{
> +	struct posix_clock *clk;
> +	int err;
> +
> +	mutex_lock(&clocks_mutex);
> +
> +	err = -ENOMEM;
> +	clk = kzalloc(sizeof(*clk), GFP_KERNEL);
> +	if (!clk)
> +		goto no_memory;
> +
> +	err = -EBUSY;
> +	clk->index = find_first_zero_bit(clocks_map, MAX_CLKDEV);
> +	if (clk->index < MAX_CLKDEV)
> +		set_bit(clk->index, clocks_map);
> +	else
> +		goto no_index;

	if (clk->index >= MAX_CLKDEV)
		goto no_index;

	set_bit(clk->index, clocks_map);

Makes it better readable.

> +static void delete_clock(struct kref *kref)
> +{
> +	struct posix_clock *clk =
> +		container_of(kref, struct posix_clock, kref);
> +
> +	mutex_lock(&clocks_mutex);
> +	clear_bit(clk->index, clocks_map);
> +	mutex_unlock(&clocks_mutex);
> +
> +	mutex_destroy(&clk->mux);
> +	kfree(clk);
> +}
> +
> +void posix_clock_destroy(struct posix_clock *clk)
> +{
> +	cdev_del(&clk->cdev);
> +
> +	mutex_lock(&clk->mux);
> +	clk->zombie = true;
> +	mutex_unlock(&clk->mux);
> +
> +	kref_put(&clk->kref, delete_clock);

I still have some headache to understand that kref / delete_clock
magic here.

Thanks,

	tglx

  parent reply	other threads:[~2010-12-16 20:57 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-16 15:41 [PATCH V7 0/8] ptp: IEEE 1588 hardware clock support Richard Cochran
2010-12-16 15:41 ` [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit Richard Cochran
2010-12-16 17:48   ` Thomas Gleixner
2010-12-17 20:16   ` Kuwahara,T.
2010-12-21  7:56     ` Richard Cochran
2010-12-21 20:57       ` Kuwahara,T.
2010-12-21 22:25         ` john stultz
2010-12-22  7:13           ` Richard Cochran
2010-12-22 20:27           ` Kuwahara,T.
2010-12-23  0:00             ` john stultz
2010-12-23  6:13             ` Richard Cochran
2010-12-25 20:38               ` Kuwahara,T.
2010-12-26 14:14                 ` Richard Cochran
2010-12-21 19:37     ` john stultz
2010-12-21 21:13       ` Kuwahara,T.
2010-12-21 21:59         ` john stultz
2010-12-22  7:11           ` Richard Cochran
2010-12-22  9:58           ` Alexander Gordeev
2010-12-16 15:42 ` [PATCH V7 2/8] posix clocks: introduce a syscall for clock tuning Richard Cochran
2010-12-16 15:51   ` Arnd Bergmann
2010-12-16 17:55   ` Thomas Gleixner
2010-12-16 15:43 ` [PATCH V7 3/8] posix clocks: introduce dynamic clocks Richard Cochran
2010-12-16 16:16   ` Arnd Bergmann
2010-12-16 20:56   ` Thomas Gleixner [this message]
2010-12-17  6:29     ` Richard Cochran
2010-12-16 15:43 ` [PATCH V7 4/8] posix clocks: hook dynamic clocks into system calls Richard Cochran
2010-12-16 23:20   ` Thomas Gleixner
2010-12-17  7:04     ` Richard Cochran
2010-12-17 10:03       ` Thomas Gleixner
2010-12-21  8:00         ` Richard Cochran
2010-12-22  8:21         ` Richard Cochran
2010-12-16 15:44 ` [PATCH V7 5/8] ptp: Added a brand new class driver for ptp clocks Richard Cochran
2010-12-16 15:57   ` Arnd Bergmann
2010-12-16 16:08   ` Rodolfo Giometti
2010-12-16 15:44 ` [PATCH V7 6/8] ptp: Added a clock that uses the eTSEC found on the MPC85xx Richard Cochran
2010-12-16 15:44 ` [PATCH V7 7/8] ptp: Added a clock driver for the IXP46x Richard Cochran
2011-01-02  8:45   ` Pavel Machek
2011-01-02  9:12     ` Richard Cochran
2011-01-02  9:20       ` Pavel Machek
2011-01-03 17:07         ` Richard Cochran
2011-01-06 20:04           ` Pavel Machek
2011-01-02  9:19     ` Richard Cochran
2010-12-16 15:45 ` [PATCH V7 8/8] ptp: Added a clock driver for the National Semiconductor PHYTER Richard Cochran

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=alpine.LFD.2.00.1012161939340.12146@localhost6.localdomain6 \
    --to=tglx@linutronix.de \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=arnd@arndb.de \
    --cc=cl@linux.com \
    --cc=davem@davemloft.net \
    --cc=giometti@linux.it \
    --cc=johnstul@us.ibm.com \
    --cc=khc@pm.waw.pl \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=richardcochran@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®