Skip to contentSkip to conversation

lib/decompress_unxz.c: removing all memory helper functions

PATCH In review T Makphaibulchoke 9 participants 18 messages Jun 12, 2012 – Jun 14, 2012

Subsystems (6)
Arm (32-bit) x86 Library code s390 +2 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lib/decompress_unxz.c: removing all memory helper functions

Cleaning up the file lib/decompress_unxz.c, moving all memory helper functions, e.g. memmove, to a new common source file, lib/boot/mem.c.

In additon to including the decompressor, any architecture supporting the XZ decompression needs to also include this new source file.

Also moving some other duplicated memory helper functions to this new source file from the arm, s390, sh and x86 preboot environments. All 4 architectures build without error when using any compression.

Adding a new file lib/boot/mem.c, containing memory helper functions required by different compression types.

Adding memcmp declaration workaround and removing the memmove and memcpy defines workaround from arch/arm/boot/compressed/decompress.c

Removing the common functions, memmove, memcmp and memset, and adding the new source file include to arch/arm/boot/compressed/string.c.

Removing the memcpy and memmove functions and adding the new source file include to arch/s390/boot/compressed/misc.c.

Removing the memset function and adding the new source file include to arch/sh/boot/compressed/misc.c

Removing the memset function from arch/x86/boot/compressed/misc.c and move the memcpy function to the file arch/x86/boot/compressed/string.c

Adding the memcpy function and the new source file include to arch/x86/boot/compressed/string.c

--
Change since v1:
* created and moved common memory helper functions to a new file.

---
 arch/arm/boot/compressed/decompress.c |    3 +-
 arch/arm/boot/compressed/string.c     |   41 ++-----------------
 arch/s390/boot/compressed/misc.c      |   23 ++---------
 arch/sh/boot/compressed/misc.c        |   11 +----
 arch/x86/boot/compressed/misc.c       |   39 ------------------
 arch/x86/boot/compressed/string.c     |   35 ++++++++++++++++
 lib/boot/mem.c                        |   58 +++++++++++++++++++++++++++
 lib/decompress_unxz.c                 |   70 +--------------------------------
 lib/xz/xz_private.h                   |    7 ++-
 9 files changed, 112 insertions(+), 175 deletions(-)
 create mode 100644 lib/boot/mem.c
arch/arm/boot/compressed/decompress.c +1 -2
index f41b38c..80a2219 100644
@@ -9,6 +9,7 @@
 extern unsigned long free_mem_ptr;
 extern unsigned long free_mem_end_ptr;
 extern void error(char *);
+extern int memcmp(const void *, const void *, size_t);
 
 #define STATIC static
 #define STATIC_RW_DATA	/* non-static please */
@@ -45,8 +46,6 @@ extern void error(char *);
 #endif
 
 #ifdef CONFIG_KERNEL_XZ
-#define memmove memmove
-#define memcpy memcpy
 #include "../../../../lib/decompress_unxz.c"
 #endif
 
arch/arm/boot/compressed/string.c +4 -37

66 lines not on this page · raw message

arch/s390/boot/compressed/misc.c +4 -19
index 465eca7..7169232 100644
@@ -67,6 +67,8 @@ static int puts(const char *s)
 	return 0;
 }
 
+#define __HAVE_PREBOOT_ARCH_MEMSET
+
 void *memset(void *s, int c, size_t n)
 {
 	char *xs;
@@ -82,25 +84,6 @@ void *memset(void *s, int c, size_t n)
 	return s;
 }
 
-void *memcpy(void *__dest, __const void *__src, size_t __n)
-{
-	return __builtin_memcpy(__dest, __src, __n);
-}
-
-void *memmove(void *__dest, __const void *__src, size_t __n)
-{
-	char *d;
-	const char *s;
-
-	if (__dest <= __src)
-		return __builtin_memcpy(__dest, __src, __n);
-	d = __dest + __n;
-	s = __src + __n;
-	while (__n--)
-		*--d = *--s;
-	return __dest;
-}
-
 static void error(char *x)
 {
 	unsigned long long psw = 0x000a0000deadbeefULL;
@@ -166,3 +149,5 @@ unsigned long decompress_kernel(void)
 	return (unsigned long) output;
 }
 
