Skip to contentSkip to conversation

x86/process: Silence KASAN warnings in get_wchan()

PATCH Superseded Andrey Ryabinin 7 participants 19 messages Oct 5, 2015 – Oct 7, 2015 x86

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/process: Silence KASAN warnings in get_wchan()

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use kasan_disable_current()/kasan_enable_current() to silence these warnings.

---

Perhaps it would be better to add something like this:

	READ_ONCE_NOCHECK()
	{
		kasan_disable_current();
		READ_ONCE();
		kasan_enable_current();
	}

?

 arch/x86/kernel/process.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
arch/x86/kernel/process.c +13 -5
index 39e585a..0488eb9 100644
@@ -13,6 +13,7 @@
 #include <linux/random.h>
 #include <linux/user-return-notifier.h>
 #include <linux/dmi.h>
+#include <linux/kasan.h>
 #include <linux/utsname.h>
 #include <linux/stackprotector.h>
 #include <linux/tick.h>
@@ -514,7 +515,7 @@ unsigned long arch_randomize_brk(struct mm_struct *mm)
  */
 unsigned long get_wchan(struct task_struct *p)
 {
-	unsigned long start, bottom, top, sp, fp, ip;
+	unsigned long start, bottom, top, sp, fp, ip, ret = 0;
 	int count = 0;
 
 	if (!p || p == current || p->state == TASK_RUNNING)
@@ -550,14 +551,21 @@ unsigned long get_wchan(struct task_struct *p)
 	if (sp < bottom || sp > top)
 		return 0;
 
+	kasan_disable_current();
 	fp = READ_ONCE(*(unsigned long *)sp);
 	do {
 		if (fp < bottom || fp > top)
-			return 0;
+			goto out;
+
 		ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
-		if (!in_sched_functions(ip))
-			return ip;
+		if (!in_sched_functions(ip)) {
+			ret = ip;
+			goto out;
+		}
 		fp = READ_ONCE(*(unsigned long *)fp);
 	} while (count++ < 16 && p->state != TASK_RUNNING);
-	return 0;
+
+out:
+	kasan_enable_current();
+	return ret;
 }
Signature · 1 line
2.4.9
Reviewed-by on v1 1/1
#1 lore raw

LGTM

Quoted 73 lines · Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use kasan_disable_current()/kasan_enable_current() to silence these warnings.

---

Perhaps it would be better to add something like this:

        READ_ONCE_NOCHECK()
        {
                kasan_disable_current();
                READ_ONCE();
                kasan_enable_current();
        }

?

 arch/x86/kernel/process.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
arch/x86/kernel/process.c +13 -6
index 39e585a..0488eb9 100644
@@ -13,6 +13,7 @@
 #include <linux/random.h>
 #include <linux/user-return-notifier.h>
 #include <linux/dmi.h>
+#include <linux/kasan.h>
 #include <linux/utsname.h>
 #include <linux/stackprotector.h>
 #include <linux/tick.h>
@@ -514,7 +515,7 @@ unsigned long arch_randomize_brk(struct mm_struct *mm)
  */
 unsigned long get_wchan(struct task_struct *p)
 {
-       unsigned long start, bottom, top, sp, fp, ip;
+       unsigned long start, bottom, top, sp, fp, ip, ret = 0;
        int count = 0;
 
        if (!p || p == current || p->state == TASK_RUNNING)
@@ -550,14 +551,21 @@ unsigned long get_wchan(struct task_struct *p)
        if (sp < bottom || sp > top)
                return 0;
 
+       kasan_disable_current();
        fp = READ_ONCE(*(unsigned long *)sp);
        do {
                if (fp < bottom || fp > top)
-                       return 0;
+                       goto out;
+
                ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
-               if (!in_sched_functions(ip))
-                       return ip;
+               if (!in_sched_functions(ip)) {
+                       ret = ip;
+                       goto out;
+               }
                fp = READ_ONCE(*(unsigned long *)fp);
        } while (count++ < 16 && p->state != TASK_RUNNING);
-       return 0;
+
+out:
+       kasan_enable_current();
+       return ret;
 }
--

2.4.9

maintainer
#2 lore raw
In reply to Andrey Ryabinin
Quoted 6 lines · Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use kasan_disable_current()/kasan_enable_current() to silence these warnings.

