Skip to contentSkip to conversation

Removed redundant code from semaphore's down family of function

PATCH In review Satendra Singh Thakur 5 participants 11 messages Aug 12, 2019 – Aug 30, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Removed redundant code from semaphore's down family of function

-The semaphore code has four funcs
down,
down_interruptible,
down_killable,
down_timeout
-These four funcs have almost similar code except that they all call lower level function __down_xyz.
-This lower level func in-turn call inline func __down_common with appropriate arguments.
-This patch creates a common macro for above family of funcs so that duplicate code is eliminated.
-Also, __down_common has been made noinline so that code is functionally similar to previous one
-For example, earlier down_killable would call __down_killable , which in-turn would call inline func __down_common Now, down_killable calls noinline __down_common directly through a macro
-The funcs __down_interruptible, __down_killable etc have been removed as they were just wrapper to __down_common

---
 kernel/locking/semaphore.c | 107 +++++++++++++------------------------
 1 file changed, 38 insertions(+), 69 deletions(-)
kernel/locking/semaphore.c +38 -69
index d9dd94defc0a..0468bc335908 100644
@@ -33,11 +33,33 @@
 #include <linux/spinlock.h>
 #include <linux/ftrace.h>
 
-static noinline void __down(struct semaphore *sem);
-static noinline int __down_interruptible(struct semaphore *sem);
-static noinline int __down_killable(struct semaphore *sem);
-static noinline int __down_timeout(struct semaphore *sem, long timeout);
+static noinline int __sched __down_common(struct semaphore *sem, long state,
+long timeout);
 static noinline void __up(struct semaphore *sem);
+/**
+ * down_common - acquire the semaphore
+ * @sem: the semaphore to be acquired
+ * @state: new state of the task
+ * @timeout: either MAX_SCHEDULE_TIMEOUT or actual specified
+ * timeout
+ * Acquires the semaphore. If no more tasks are allowed to
+ * acquire the semaphore, calling this macro will put the task
+ * to sleep until the semaphore is released.
+ *
+ * This internally calls another func __down_common.
+ */
+#define down_common(sem, state, timeout)\
+({\
+int ret = 0;\
+unsigned long flags;\
+raw_spin_lock_irqsave(&(sem)->lock, flags);\
+if (likely((sem)->count > 0))\
+(sem)->count--;\
+else\
+ret = __down_common(sem, state, timeout);\
+raw_spin_unlock_irqrestore(&(sem)->lock, flags);\
+ret;\
+})
 
 /**
  * down - acquire the semaphore
@@ -52,14 +74,7 @@ static noinline void __up(struct semaphore *sem);
  */
 void down(struct semaphore *sem)
 {
-unsigned long flags;
-
-raw_spin_lock_irqsave(&sem->lock, flags);
-if (likely(sem->count > 0))
-sem->count--;
-else
-__down(sem);
-raw_spin_unlock_irqrestore(&sem->lock, flags);
+down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
 }
 EXPORT_SYMBOL(down);
 
@@ -74,17 +89,7 @@ EXPORT_SYMBOL(down);
  */
 int down_interruptible(struct semaphore *sem)
 {
-unsigned long flags;
-int result = 0;
-
-raw_spin_lock_irqsave(&sem->lock, flags);
-if (likely(sem->count > 0))
-sem->count--;
-else
-result = __down_interruptible(sem);
-raw_spin_unlock_irqrestore(&sem->lock, flags);
-
-return result;
+return down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
 }
 EXPORT_SYMBOL(down_interruptible);
 
@@ -100,17 +105,7 @@ EXPORT_SYMBOL(down_interruptible);
  */
 int down_killable(struct semaphore *sem)
 {
-unsigned long flags;
-int result = 0;
-
-raw_spin_lock_irqsave(&sem->lock, flags);
-if (likely(sem->count > 0))
-sem->count--;
-else
-result = __down_killable(sem);
-raw_spin_unlock_irqrestore(&sem->lock, flags);
-
-return result;
+return down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
 }
 EXPORT_SYMBOL(down_killable);
 