+#include "../../../../lib/boot/mem.c"
+
arch/sh/boot/compressed/misc.c +3 -8
index 95470a4..b1dde68 100644
@@ -75,14 +75,7 @@ int puts(const char *s)
 	return 0;
 }
 
-void* memset(void* s, int c, size_t n)
-{
-	int i;
-	char *ss = (char*)s;
-
-	for (i=0;i<n;i++) ss[i] = c;
-	return s;
-}
+#define __HAVE_PREBOOT_ARCH_MEMCPY
 
 void* memcpy(void* __dest, __const void* __src,
 			    size_t __n)
@@ -136,3 +129,5 @@ void decompress_kernel(void)
 	cache_control(CACHE_DISABLE);
 	puts("Ok, booting the kernel.\n");
 }
+
+#include "../../../../lib/boot/mem.c"
arch/x86/boot/compressed/misc.c +0 -39
index 7116dcb..ac07fbb 100644
@@ -224,45 +224,6 @@ void __putstr(int error, const char *s)
 	outb(0xff & (pos >> 1), vidport+1);
 }
 
-void *memset(void *s, int c, size_t n)
-{
-	int i;
-	char *ss = s;
-
-	for (i = 0; i < n; i++)
-		ss[i] = c;
-	return s;
-}
-#ifdef CONFIG_X86_32
-void *memcpy(void *dest, const void *src, size_t n)
-{
-	int d0, d1, d2;
-	asm volatile(
-		"rep ; movsl\n\t"
-		"movl %4,%%ecx\n\t"
-		"rep ; movsb\n\t"
-		: "=&c" (d0), "=&D" (d1), "=&S" (d2)
-		: "0" (n >> 2), "g" (n & 3), "1" (dest), "2" (src)
-		: "memory");
-
-	return dest;
-}
-#else
-void *memcpy(void *dest, const void *src, size_t n)
-{
-	long d0, d1, d2;
-	asm volatile(
-		"rep ; movsq\n\t"
-		"movq %4,%%rcx\n\t"
-		"rep ; movsb\n\t"
-		: "=&c" (d0), "=&D" (d1), "=&S" (d2)
-		: "0" (n >> 3), "g" (n & 7), "1" (dest), "2" (src)
-		: "memory");
-
-	return dest;
-}
-#endif
-
 static void error(char *x)
 {
 	__putstr(1, "\n\n");
arch/x86/boot/compressed/string.c +35 -0
index ffb9c5c..d6b8784 100644
@@ -1,5 +1,7 @@
 #include "misc.h"
 
+#define __HAVE_PREBOOT_ARCH_MEMCMP
+
 int memcmp(const void *s1, const void *s2, size_t len)
 {
 	u8 diff;
@@ -8,4 +10,37 @@ int memcmp(const void *s1, const void *s2, size_t len)
 	return diff;
 }
 
+#define __HAVE_PREBOOT_ARCH_MEMCPY
+
+#ifdef CONFIG_X86_32
+void *memcpy(void *dest, const void *src, size_t n)
+{
+	int d0, d1, d2;
+	asm volatile(
+		"rep ; movsl\n\t"
+		"movl %4,%%ecx\n\t"
+		"rep ; movsb\n\t"
+		: "=&c" (d0), "=&D" (d1), "=&S" (d2)
+		: "0" (n >> 2), "g" (n & 3), "1" (dest), "2" (src)
+		: "memory");
+
+	return dest;
+}
+#else
+void *memcpy(void *dest, const void *src, size_t n)
+{
+	long d0, d1, d2;
+	asm volatile(
+		"rep ; movsq\n\t"
+		"movq %4,%%rcx\n\t"
+		"rep ; movsb\n\t"
+		: "=&c" (d0), "=&D" (d1), "=&S" (d2)
+		: "0" (n >> 3), "g" (n & 7), "1" (dest), "2" (src)
+		: "memory");
+
+	return dest;
+}
+#endif
+
+#include "../../../../lib/boot/mem.c"
 #include "../string.c"
lib/boot/mem.c new file +58 -0

58 lines not on this page · raw message

lib/decompress_unxz.c +2 -68

78 lines not on this page · raw message

lib/xz/xz_private.h +5 -2
index 482b90f..a997dca 100644
@@ -37,9 +37,12 @@
 #		ifdef CONFIG_XZ_DEC_SPARC
 #			define XZ_DEC_SPARC
 #		endif
-#		define memeq(a, b, size) (memcmp(a, b, size) == 0)
-#		define memzero(buf, size) memset(buf, 0, size)
 #	endif
+	/* Make all environments, including preboot, use memcmp for memeq */
+#	define memeq(a, b, size) (memcmp(a, b, size) == 0)
+	/* To suppress redefine warning in some architecture's preboot */
+#	undef memzero
+#	define memzero(buf, size) memset(buf, 0, size)
 #	define get_le32(p) le32_to_cpup((const uint32_t *)(p))
 #else
 	/*
Signature · 1 line
1.7.1
#1 lore raw
Quoted 22 lines · T Makphaibulchoke

Cleaning up the file lib/decompress_unxz.c, moving all memory helper functions, e.g. memmove, to a new common source file, lib/boot/mem.c.

In additon to including the decompressor, any architecture supporting the XZ decompression needs to also include this new source file.

Also moving some other duplicated memory helper functions to this new source file from the arm, s390, sh and x86 preboot environments. All 4 architectures build without error when using any compression.

Adding a new file lib/boot/mem.c, containing memory helper functions required by different compression types.

Adding memcmp declaration workaround and removing the memmove and memcpy defines workaround from arch/arm/boot/compressed/decompress.c

Removing the common functions, memmove, memcmp and memset, and adding the new source file include to arch/arm/boot/compressed/string.c.

Removing the memcpy and memmove functions and adding the new source file include to arch/s390/boot/compressed/misc.c.

Quoted 9 lines

Removing the memset function and adding the new source file include to arch/sh/boot/compressed/misc.c

Removing the memset function from arch/x86/boot/compressed/misc.c and move the memcpy function to the file arch/x86/boot/compressed/string.c

Adding the memcpy function and the new source file include to arch/x86/boot/compressed/string.c

I can take this if Russell, Martin or Heiko, and Paul are willing to ack it.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#2 lore raw
Quoted 26 lines · H. Peter Anvin
Quoted 25 lines · T Makphaibulchoke

Cleaning up the file lib/decompress_unxz.c, moving all memory helper functions, e.g. memmove, to a new common source file, lib/boot/mem.c.

In additon to including the decompressor, any architecture supporting the XZ decompression needs to also include this new source file.

Also moving some other duplicated memory helper functions to this new source file from the arm, s390, sh and x86 preboot environments. All 4 architectures build without error when using any compression.

Adding a new file lib/boot/mem.c, containing memory helper functions required by different compression types.

Adding memcmp declaration workaround and removing the memmove and memcpy defines workaround from arch/arm/boot/compressed/decompress.c

Removing the common functions, memmove, memcmp and memset, and adding the new source file include to arch/arm/boot/compressed/string.c.

Removing the memcpy and memmove functions and adding the new source file include to arch/s390/boot/compressed/misc.c.

Removing the memset function and adding the new source file include to arch/sh/boot/compressed/misc.c

Quoted 8 lines
Quoted 6 lines

Removing the memset function from arch/x86/boot/compressed/misc.c and move the memcpy function to the file arch/x86/boot/compressed/string.c

Adding the memcpy function and the new source file include to arch/x86/boot/compressed/string.c

I can take this if Russell, Martin or Heiko, and Paul are willing to ack it.

I'd like to toss this into my kautobuild+boot, but at the moment that's rather pointless because OMAP has been broken since towards the end of the merge window, and so far no sign of the fix hitting mainline any time soon...

#3 lore raw
Quoted 4 lines · Russell King - ARM Linux

I'd like to toss this into my kautobuild+boot, but at the moment that's rather pointless because OMAP has been broken since towards the end of the merge window, and so far no sign of the fix hitting mainline any time soon...

Okay, given the issue raised with memcpy() calling __builtin_memcpy(), I don't think there's any value to me actually testing this, because that seems to need to be solved first?

#4 lore raw
Quoted 9 lines · Russell King - ARM Linux
Quoted 4 lines · Russell King - ARM Linux

I'd like to toss this into my kautobuild+boot, but at the moment that's rather pointless because OMAP has been broken since towards the end of the merge window, and so far no sign of the fix hitting mainline any time soon...

Okay, given the issue raised with memcpy() calling __builtin_memcpy(), I don't think there's any value to me actually testing this, because that seems to need to be solved first?

You mean __builtin_memcpy() calling memcpy(), right?

-hpa

#5 lore raw
Quoted 7 lines · H. Peter Anvin
Quoted 6 lines · Russell King - ARM Linux
Quoted 4 lines · Russell King - ARM Linux

I'd like to toss this into my kautobuild+boot, but at the moment that's rather pointless because OMAP has been broken since towards the end of the merge window, and so far no sign of the fix hitting mainline any time soon...

Quoted 5 lines
Quoted 3 lines

Okay, given the issue raised with memcpy() calling __builtin_memcpy(), I don't think there's any value to me actually testing this, because that seems to need to be solved first?

You mean __builtin_memcpy() calling memcpy(), right?

Indeed I do.

#6 lore raw
Quoted 8 lines · Russell King - ARM Linux
Quoted 6 lines
Quoted 4 lines

Okay, given the issue raised with memcpy() calling __builtin_memcpy(), I don't think there's any value to me actually testing this, because that seems to need to be solved first?

You mean __builtin_memcpy() calling memcpy(), right?

Indeed I do.

Right... that would manifest itself as a build failure, but it needs to be fixed, so...

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#7 lore raw
In reply to H. Peter Anvin
Quoted 26 lines · On Mon, 11 Jun 2012 20:16:02 -0700 "H. Peter Anvin" <[email protected]> wrote:
Quoted 25 lines · T Makphaibulchoke

Cleaning up the file lib/decompress_unxz.c, moving all memory helper functions, e.g. memmove, to a new common source file, lib/boot/mem.c.

In additon to including the decompressor, any architecture supporting the XZ decompression needs to also include this new source file.

Also moving some other duplicated memory helper functions to this new source file from the arm, s390, sh and x86 preboot environments. All 4 architectures build without error when using any compression.

Adding a new file lib/boot/mem.c, containing memory helper functions required by different compression types.

Adding memcmp declaration workaround and removing the memmove and memcpy defines workaround from arch/arm/boot/compressed/decompress.c

Removing the common functions, memmove, memcmp and memset, and adding the new source file include to arch/arm/boot/compressed/string.c.

Removing the memcpy and memmove functions and adding the new source file include to arch/s390/boot/compressed/misc.c.

Removing the memset function and adding the new source file include to arch/sh/boot/compressed/misc.c

Quoted 8 lines
Quoted 6 lines

Removing the memset function from arch/x86/boot/compressed/misc.c and move the memcpy function to the file arch/x86/boot/compressed/string.c

Adding the memcpy function and the new source file include to arch/x86/boot/compressed/string.c

I can take this if Russell, Martin or Heiko, and Paul are willing to ack it.

Just tested kernel compression with all five compression algorithms, works fine for s390.

Signature · 4 lines
blue skies,
   Martin.

"Reality continues to ruin my life." - Calvin.
Acked-by Russell King Tested-by Russell King
#8 lore raw
In reply to H. Peter Anvin
Quoted 26 lines · H. Peter Anvin
Quoted 25 lines · T Makphaibulchoke

Cleaning up the file lib/decompress_unxz.c, moving all memory helper functions, e.g. memmove, to a new common source file, lib/boot/mem.c.

In additon to including the decompressor, any architecture supporting the XZ decompression needs to also include this new source file.

Also moving some other duplicated memory helper functions to this new source file from the arm, s390, sh and x86 preboot environments. All 4 architectures build without error when using any compression.

Adding a new file lib/boot/mem.c, containing memory helper functions required by different compression types.

Adding memcmp declaration workaround and removing the memmove and memcpy defines workaround from arch/arm/boot/compressed/decompress.c

Removing the common functions, memmove, memcmp and memset, and adding the new source file include to arch/arm/boot/compressed/string.c.

Removing the memcpy and memmove functions and adding the new source file include to arch/s390/boot/compressed/misc.c.

Removing the memset function and adding the new source file include to arch/sh/boot/compressed/misc.c

Quoted 8 lines
Quoted 6 lines

Removing the memset function from arch/x86/boot/compressed/misc.c and move the memcpy function to the file arch/x86/boot/compressed/string.c

Adding the memcpy function and the new source file include to arch/x86/boot/compressed/string.c

I can take this if Russell, Martin or Heiko, and Paul are willing to ack it.

(Tested on OMAP4430SDP).

maintainer
#10 lore raw
In reply to T Makphaibulchoke
Quoted 6 lines · On 2012-06-11 T Makphaibulchoke wrote:
lib/boot/mem.c new file +3 -0
@@ -0,0 +1,58 @@
+/*
+ * Small subset of simple memory helper functions required by different
+ * compressors for preboot environment.

lib/boot/mem.c line 13

Quoted 10 lines
lib/boot/mem.c new file +10 -0
+ */
+
+#include <linux/string.h>
+
+#ifndef __HAVE_PREBOOT_ARCH_MEMCPY
+void *memcpy(void *__dest, __const void *__src, size_t __n)
+{
+	return __builtin_memcpy(__dest, __src, __n);
+}
+#endif

GCC will replace __builtin_memcpy above with a call to memcpy, creating an infinite loop. I confirmed this on x86_64 by first uncommenting the memcpy from arch/x86/boot/compressed/string.c.

You need to provide a memcpy implementation yourself without using __builtin functions. For example, copy the memcpy implementation from lib/string.c.

Quoted 7 lines
lib/boot/mem.c new file +7 -0
+#ifndef __HAVE_PREBOOT_ARCH_MEMMOVE
+void *memmove(void *__dest, __const void *__src, size_t count)
+{
+	unsigned char *d = __dest;
+	const unsigned char *s = __src;
+
+	if (__dest == __src)

lib/boot/mem.c line 31

Quoted 10 lines
lib/boot/mem.c new file +10 -0
+		return __dest;
+
+	if (__dest < __src)
+		return memcpy(__dest, __src, count);
+
+	while (count--)
+		d[count] = s[count];
+	return __dest;
+}
+#endif

The use of memcpy when __dest < __src is OK with some memcpy implementations, but in a generic case it isn't guaranteed to work. I think it would be better to just copy memmove from lib/string.c.

Signature · 1 line
Lasse Collin  |  IRC: Larhzu @ IRCnet & Freenode
#11 lore raw
Quoted 13 lines · Lasse Collin
Quoted 12 lines · On 2012-06-11 T Makphaibulchoke wrote:
lib/boot/mem.c new file +12 -0
+#ifndef __HAVE_PREBOOT_ARCH_MEMMOVE
+void *memmove(void *__dest, __const void *__src, size_t count)
+{
+	unsigned char *d = __dest;
+	const unsigned char *s = __src;
+
+	if (__dest == __src)
+		return __dest;
+
+	if (__dest < __src)
+		return memcpy(__dest, __src, count);
+
Quoted 8 lines

lib/boot/mem.c line 31

Quoted 5 lines
lib/boot/mem.c new file +5 -0
+	while (count--)
+		d[count] = s[count];
+	return __dest;
+}
+#endif

The use of memcpy when __dest < __src is OK with some memcpy implementations, but in a generic case it isn't guaranteed to work.

Of course, if the memcpy implementation is also provided along with this memmove code, as it was the case on ARM before this patch, then you can guarantee it. And if that memcpy() happens to be slightly more optimized then this is a win.

Quoted 1 line

I think it would be better to just copy memmove from lib/string.c.

Instead of copying, maybe this would be much better to make the content of lib/string.c usable in a pre-boot environment.

Nicolas

#12 lore raw
Quoted 4 lines · Nicolas Pitre

Instead of copying, maybe this would be much better to make the content of lib/string.c usable in a pre-boot environment.

Not sure about that... a lot of it is #ifndef __HAVE_ARCH_* and you *really* want those optimizations on some arches.

-hpa

#13 lore raw
Quoted 7 lines · H. Peter Anvin
Quoted 3 lines · Nicolas Pitre

Instead of copying, maybe this would be much better to make the content of lib/string.c usable in a pre-boot environment.

Not sure about that... a lot of it is #ifndef __HAVE_ARCH_* and you *really* want those optimizations on some arches.

Can __weak be used?

#14 lore raw
Quoted 11 lines · Joe Perches
Quoted 7 lines · H. Peter Anvin
Quoted 3 lines · Nicolas Pitre

Instead of copying, maybe this would be much better to make the content of lib/string.c usable in a pre-boot environment.

Not sure about that... a lot of it is #ifndef __HAVE_ARCH_* and you *really* want those optimizations on some arches.

Can __weak be used?

lib/string.c contains a lot of stuff that are not needed in preboot, and would increase the kernel image size. I'm not sure if we will be able to make it usable in pre-boot and only bringing in what is needed by preboot, withtut unnecessarily complicated lib/string.c.

Thanks Joe for the suggestion. I guess we could remove the #ifndef by renaming all arch specific mem helper functions to arch_<name>, and declaring them with a weak attribute in mem.c and let the mem.c check to see if it is provided, Here is an example for memcpy

Architecture specific,

void *arch_memcpy(void *__dest, __const void *__src, size_t __n) {
...
}

lib/mem.c

extern void *arch_memcpy(void *__dest, __const void *__src, size_t __n) __attribute __((weak));

void *memcpy(void *__dest, __const void *__src, size_t __n)

{
        if (arch_memcpy) {
		arch_memcpgoogly();
		return;	
	}

}

But this does not solve the memmove issue. Could we always trust arch_memcpy in all architectures? I guess the safest way would be to use memmove in string.c. Please let me know if you have any thoughts or suggestions.

Thanks,
Mak.

.

#15 lore raw
In reply to H. Peter Anvin
Quoted 8 lines · H. Peter Anvin
Quoted 4 lines · Nicolas Pitre

Instead of copying, maybe this would be much better to make the content of lib/string.c usable in a pre-boot environment.

Not sure about that... a lot of it is #ifndef __HAVE_ARCH_* and you *really* want those optimizations on some arches.

Sure. What about:

lib/boot/mem.c: lib/string.c

	sed 's/^#ifndef __HAVE_ARCH_/#ifndef __HAVE_PREBOOT_ARCH_/' < $< > $@

This is over simplified but you get the idea. Maybe lib/string.c itself should be split into several files to help with a finer grained selection.

Nicolas

#16 lore raw
Quoted 11 lines · Nicolas Pitre
Quoted 8 lines · H. Peter Anvin
Quoted 4 lines · Nicolas Pitre

Instead of copying, maybe this would be much better to make the content of lib/string.c usable in a pre-boot environment.

Not sure about that... a lot of it is #ifndef __HAVE_ARCH_* and you *really* want those optimizations on some arches.

Quoted 9 lines

Sure. What about:

lib/boot/mem.c: lib/string.c

	sed 's/^#ifndef __HAVE_ARCH_/#ifndef __HAVE_PREBOOT_ARCH_/' < $< > $@

This is over simplified but you get the idea. Maybe lib/string.c itself should be split into several files to help with a finer grained selection.

The point is that I think we pretty much end up with a null usable set in the end.

-hpa

#17 lore raw
Quoted 4 lines · H. Peter Anvin

The point is that I think we pretty much end up with a null usable set in the end.

s/usable/useful/

-hpa