So we're going to be sprinkling those around code which kasan doesn't like? Can't we do better, without touching all code everywhere?

Probably not but let me ask that just in case...

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#3 lore raw
In reply to Andrey Ryabinin
Quoted 45 lines · Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use kasan_disable_current()/kasan_enable_current() to silence these warnings.

---

Perhaps it would be better to add something like this:

	READ_ONCE_NOCHECK()
	{
		kasan_disable_current();
		READ_ONCE();
		kasan_enable_current();
	}

?

 arch/x86/kernel/process.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
arch/x86/kernel/process.c +2 -1
index 39e585a..0488eb9 100644
@@ -13,6 +13,7 @@
 #include <linux/random.h>
 #include <linux/user-return-notifier.h>
 #include <linux/dmi.h>
+#include <linux/kasan.h>
 #include <linux/utsname.h>
 #include <linux/stackprotector.h>
 #include <linux/tick.h>
@@ -514,7 +515,7 @@ unsigned long arch_randomize_brk(struct mm_struct *mm)
  */
 unsigned long get_wchan(struct task_struct *p)
 {
-	unsigned long start, bottom, top, sp, fp, ip;
+	unsigned long start, bottom, top, sp, fp, ip, ret = 0;
 	int count = 0;
 
 	if (!p || p == current || p->state == TASK_RUNNING)

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 558

Quoted 9 lines
arch/x86/kernel/process.c +2 -1
@@ -550,14 +551,21 @@ unsigned long get_wchan(struct task_struct *p)
 	if (sp < bottom || sp > top)
 		return 0;
 
+	kasan_disable_current();
 	fp = READ_ONCE(*(unsigned long *)sp);
 	do {
 		if (fp < bottom || fp > top)
-			return 0;
+			goto out;

a break would do just fine too.

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 563

Quoted 7 lines
arch/x86/kernel/process.c +4 -2
+
 		ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
-		if (!in_sched_functions(ip))
-			return ip;
+		if (!in_sched_functions(ip)) {
+			ret = ip;
+			goto out;

ditto.

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 568

Quoted 6 lines
arch/x86/kernel/process.c +3 -1
+		}
 		fp = READ_ONCE(*(unsigned long *)fp);
 	} while (count++ < 16 && p->state != TASK_RUNNING);
-	return 0;
+
+out:

and then the label would not be needed.

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 570

Quoted 2 lines
arch/x86/kernel/process.c +2 -0
+	kasan_enable_current();
+	return ret;

But that's all pretty disgusting really.

Cannot we do better, such as annotating the function and then KASAN sorting out its false positives, or something like that?

Thanks,

Ingo

#4 lore raw
Quoted 79 lines · Ingo Molnar
Quoted 45 lines · Andrey Ryabinin

get_wchan() is racy by design, it may access volatile stack of running task, thus it may access redzone in a stack frame and cause KASAN to warn about this.

Use kasan_disable_current()/kasan_enable_current() to silence these warnings.

---

Perhaps it would be better to add something like this:

      READ_ONCE_NOCHECK()
      {
              kasan_disable_current();
              READ_ONCE();
              kasan_enable_current();
      }

?

 arch/x86/kernel/process.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
arch/x86/kernel/process.c +2 -1
index 39e585a..0488eb9 100644
@@ -13,6 +13,7 @@
 #include <linux/random.h>
 #include <linux/user-return-notifier.h>
 #include <linux/dmi.h>
+#include <linux/kasan.h>
 #include <linux/utsname.h>
 #include <linux/stackprotector.h>
 #include <linux/tick.h>
@@ -514,7 +515,7 @@ unsigned long arch_randomize_brk(struct mm_struct *mm)
  */
 unsigned long get_wchan(struct task_struct *p)
 {
-     unsigned long start, bottom, top, sp, fp, ip;
+     unsigned long start, bottom, top, sp, fp, ip, ret = 0;
      int count = 0;
 
      if (!p || p == current || p->state == TASK_RUNNING)

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 558

Quoted 9 lines
arch/x86/kernel/process.c +2 -1
@@ -550,14 +551,21 @@ unsigned long get_wchan(struct task_struct *p)
      if (sp < bottom || sp > top)
              return 0;
 
+     kasan_disable_current();
      fp = READ_ONCE(*(unsigned long *)sp);
      do {
              if (fp < bottom || fp > top)
-                     return 0;
+                     goto out;

a break would do just fine too.

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 563

Quoted 7 lines
arch/x86/kernel/process.c +4 -2
+
              ip = READ_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
-             if (!in_sched_functions(ip))
-                     return ip;
+             if (!in_sched_functions(ip)) {
+                     ret = ip;
+                     goto out;

ditto.

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 568

Quoted 6 lines
arch/x86/kernel/process.c +3 -1
+             }
              fp = READ_ONCE(*(unsigned long *)fp);
      } while (count++ < 16 && p->state != TASK_RUNNING);
-     return 0;
+
+out:

and then the label would not be needed.

Quoted 7 lines

arch/x86/kernel/process.c unsigned long get_wchan(struct task_struct *p) · line 570

Quoted 2 lines
arch/x86/kernel/process.c +2 -0
+     kasan_enable_current();
+     return ret;

But that's all pretty disgusting really.

Cannot we do better, such as annotating the function and then KASAN sorting out its false positives, or something like that?

We also plug __attribute__((no_sanitize_address)) on the function.

#5 lore raw
Quoted 9 lines · Dmitry Vyukov
Quoted 5 lines

But that's all pretty disgusting really.

Cannot we do better, such as annotating the function and then KASAN sorting out its false positives, or something like that?

We also plug __attribute__((no_sanitize_address)) on the function.

It's absolutely unusable:( https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
If we add it here, it won't built because of: '__always_inline __read_once_size()'

But, I think I have the solution.
We could have some blacklist - list of function names which we should be ignored.
In kasan_report() we could resolve return address to function name and compare it with name in list. If name in list -> ignore report.

maintainer
#6 lore raw
Quoted 7 lines · Andrey Ryabinin

It's absolutely unusable:( https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
If we add it here, it won't built because of: '__always_inline __read_once_size()'

But, I think I have the solution.
We could have some blacklist - list of function names which we should be ignored.
In kasan_report() we could resolve return address to function name and compare it with name in list. If name in list -> ignore report.

Sounds better. :)

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#7 lore raw
Quoted 10 lines · Borislav Petkov
Quoted 7 lines · Andrey Ryabinin

It's absolutely unusable:( https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
If we add it here, it won't built because of: '__always_inline __read_once_size()'

But, I think I have the solution.
We could have some blacklist - list of function names which we should be ignored.
In kasan_report() we could resolve return address to function name and compare it with name in list. If name in list -> ignore report.

Sounds better. :)

