2011-02-01 13:52:45

by Thomas Gleixner

[permalink] [raw]
Subject: [patch 27/28] posix-timers: Add support for fd based clocks

From: Richard Cochran <[email protected]>

Extend the negative clockids which are currently used by posix cpu
timers to encode the PID with a file descriptor based type which
encodes the fd in the upper bits.

Originally-from: Richard Cochran <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Cc: John Stultz <[email protected]>
---
include/linux/posix-timers.h | 13 +++++++++++++
kernel/posix-timers.c | 2 +-
2 files changed, 14 insertions(+), 1 deletion(-)

Index: linux-2.6-tip/include/linux/posix-timers.h
===================================================================
--- linux-2.6-tip.orig/include/linux/posix-timers.h
+++ linux-2.6-tip/include/linux/posix-timers.h
@@ -18,6 +18,17 @@ struct cpu_timer_list {
int firing;
};

+/*
+ * Bit fields within a clockid:
+ *
+ * The most significant 29 bits hold either a pid or a file descriptor.
+ *
+ * Bit 2 indicates whether a cpu clock refers to a thread or a process.
+ *
+ * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+ *
+ * A clockid is invalid if bits 2, 1, and 0 all set.
+ */
#define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))
#define CPUCLOCK_PERTHREAD(clock) \
(((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
@@ -29,6 +40,8 @@ struct cpu_timer_list {
#define CPUCLOCK_VIRT 1
#define CPUCLOCK_SCHED 2
#define CPUCLOCK_MAX 3
+#define CLOCKFD CPUCLOCK_MAX
+#define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)

#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
Index: linux-2.6-tip/kernel/posix-timers.c
===================================================================
--- linux-2.6-tip.orig/kernel/posix-timers.c
+++ linux-2.6-tip/kernel/posix-timers.c
@@ -477,7 +477,7 @@ static void release_posix_timer(struct k
static struct k_clock *clockid_to_kclock(const clockid_t id)
{
if (id < 0)
- return &clock_posix_cpu;
+ return (id & CLOCKFD_MASK) == CLOCKFD ? NULL : &clock_posix_cpu;

if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
return NULL;


2011-02-01 21:39:03

by john stultz

[permalink] [raw]
Subject: Re: [patch 27/28] posix-timers: Add support for fd based clocks

On Tue, 2011-02-01 at 13:52 +0000, Thomas Gleixner wrote:
> plain text document attachment
> (posix-timers-add-support-for-fd-based-clocks.patch)
> From: Richard Cochran <[email protected]>
>
> Extend the negative clockids which are currently used by posix cpu
> timers to encode the PID with a file descriptor based type which
> encodes the fd in the upper bits.
>
> Originally-from: Richard Cochran <[email protected]>
> Signed-off-by: Thomas Gleixner <[email protected]>
> Cc: John Stultz <[email protected]>

Acked-by: John Stultz <[email protected]>

2011-02-02 09:19:10

by Richard Cochran

[permalink] [raw]
Subject: Re: [patch 27/28] posix-timers: Add support for fd based clocks

On Tue, Feb 01, 2011 at 01:52:32PM -0000, Thomas Gleixner wrote:
> --- linux-2.6-tip.orig/include/linux/posix-timers.h
> +++ linux-2.6-tip/include/linux/posix-timers.h

> +/*
> + * Bit fields within a clockid:
> + *
> + * The most significant 29 bits hold either a pid or a file descriptor.
> + *
> + * Bit 2 indicates whether a cpu clock refers to a thread or a process.
> + *
> + * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
> + *
> + * A clockid is invalid if bits 2, 1, and 0 all set.

I should have said, "if bits 2, 1, and 0 *are* all set."

Thanks,
Richard

2011-02-02 10:33:25

by Thomas Gleixner

[permalink] [raw]
Subject: Re: [patch 27/28] posix-timers: Add support for fd based clocks

On Wed, 2 Feb 2011, Richard Cochran wrote:

> On Tue, Feb 01, 2011 at 01:52:32PM -0000, Thomas Gleixner wrote:
> > --- linux-2.6-tip.orig/include/linux/posix-timers.h
> > +++ linux-2.6-tip/include/linux/posix-timers.h
>
> > +/*
> > + * Bit fields within a clockid:
> > + *
> > + * The most significant 29 bits hold either a pid or a file descriptor.
> > + *
> > + * Bit 2 indicates whether a cpu clock refers to a thread or a process.
> > + *
> > + * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
> > + *
> > + * A clockid is invalid if bits 2, 1, and 0 all set.
>
> I should have said, "if bits 2, 1, and 0 *are* all set."

Fixed.

2011-02-02 22:06:05

by Richard Cochran

[permalink] [raw]
Subject: [tip:timers/core] posix-timers: Add support for fd based clocks

Commit-ID: 81e294cba2596f5f10848bbe19d98b344c2a2d5c
Gitweb: http://git.kernel.org/tip/81e294cba2596f5f10848bbe19d98b344c2a2d5c
Author: Richard Cochran <[email protected]>
AuthorDate: Tue, 1 Feb 2011 13:52:32 +0000
Committer: Thomas Gleixner <[email protected]>
CommitDate: Wed, 2 Feb 2011 15:28:19 +0100

posix-timers: Add support for fd based clocks

Extend the negative clockids which are currently used by posix cpu
timers to encode the PID with a file descriptor based type which
encodes the fd in the upper bits.

Originally-from: Richard Cochran <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Acked-by: John Stultz <[email protected]>
LKML-Reference: <[email protected]>
---
include/linux/posix-timers.h | 13 +++++++++++++
kernel/posix-timers.c | 2 +-
2 files changed, 14 insertions(+), 1 deletions(-)

diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h
index 79a1cea..88b9256 100644
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -18,6 +18,17 @@ struct cpu_timer_list {
int firing;
};

+/*
+ * Bit fields within a clockid:
+ *
+ * The most significant 29 bits hold either a pid or a file descriptor.
+ *
+ * Bit 2 indicates whether a cpu clock refers to a thread or a process.
+ *
+ * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+ *
+ * A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
#define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))
#define CPUCLOCK_PERTHREAD(clock) \
(((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
@@ -29,6 +40,8 @@ struct cpu_timer_list {
#define CPUCLOCK_VIRT 1
#define CPUCLOCK_SCHED 2
#define CPUCLOCK_MAX 3
+#define CLOCKFD CPUCLOCK_MAX
+#define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)

#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
diff --git a/kernel/posix-timers.c b/kernel/posix-timers.c
index 5a5a4f1..df629d8 100644
--- a/kernel/posix-timers.c
+++ b/kernel/posix-timers.c
@@ -488,7 +488,7 @@ static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
static struct k_clock *clockid_to_kclock(const clockid_t id)
{
if (id < 0)
- return &clock_posix_cpu;
+ return (id & CLOCKFD_MASK) == CLOCKFD ? NULL : &clock_posix_cpu;

if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
return NULL;