Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arch/lkl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ LKL_ENTRY_POINTS += \
endif

core-y += arch/lkl/kernel/
core-y += arch/lkl/lib/
core-y += arch/lkl/mm/
core-y += arch/lkl/drivers/

Expand Down
6 changes: 3 additions & 3 deletions arch/lkl/include/asm/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/* use __mem* names to avoid conflict with KASAN's mem* functions. */

#define __HAVE_ARCH_MEMCPY
extern void *memcpy(void *dest, const void *src, size_t count);
static inline void *__memcpy(void *dest, const void *src, size_t count)
{
char *tmp = dest;
Expand All @@ -24,6 +25,7 @@ static inline void *__memcpy(void *dest, const void *src, size_t count)
}

#define __HAVE_ARCH_MEMSET
extern void *memset(void *s, int c, size_t count);
static inline void *__memset(void *s, int c, size_t count)
{
char *xs = s;
Expand All @@ -39,6 +41,7 @@ static inline void *__memset(void *s, int c, size_t count)
}

#define __HAVE_ARCH_MEMMOVE
extern void *memmove(void *dest, const void *src, size_t count);
static inline void *__memmove(void *dest, const void *src, size_t count)
{
char *tmp;
Expand Down Expand Up @@ -86,9 +89,6 @@ static inline void *__memmove(void *dest, const void *src, size_t count)
#undef memcpy
#undef memset
#undef memmove
extern void *memset(void *dst, int c, __kernel_size_t count);
extern void *memcpy(void *dst, const void *src, __kernel_size_t count);
extern void *memmove(void *dest, const void *src, size_t count);

#endif /* __SANITIZE_ADDRESS__ */

Expand Down
3 changes: 3 additions & 0 deletions arch/lkl/lib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0

obj-y += string.o
35 changes: 35 additions & 0 deletions arch/lkl/lib/string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: GPL-2.0

#include <linux/string.h>
#include <linux/export.h>

#if !defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX)
/*
* If CONFIG_KASAN_GENERIC is on but CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX is
* off, mm/kasan/shadow.c will define the kasan version memcpy and its friends
* for us. We should not do anything, otherwise the symbols will conflict.
*/

#undef memcpy
#undef memset
#undef memmove

__visible void *memcpy(void *dest, const void *src, size_t count)
{
return __memcpy(dest, src, count);
}
EXPORT_SYMBOL(memcpy);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this redirect already happen via the preprocessor using the #defines at https://github.com/lkl/linux/blob/master/arch/lkl/include/asm/string.h#L68 ? Are you building with or without CONFIG_KASAN?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this redirect already happen via the preprocessor using the #defines at https://github.com/lkl/linux/blob/master/arch/lkl/include/asm/string.h#L68 ?

There is code that uses __builtin_memcpy directly. Our #define memcpy will have no effect on such usage. The compiler will generate direct calls to memcpy, which is an undefined symbol because we never define it.

__builtin_memcpy(dest, src, len);

Are you building with or without CONFIG_KASAN?

I built without CONFIG_KASAN and found these undefined symbols.

My patch was tested with and without CONFIG_KASAN. It should work either way.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, I wasn't familiar with how the compiler handled __builtin_memcpy. This change looks reasonable to me, although can't we now drop those #defines?

Copy link
Author

@lrh2000 lrh2000 Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review too!

can't we now drop those #defines?

I think it's okay. The only difference is that if we use static inline void *__memcpy(..) and #define memcpy __memcpy then most of memcpy will be inline, but if we have extern void *memcpy(..) then memcpy won't be inline.

I don't think the difference really matters here. I chose an implementation that requires minimal changes (and preserves the original inline semantics). But if we prefer to move the __memcpy implementation from string.h to string.c and then remove the #define altogether, I think that's perfectly fine and will update the patch accordingly.


__visible void *memset(void *s, int c, size_t count)
{
return __memset(s, c, count);
}
EXPORT_SYMBOL(memset);

__visible void *memmove(void *dest, const void *src, size_t count)
{
return __memmove(dest, src, count);
}
EXPORT_SYMBOL(memmove);

#endif
Loading