These ought to be rare. probe_kernel_xyz are hopefully already covered, which leaves strange accesses to things that are known to be out of bounds, which ought to be just the memory allocation stuff (already covered?) and stack walking code (this stuff).

I think that READ_ONCE_NOCHECK is a decent solution that isn't too messy.

--Andy

#8 lore raw
In reply to Andrey Ryabinin
Quoted 4 lines

But, I think I have the solution.
We could have some blacklist - list of function names which we should be ignored.
In kasan_report() we could resolve return address to function name and compare it with name in list. If name in list -> ignore report.

I think annotating statements is cleaner than functions, even if it is more code. Much better documentation

But if you really want to annotate on the function level:

It's better to annotate the function directly than some hidden away list. This way there is some indication that there are races in there, which is generally useful documentation.

__racy_function or similar.

Also central lists are generally annoying as they cause patch conflicts.

If disabling with an attribute doesn't work, you could put it into a special section with __attribute__((section ...)) and check the start/end symbol before reporting. That's how kprobes solves similar issues. It also has the advantage that it stops inlining.

-Andi

#9 lore raw
Quoted 8 lines · Andi Kleen
Quoted 4 lines

But, I think I have the solution.
We could have some blacklist - list of function names which we should be ignored.
In kasan_report() we could resolve return address to function name and compare it with name in list. If name in list -> ignore report.

I think annotating statements is cleaner than functions, even if it is more code. Much better documentation

I agree with that, that's why I suggested to add READ_ONCE_NOCHECK():

	READ_ONCE_NOCHECK()
	{
		kasan_disable_current();
		READ_ONCE();
		kasan_enable_current();
	}

Anywone objects?

Quoted 6 lines

But if you really want to annotate on the function level:

It's better to annotate the function directly than some hidden away list. This way there is some indication that there are races in there, which is generally useful documentation.

Quoted 8 lines

__racy_function or similar.

Also central lists are generally annoying as they cause patch conflicts.

If disabling with an attribute doesn't work, you could put it into a special section with __attribute__((section ...)) and check the start/end symbol before reporting. That's how kprobes solves similar issues. It also has the advantage that it stops inlining.

Yes, it might be better. Although, because of broken -fconserve-stack, this may not work in some cases - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
Function splitter may split original function into two parts and it always puts one split part in default .text section.

#10 lore raw
Quoted 4 lines

Yes, it might be better. Although, because of broken -fconserve-stack, this may not work in some cases - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
Function splitter may split original function into two parts and it always puts one split part in default .text section.

Interesting.

I guess could just add noinline too. It only happens with inlining, right?

Probably that needs to be added to all the existing section users too, like __kprobes.

-Andi

#11 lore raw
In reply to Andrey Ryabinin
Quoted 11 lines · Andrey Ryabinin
Quoted 8 lines · Andi Kleen
Quoted 4 lines

But, I think I have the solution.
We could have some blacklist - list of function names which we should be ignored.
In kasan_report() we could resolve return address to function name and compare it with name in list. If name in list -> ignore report.

I think annotating statements is cleaner than functions, even if it is more code. Much better documentation

I agree with that, that's why I suggested to add READ_ONCE_NOCHECK():

Quoted 8 lines
	READ_ONCE_NOCHECK()
	{
		kasan_disable_current();
		READ_ONCE();
		kasan_enable_current();
	}

Anywone objects?

Sounds good to me! As long as it's hidden from plain .c files I'm a happy camper.

This should probably also be faster for KASAN than triggering a warning and having to parse a blacklist, right?

Quoted 9 lines
Quoted 4 lines

If disabling with an attribute doesn't work, you could put it into a special section with __attribute__((section ...)) and check the start/end symbol before reporting. That's how kprobes solves similar issues. It also has the advantage that it stops inlining.

Yes, it might be better. Although, because of broken -fconserve-stack, this may not work in some cases - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
Function splitter may split original function into two parts and it always puts one split part in default .text section.

We do a _ton_ of such section tricks in the kernel (all of exception handling is based on that) - if that's broken by -fconserve-stack then the kernel is broken much more widely.

So unless KASAN wants to do something special here you can rely on sections just fine.

Thanks,

Ingo

#12 lore raw
Quoted 21 lines · Ingo Molnar
Quoted 18 lines · Andrey Ryabinin
Quoted 8 lines · Andi Kleen
Quoted 4 lines

But, I think I have the solution.
We could have some blacklist - list of function names which we should be ignored.
In kasan_report() we could resolve return address to function name and compare it with name in list. If name in list -> ignore report.

I think annotating statements is cleaner than functions, even if it is more code. Much better documentation

I agree with that, that's why I suggested to add READ_ONCE_NOCHECK():

	READ_ONCE_NOCHECK()
	{
		kasan_disable_current();
		READ_ONCE();
		kasan_enable_current();
	}
Quoted 7 lines
Quoted 1 line

Anywone objects?

Sounds good to me! As long as it's hidden from plain .c files I'm a happy camper.

This should probably also be faster for KASAN than triggering a warning and having to parse a blacklist, right?

Sure.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

If disabling with an attribute doesn't work, you could put it into a special section with __attribute__((section ...)) and check the start/end symbol before reporting. That's how kprobes solves similar issues. It also has the advantage that it stops inlining.

Quoted 9 lines
Quoted 4 lines

Yes, it might be better. Although, because of broken -fconserve-stack, this may not work in some cases - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
Function splitter may split original function into two parts and it always puts one split part in default .text section.

We do a _ton_ of such section tricks in the kernel (all of exception handling is based on that) - if that's broken by -fconserve-stack then the kernel is broken much more widely.

I'm mistaken here. It was broken once, at some point of development of gcc 5, but this was fixed eventually. I just checked gcc 5.2, 4.9.2, 4.8.4, all of them are ok.

Quoted 7 lines

So unless KASAN wants to do something special here you can rely on sections just fine.

Thanks,

Ingo

