cleanup: use bool as return type for rwsem_is_locked
Subsystems (2)
RFC PATCH 1/1 cleanup: use bool as return type for rwsem_is_locked
I see that there are functions like this which basically say:
return 1 if true else return 0. Is it worth cleaning them up? Or is there any reason why this convention is followed?
use bool as the return type. No reason for return type to be int.
--- include/linux/rwsem-spinlock.h | 2 +- include/linux/rwsem.h | 2 +- kernel/locking/rwsem-spinlock.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
include/linux/rwsem-spinlock.h +1 -1
@@ -39,7 +39,7 @@ extern int __down_write_trylock(struct rw_semaphore *sem);extern void __up_read (struct rw_semaphore *sem);extern void __up_write (struct rw_semaphore *sem);extern void __downgrade_write (struct rw_semaphore *sem); -extern int rwsem_is_locked (struct rw_semaphore *sem); +extern bool rwsem_is_locked (struct rw_semaphore *sem);#endif /* __KERNEL__ */ #endif /* _LINUX_RWSEM_SPINLOCK_H */
include/linux/rwsem.h +1 -1
@@ -49,7 +49,7 @@ extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);#include <asm/rwsem.h> /* In all implementations count != 0 means locked */ -static inline int rwsem_is_locked (struct rw_semaphore *sem) +static inline bool rwsem_is_locked (struct rw_semaphore *sem) {return sem->count !=0 ; }
kernel/locking/rwsem-spinlock.c +1 -1
@@ -20,7 +20,7 @@ struct rwsem_waiter {enum rwsem_waiter_type type; }; -int rwsem_is_locked (struct rw_semaphore *sem) +bool rwsem_is_locked (struct rw_semaphore *sem) {int ret =1 ;unsigned long flags;
Signature · 1 line
1.7.9.5
Peter Zijlstra
maintainerQuoted 4 lines · Pranith Kumar
I see that there are functions like this which basically say:
return 1 if true else return 0. Is it worth cleaning them up? Or is there any reason why this convention is followed?
Hysterical raisins, a lot of people learnt C before it grew bool, including me.
Quoted 10 lines
use bool as the return type. No reason for return type to be int.
Signed-off-by Pranith Kumar <[email protected]>---
kernel/locking/rwsem-spinlock.c +0
-0
kernel/locking/rwsem-spinlock.c struct rwsem_waiter { · line 26
Quoted 8 lines
kernel/locking/rwsem-spinlock.c +1
-1@@ -20,7 +20,7 @@ struct rwsem_waiter {enum rwsem_waiter_type type; }; -int rwsem_is_locked (struct rw_semaphore *sem) +bool rwsem_is_locked (struct rw_semaphore *sem) {int ret =1 ;unsigned long flags;
Now, see that's a half arsed change, if you change the function return value, you should also change the value we actually return, @ret above to bool, and you should then also change the values used to 'true' and 'false'.
Now in general, I don't particularly like such superfluous changes, so unless you can show that GCC actually generates better code, I'd prefer to keep things as they are.
Quoted 4 lines · Peter Zijlstra
Now in general, I don't particularly like such superfluous changes, so unless you can show that GCC actually generates better code, I'd prefer to keep things as they are.
Fixed and checked the assembly. It saves us 2 bytes of code, not much. I am not sure if that is worth it :(
use bool as the return type for rwsem_is_locked() instead of int
--- include/linux/rwsem-spinlock.h | 2 +- include/linux/rwsem.h | 2 +- kernel/locking/rwsem-spinlock.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
include/linux/rwsem-spinlock.h +1 -1
@@ -39,7 +39,7 @@ extern int __down_write_trylock(struct rw_semaphore *sem);extern void __up_read (struct rw_semaphore *sem);extern void __up_write (struct rw_semaphore *sem);extern void __downgrade_write (struct rw_semaphore *sem); -extern int rwsem_is_locked (struct rw_semaphore *sem); +extern bool rwsem_is_locked (struct rw_semaphore *sem);#endif /* __KERNEL__ */ #endif /* _LINUX_RWSEM_SPINLOCK_H */
include/linux/rwsem.h +1 -1
@@ -40,7 +40,7 @@ extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);#include <asm/rwsem.h> /* In all implementations count != 0 means locked */ -static inline int rwsem_is_locked (struct rw_semaphore *sem) +static inline bool rwsem_is_locked (struct rw_semaphore *sem) {return sem->count !=0 ; }
kernel/locking/rwsem-spinlock.c +2 -2
@@ -20,9 +20,9 @@ struct rwsem_waiter {enum rwsem_waiter_type type; }; -int rwsem_is_locked (struct rw_semaphore *sem) +bool rwsem_is_locked (struct rw_semaphore *sem) { -int ret =1 ; +bool ret = true;unsigned long flags;if (raw_spin_trylock_irqsave (&sem->wait_lock, flags)) {
Signature · 1 line
1.7.9.5
Peter Zijlstra
maintainerQuoted 7 lines · Pranith Kumar
Quoted 4 lines · Peter Zijlstra
Now in general, I don't particularly like such superfluous changes, so unless you can show that GCC actually generates better code, I'd prefer to keep things as they are.
Fixed and checked the assembly. It saves us 2 bytes of code, not much. I am not sure if that is worth it :(
still 2 bytes, so sure.
Which gcc did you use and what arch did you build? That might be useful info for the changelog.
Quoted 6 lines · Peter Zijlstra
Quoted 1 line
Fixed and checked the assembly. It saves us 2 bytes of code, not much. I am not sure if that is worth it :(
still 2 bytes, so sure.
Which gcc did you use and what arch did you build? That might be useful info for the changelog.
I am using gcc (Ubuntu/Linaro 4.7.3-2ubuntu1~12.04) 4.7.3. I tested this using ARCH=um with a Pentium processor to turn on CONFIG_RWSEM_GENERIC_SPINLOCK.
Signature · 1 line
Pranith
Quoted 7 lines · Peter Zijlstra
Quoted 6 lines · Pranith Kumar
Quoted 4 lines · Peter Zijlstra
Now in general, I don't particularly like such superfluous changes, so unless you can show that GCC actually generates better code, I'd prefer to keep things as they are.
Quoted 6 lines
Quoted 1 line
Fixed and checked the assembly. It saves us 2 bytes of code, not much. I am not sure if that is worth it :(
still 2 bytes, so sure.
Which gcc did you use and what arch did you build? That might be useful info for the changelog.
Yeah, please attach the output of 'size kernel/locking/rwsem.o' for both before and after. I think there's similar opportunity in other locking code as well.
Quoted 10 lines · Davidlohr Bueso
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Pranith Kumar
Quoted 4 lines · Peter Zijlstra
Now in general, I don't particularly like such superfluous changes, so unless you can show that GCC actually generates better code, I'd prefer to keep things as they are.
Fixed and checked the assembly. It saves us 2 bytes of code, not much. I am not sure if that is worth it :(
Quoted 9 lines
Quoted 4 lines
still 2 bytes, so sure.
Which gcc did you use and what arch did you build? That might be useful info for the changelog.
Yeah, please attach the output of 'size
kernel/locking/rwsem.o' for both before and after. I think there's similar opportunity in other locking code as well.
Ok. Here is the data.
size kernel/locking/rwsem-spinlock.o
Before change to bool:
text data bss dec hex
1336 0 0 1336 538
After change to bool:
text data bss dec hex
1334 0 0 1334 536
Signature · 1 line
Pranith
Quoted 50 lines · Pranith Kumar
Quoted 4 lines · Peter Zijlstra
Now in general, I don't particularly like such superfluous changes, so unless you can show that GCC actually generates better code, I'd prefer to keep things as they are.
Fixed and checked the assembly. It saves us 2 bytes of code, not much. I am not sure if that is worth it :(
use bool as the return type for
rwsem_is_locked()instead of intSigned-off-by Pranith Kumar <[email protected]>--- include/linux/rwsem-spinlock.h | 2 +- include/linux/rwsem.h | 2 +- kernel/locking/rwsem-spinlock.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)include/linux/rwsem-spinlock.h +1
-1@@ -39,7 +39,7 @@ extern int __down_write_trylock(struct rw_semaphore *sem);extern void __up_read (struct rw_semaphore *sem);extern void __up_write (struct rw_semaphore *sem);extern void __downgrade_write (struct rw_semaphore *sem); -extern int rwsem_is_locked (struct rw_semaphore *sem); +extern bool rwsem_is_locked (struct rw_semaphore *sem);#endif /* __KERNEL__ */ #endif /* _LINUX_RWSEM_SPINLOCK_H */ include/linux/rwsem.h +1
-1@@ -40,7 +40,7 @@ extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);#include <asm/rwsem.h> /* In all implementations count != 0 means locked */ -static inline int rwsem_is_locked (struct rw_semaphore *sem) +static inline bool rwsem_is_locked (struct rw_semaphore *sem) {return sem->count !=0 ; }kernel/locking/rwsem-spinlock.c +0
-0@@ -20,9 +20,9 @@ struct rwsem_waiter {enum rwsem_waiter_type type;
kernel/locking/rwsem-spinlock.c struct rwsem_waiter { · line 28
Quoted 10 lines
kernel/locking/rwsem-spinlock.c +2
-2}; -int rwsem_is_locked (struct rw_semaphore *sem) +bool rwsem_is_locked (struct rw_semaphore *sem) { -int ret =1 ; +bool ret = true;unsigned long flags;if (raw_spin_trylock_irqsave (&sem->wait_lock, flags)) {
I observed one other user of rwsem_is_locked() in xfs, change accordingly
--- fs/xfs/xfs_inode.c | 2 +- fs/xfs/xfs_inode.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
fs/xfs/xfs_inode.c +1 -1
@@ -285,7 +285,7 @@ xfs_ilock_demote( }#if defined(DEBUG ) || defined(XFS_WARN ) -int +bool xfs_isilocked (xfs_inode_t *ip,uint lock_flags)
fs/xfs/xfs_inode.h +1 -1
@@ -348,7 +348,7 @@ void xfs_ilock(xfs_inode_t *, uint);int xfs_ilock_nowait (xfs_inode_t *,uint );void xfs_iunlock (xfs_inode_t *,uint );void xfs_ilock_demote (xfs_inode_t *,uint ); -int xfs_isilocked (xfs_inode_t *,uint ); +bool xfs_isilocked (xfs_inode_t *,uint );uint xfs_ilock_data_map_shared (struct xfs_inode *);uint xfs_ilock_attr_map_shared (struct xfs_inode *); int xfs_ialloc(struct xfs_trans *, xfs_inode_t *, umode_t,
Signature · 1 line
1.7.9.5
Quoted 7 lines · Pranith Kumar
Quoted 6 lines · Pranith Kumar
Quoted 4 lines · Peter Zijlstra
Now in general, I don't particularly like such superfluous changes, so unless you can show that GCC actually generates better code, I'd prefer to keep things as they are.
Quoted 5 lines
Quoted 5 lines
Fixed and checked the assembly. It saves us 2 bytes of code, not much. I am not sure if that is worth it :(
use bool as the return type for
rwsem_is_locked()instead of intSigned-off-by Pranith Kumar <[email protected]>
....
Makes sense to me.
Quoted 13 lines
I observed one other user of
rwsem_is_locked()in xfs, change accordinglySigned-off-by Pranith Kumar <[email protected]>--- fs/xfs/xfs_inode.c | 2 +- fs/xfs/xfs_inode.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)fs/xfs/xfs_inode.c +0
-0
fs/xfs/xfs_inode.c xfs_ilock_demote( · line 291
Quoted 8 lines
fs/xfs/xfs_inode.c +1
-1@@ -285,7 +285,7 @@ xfs_ilock_demote( }#if defined(DEBUG ) || defined(XFS_WARN ) -int +bool xfs_isilocked (xfs_inode_t *ip,uint lock_flags)
If you are going to change the return type to bool, then you should also remove the manual "!!" conversions to a boolean return and let the compiler do it in the most optimal way.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Quoted 10 lines · Dave Chinner
Quoted 9 lines · Pranith Kumar
Quoted 8 lines · Pranith Kumar
Quoted 4 lines · Peter Zijlstra
Now in general, I don't particularly like such superfluous changes, so unless you can show that GCC actually generates better code, I'd prefer to keep things as they are.
Fixed and checked the assembly. It saves us 2 bytes of code, not much. I am not sure if that is worth it :(
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
use bool as the return type for
rwsem_is_locked()instead of intIf you are going to change the return type to bool, then you should also remove the manual "!!" conversions to a boolean return and let the compiler do it in the most optimal way.
Agreed, please find patch below:
change return type to bool to follow rwsem_is_locked()
--- fs/xfs/xfs_inode.c | 8 ++++---- fs/xfs/xfs_inode.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-)
fs/xfs/xfs_inode.c +4 -4
@@ -285,25 +285,25 @@ xfs_ilock_demote( }#if defined(DEBUG ) || defined(XFS_WARN ) -int +bool xfs_isilocked (xfs_inode_t *ip,uint lock_flags) {if (lock_flags & (XFS_ILOCK_EXCL |XFS_ILOCK_SHARED )) {if (!(lock_flags &XFS_ILOCK_SHARED )) -return !!ip->i_lock.mr_writer; +return !(ip->i_lock.mr_writer ==0 );return rwsem_is_locked (&ip->i_lock.mr_lock); }if (lock_flags & (XFS_IOLOCK_EXCL |XFS_IOLOCK_SHARED )) {if (!(lock_flags &XFS_IOLOCK_SHARED )) -return !!ip->i_iolock.mr_writer; +return !(ip->i_iolock.mr_writer ==0 );return rwsem_is_locked (&ip->i_iolock.mr_lock); }ASSERT (0 ); -return 0 ; +return false; }#endif
fs/xfs/xfs_inode.h +1 -1
@@ -346,7 +346,7 @@ void xfs_ilock(xfs_inode_t *, uint);int xfs_ilock_nowait (xfs_inode_t *,uint );void xfs_iunlock (xfs_inode_t *,uint );void xfs_ilock_demote (xfs_inode_t *,uint ); -int xfs_isilocked (xfs_inode_t *,uint ); +bool xfs_isilocked (xfs_inode_t *,uint );uint xfs_ilock_data_map_shared (struct xfs_inode *);uint xfs_ilock_attr_map_shared (struct xfs_inode *); int xfs_ialloc(struct xfs_trans *, xfs_inode_t *, umode_t,
Signature · 6 lines
1.9.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 15 lines · Pranith Kumar
Quoted 14 lines · Dave Chinner
Quoted 11 lines · Pranith Kumar
Quoted 10 lines · Pranith Kumar
Quoted 4 lines · Peter Zijlstra
Now in general, I don't particularly like such superfluous changes, so unless you can show that GCC actually generates better code, I'd prefer to keep things as they are.
Fixed and checked the assembly. It saves us 2 bytes of code, not much. I am not sure if that is worth it :(
use bool as the return type for
rwsem_is_locked()instead of int
Quoted 7 lines
Quoted 4 lines
If you are going to change the return type to bool, then you should also remove the manual "!!" conversions to a boolean return and let the compiler do it in the most optimal way.
Agreed, please find patch below:
Simplify the "!!" condition. This is much simpler. :)
change return type to bool to follow rwsem_is_locked()
--- fs/xfs/xfs_inode.c | 8 ++++---- fs/xfs/xfs_inode.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-)
fs/xfs/xfs_inode.c +4 -4
@@ -285,25 +285,25 @@ xfs_ilock_demote( }#if defined(DEBUG ) || defined(XFS_WARN ) -int +bool xfs_isilocked (xfs_inode_t *ip,uint lock_flags) {if (lock_flags & (XFS_ILOCK_EXCL |XFS_ILOCK_SHARED )) {if (!(lock_flags &XFS_ILOCK_SHARED )) -return !!ip->i_lock.mr_writer; +return (ip->i_lock.mr_writer !=0 );return rwsem_is_locked (&ip->i_lock.mr_lock); }if (lock_flags & (XFS_IOLOCK_EXCL |XFS_IOLOCK_SHARED )) {if (!(lock_flags &XFS_IOLOCK_SHARED )) -return !!ip->i_iolock.mr_writer; +return (ip->i_iolock.mr_writer !=0 );return rwsem_is_locked (&ip->i_iolock.mr_lock); }ASSERT (0 ); -return 0 ; +return false; }#endif
fs/xfs/xfs_inode.h +1 -1
@@ -346,7 +346,7 @@ void xfs_ilock(xfs_inode_t *, uint);int xfs_ilock_nowait (xfs_inode_t *,uint );void xfs_iunlock (xfs_inode_t *,uint );void xfs_ilock_demote (xfs_inode_t *,uint ); -int xfs_isilocked (xfs_inode_t *,uint ); +bool xfs_isilocked (xfs_inode_t *,uint );uint xfs_ilock_data_map_shared (struct xfs_inode *);uint xfs_ilock_attr_map_shared (struct xfs_inode *); int xfs_ialloc(struct xfs_trans *, xfs_inode_t *, umode_t,
Signature · 1 line
1.9.1
Quoted 7 lines · Pranith Kumar
Quoted 5 lines · Pranith Kumar
Quoted 3 lines · Dave Chinner
If you are going to change the return type to bool, then you should also remove the manual "!!" conversions to a boolean return and let the compiler do it in the most optimal way.
Agreed, please find patch below:
Simplify the "!!" condition. This is much simpler. :)
[]
Quoted 6 lines
fs/xfs/xfs_inode.c +0
-0@@ -285,25 +285,25 @@ xfs_ilock_demote( }#if defined(DEBUG ) || defined(XFS_WARN )
fs/xfs/xfs_inode.c xfs_ilock_demote( · line 295
Quoted 10 lines
fs/xfs/xfs_inode.c +2
-2-int +bool xfs_isilocked (xfs_inode_t *ip,uint lock_flags) {if (lock_flags & (XFS_ILOCK_EXCL |XFS_ILOCK_SHARED )) {if (!(lock_flags &XFS_ILOCK_SHARED )) -return !!ip->i_lock.mr_writer; +return (ip->i_lock.mr_writer !=0 );
simpler still would be just removing the !! completely. I presume in no case would it make an actual difference in emitted code.
ie:
return ip->i_lock.mr_writer;
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Quoted 30 lines · Joe Perches
Quoted 7 lines · Pranith Kumar
Quoted 5 lines · Pranith Kumar
Quoted 3 lines · Dave Chinner
If you are going to change the return type to bool, then you should also remove the manual "!!" conversions to a boolean return and let the compiler do it in the most optimal way.
Agreed, please find patch below:
Simplify the "!!" condition. This is much simpler. :)
[]
Quoted 6 lines
fs/xfs/xfs_inode.c +0
-0@@ -285,25 +285,25 @@ xfs_ilock_demote( }#if defined(DEBUG ) || defined(XFS_WARN )fs/xfs/xfs_inode.c xfs_ilock_demote( · line 295
Quoted 10 lines
fs/xfs/xfs_inode.c +2
-2-int +bool xfs_isilocked (xfs_inode_t *ip,uint lock_flags) {if (lock_flags & (XFS_ILOCK_EXCL |XFS_ILOCK_SHARED )) {if (!(lock_flags &XFS_ILOCK_SHARED )) -return !!ip->i_lock.mr_writer; +return (ip->i_lock.mr_writer !=0 );simpler still would be just removing the !! completely. I presume in no case would it make an actual difference in emitted code.
Quoted 2 lines
ie:
return ip->i_lock.mr_writer;
Yup, that's exactly what I meant. Casting to a bool type does all the work of squashing all non-zero values to 1...
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Quoted 11 lines · Dave Chinner
Quoted 10 lines · Joe Perches
Quoted 5 lines · Pranith Kumar
Quoted 4 lines · Pranith Kumar
Quoted 3 lines · Dave Chinner
If you are going to change the return type to bool, then you should also remove the manual "!!" conversions to a boolean return and let the compiler do it in the most optimal way.
simpler still would be just removing the !! completely. I presume in no case would it make an actual difference in emitted code.
Quoted 6 lines
Quoted 2 lines
ie:
return ip->i_lock.mr_writer;Yup, that's exactly what I meant. Casting to a bool type does all the work of squashing all non-zero values to 1...
change return type if xfs_isilocked() to bool to follow rwsem_is_locked()
--- fs/xfs/xfs_inode.c | 8 ++++---- fs/xfs/xfs_inode.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-)
fs/xfs/xfs_inode.c +4 -4
@@ -285,25 +285,25 @@ xfs_ilock_demote( }#if defined(DEBUG ) || defined(XFS_WARN ) -int +bool xfs_isilocked (xfs_inode_t *ip,uint lock_flags) {if (lock_flags & (XFS_ILOCK_EXCL |XFS_ILOCK_SHARED )) {if (!(lock_flags &XFS_ILOCK_SHARED )) -return !!ip->i_lock.mr_writer; +return ip->i_lock.mr_writer;return rwsem_is_locked (&ip->i_lock.mr_lock); }if (lock_flags & (XFS_IOLOCK_EXCL |XFS_IOLOCK_SHARED )) {if (!(lock_flags &XFS_IOLOCK_SHARED )) -return !!ip->i_iolock.mr_writer; +return ip->i_iolock.mr_writer;return rwsem_is_locked (&ip->i_iolock.mr_lock); }ASSERT (0 ); -return 0 ; +return false; }#endif
fs/xfs/xfs_inode.h +1 -1
@@ -346,7 +346,7 @@ void xfs_ilock(xfs_inode_t *, uint);int xfs_ilock_nowait (xfs_inode_t *,uint );void xfs_iunlock (xfs_inode_t *,uint );void xfs_ilock_demote (xfs_inode_t *,uint ); -int xfs_isilocked (xfs_inode_t *,uint ); +bool xfs_isilocked (xfs_inode_t *,uint );uint xfs_ilock_data_map_shared (struct xfs_inode *);uint xfs_ilock_attr_map_shared (struct xfs_inode *); int xfs_ialloc(struct xfs_trans *, xfs_inode_t *, umode_t,
Signature · 1 line
1.9.1