linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] 2.4: introduce get_cpu() and put_cpu()
@ 2002-10-06 19:45 Robert Love
  2002-10-06 20:36 ` Kasper Dupont
  0 siblings, 1 reply; 3+ messages in thread
From: Robert Love @ 2002-10-06 19:45 UTC (permalink / raw)
  To: marcelo; +Cc: linux-kernel

Master Marcelo,

In 2.5, it is unsafe to assume the current processor does not change out
from under you - thus you must be atomic when calling
smp_processor_id().  We introduced the get_cpu() and put_cpu() methods
to provide a safe way to access the current processor.

This patch back-ports those interfaces to 2.4.  Note they do nothing
special: get_cpu() simply returns smp_processor_id() and put_cpu() is a
no-op.  But this will allow easier back-porting from 2.5 and common
code-base for drivers, etc.  It also does not hurt to be explicit that
you do not want the processor to change.

As an example, I converted one use of smp_processor_id() to the new
interface.

Patch is against 2.4.20-pre9, please apply.

	Robert Love

diff -urN linux-2.4.20-pre9/arch/i386/kernel/ioport.c linux/arch/i386/kernel/ioport.c
--- linux-2.4.20-pre9/arch/i386/kernel/ioport.c	2002-10-06 14:58:01.000000000 -0400
+++ linux/arch/i386/kernel/ioport.c	2002-10-06 15:21:04.000000000 -0400
@@ -55,12 +55,15 @@
 asmlinkage int sys_ioperm(unsigned long from, unsigned long num, int turn_on)
 {
 	struct thread_struct * t = &current->thread;
-	struct tss_struct * tss = init_tss + smp_processor_id();
+	struct tss_struct * tss;
 
 	if ((from + num <= from) || (from + num > IO_BITMAP_SIZE*32))
 		return -EINVAL;
 	if (turn_on && !capable(CAP_SYS_RAWIO))
 		return -EPERM;
+
+	tss = init_tss + get_cpu();
+
 	/*
 	 * If it's the first ioperm() call in this thread's lifetime, set the
 	 * IO bitmap up. ioperm() is much less timing critical than clone(),
diff -urN linux-2.4.20-pre9/include/linux/smp.h linux/include/linux/smp.h
--- linux-2.4.20-pre9/include/linux/smp.h	2002-10-06 14:57:20.000000000 -0400
+++ linux/include/linux/smp.h	2002-10-06 15:11:00.000000000 -0400
@@ -87,5 +87,9 @@
 #define smp_call_function(func,info,retry,wait)	({ 0; })
 #define cpu_online_map				1
 
-#endif
-#endif
+#endif /* !CONFIG_SMP */
+
+#define get_cpu()	smp_processor_id()
+#define put_cpu()	do { } while(0)
+
+#endif /* __LINUX_SMP_H */


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] 2.4: introduce get_cpu() and put_cpu()
  2002-10-06 19:45 [PATCH] 2.4: introduce get_cpu() and put_cpu() Robert Love
@ 2002-10-06 20:36 ` Kasper Dupont
  2002-10-06 21:08   ` Robert Love
  0 siblings, 1 reply; 3+ messages in thread
From: Kasper Dupont @ 2002-10-06 20:36 UTC (permalink / raw)
  To: Robert Love; +Cc: marcelo, linux-kernel

Robert Love wrote:
> 
> diff -urN linux-2.4.20-pre9/arch/i386/kernel/ioport.c linux/arch/i386/kernel/ioport.c
> --- linux-2.4.20-pre9/arch/i386/kernel/ioport.c 2002-10-06 14:58:01.000000000 -0400
> +++ linux/arch/i386/kernel/ioport.c     2002-10-06 15:21:04.000000000 -0400
> @@ -55,12 +55,15 @@
>  asmlinkage int sys_ioperm(unsigned long from, unsigned long num, int turn_on)
>  {
>         struct thread_struct * t = &current->thread;
> -       struct tss_struct * tss = init_tss + smp_processor_id();
> +       struct tss_struct * tss;
> 
>         if ((from + num <= from) || (from + num > IO_BITMAP_SIZE*32))
>                 return -EINVAL;
>         if (turn_on && !capable(CAP_SYS_RAWIO))
>                 return -EPERM;
> +
> +       tss = init_tss + get_cpu();
> +
>         /*
>          * If it's the first ioperm() call in this thread's lifetime, set the
>          * IO bitmap up. ioperm() is much less timing critical than clone(),

To me it really looks like you are missing a put_cpu() call somewhere.
I know it is a no-op, but since you intend to show how to use it, I
it really ought to be there.

Does this look right?

diff -Nur linux.old/arch/i386/kernel/ioport.c linux.new/arch/i386/kernel/ioport.c
--- linux.old/arch/i386/kernel/ioport.c	Sun Oct  6 22:33:22 2002
+++ linux.new/arch/i386/kernel/ioport.c	Sun Oct  6 22:33:53 2002
@@ -87,6 +87,8 @@
 	set_bitmap(t->io_bitmap, from, num, !turn_on);
 	set_bitmap(tss->io_bitmap, from, num, !turn_on);
 
+	put_cpu();
+
 	return 0;
 }
 

-- 
Kasper Dupont -- der bruger for meget tid på usenet.
For sending spam use mailto:aaarep@daimi.au.dk
or mailto:mcxumhvenwblvtl@skrammel.yaboo.dk

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] 2.4: introduce get_cpu() and put_cpu()
  2002-10-06 20:36 ` Kasper Dupont