#13 lore raw
In reply to Ingo Molnar
Quoted 38 lines · Ingo Molnar
Quoted 11 lines · Andrey Ryabinin
Quoted 8 lines · Andi Kleen
Quoted 4 lines

But, I think I have the solution.
We could have some blacklist - list of function names which we should be ignored.
In kasan_report() we could resolve return address to function name and compare it with name in list. If name in list -> ignore report.

I think annotating statements is cleaner than functions, even if it is more code. Much better documentation

I agree with that, that's why I suggested to add READ_ONCE_NOCHECK():

Quoted 8 lines
      READ_ONCE_NOCHECK()
      {
              kasan_disable_current();
              READ_ONCE();
              kasan_enable_current();
      }

Anywone objects?

Sounds good to me! As long as it's hidden from plain .c files I'm a happy camper.

This should probably also be faster for KASAN than triggering a warning and having to parse a blacklist, right?

Quoted 9 lines
Quoted 4 lines

If disabling with an attribute doesn't work, you could put it into a special section with __attribute__((section ...)) and check the start/end symbol before reporting. That's how kprobes solves similar issues. It also has the advantage that it stops inlining.

Yes, it might be better. Although, because of broken -fconserve-stack, this may not work in some cases - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
Function splitter may split original function into two parts and it always puts one split part in default .text section.

Quoted 6 lines

We do a _ton_ of such section tricks in the kernel (all of exception handling is based on that) - if that's broken by -fconserve-stack then the kernel is broken much more widely.

So unless KASAN wants to do something special here you can rely on sections just fine.

Kprobes is moving away from a section approach for some reason (not sure why), but the kprobe approach should work, too.

But what's wrong with the GCC attribute mechanism? Surely GCC ought to be able to generate the code, at least in the simple cases, and the attribute already exists. The attribute and READ_ONCE_NOCHECK seem like the least messy in the C code.

--Andy

#14 lore raw
Quoted 43 lines · Andy Lutomirski
Quoted 42 lines · Ingo Molnar
Quoted 11 lines · Andrey Ryabinin
Quoted 8 lines · Andi Kleen
Quoted 4 lines

But, I think I have the solution.
We could have some blacklist - list of function names which we should be ignored.
In kasan_report() we could resolve return address to function name and compare it with name in list. If name in list -> ignore report.

I think annotating statements is cleaner than functions, even if it is more code. Much better documentation

I agree with that, that's why I suggested to add READ_ONCE_NOCHECK():

Quoted 8 lines
      READ_ONCE_NOCHECK()
      {
              kasan_disable_current();
              READ_ONCE();
              kasan_enable_current();
      }

Anywone objects?

Sounds good to me! As long as it's hidden from plain .c files I'm a happy camper.

This should probably also be faster for KASAN than triggering a warning and having to parse a blacklist, right?

Quoted 9 lines
Quoted 4 lines

If disabling with an attribute doesn't work, you could put it into a special section with __attribute__((section ...)) and check the start/end symbol before reporting. That's how kprobes solves similar issues. It also has the advantage that it stops inlining.

Yes, it might be better. Although, because of broken -fconserve-stack, this may not work in some cases - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
Function splitter may split original function into two parts and it always puts one split part in default .text section.

We do a _ton_ of such section tricks in the kernel (all of exception handling is based on that) - if that's broken by -fconserve-stack then the kernel is broken much more widely.

Quoted 5 lines
Quoted 2 lines

So unless KASAN wants to do something special here you can rely on sections just fine.

Kprobes is moving away from a section approach for some reason (not sure why), but the kprobe approach should work, too.

Do you mean NOKPROBE_SYMBOL() vs __kprobes?

So one concern is with functions being in multiple blacklists, so yeah, the NOKPROBE_SYMBOL() approach might be more robust than __kprobes.

But note that NOKPROBE_SYMBOL() itself is still section based:

#define __NOKPROBE_SYMBOL(fname)                        \
static unsigned long __used                             \
        __attribute__((section("_kprobe_blacklist")))   \
        _kbl_addr_##fname = (unsigned long)fname;

Thanks,

Ingo

#15 lore raw
In reply to Andy Lutomirski
Quoted 5 lines · Andy Lutomirski

But what's wrong with the GCC attribute mechanism? Surely GCC ought to be able to generate the code, at least in the simple cases, and the attribute already exists. The attribute and READ_ONCE_NOCHECK seem like the least messy in the C code.

