Skip to contentSkip to conversation

latest linus-2.5 BK broken

DISCUSSION Linus Torvalds 2 participants 4 messages Jun 19, 2002 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Review

Quoted 7 lines · Rusty Russell
(quoted diff) +5 -1
-	new_mask &= cpu_online_map;
+	/* Eliminate offline cpus from the mask */
+	for (i = 0; i < NR_CPUS; i++)
+		if (!cpu_online(i))
+			new_mask &= ~(1<<i);
+

And why can't cpu_online_map be a bitmap?

What's your beef against sane and efficient data structures? The above is just crazy.

Just add a

	#define NRCPUWORDS ROUND_UP(NR_CPU, BITS_PER_LONG)

	struct cpu_mask {
		unsigned long mask[NRCPUWORDS];
	} cpu_mask_t;

and then add a few simple operations like

	cpumask_and(cpu_mask_t * res, cpu_mask_t *a, cpu_mask_t *b);

and friends.. See how we handle this issue in <linux/signal.h>, which has perfectly efficient things to do all the same issues (ie see how "sigemptyset()" and friends compile to efficient code for the "normal" cases.

This is not rocket science, and I find it ridiculous that you claim to worry about scaling up to thousands of CPU's, and then you try to send me absolute crap like the above which clearly is unacceptable for lots of CPU's.

No, C doesn't have built-in support for bitmap operations except on a small scale level (ie single words), and yes, clearly that's why Linux tends to prefer only small bitmaps, but NO, that does not make bitmaps evil.

Linus

#2 lore raw

In message <[email protected]> you write:

Quoted 12 lines · Rusty Russell

Review

Quoted 7 lines
(quoted diff) +5 -1
-	new_mask &= cpu_online_map;
+	/* Eliminate offline cpus from the mask */
+	for (i = 0; i < NR_CPUS; i++)
+		if (!cpu_online(i))
+			new_mask &= ~(1<<i);
+

And why can't cpu_online_map be a bitmap?

Quoted 2 lines

What's your beef against sane and efficient data structures? The above is just crazy.

Oh, it can be. I wasn't going to require something from all archs for this one case (well, it was more like zero cases when I first did the patch).

Quoted 3 lines

and then add a few simple operations like

	cpumask_and(cpu_mask_t * res, cpu_mask_t *a, cpu_mask_t *b);

Sure... or just make all archs supply a "cpus_online_of(mask)" which does that, unless there are other interesting cases. Or we can go the other way and have a general "and_region(void *res, void *a, void *b, int len)". Which one do you want?

Quoted 4 lines

This is not rocket science, and I find it ridiculous that you claim to worry about scaling up to thousands of CPU's, and then you try to send me absolute crap like the above which clearly is unacceptable for lots of CPU's.

Spinning 1000 times doesn't phase me until someone complains. Breaking userspace code does. One can be fixed if it proves to be a bottleneck. Understand?

Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

#3 lore raw
Quoted 8 lines · Rusty Russell
Quoted 3 lines

and then add a few simple operations like

	cpumask_and(cpu_mask_t * res, cpu_mask_t *a, cpu_mask_t *b);

Sure... or just make all archs supply a "cpus_online_of(mask)" which does that, unless there are other interesting cases. Or we can go the other way and have a general "and_region(void *res, void *a, void *b, int len)". Which one do you want?

There are definitely other "interesting" cases that already do the full bitwise and/or on bitmasks - see sigset_t and sigaddset/sigdelset/ sigfillset. It's really the exact same code, and the exact same issues.

The problem with a generic "and_region" is that it's a slight amount of work to make sure that we optimize for the common cases (and since I'm not a huge believer in hundreds of nodes, I consider the common case to be a single word). And do things like just automatically get the UP case right:
which we do right now by just virtue of having a constant cpu_online_mask, and letting the compiler just do the (obvious) optimizations.