@ 2002-10-06 21:08   ` Robert Love
  0 siblings, 0 replies; 3+ messages in thread
From: Robert Love @ 2002-10-06 21:08 UTC (permalink / raw)
  To: Kasper Dupont; +Cc: marcelo, linux-kernel

On Sun, 2002-10-06 at 16:36, Kasper Dupont wrote:

> To me it really looks like you are missing a put_cpu() call somewhere.
> I know it is a no-op, but since you intend to show how to use it, I
> it really ought to be there.

Whoops, you are right.

> Does this look right?

Yep, thanks.

Marcelo, attached patch is a resend with the missing put_cpu()
included.  Please, apply.

	Robert Love

diff -urN linux-2.4.20-pre9/arch/i386/kernel/ioport.c linux/arch/i386/kernel/ioport.c
--- linux-2.4.20-pre9/arch/i386/kernel/ioport.c	2002-10-06 14:58:01.000000000 -0400
+++ linux/arch/i386/kernel/ioport.c	2002-10-06 16:53:04.000000000 -0400
@@ -55,12 +55,15 @@
 asmlinkage int sys_ioperm(unsigned long from, unsigned long num, int turn_on)
 {
 	struct thread_struct * t = &current->thread;
-	struct tss_struct * tss = init_tss + smp_processor_id();
+	struct tss_struct * tss;
 
 	if ((from + num <= from) || (from + num > IO_BITMAP_SIZE*32))
 		return -EINVAL;
 	if (turn_on && !capable(CAP_SYS_RAWIO))
 		return -EPERM;
+
+	tss = init_tss + get_cpu();
+
 	/*
 	 * If it's the first ioperm() call in this thread's lifetime, set the
 	 * IO bitmap up. ioperm() is much less timing critical than clone(),
@@ -84,6 +87,8 @@
 	set_bitmap(t->io_bitmap, from, num, !turn_on);
 	set_bitmap(tss->io_bitmap, from, num, !turn_on);
 
+	put_cpu();
+
 	return 0;
 }
 
diff -urN linux-2.4.20-pre9/include/linux/smp.h linux/include/linux/smp.h
--- linux-2.4.20-pre9/include/linux/smp.h	2002-10-06 14:57:20.000000000 -0400
+++ linux/include/linux/smp.h	2002-10-06 16:52:45.000000000 -0400
@@ -87,5 +87,9 @@
 #define smp_call_function(func,info,retry,wait)	({ 0; })
 #define cpu_online_map				1
 
-#endif
-#endif
+#endif /* !CONFIG_SMP */
+
+#define get_cpu()	smp_processor_id()
+#define put_cpu()	do { } while(0)
+
+#endif /* __LINUX_SMP_H */


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-10-06 21:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-06 19:45 [PATCH] 2.4: introduce get_cpu() and put_cpu() Robert Love
2002-10-06 20:36 ` Kasper Dupont
2002-10-06 21:08   ` Robert Love

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).