@@ -154,17 +149,7 @@ EXPORT_SYMBOL(down_trylock);
  */
 int down_timeout(struct semaphore *sem, long timeout)
 {
-unsigned long flags;
-int result = 0;
-
-raw_spin_lock_irqsave(&sem->lock, flags);
-if (likely(sem->count > 0))
-sem->count--;
-else
-result = __down_timeout(sem, timeout);
-raw_spin_unlock_irqrestore(&sem->lock, flags);
-
-return result;
+return down_common(sem, TASK_UNINTERRUPTIBLE, timeout);
 }
 EXPORT_SYMBOL(down_timeout);
 
@@ -196,12 +181,15 @@ struct semaphore_waiter {
 bool up;
 };
 
-/*
- * Because this function is inlined, the 'state' parameter will be
- * constant, and thus optimised away by the compiler.  Likewise the
- * 'timeout' parameter for the cases without timeouts.
+/**
+ * __down_common - Adds the current task to wait list
+ * puts the task to sleep until signal, timeout or up flag
+ * @sem: the semaphore to be acquired
+ * @state: the state of the calling task
+ * @timeout: either MAX_SCHEDULE_TIMEOUT or actual specified
+ * timeout
  */
-static inline int __sched __down_common(struct semaphore *sem, long state,
+static noinline int __sched __down_common(struct semaphore *sem, long state,
 long timeout)
 {
 struct semaphore_waiter waiter;
@@ -232,25 +220,6 @@ static inline int __sched __down_common(struct semaphore *sem, long state,
 return -EINTR;
 }
 
-static noinline void __sched __down(struct semaphore *sem)
-{
-__down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
-}
-
-static noinline int __sched __down_interruptible(struct semaphore *sem)
-{
-return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
-}
-
-static noinline int __sched __down_killable(struct semaphore *sem)
-{
-return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
-}
-
-static noinline int __sched __down_timeout(struct semaphore *sem, long timeout)
-{
-return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout);
-}
 
 static noinline void __sched __up(struct semaphore *sem)
 {

--
2.17.1

::DISCLAIMER::
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents (with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of this message without the prior written consent of authorized representative of HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately. Before opening any email and/or attachments, please check them for viruses and other defects.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

maintainer
#1 lore raw
Quoted 20 lines · Satendra Singh Thakur

-The semaphore code has four funcs
down,
down_interruptible,
down_killable,
down_timeout
-These four funcs have almost similar code except that they all call lower level function __down_xyz.
-This lower level func in-turn call inline func __down_common with appropriate arguments.
-This patch creates a common macro for above family of funcs so that duplicate code is eliminated.
-Also, __down_common has been made noinline so that code is functionally similar to previous one
-For example, earlier down_killable would call __down_killable , which in-turn would call inline func __down_common Now, down_killable calls noinline __down_common directly through a macro
-The funcs __down_interruptible, __down_killable etc have been removed as they were just wrapper to __down_common

Quoted 4 lines
---
 kernel/locking/semaphore.c | 107 +++++++++++++------------------------
 1 file changed, 38 insertions(+), 69 deletions(-)

Something has gone very wrong with your mail setup, so this patch is mangled beyond repair.

Please read Documentation/process/email-clients.rst and also work with your employer to remove the disclaimer at the end of your email too.

Thanks,

Will

#2 lore raw

PATCH 1/1 Removed redundant code from semaphore's down family of function

In reply to Satendra Singh Thakur

-The semaphore code has four funcs
down,
down_interruptible,
down_killable,
down_timeout
-These four funcs have almost similar code except that they all call lower level function __down_xyz.
-This lower level func in-turn call inline func __down_common with appropriate arguments.
-This patch creates a common macro for above family of funcs so that duplicate code is eliminated.
-Also, __down_common has been made noinline so that code is functionally similar to previous one
-For example, earlier down_killable would call __down_killable , which in-turn would call inline func __down_common Now, down_killable calls noinline __down_common directly through a macro
-The funcs __down_interruptible, __down_killable etc have been removed as they were just wrapper to __down_common

---
v1: removed disclaimer appended automatically by company mail policy

 kernel/locking/semaphore.c | 107 +++++++++++++------------------------
 1 file changed, 38 insertions(+), 69 deletions(-)
kernel/locking/semaphore.c +38 -69
index d9dd94defc0a..0468bc335908 100644
@@ -33,11 +33,33 @@
 #include <linux/spinlock.h>
 #include <linux/ftrace.h>
 
-static noinline void __down(struct semaphore *sem);
-static noinline int __down_interruptible(struct semaphore *sem);
-static noinline int __down_killable(struct semaphore *sem);
-static noinline int __down_timeout(struct semaphore *sem, long timeout);
+static noinline int __sched __down_common(struct semaphore *sem, long state,
+								long timeout);
 static noinline void __up(struct semaphore *sem);
+/**
+ * down_common - acquire the semaphore
+ * @sem: the semaphore to be acquired
+ * @state: new state of the task
+ * @timeout: either MAX_SCHEDULE_TIMEOUT or actual specified
+ * timeout
+ * Acquires the semaphore. If no more tasks are allowed to
+ * acquire the semaphore, calling this macro will put the task
+ * to sleep until the semaphore is released.
+ *
+ * This internally calls another func __down_common.
+ */
+#define down_common(sem, state, timeout)	\
+({	\
+	int ret = 0;	\
+	unsigned long flags;	\
+	raw_spin_lock_irqsave(&(sem)->lock, flags);	\
+	if (likely((sem)->count > 0))	\
+		(sem)->count--;	\
+	else	\
+		ret = __down_common(sem, state, timeout);	\
+	raw_spin_unlock_irqrestore(&(sem)->lock, flags);	\
+	ret;	\
+})
 
 /**
  * down - acquire the semaphore
@@ -52,14 +74,7 @@ static noinline void __up(struct semaphore *sem);
  */
 void down(struct semaphore *sem)
 {
-	unsigned long flags;
-
-	raw_spin_lock_irqsave(&sem->lock, flags);
-	if (likely(sem->count > 0))
-		sem->count--;
-	else
-		__down(sem);
-	raw_spin_unlock_irqrestore(&sem->lock, flags);
+	down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
 }
 EXPORT_SYMBOL(down);
 
@@ -74,17 +89,7 @@ EXPORT_SYMBOL(down);
  */
 int down_interruptible(struct semaphore *sem)
 {
-	unsigned long flags;
-	int result = 0;
-
-	raw_spin_lock_irqsave(&sem->lock, flags);
-	if (likely(sem->count > 0))
-		sem->count--;
-	else
-		result = __down_interruptible(sem);
-	raw_spin_unlock_irqrestore(&sem->lock, flags);
-
-	return result;
+	return down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
 }
 EXPORT_SYMBOL(down_interruptible);
 
@@ -100,17 +105,7 @@ EXPORT_SYMBOL(down_interruptible);
  */
 int down_killable(struct semaphore *sem)
 {
-	unsigned long flags;
-	int result = 0;
-
-	raw_spin_lock_irqsave(&sem->lock, flags);
-	if (likely(sem->count > 0))
-		sem->count--;
-	else
-		result = __down_killable(sem);
-	raw_spin_unlock_irqrestore(&sem->lock, flags);
-
-	return result;
+	return down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
 }
 EXPORT_SYMBOL(down_killable);
 
@@ -154,17 +149,7 @@ EXPORT_SYMBOL(down_trylock);
  */
 int down_timeout(struct semaphore *sem, long timeout)
 {
-	unsigned long flags;
-	int result = 0;
-
-	raw_spin_lock_irqsave(&sem->lock, flags);
-	if (likely(sem->count > 0))
-		sem->count--;
-	else
-		result = __down_timeout(sem, timeout);
-	raw_spin_unlock_irqrestore(&sem->lock, flags);
-
-	return result;
+	return down_common(sem, TASK_UNINTERRUPTIBLE, timeout);
 }
 EXPORT_SYMBOL(down_timeout);
 
@@ -196,12 +181,15 @@ struct semaphore_waiter {
 	bool up;
 };
 
-/*
- * Because this function is inlined, the 'state' parameter will be
- * constant, and thus optimised away by the compiler.  Likewise the
- * 'timeout' parameter for the cases without timeouts.
+/**
+ * __down_common - Adds the current task to wait list
+ * puts the task to sleep until signal, timeout or up flag
+ * @sem: the semaphore to be acquired
+ * @state: the state of the calling task
+ * @timeout: either MAX_SCHEDULE_TIMEOUT or actual specified
+ * timeout
  */
-static inline int __sched __down_common(struct semaphore *sem, long state,
+static noinline int __sched __down_common(struct semaphore *sem, long state,
 								long timeout)
 {
 	struct semaphore_waiter waiter;
@@ -232,25 +220,6 @@ static inline int __sched __down_common(struct semaphore *sem, long state,
 	return -EINTR;
 }
 
-static noinline void __sched __down(struct semaphore *sem)
-{
-	__down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
-}
-
-static noinline int __sched __down_interruptible(struct semaphore *sem)
-{
-	return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
-}
-
-static noinline int __sched __down_killable(struct semaphore *sem)
-{
-	return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
-}
-
-static noinline int __sched __down_timeout(struct semaphore *sem, long timeout)
-{
-	return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout);
-}
 
 static noinline void __sched __up(struct semaphore *sem)
 {
Signature · 1 line
2.17.1
maintainer
#3 lore raw
Quoted 11 lines · Satendra Singh Thakur

-The semaphore code has four funcs
down,
down_interruptible,
down_killable,
down_timeout
-These four funcs have almost similar code except that they all call lower level function __down_xyz. -This lower level func in-turn call inline func __down_common with appropriate arguments.
-This patch creates a common macro for above family of funcs so that duplicate code is eliminated.

Quoted 8 lines

-Also, __down_common has been made noinline so that code is functionally similar to previous one
-For example, earlier down_killable would call __down_killable , which in-turn would call inline func __down_common Now, down_killable calls noinline __down_common directly through a macro
-The funcs __down_interruptible, __down_killable etc have been removed as they were just wrapper to __down_common

The above is unreadable and seems to lack a reason for this change.

AFAICT from the actual patch, you're destroying the explicit instantiation of the __down*() functions through constant propagation into __down_common().

#4 lore raw
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Satendra Singh Thakur

-The semaphore code has four funcs
down,
down_interruptible,
down_killable,
down_timeout
-These four funcs have almost similar code except that they all call lower level function __down_xyz. -This lower level func in-turn call inline func __down_common with appropriate arguments.
-This patch creates a common macro for above family of funcs so that duplicate code is eliminated.
-Also, __down_common has been made noinline so that code is functionally similar to previous one

Quoted 8 lines
Quoted 6 lines

-For example, earlier down_killable would call __down_killable , which in-turn would call inline func __down_common Now, down_killable calls noinline __down_common directly through a macro
-The funcs __down_interruptible, __down_killable etc have been removed as they were just wrapper to __down_common

The above is unreadable and seems to lack a reason for this change.

Hi Mr Peter,
Thanks for the comments.
I will try to explain it further:

The semaphore has four functions named down*.
The call flow of the functions is

down* ----> __down* ----> inline __down_common

The code of down* and __down* is redundant/duplicate except that the __down_common is called with different arguments from __down* functions.

This patch defines a macro down_common which contain this common code of all down* functions.

new call flow is

down* ----> noinline __down_common (through a macro down_common).

Quoted 3 lines

AFAICT from the actual patch, you're destroying the explicit instantiation of the __down*() functions
through constant propagation into __down_common().

Intead of instantiation of __down* functions, we are instaintiating __down_common, is it a problem ?

Thanks
Satendra

maintainer
#5 lore raw
Quoted 41 lines · Satendra Singh Thakur
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Satendra Singh Thakur

-The semaphore code has four funcs
down,
down_interruptible,
down_killable,
down_timeout
-These four funcs have almost similar code except that they all call lower level function __down_xyz. -This lower level func in-turn call inline func __down_common with appropriate arguments.
-This patch creates a common macro for above family of funcs so that duplicate code is eliminated.
-Also, __down_common has been made noinline so that code is functionally similar to previous one

Quoted 8 lines
Quoted 6 lines

-For example, earlier down_killable would call __down_killable , which in-turn would call inline func __down_common Now, down_killable calls noinline __down_common directly through a macro
-The funcs __down_interruptible, __down_killable etc have been removed as they were just wrapper to __down_common

The above is unreadable and seems to lack a reason for this change.

Hi Mr Peter,
Thanks for the comments.
I will try to explain it further:

The semaphore has four functions named down*.
The call flow of the functions is

down* ----> __down* ----> inline __down_common

The code of down* and __down* is redundant/duplicate except that the __down_common is called with different arguments from __down* functions.

This patch defines a macro down_common which contain this common code of all down* functions.

new call flow is

Quoted 7 lines

down* ----> noinline __down_common (through a macro down_common).

Quoted 3 lines

AFAICT from the actual patch, you're destroying the explicit instantiation of the __down*() functions
through constant propagation into __down_common().

Intead of instantiation of __down* functions, we are instaintiating __down_common, is it a problem ?

Then you loose the constant propagation into __down_common and you'll not DCE the signal and/or timeout code. The function even has a comment on explaining that. That said; the timeout DCE seems suboptimal.

Also; I'm thinking you can do it without that ugly CPP.

---
Subject: locking/semaphore: Simplify code flow

Ever since we got rid of the per arch assembly the structure of the semaphore code is more complicated than it needs to be.

Specifically, the slow path calling convention is no more. With that, Satendara noted that all functions calling __down_common() (aka the slow path) were basically the same.

Fold the lot.

(XXX, we should probably move the schedule_timeout() thing into its own patch)

---
 include/linux/sched.h      |  13 +++++-
 kernel/locking/semaphore.c | 105 +++++++++++++--------------------------------
 kernel/time/timer.c        |  44 +++++++------------
 3 files changed, 58 insertions(+), 104 deletions(-)
include/linux/sched.h +12 -1
index f0edee94834a..8f09e8cebe5a 100644
@@ -214,7 +214,18 @@ extern void scheduler_tick(void);
 
 #define	MAX_SCHEDULE_TIMEOUT		LONG_MAX
 
-extern long schedule_timeout(long timeout);
+extern long __schedule_timeout(long timeout);
+
+static inline long schedule_timeout(long timeout)
+{
+	if (timeout == MAX_SCHEDULE_TIMEOUT) {
+		schedule();
+		return timeout;
+	}
+
+	return __schedule_timeout(timeout);
+}
+
 extern long schedule_timeout_interruptible(long timeout);
 extern long schedule_timeout_killable(long timeout);
 extern long schedule_timeout_uninterruptible(long timeout);
kernel/locking/semaphore.c +31 -74

157 lines not on this page · raw message

kernel/time/timer.c +15 -29

63 lines not on this page · raw message

maintainer
#6 lore raw
Quoted 2 lines · Peter Zijlstra

(XXX, we should probably move the schedule_timeout() thing into its own patch)

A better version here...

---
Subject: sched,time: Allow better constprop/DCE for schedule_timeout()

If timeout is constant and MAX_SCHEDULE_TIMEOUT, it would be nice to allow to optimize away everything timeout.

---
 include/linux/sched.h | 13 ++++++++++++-
 kernel/time/timer.c   | 52 ++++++++++++++++++++++++---------------------------
 2 files changed, 36 insertions(+), 29 deletions(-)
include/linux/sched.h +12 -1
index f0edee94834a..6003e96bce52 100644
@@ -214,7 +214,18 @@ extern void scheduler_tick(void);
 
 #define	MAX_SCHEDULE_TIMEOUT		LONG_MAX
 
-extern long schedule_timeout(long timeout);
+extern long __schedule_timeout(long timeout);
+
+static inline long schedule_timeout(long timeout)
+{
+	if (__builtin_constant_p(timeout) && timeout == MAX_SCHEDULE_TIMEOUT) {
+		schedule();
+		return timeout;
+	}
+
+	return __schedule_timeout(timeout);
+}
+
 extern long schedule_timeout_interruptible(long timeout);
 extern long schedule_timeout_killable(long timeout);
 extern long schedule_timeout_uninterruptible(long timeout);
kernel/time/timer.c +24 -28

72 lines not on this page · raw message

#7 lore raw

Re: sched,time: Allow better constprop/DCE for schedule_timeout()

I love your patch! Yet something to improve:

Hi Peter,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.3-rc6 next-20190826]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Peter-Zijlstra/sched-time-Allow-better-constprop-DCE-for-schedule_timeout/20190827-061612
config: um-x86_64_defconfig (attached as .config)
compiler: gcc-7 (Debian 7.4.0-10) 7.4.0 reproduce:

        # save the attached .config to linux build tree
        make ARCH=um SUBARCH=x86_64

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

In file included from arch/x86/um/shared/sysdep/kernel-offsets.h:3:0,
from arch/um/kernel/asm-offsets.c:1:
include/linux/sched.h: In function 'schedule_timeout':

>> include/linux/sched.h:222:3: error: implicit declaration of function 'schedule'; did you mean 'schedule_work'? [-Werror=implicit-function-declaration]
      schedule();
      ^~~~~~~~
      schedule_work
   include/linux/sched.h: At top level:
   include/linux/sched.h:233:17: warning: conflicting types for 'schedule'
    asmlinkage void schedule(void);
                    ^~~~~~~~
   include/linux/sched.h:222:3: note: previous implicit declaration of 'schedule' was here
      schedule();
      ^~~~~~~~

cc1: some warnings being treated as errors make[2]: *** [arch/um/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors. make[1]: *** [prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [sub-make] Error 2
8 real 6 user 4 sys 130.06% cpu make prepare

vim +222 include/linux/sched.h

218
219 static inline long schedule_timeout(long timeout)
220 {
221 if (__builtin_constant_p(timeout) && timeout == MAX_SCHEDULE_TIMEOUT) {

 > 222			schedule();

223 return timeout;
224 }
225
226 return __schedule_timeout(timeout);
227 }
228

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#8 lore raw

Re: sched,time: Allow better constprop/DCE for schedule_timeout()

In reply to Peter Zijlstra
I love your patch! Yet something to improve:

Hi Peter,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.3-rc6 next-20190826]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Peter-Zijlstra/sched-time-Allow-better-constprop-DCE-for-schedule_timeout/20190827-061612
config: x86_64-lkp (attached as .config)
compiler: gcc-7 (Debian 7.4.0-10) 7.4.0 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

In file included from include/linux/uaccess.h:5:0,

                    from include/linux/crypto.h:21,
                    from arch/x86/kernel/asm-offsets.c:9:
   include/linux/sched.h: In function 'schedule_timeout':
>> include/linux/sched.h:222:3: error: implicit declaration of function 'schedule' [-Werror=implicit-function-declaration]
      schedule();
      ^~~~~~~~
   include/linux/sched.h: At top level:
   include/linux/sched.h:233:17: warning: conflicting types for 'schedule'
    asmlinkage void schedule(void);
                    ^~~~~~~~
   include/linux/sched.h:222:3: note: previous implicit declaration of 'schedule' was here
      schedule();
      ^~~~~~~~

cc1: some warnings being treated as errors make[2]: *** [arch/x86/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors. make[1]: *** [prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [sub-make] Error 2
18 real 10 user 3 sys 78.71% cpu make prepare

vim +/schedule +222 include/linux/sched.h

218
219 static inline long schedule_timeout(long timeout)
220 {
221 if (__builtin_constant_p(timeout) && timeout == MAX_SCHEDULE_TIMEOUT) {

 > 222			schedule();

223 return timeout;
224 }
225
226 return __schedule_timeout(timeout);
227 }
228

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#9 lore raw
In reply to Peter Zijlstra
Quoted 64 lines · Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

(XXX, we should probably move the schedule_timeout() thing into its own patch)

A better version here...

---
Subject: sched,time: Allow better constprop/DCE for schedule_timeout()

If timeout is constant and MAX_SCHEDULE_TIMEOUT, it would be nice to allow to optimize away everything timeout.

---
include/linux/sched.h | 13 ++++++++++++-
kernel/time/timer.c | 52 ++++++++++++++++++++++++---------------------------

2 files changed, 36 insertions(+), 29 deletions(-)
include/linux/sched.h +12 -1
index f0edee94834a..6003e96bce52 100644
@@ -214,7 +214,18 @@ extern void scheduler_tick(void);
 
 #define	MAX_SCHEDULE_TIMEOUT		LONG_MAX
 
-extern long schedule_timeout(long timeout);
+extern long __schedule_timeout(long timeout);
+
+static inline long schedule_timeout(long timeout)
+{
+	if (__builtin_constant_p(timeout) && timeout == MAX_SCHEDULE_TIMEOUT) {
+		schedule();
+		return timeout;
+	}
+
+	return __schedule_timeout(timeout);
+}
+
 extern long schedule_timeout_interruptible(long timeout);
 extern long schedule_timeout_killable(long timeout);
 extern long schedule_timeout_uninterruptible(long timeout);
kernel/time/timer.c +1 -9
index 0e315a2e77ae..912ae56b96b8 100644
@@ -1851,38 +1851,34 @@ static void process_timeout(struct timer_list *t)
 * jiffies will be returned.  In all cases the return value is guaranteed
 * to be non-negative.
 */
-signed long __sched schedule_timeout(signed long timeout)
+signed long __sched __schedule_timeout(signed long timeout)
 {
 	struct process_timer timer;
 	unsigned long expire;
 
-	switch (timeout)
-	{
-	case MAX_SCHEDULE_TIMEOUT:
-		/*
-		 * These two special cases are useful to be comfortable
-		 * in the caller. Nothing more. We could take
-		 * MAX_SCHEDULE_TIMEOUT from one of the negative value
-		 * but I' d like to return a valid offset (>=0) to allow

kernel/time/timer.c static void process_timeout(struct timer_list *t) · line 1870

Quoted 10 lines
kernel/time/timer.c +6 -3
-		 * the caller to do everything it want with the retval.
-		 */
+	/*
+	 * We could take MAX_SCHEDULE_TIMEOUT from one of the negative values,
+	 * but I'd like to return a valid offset (>= 0) to allow the caller to
+	 * do everything it wants with the retval.
+	 */
+	if (timeout == MAX_SCHEDULE_TIMEOUT) {
 		schedule();
-		goto out;

Hi Mr Peter,
I have a suggestion here:
The condition timeout == MAX_SCHEDULE_TIMEOUT is already handled in schedule_timeout function and same conditon is handled again in __schedule_timeout. Currently, no other function calls __schedule_timeout except schedule_timeout. Therefore, it seems this condition will never become true.

This conditon will only be true when __schedule_timeout is called directly from kernel with timeout = MAX_SCHEDULE_TIMEOUT. This case may be rare. Therefore, we can modify it as

if (unlikely(timeout == MAX_SCHEDULE_TIMEOUT))

Please let me know your comments.
Thanks
Satendra

Quoted 46 lines
kernel/time/timer.c +17 -16
-	default:
-		/*
-		 * Another bit of PARANOID. Note that the retval will be
-		 * 0 since no piece of kernel is supposed to do a check
-		 * for a negative retval of schedule_timeout() (since it
-		 * should never happens anyway). You just have the printk()
-		 * that will tell you if something is gone wrong and where.
-		 */
-		if (timeout < 0) {
-			printk(KERN_ERR "schedule_timeout: wrong timeout "
+		return timeout;
+	}
+
+	/*
+	 * Another bit of PARANOID. Note that the retval will be 0 since no
+	 * piece of kernel is supposed to do a check for a negative retval of
+	 * schedule_timeout() (since it should never happens anyway). You just
+	 * have the printk() that will tell you if something is gone wrong and
+	 * where.
+	 */
+	if (timeout < 0) {
+		printk(KERN_ERR "schedule_timeout: wrong timeout "
 				"value %lx\n", timeout);
-			dump_stack();
-			current->state = TASK_RUNNING;
-			goto out;
-		}
+		dump_stack();
+		current->state = TASK_RUNNING;
+		goto out;
 	}
 
 	expire = timeout + jiffies;
@@ -1898,10 +1894,10 @@ signed long __sched schedule_timeout(signed long timeout)
 
 	timeout = expire - jiffies;
 
- out:
+out:
 	return timeout < 0 ? 0 : timeout;
 }
-EXPORT_SYMBOL(schedule_timeout);
+EXPORT_SYMBOL(__schedule_timeout);
 
 /*
 * We can use __set_current_state() here because schedule_timeout() calls
maintainer
#10 lore raw
Quoted 10 lines · Satendra Singh Thakur
Quoted 9 lines
include/linux/sched.h +9 -0
+static inline long schedule_timeout(long timeout)
+{
+	if (__builtin_constant_p(timeout) && timeout == MAX_SCHEDULE_TIMEOUT) {
+		schedule();
+		return timeout;
+	}
+
+	return __schedule_timeout(timeout);
+}
Quoted 7 lines

Review

Quoted 3 lines
kernel/time/timer.c +1 -0
+	if (timeout == MAX_SCHEDULE_TIMEOUT) {
		schedule();
(quoted diff) +0 -1
-		goto out;

Hi Mr Peter,
I have a suggestion here:
The condition timeout == MAX_SCHEDULE_TIMEOUT is already handled in schedule_timeout function

Only if it is a compile time constant; otherwise it will end up here.

Quoted 3 lines

and same conditon is handled again in __schedule_timeout. Currently, no other function calls __schedule_timeout except schedule_timeout. Therefore, it seems this condition will never become true.

Please read more careful.