[PATCH v1 2/2] locking/rwsem: Make reader optimistic spinning optional

Bongkyu Kim posted 2 patches 2 years, 3 months ago
There is a newer version of this series
[PATCH v1 2/2] locking/rwsem: Make reader optimistic spinning optional
Posted by Bongkyu Kim 2 years, 3 months ago
Disable reader optimistic spinning by default. And, can enable it
by "rwsem.opt_rspin" cmdline.

Also, fix compile error without CONFIG_RWSEM_SPIN_ON_OWNER (reported by
kernel test robot)

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202306010043.VJHcuCnb-lkp@intel.com/
Signed-off-by: Bongkyu Kim <bongkyu7.kim@samsung.com>
---
 .../admin-guide/kernel-parameters.txt          |  9 +++++++++
 kernel/locking/rwsem.c                         | 18 +++++++++++++++++-
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 772b54df084b..adf16a07fe4d 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -5605,6 +5605,15 @@
 
 	rw		[KNL] Mount root device read-write on boot
 
+	rwsem.opt_rspin [KNL]
+			Use rwsem reader optimistic spinning. Reader optimistic
+			spinning is helpful when the reader critical section is
+			short and there aren't that many readers around.
+			For example, enable this option may improve performance
+			in mobile workload that there're not many readers, but
+			may reduce performance in server workload that there're
+			many readers.
+
 	S		[KNL] Run init in single mode
 
 	s390_iommu=	[HW,S390]
diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
index 9c0462d515c1..de3f173d94d4 100644
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -117,6 +117,19 @@
 # define DEBUG_RWSEMS_WARN_ON(c, sem)
 #endif
 
+#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
+static bool rwsem_opt_rspin;
+
+static int __init opt_rspin(char *str)
+{
+        rwsem_opt_rspin = true;
+
+        return 0;
+}
+
+early_param("rwsem.opt_rspin", opt_rspin);
+#endif
+
 /*
  * On 64-bit architectures, the bit definitions of the count are:
  *
@@ -1083,7 +1096,7 @@ static inline bool rwsem_reader_phase_trylock(struct rw_semaphore *sem,
 	return false;
 }
 
-static inline bool rwsem_no_spinners(sem)
+static inline bool rwsem_no_spinners(struct rw_semaphore *sem)
 {
 	return false;
 }
@@ -1157,6 +1170,9 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
 		return sem;
 	}
 
+	if (!IS_ENABLED(CONFIG_RWSEM_SPIN_ON_OWNER) || !rwsem_opt_rspin)
+		goto queue;
+
 	/*
 	 * Save the current read-owner of rwsem, if available, and the
 	 * reader nonspinnable bit.
-- 
2.36.1
Re: [PATCH v1 2/2] locking/rwsem: Make reader optimistic spinning optional
Posted by Waiman Long 2 years, 3 months ago
On 8/30/23 23:00, Bongkyu Kim wrote:
> Disable reader optimistic spinning by default. And, can enable it
> by "rwsem.opt_rspin" cmdline.
>
> Also, fix compile error without CONFIG_RWSEM_SPIN_ON_OWNER (reported by
> kernel test robot)
>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202306010043.VJHcuCnb-lkp@intel.com/
> Signed-off-by: Bongkyu Kim <bongkyu7.kim@samsung.com>
> ---
>   .../admin-guide/kernel-parameters.txt          |  9 +++++++++
>   kernel/locking/rwsem.c                         | 18 +++++++++++++++++-
>   2 files changed, 26 insertions(+), 1 deletion(-)
>
> diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
> index 772b54df084b..adf16a07fe4d 100644
> --- a/Documentation/admin-guide/kernel-parameters.txt
> +++ b/Documentation/admin-guide/kernel-parameters.txt
> @@ -5605,6 +5605,15 @@
>   
>   	rw		[KNL] Mount root device read-write on boot
>   
> +	rwsem.opt_rspin [KNL]
> +			Use rwsem reader optimistic spinning. Reader optimistic
> +			spinning is helpful when the reader critical section is
> +			short and there aren't that many readers around.
> +			For example, enable this option may improve performance
> +			in mobile workload that there're not many readers, but
> +			may reduce performance in server workload that there're
> +			many readers.
> +
>   	S		[KNL] Run init in single mode
>   
>   	s390_iommu=	[HW,S390]
> diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
> index 9c0462d515c1..de3f173d94d4 100644
> --- a/kernel/locking/rwsem.c
> +++ b/kernel/locking/rwsem.c
> @@ -117,6 +117,19 @@
>   # define DEBUG_RWSEMS_WARN_ON(c, sem)
>   #endif
>   
> +#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
> +static bool rwsem_opt_rspin;

You need to move this definition out of the "ifdef 
CONFIG_RWSEM_SPIN_ON_OWNER" block or a compilation error in 
rwsem_down_read_slowpath() may happen if CONFIG_RWSEM_SPIN_ON_OWNER 
isn't defined. You may also add __ro_after_init to rwsem_opt_rspin.

Cheers,
Longman


> +
> +static int __init opt_rspin(char *str)
> +{
> +        rwsem_opt_rspin = true;
> +
> +        return 0;
> +}
> +
> +early_param("rwsem.opt_rspin", opt_rspin);
> +#endif
> +
>   /*
>    * On 64-bit architectures, the bit definitions of the count are:
>    *
> @@ -1083,7 +1096,7 @@ static inline bool rwsem_reader_phase_trylock(struct rw_semaphore *sem,
>   	return false;
>   }
>   
> -static inline bool rwsem_no_spinners(sem)
> +static inline bool rwsem_no_spinners(struct rw_semaphore *sem)
>   {
>   	return false;
>   }
> @@ -1157,6 +1170,9 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
>   		return sem;
>   	}
>   
> +	if (!IS_ENABLED(CONFIG_RWSEM_SPIN_ON_OWNER) || !rwsem_opt_rspin)
> +		goto queue;
> +
>   	/*
>   	 * Save the current read-owner of rwsem, if available, and the
>   	 * reader nonspinnable bit.