I'm a _huge_ believer in having generic code that is automatically optimized away by the compiler into nothingness. (And by contrast, I absolutely _detest_ #ifdef's in source code that makes those optimizations explicit). But that sometimes requires some thought, notably making sure that all constants hang around as constants all the way to the code generation phase (this tends to mean inline functions and #defines).

It _would_ probably be worthwhile to try to have better support for "bitmaps" as real kernel data structures, since we actually have this problem in multiple places. Right now we already use bitmaps for signal handling (one or two words, constant size), for FD_SET's (variable size), for various filesystems (variable size, largish), and for a lot of random drivers (some variable, some constant).

It wasn't that long ago that I added a "bitmap_member()" macro to <linux/types.h> to declare bitmaps exactly because a lot of people _were_ doing it and getting it wrong. Actually, the most common case was not a bug, but a latent problem with code that did something like

	unsigned char bitmap[BITMAP_SIZE/8];

which works on x86 as long as the bitmap size was a multiple of 8.

It would probably make sense to make a real <linux/bitmap.h>, move the bitmap_member() there (and rename to "bitmap_declare()" - it's called member because all the places I first looked at were structure members), and add some simple generic routines for handling these things.

(We've obviously had the bit_set/clear/test() stuff forever, but the more involved stuff should be fairly easy to abstract out too, instead of having special functions for signal masks).

Quoted 2 lines

Breaking userspace code does. One can be fixed if it proves to be a bottleneck. Understand?

What I don't understand is why you don't accept the fact that these things can be considered infinitely big. There's nothing fundamentally wrong with static allocation.

People who build thousand-node systems _are_ going to compile their own distribution. Trust me. They aren't just going to slap down redhat-7.3 on a 16k-node ASCI Purple. It makes no sense to do that. They may want to run quake or something standard on it without recompiling, but especially the maintenance stuff - the stuff which cares about CPU affinity - is a nobrainer.

So you can easily just accept the fact that at some point the max number of CPU's can be considered fixed. And that "some point" isn't even very high, especially since bitmaps _are_ so dense that there is basically no overhead to just starting out with

	#define MAX_CPU (1024)

	bitmap_declare(cpu_bitmap, MAX_CPU);

and let it be at that. That 1024 is already ridiculously high, in my opinion - simply because people who are playing with bigger numbers _are_ going to be able to just increase the number and recompile.

Linus

#4 lore raw

In message <[email protected]> you wri te:

Quoted 16 lines · Rusty Russell
Quoted 8 lines
Quoted 3 lines

and then add a few simple operations like

	cpumask_and(cpu_mask_t * res, cpu_mask_t *a, cpu_mask_t *b);

Sure... or just make all archs supply a "cpus_online_of(mask)" which does that, unless there are other interesting cases. Or we can go the other way and have a general "and_region(void *res, void *a, void *b, int len)". Which one do you want?

There are definitely other "interesting" cases that already do the full bitwise and/or on bitmasks - see sigset_t and sigaddset/sigdelset/ sigfillset. It's really the exact same code, and the exact same issues.

Quoted 6 lines

The problem with a generic "and_region" is that it's a slight amount of work to make sure that we optimize for the common cases (and since I'm not a huge believer in hundreds of nodes, I consider the common case to be a single word). And do things like just automatically get the UP case right:
which we do right now by just virtue of having a constant cpu_online_mask, and letting the compiler just do the (obvious) optimizations.

Sure, completely agreed.

Normal tricks here: 1 long turns into equivalent to dst = a & b, the other cases are handled with varying amount of suckiness. Code and optimization tested on 2.95.4 and 3.0.4 (both PPC), kernel compiled on my x86 box back in .au.

Quoted 4 lines

It would probably make sense to make a real <linux/bitmap.h>, move the bitmap_member() there (and rename to "bitmap_declare()" - it's called member because all the places I first looked at were structure members), and add some simple generic routines for handling these things.

I renamed it to DECLARE_BITMAP() to match list, mutex et al. and moved it to linux/bitops.h.

PS. Please sort out merging with Paulus's stuff: I'd like to compile
on PPC soon since I'm laptop-only for two more weeks 8)
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

include/linux/bitops.h +21 -3
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.23/include/linux/bitops.h working-2.5.23-bitops/include/linux/bitops.h
@@ -2,6 +2,27 @@
 #define _LINUX_BITOPS_H
 #include <asm/bitops.h>
 
+#define DECLARE_BITMAP(name,bits) \
+	unsigned long name[((bits)+BITS_PER_LONG-1)/BITS_PER_LONG]
+
+#ifndef HAVE_ARCH_AND_REGION
+void __and_region(unsigned long num, unsigned char *dst,
+		  const unsigned char *a, const unsigned char *b);
+#endif
+
+/* For the moment, handle 1 long case fast, leave rest to __and_region. */
+#define and_region(num,dst,a,b)						     \
+do {									     \
+	if (__alignof__(*(a)) == __alignof__(long)			     \
+	    && __alignof__(*(b)) == __alignof__(long)			     \
+	    && __builtin_constant_p(num)				     \
+	    && (num) == sizeof(long)) {					     \
+		    *((unsigned long *)(dst)) =				     \
+			    (*(unsigned long *)(a) & *(unsigned long *)(b)); \
+	} else								     \
+		__and_region((num), (void*)(dst), (void*)(a), (void*)(b));   \
+} while(0)
+
 /*
  * ffs: find first bit set. This is defined the same way as
  * the libc and compiler builtin ffs routines, therefore
@@ -106,8 +127,5 @@
         res = (res & 0x33) + ((res >> 2) & 0x33);
         return (res & 0x0F) + ((res >> 4) & 0x0F);
 }
-
-#include <asm/bitops.h>
-
 
 #endif
include/linux/types.h +0 -3
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.23/include/linux/types.h working-2.5.23-bitops/include/linux/types.h
@@ -3,9 +3,6 @@
 
 #ifdef	__KERNEL__
 #include <linux/config.h>
-
-#define bitmap_member(name,bits) \
-	unsigned long name[((bits)+BITS_PER_LONG-1)/BITS_PER_LONG]
 #endif
 
 #include <linux/posix_types.h>
include/sound/ac97_codec.h +2 -1
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.23/include/sound/ac97_codec.h working-2.5.23-bitops/include/sound/ac97_codec.h
@@ -25,6 +25,7 @@
  *
  */
 
+#include <linux/bitops.h>
 #include "control.h"
 #include "info.h"
 
@@ -160,7 +161,7 @@
 	unsigned int rates_mic_adc;
 	unsigned int spdif_status;
 	unsigned short regs[0x80]; /* register cache */
-	bitmap_member(reg_accessed, 0x80); /* bit flags */
+	DECLARE_BITMAP(reg_accessed, 0x80); /* bit flags */
 	union {			/* vendor specific code */
 		struct {
 			unsigned short unchained[3];	// 0 = C34, 1 = C79, 2 = C69
kernel/Makefile +2 -2
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.23/kernel/Makefile working-2.5.23-bitops/kernel/Makefile
@@ -10,12 +10,12 @@
 O_TARGET := kernel.o
 
 export-objs = signal.o sys.o kmod.o context.o ksyms.o pm.o exec_domain.o \
-		printk.o platform.o suspend.o
+		printk.o platform.o suspend.o bitops.o
 
 obj-y     = sched.o dma.o fork.o exec_domain.o panic.o printk.o \
 	    module.o exit.o itimer.o time.o softirq.o resource.o \
 	    sysctl.o capability.o ptrace.o timer.o user.o \
-	    signal.o sys.o kmod.o context.o futex.o platform.o
+	    signal.o sys.o kmod.o context.o futex.o platform.o bitops.o
 
 obj-$(CONFIG_UID16) += uid16.o
 obj-$(CONFIG_MODULES) += ksyms.o
kernel/bitops.c +32 -0
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.23/kernel/bitops.c working-2.5.23-bitops/kernel/bitops.c
@@ -0,0 +1,32 @@
+#include <linux/config.h>
+#include <linux/bitops.h>
+#include <linux/module.h>
+
+#ifndef HAVE_ARCH_AND_REGION
+/* Generic is fairly stupid: archs should optimize properly. */
+void __and_region(unsigned long num, unsigned char *dst,
+		  const unsigned char *a, const unsigned char *b)
+{
+	unsigned long i;
+
+	/* Copy first bytes, until one is long aligned. */
+	for (i = 0; i < num && ((unsigned long)a+i) % __alignof__(long); i++)
+		dst[i] = (a[i] & b[i]);
+
+	/* If they are all aligned, do long-at-a-time copy */
+	if (((unsigned long)b+i)%__alignof__(long) == 0
+	    && ((unsigned long)dst+i)%__alignof__(long) == 0) {
+		for (; i + sizeof(long) <= num; i += sizeof(long)) {
+			*(unsigned long *)(dst+i)
+				= (*(unsigned long *)(a+i)
+				   & *(unsigned long *)(b+i));
+		}
+	}
+
+	/* Do whatever is left. */
+	for (; i < num; i++)
+		dst[i] = (a[i] & b[i]);
+}
+
+EXPORT_SYMBOL(__and_region);
+#endif
sound/core/seq/seq_clientmgr.h +2 -2
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.23/sound/core/seq/seq_clientmgr.h working-2.5.23-bitops/sound/core/seq/seq_clientmgr.h
@@ -53,8 +53,8 @@
 	char name[64];		/* client name */
 	int number;		/* client number */
 	unsigned int filter;	/* filter flags */
-	bitmap_member(client_filter, 256);
-	bitmap_member(event_filter, 256);
+	DECLARE_BITMAP(client_filter, 256);
+	DECLARE_BITMAP(event_filter, 256);
 	snd_use_lock_t use_lock;
 	int event_lost;
 	/* ports */
sound/core/seq/seq_queue.h +2 -1
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.23/sound/core/seq/seq_queue.h working-2.5.23-bitops/sound/core/seq/seq_queue.h
@@ -26,6 +26,7 @@
 #include "seq_lock.h"
 #include <linux/interrupt.h>
 #include <linux/list.h>
+#include <linux/bitops.h>
 
 #define SEQ_QUEUE_NO_OWNER (-1)
 
@@ -51,7 +52,7 @@
 	spinlock_t check_lock;
 
 	/* clients which uses this queue (bitmap) */
-	bitmap_member(clients_bitmap,SNDRV_SEQ_MAX_CLIENTS);
+	DECLARE_BITMAP(clients_bitmap,SNDRV_SEQ_MAX_CLIENTS);
 	unsigned int clients;	/* users of this queue */
 	struct semaphore timer_mutex;