mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Waiman Long <longman@redhat.com>
To: yongli-oc <yongli-oc@zhaoxin.com>,
	peterz@infradead.org, mingo@redhat.com, will@kernel.org,
	boqun.feng@gmail.com
Cc: linux-kernel@vger.kernel.org, yongli@zhaoxin.com,
	louisqi@zhaoxin.com, cobechen@zhaoxin.com,
	jiangbowang@zhaoxin.com
Subject: Re: [PATCH 2/4] locking/osq_lock: Turn to dynamic switch function from osq_lock/unlock.
Date: Sat, 14 Sep 2024 12:06:05 -0400	[thread overview]
Message-ID: <74f04210-9f95-483e-a554-460140555cca@redhat.com> (raw)
In-Reply-To: <20240914085327.32912-3-yongli-oc@zhaoxin.com>


On 9/14/24 04:53, yongli-oc wrote:
> To support numa-aware osq lock, the struct optimistic_spin_queue
> is accessed as three members, numa_enable, index, tail16, by union.
> The size of the struct is the same as before.
> If dynamic numa-ware lock enable, turns to the crossing, x_osq_lock to
> check contention level and starts dynamic switch.
>
> Signed-off-by: yongli-oc <yongli-oc@zhaoxin.com>
> ---
>   include/linux/osq_lock.h  | 33 ++++++++++++++++++++-
>   kernel/locking/osq_lock.c | 62 +++++++++++++++++++++++++++++++++++++--
>   2 files changed, 91 insertions(+), 4 deletions(-)
>
> diff --git a/include/linux/osq_lock.h b/include/linux/osq_lock.h
> index ea8fb31379e3..37a7bc4ab530 100644
> --- a/include/linux/osq_lock.h
> +++ b/include/linux/osq_lock.h
> @@ -12,14 +12,42 @@ struct optimistic_spin_queue {
>   	 * Stores an encoded value of the CPU # of the tail node in the queue.
>   	 * If the queue is empty, then it's set to OSQ_UNLOCKED_VAL.
>   	 */
> +#ifdef CONFIG_LOCK_SPIN_ON_OWNER_NUMA
> +	union {
> +		atomic_t tail;
> +		u32 val;
> +#ifdef __LITTLE_ENDIAN
> +		struct {
> +			u16 tail16;
> +			u8 index;
> +			u8 numa_enable;
> +		};
> +#else
> +		struct {
> +			u8 numa_enable;
> +			u8 index;
> +			u16 tail16;
> +		};
> +#endif
> +	};
> +#else
>   	atomic_t tail;
> +#endif
>   };
>   
>   #define OSQ_UNLOCKED_VAL (0)
>   
>   /* Init macro and function. */
> +#ifdef CONFIG_LOCK_SPIN_ON_OWNER_NUMA
> +
> +#define OSQ_LOCK_UNLOCKED { .tail = ATOMIC_INIT(OSQ_UNLOCKED_VAL) }
> +
> +#else
> +
>   #define OSQ_LOCK_UNLOCKED { ATOMIC_INIT(OSQ_UNLOCKED_VAL) }
>   
> +#endif
> +
>   static inline void osq_lock_init(struct optimistic_spin_queue *lock)
>   {
>   	atomic_set(&lock->tail, OSQ_UNLOCKED_VAL);
> @@ -28,9 +56,12 @@ static inline void osq_lock_init(struct optimistic_spin_queue *lock)
>   extern bool osq_lock(struct optimistic_spin_queue *lock);
>   extern void osq_unlock(struct optimistic_spin_queue *lock);
>   
> +#ifdef CONFIG_LOCK_SPIN_ON_OWNER_NUMA
> +extern bool osq_is_locked(struct optimistic_spin_queue *lock);
> +#else
>   static inline bool osq_is_locked(struct optimistic_spin_queue *lock)
>   {
>   	return atomic_read(&lock->tail) != OSQ_UNLOCKED_VAL;
>   }
> -
> +#endif
>   #endif
> diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
> index 75a6f6133866..a7b516939e00 100644
> --- a/kernel/locking/osq_lock.c
> +++ b/kernel/locking/osq_lock.c
> @@ -2,7 +2,10 @@
>   #include <linux/percpu.h>
>   #include <linux/sched.h>
>   #include <linux/osq_lock.h>
> -
> +#ifdef CONFIG_LOCK_SPIN_ON_OWNER_NUMA
> +#include "numa.h"
> +#include "numa_osq.h"
> +#endif

These header files are defined in patch 3. You need to rethink about 
patch ordering in order not to break bisection.


>   /*
>    * An MCS like lock especially tailored for optimistic spinning for sleeping
>    * lock implementations (mutex, rwsem, etc).
> @@ -12,12 +15,34 @@
>    * spinning.
>    */
>   
> +#ifdef CONFIG_LOCK_SPIN_ON_OWNER_NUMA
> +DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
> +/*
> + * We use the value 0 to represent "no CPU", thus the encoded value
> + * will be the CPU number incremented by 1.
> + */
> +inline int encode_cpu(int cpu_nr)
> +{
> +	return cpu_nr + 1;
> +}
> +
> +inline int node_cpu(struct optimistic_spin_node *node)
> +{
> +	return node->cpu - 1;
> +}
> +
> +inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
> +{
> +	int cpu_nr = encoded_cpu_val - 1;
> +
> +	return per_cpu_ptr(&osq_node, cpu_nr);
> +}
> +#else
>   struct optimistic_spin_node {
>   	struct optimistic_spin_node *next, *prev;
>   	int locked; /* 1 if lock acquired */
>   	int cpu; /* encoded CPU # + 1 value */
>   };
> -
>   static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
>   
>   /*
> @@ -40,6 +65,7 @@ static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
>   
>   	return per_cpu_ptr(&osq_node, cpu_nr);
>   }
> +#endif
>   
>   /*
>    * Get a stable @node->next pointer, either for unlock() or unqueue() purposes.
> @@ -97,6 +123,14 @@ bool osq_lock(struct optimistic_spin_queue *lock)
>   	int curr = encode_cpu(smp_processor_id());
>   	int old;
>   
> +#ifdef CONFIG_LOCK_SPIN_ON_OWNER_NUMA
> +	if (unlikely(enable_zx_numa_osq_lock > 1)) {
> +		node->numa = 1;
> +		return x_osq_lock(lock);
> +	}
> +	node->numa = 0;
> +#endif
> +
>   	node->locked = 0;
>   	node->next = NULL;
>   	node->cpu = curr;
> @@ -108,6 +142,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
>   	 * the lock tail.
>   	 */
>   	old = atomic_xchg(&lock->tail, curr);
> +#ifdef CONFIG_LOCK_SPIN_ON_OWNER_NUMA
> +	if (enable_zx_numa_osq_lock > 0)
> +	//enable means all cpu cores are less tan 65534.
> +		old = old & 0xffff;
> +#endif
>   	if (old == OSQ_UNLOCKED_VAL)
>   		return true;
>   
> @@ -212,6 +251,14 @@ void osq_unlock(struct optimistic_spin_queue *lock)
>   	struct optimistic_spin_node *node, *next;
>   	int curr = encode_cpu(smp_processor_id());
>   
> +	node = this_cpu_ptr(&osq_node);
> +
> +#ifdef CONFIG_LOCK_SPIN_ON_OWNER_NUMA
> +	if (unlikely(enable_zx_numa_osq_lock > 1 &&
> +		node->numa == 1))
> +		return x_osq_unlock(lock);
> +#endif
> +
>   	/*
>   	 * Fast path for the uncontended case.
>   	 */
> @@ -222,7 +269,6 @@ void osq_unlock(struct optimistic_spin_queue *lock)
>   	/*
>   	 * Second most likely case.
>   	 */
> -	node = this_cpu_ptr(&osq_node);
>   	next = xchg(&node->next, NULL);
>   	if (next) {
>   		WRITE_ONCE(next->locked, 1);
> @@ -233,3 +279,13 @@ void osq_unlock(struct optimistic_spin_queue *lock)
>   	if (next)
>   		WRITE_ONCE(next->locked, 1);
>   }
> +#ifdef CONFIG_LOCK_SPIN_ON_OWNER_NUMA
> +bool osq_is_locked(struct optimistic_spin_queue *lock)
> +{
> +	if (unlikely(enable_zx_numa_osq_lock > 1))
> +		return x_osq_is_locked(lock);
> +	return atomic_read(&lock->tail) != OSQ_UNLOCKED_VAL;
> +}
> +#endif
> +
> +


  reply	other threads:[~2024-09-14 16:06 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-14  8:53 [PATCH 0/4] locking/osq_lock: Update osq_lock to dynamic yongli-oc
2024-09-14  8:53 ` [PATCH 1/4] locking/osq_lock: The Kconfig for dynamic numa-aware osq lock yongli-oc
2024-09-14  8:53 ` [PATCH 2/4] locking/osq_lock: Turn to dynamic switch function from osq_lock/unlock yongli-oc
2024-09-14 16:06   ` Waiman Long [this message]
2024-09-19  9:32     ` yongli-os
2024-09-14  8:53 ` [PATCH 3/4] locking/osq_lock: From x_osq_lock/unlock to numa-aware lock/unlock yongli-oc
2024-09-14 17:11   ` Waiman Long
2024-09-19  9:35     ` yongli-os
2024-09-15 18:44   ` Uros Bizjak
2024-09-15 21:06     ` Waiman Long
2024-09-14  8:53 ` [PATCH 4/4] locking/osq_lock: The numa-aware lock memory prepare, assign and cleanup yongli-oc
2024-09-14 17:21   ` Waiman Long
2024-09-19  9:41     ` yongli-os
2024-09-19 11:28       ` Waiman Long
2024-09-15 16:43   ` kernel test robot
2024-09-30  7:23 ` [PATCH v2 0/3] locking/osq_lock: Update osq_lock to dynamic yongli-oc
2024-09-30  7:23   ` [PATCH v2 1/3] locking/osq_lock: The Kconfig for dynamic numa-aware osq lock yongli-oc
2024-09-30  7:23   ` [PATCH v2 2/3] locking/osq_lock: Define osq by union to support dynamic numa-aware lock yongli-oc
2024-09-30  7:23   ` [PATCH v2 3/3] locking/osq_lock: Turn from 2-byte osq_lock/unlock to numa lock/unlock yongli-oc

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=74f04210-9f95-483e-a554-460140555cca@redhat.com \
    --to=longman@redhat.com \
    --cc=boqun.feng@gmail.com \
    --cc=cobechen@zhaoxin.com \
    --cc=jiangbowang@zhaoxin.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=louisqi@zhaoxin.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=will@kernel.org \
    --cc=yongli-oc@zhaoxin.com \
    --cc=yongli@zhaoxin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®