The problem with 'no_sanitize_address' attribute is incompatibility with inlining.
GCC can't inline function with that attribute into function without it.
And the contrary is also true - GCC can't inline function without attribute into function with such attribute.

Failure to inline always_inline function leads to build failure. And under CONFIG_OPTIMIZE=n 'inline' means 'always_inline'.

include/linux/compiler-gcc.h:

#if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) ||		\
    !defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4)

#define inline inline __attribute__((always_inline)) notrace

#16 lore raw
Quoted 11 lines · Andrey Ryabinin
Quoted 5 lines · Andy Lutomirski

But what's wrong with the GCC attribute mechanism? Surely GCC ought to be able to generate the code, at least in the simple cases, and the attribute already exists. The attribute and READ_ONCE_NOCHECK seem like the least messy in the C code.

The problem with 'no_sanitize_address' attribute is incompatibility with inlining.
GCC can't inline function with that attribute into function without it.
And the contrary is also true - GCC can't inline function without attribute into function with such attribute.

Quoted 9 lines

Failure to inline always_inline function leads to build failure. And under CONFIG_OPTIMIZE=n 'inline' means 'always_inline'.

include/linux/compiler-gcc.h:

#if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) ||		\
    !defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4)

#define inline inline __attribute__((always_inline)) notrace

Huh, 'inline' effectively means 'always_inline' on every arch, except x86.
This looks like a bug IMO.
Allowing gcc to uninline functions marked 'inline' could be beneficial for some arches/configs.

$ git grep ARCH_SUPPORTS_OPTIMIZED_INLINING
	arch/tile/Kconfig:config ARCH_SUPPORTS_OPTIMIZED_INLINING
	arch/x86/Kconfig:config ARCH_SUPPORTS_OPTIMIZED_INLINING
	include/linux/compiler-gcc.h:#if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) ||           \
$ git grep OPTIMIZE_INLINING
	arch/x86/Kconfig.debug:config OPTIMIZE_INLINING
	arch/x86/configs/i386_defconfig:CONFIG_OPTIMIZE_INLINING=y
	arch/x86/configs/x86_64_defconfig:CONFIG_OPTIMIZE_INLINING=y
	arch/x86/entry/vdso/vdso32/vclock_gettime.c:#undef CONFIG_OPTIMIZE_INLINING
	include/linux/compiler-gcc.h:    !defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4)
	kernel/configs/tiny.config:CONFIG_OPTIMIZE_INLINING=y
#17 lore raw
In reply to Andrey Ryabinin
Quoted 7 lines · Andrey Ryabinin
Quoted 5 lines · Andy Lutomirski

But what's wrong with the GCC attribute mechanism? Surely GCC ought to be able to generate the code, at least in the simple cases, and the attribute already exists. The attribute and READ_ONCE_NOCHECK seem like the least messy in the C code.

Quoted 5 lines

The problem with 'no_sanitize_address' attribute is incompatibility with inlining.
GCC can't inline function with that attribute into function without it.
And the contrary is also true - GCC can't inline function without attribute into function with such attribute.

Failure to inline always_inline function leads to build failure.

So just don't do that? Don't set the attribute on functions marked inline. Where do you see this anyways?

-Andi

#18 lore raw

2015-10-07 19:27 GMT+03:00 Andi Kleen <[email protected]>:

Quoted 8 lines · Andrey Ryabinin
Quoted 7 lines · Andy Lutomirski
Quoted 5 lines

But what's wrong with the GCC attribute mechanism? Surely GCC ought to be able to generate the code, at least in the simple cases, and the attribute already exists. The attribute and READ_ONCE_NOCHECK seem like the least messy in the C code.

Quoted 8 lines
Quoted 5 lines

The problem with 'no_sanitize_address' attribute is incompatibility with inlining.
GCC can't inline function with that attribute into function without it.
And the contrary is also true - GCC can't inline function without attribute into function with such attribute.

Failure to inline always_inline function leads to build failure.

So just don't do that? Don't set the attribute on functions marked inline. Where do you see this anyways?

Besides that we can't set the attribute on functions that *call* inline functions.
So we can't set it on get_wchan() because it calls __read_once_size().

Quoted 2 lines

-Andi