Skip to contentSkip to conversation

Shrinking rwsem

RFC In review Matthew Wilcox (Oracle) 4 participants 11 messages Feb 17 – Mar 4

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

cover Shrinking rwsem

Some of Christoph's recent work has pointed out that we're achingly close to being able to squeeze one more inode into each slab [1]. There are currently three rwsems embedded in struct inode -- invalidate_lock, i_mmap_rwsem and i_rwsem, so this saves us 24 bytes. Of course, it'll shrink a lot of other data structures that embed an rwsem (anon_vma and mm_struct come to mind).

We can do the same trick to struct mutex, but I thought I'd send this out to see how people feel about the extra code complexity to make these savings before investing any effort in doing that.

Maybe the performance bots will come back with some numbers, although they've been weirdly sensitive to rwsem alignment in the past, so I'm not sure I'd believe their numbers.

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

Matthew Wilcox (Oracle) (1):
rwsem: Shrink rwsem by one pointer

 include/linux/rwsem.h  |  8 ++---
 kernel/locking/rwsem.c | 74 +++++++++++++++++++++++++++++++-----------
 2 files changed, 59 insertions(+), 23 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

RFC 1/1 rwsem: Shrink rwsem by one pointer

Instead of embedding a list_head in struct rw_semaphore, store a pointer to the first waiter. The list of waiters remains a doubly linked list so we can efficiently add to the tail of the list, remove from the front (or middle) of the list.

Some of the list manipulation becomes more complicated, but it's a reasonable tradeoff on the slow paths to shrink some core data structures like struct inode.

---
 include/linux/rwsem.h  |  8 ++---
 kernel/locking/rwsem.c | 74 +++++++++++++++++++++++++++++++-----------
 2 files changed, 59 insertions(+), 23 deletions(-)
include/linux/rwsem.h +4 -4
index f1aaf676a874..1771c96a01d2 100644
@@ -57,7 +57,7 @@ struct rw_semaphore {
 	struct optimistic_spin_queue osq; /* spinner MCS lock */
 #endif
 	raw_spinlock_t wait_lock;
-	struct list_head wait_list;
+	struct rwsem_waiter *first_waiter;
 #ifdef CONFIG_DEBUG_RWSEMS
 	void *magic;
 #endif
@@ -104,7 +104,7 @@ static inline void rwsem_assert_held_write_nolockdep(const struct rw_semaphore *
 	  .owner = ATOMIC_LONG_INIT(0),				\
 	  __RWSEM_OPT_INIT(name)				\
 	  .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\
-	  .wait_list = LIST_HEAD_INIT((name).wait_list),	\
+	  .first_waiter = NULL,					\
 	  __RWSEM_DEBUG_INIT(name)				\
 	  __RWSEM_DEP_MAP_INIT(name) }
 
@@ -127,9 +127,9 @@ do {								\
  * rwsem to see if somebody from an incompatible type is wanting access to the
  * lock.
  */
-static inline int rwsem_is_contended(struct rw_semaphore *sem)
+static inline bool rwsem_is_contended(struct rw_semaphore *sem)
 {
-	return !list_empty(&sem->wait_list);
+	return sem->first_waiter != NULL;
 }
 
 #if defined(CONFIG_DEBUG_RWSEMS) || defined(CONFIG_DETECT_HUNG_TASK_BLOCKER)
kernel/locking/rwsem.c +55 -19

183 lines not on this page · raw message

Signature · 1 line
2.47.3
#2 lore raw

Re: [RFC 1/1] rwsem: Shrink rwsem by one pointer

Quoted 9 lines · Matthew Wilcox (Oracle)

Instead of embedding a list_head in struct rw_semaphore, store a pointer to the first waiter. The list of waiters remains a doubly linked list so we can efficiently add to the tail of the list, remove from the front (or middle) of the list.

Some of the list manipulation becomes more complicated, but it's a reasonable tradeoff on the slow paths to shrink some core data structures like struct inode.

I like this, but I have to say that I dislike how rwsem_add_waiter() in particular ends up looking.

Not because it's horrible on its own, but when you look at the call-sites, that function ends up being entirely pointless.

It does two things:

- it asserts that the wait_lock is held

- it now checks whether the new waiter is the first one and does two
different things depending on that

And lookie here: there are exactly two call sites, and both of them are immediately preceded by a

        raw_spin_lock_irq(&sem->wait_lock);

and one of them then goes on to check whether there are no waiters before it calls rwsem_add_waiter().

And the other? Immediately *after* the call, it does

        /* we're now waiting on the lock */
        if (rwsem_first_waiter(sem) != &waiter) {

so that other call-site *ALSO* basically ends up having special code for "am I the first writer"?

Put another way: this helper function seems all kinds of pointless and it got worse in this iteration, because the pointlessness is now in some of that added complexity.

So considering that there iareonly two callers. and both of them *already* fundamentally know about this whole "first waiter is special" situation, that helper is actually the opposite of a helper. It's actually hiding what is going on, and just making code generation worse.

If we want helper functions, can we please just make them be "add_first_waiter()" and "add_to_waiter_list()", and make them actually be what the callers need and want?

Somewhat similarly, I also reacted to this part:

    -#define rwsem_first_waiter(sem) \
    -       list_first_entry(&sem->wait_list, struct rwsem_waiter, list)
    +#define rwsem_first_waiter(sem)        sem->first_waiter

that rwsem_first_waiter() macro used to make sense as a syntactic helper function. But now it really doesn't. It is literally more typing and *less* legible than just accessing that new "sem->first_waiter" field.

In other words, I like the direction you're taking, and I think the code already is set up to have that whole "first waiter" thing that your patch just makes much more explicit and obvious. That all argues that yes, having a "first waiter" pointer instead of a "struct list_head" is a good change.

But I think some of those existing helpers were because we did *not* use to do it that better way, and they were designed for the old world order, and they no longer make any sense with your changes.

Hmm?

Linus

#3 lore raw

Re: [RFC 1/1] rwsem: Shrink rwsem by one pointer

Quoted 5 lines · Linus Torvalds

I like this, but I have to say that I dislike how rwsem_add_waiter() in particular ends up looking.

Not because it's horrible on its own, but when you look at the call-sites, that function ends up being entirely pointless.

I confess, I didn't look at the callers. Good catch; I've integrated your suggestion and it looks better.

I was most concerned with just how ugly __rwsem_del_waiter() looked. I had a good think about it and have an improved version.

Quoted 10 lines

Somewhat similarly, I also reacted to this part:

    -#define rwsem_first_waiter(sem) \
    -       list_first_entry(&sem->wait_list, struct rwsem_waiter, list)
    +#define rwsem_first_waiter(sem)        sem->first_waiter

that rwsem_first_waiter() macro used to make sense as a syntactic helper function. But now it really doesn't. It is literally more typing and *less* legible than just accessing that new "sem->first_waiter" field.

Yep, I did notice that too, just decided not to fix it. Also taken care of in the next version.

Here's all the changes I made, and I'll post a rolled-up version next.

kernel/locking/rwsem.c +24 -38
index 4226eb0ec5da..16f3db35652a 100644
@@ -341,7 +341,6 @@ struct rwsem_waiter {
 	unsigned long timeout;
 	bool handoff_set;
 };
-#define rwsem_first_waiter(sem)	sem->first_waiter
 
 enum rwsem_wake_type {
 	RWSEM_WAKE_ANY,		/* Wake whatever's at head of wait list */
@@ -364,36 +363,19 @@ enum rwsem_wake_type {
  */
 #define MAX_READERS_WAKEUP	0x100
 
-static inline void
-rwsem_add_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter)
-{
-	struct rwsem_waiter *first = sem->first_waiter;
-	lockdep_assert_held(&sem->wait_lock);
-	if (first) {
-		list_add_tail(&waiter->list, &first->list);
-	} else {
-		INIT_LIST_HEAD(&waiter->list);
-		sem->first_waiter = waiter;
-	}
-	/* caller will set RWSEM_FLAG_WAITERS */
-}
-
 static inline
 bool __rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter)
 {
-	if (sem->first_waiter == waiter) {
-		if (list_empty(&waiter->list)) {
-			sem->first_waiter = NULL;
-			return true;
-		} else {
-			sem->first_waiter = list_first_entry(&waiter->list,
-					struct rwsem_waiter, list);
-			list_del(&waiter->list);
-		}
-	} else {
-		list_del(&waiter->list);
+	if (list_empty(&waiter->list)) {
+		sem->first_waiter = NULL;
+		return true;
 	}
 
+	if (sem->first_waiter == waiter)
+		sem->first_waiter = list_first_entry(&waiter->list,
+				struct rwsem_waiter, list);
+	list_del(&waiter->list);
+
 	return false;
 }
 
@@ -453,7 +435,7 @@ static void rwsem_mark_wake(struct rw_semaphore *sem,
 	 * Take a peek at the queue head waiter such that we can determine
 	 * the wakeup(s) to perform.
 	 */
-	waiter = rwsem_first_waiter(sem);
+	waiter = sem->first_waiter;
 
 	if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
 		if (wake_type == RWSEM_WAKE_ANY) {
@@ -612,8 +594,6 @@ rwsem_del_wake_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter,
 		      struct wake_q_head *wake_q)
 		      __releases(&sem->wait_lock)
 {
-	bool first = rwsem_first_waiter(sem) == waiter;
-
 	wake_q_init(wake_q);
 
 	/*
@@ -621,7 +601,7 @@ rwsem_del_wake_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter,
 	 * the first waiter, we wake up the remaining waiters as they may
 	 * be eligible to acquire or spin on the lock.
 	 */
-	if (rwsem_del_waiter(sem, waiter) && first)
+	if (rwsem_del_waiter(sem, waiter) && sem->first_waiter == waiter)
 		rwsem_mark_wake(sem, RWSEM_WAKE_ANY, wake_q);
 	raw_spin_unlock_irq(&sem->wait_lock);
 	if (!wake_q_empty(wake_q))
@@ -638,7 +618,7 @@ rwsem_del_wake_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter,
 static inline bool rwsem_try_write_lock(struct rw_semaphore *sem,
 					struct rwsem_waiter *waiter)
 {
-	struct rwsem_waiter *first = rwsem_first_waiter(sem);
+	struct rwsem_waiter *first = sem->first_waiter;
 	long count, new;
 
 	lockdep_assert_held(&sem->wait_lock);
@@ -1030,7 +1010,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
 {
 	long adjustment = -RWSEM_READER_BIAS;
 	long rcnt = (count >> RWSEM_READER_SHIFT);
-	struct rwsem_waiter waiter;
+	struct rwsem_waiter waiter, *first;
 	DEFINE_WAKE_Q(wake_q);
 
 	/*
@@ -1071,7 +1051,8 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
 	waiter.handoff_set = false;
 
 	raw_spin_lock_irq(&sem->wait_lock);
-	if (!sem->first_waiter) {
+	first = sem->first_waiter;
+	if (!first) {
 		/*
 		 * In case the wait queue is empty and the lock isn't owned
 		 * by a writer, this reader can exit the slowpath and return
@@ -1087,8 +1068,11 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
 			return sem;
 		}
 		adjustment += RWSEM_FLAG_WAITERS;
+		INIT_LIST_HEAD(&waiter.list);
+		sem->first_waiter = &waiter;
+	} else {
+		list_add_tail(&waiter.list, &first->list);
 	}
-	rwsem_add_waiter(sem, &waiter);
 
 	/* we're now waiting on the lock, but no longer actively locking */
 	count = atomic_long_add_return(adjustment, &sem->count);
@@ -1146,7 +1130,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
 static struct rw_semaphore __sched *
 rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
 {
-	struct rwsem_waiter waiter;
+	struct rwsem_waiter waiter, *first;
 	DEFINE_WAKE_Q(wake_q);
 
 	/* do optimistic spinning and steal lock if possible */
@@ -1165,10 +1149,10 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
 	waiter.handoff_set = false;
 
 	raw_spin_lock_irq(&sem->wait_lock);
-	rwsem_add_waiter(sem, &waiter);
 
-	/* we're now waiting on the lock */
-	if (rwsem_first_waiter(sem) != &waiter) {
+	first = sem->first_waiter;
+	if (first) {
+		list_add_tail(&waiter.list, &first->list);
 		rwsem_cond_wake_waiter(sem, atomic_long_read(&sem->count),
 				       &wake_q);
 		if (!wake_q_empty(&wake_q)) {
@@ -1181,6 +1165,8 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
 			raw_spin_lock_irq(&sem->wait_lock);
 		}
 	} else {
+		INIT_LIST_HEAD(&waiter.list);
+		sem->first_waiter = &waiter;
 		atomic_long_or(RWSEM_FLAG_WAITERS, &sem->count);
 	}
 
#4 lore raw

Re: [RFC 1/1] rwsem: Shrink rwsem by one pointer

Quoted 3 lines · Matthew Wilcox

Here's all the changes I made, and I'll post a rolled-up version next.

Thanks, this all looks good to me.

Linus

maintainer
#5 lore raw

Re: [RFC 1/1] rwsem: Shrink rwsem by one pointer

In reply to Matthew Wilcox
Quoted 2 lines · Matthew Wilcox

Here's all the changes I made, and I'll post a rolled-up version next.

That all seems reasonable.

One small nit:

kernel/locking/rwsem.c enum rwsem_wake_type { · line 376

Quoted 8 lines
kernel/locking/rwsem.c +6 -0
+	if (list_empty(&waiter->list)) {
+		sem->first_waiter = NULL;
+		return true;
 	}
 
+	if (sem->first_waiter == waiter)
+		sem->first_waiter = list_first_entry(&waiter->list,
+				struct rwsem_waiter, list);

Since that's multiple lines, could you wrap in {}, also I tend to prefer cino=(0:0 style wrapping. That is, something like so:

        if (sem->first_waiter == waiter) {
                sem->first_waiter = list_first_entry(&waiter->list,
                                                     struct rwsem_waiter, list);
        }
Quoted 4 lines
kernel/locking/rwsem.c +2 -0
+	list_del(&waiter->list);
+
 	return false;
 }
#6 lore raw

Re: [RFC 1/1] rwsem: Shrink rwsem by one pointer

In reply to Matthew Wilcox

Oh, I already said "this looks good", but I take it back. Looking a bit closer, I noticed a problem:

Quoted 11 lines · Matthew Wilcox
kernel/locking/rwsem.c +0 -2
@@ -612,8 +594,6 @@ rwsem_del_wake_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter,
                      struct wake_q_head *wake_q)
                      __releases(&sem->wait_lock)
 {
-       bool first = rwsem_first_waiter(sem) == waiter;
-
        wake_q_init(wake_q);
 
        /*

kernel/locking/rwsem.c rwsem_del_wake_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter, · line 605

Quoted 6 lines
kernel/locking/rwsem.c +1 -1
@@ -621,7 +601,7 @@ rwsem_del_wake_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter,
         * the first waiter, we wake up the remaining waiters as they may
         * be eligible to acquire or spin on the lock.
         */
-       if (rwsem_del_waiter(sem, waiter) && first)
+       if (rwsem_del_waiter(sem, waiter) && sem->first_waiter == waiter)
                rwsem_mark_wake(sem, RWSEM_WAKE_ANY, wake_q);

This looks wrong.

Notice how "first" used to be done before deletion.

How it's doing that

     sem->first_waiter == waiter

test *after* the deletion, which makes no sense at all, and will never trigger, afaik.

I think that "&& first" was always pointless, because rwsem_del_waiter() would only return true if it was the only waiter, and if it was the only waiter then it was first by definition. But now it's gone from "pointless" to "actively wrong", because now that "first" test will be false.

So I think the whole test should just be deleted. The return value of rwsem_del_waiter() is already the right value.

Anyway, this is all from just looking at the patch, so maybe I missed something, but it does look very wrong.

Linus

#7 lore raw

Re: [RFC 1/1] rwsem: Shrink rwsem by one pointer

Quoted 3 lines · Linus Torvalds

Anyway, this is all from just looking at the patch, so maybe I missed something, but it does look very wrong.

Bah. And immediately after sending, I went "maybe I should look at the code" more closely.

I think my suggestion to just remove the check was right, but the return value of rwsem_del_waiter() needs to be fixed to be the "I used to be the first waiter, but there are other waiters and I updated the first waiter pointer".

Linus

#8 lore raw

Re: [RFC 1/1] rwsem: Shrink rwsem by one pointer

Quoted 6 lines · Linus Torvalds
Quoted 3 lines · Linus Torvalds

Anyway, this is all from just looking at the patch, so maybe I missed something, but it does look very wrong.

Quoted 7 lines

Bah. And immediately after sending, I went "maybe I should look at the code" more closely.

I think my suggestion to just remove the check was right, but the return value of rwsem_del_waiter() needs to be fixed to be the "I used to be the first waiter, but there are other waiters and I updated the first waiter pointer".

I tried to make this work, but it's a bit ugly. rwsem_del_waiter() needs to know whether there are remaining waiters, and rwsem_del_wake_waiter() needs to know whether we deleted the first waiter _and_ there are remaining ones. So we end up returning a tristate from __rwsem_del_waiter() and I find it less clear.

static inline
-bool __rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter) +int __rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter)
{
+ int ret = 1;
+

        if (list_empty(&waiter->list)) {
                sem->first_waiter = NULL;
-               return true;
+               return 0;
        }

- if (sem->first_waiter == waiter)

+       if (sem->first_waiter == waiter) {
                sem->first_waiter = list_first_entry(&waiter->list,
                                struct rwsem_waiter, list);
+               ret = 2;
+       }
        list_del(&waiter->list);

- return false;
+ return ret;
}
[...]
-static inline bool
-rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter) +static inline
+bool rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter)
{
+ int del_case;
+

        lockdep_assert_held(&sem->wait_lock);
-       if (__rwsem_del_waiter(sem, waiter))
-               return true;
-       atomic_long_andnot(RWSEM_FLAG_HANDOFF | RWSEM_FLAG_WAITERS, &sem->count);
-       return false;
+       del_case = __rwsem_del_waiter(sem, waiter);
+       if (del_case > 0)

+ atomic_long_andnot(RWSEM_FLAG_HANDOFF | RWSEM_FLAG_WAITERS,
+ &sem->count);
+ return del_case == 2;
}
[...]
{
- bool first = sem->first_waiter == waiter;
-

        wake_q_init(wake_q);

        /*
-        * If the wait_list isn't empty and the waiter to be deleted is
-        * the first waiter, we wake up the remaining waiters as they may
-        * be eligible to acquire or spin on the lock.
+        * If the deleted waiter was the first one and there are other
+        * waiters, we wake them up as they may be eligible to acquire
+        * or spin on the lock.
         */
-       if (rwsem_del_waiter(sem, waiter) && first)
+       if (rwsem_del_waiter(sem, waiter))
                rwsem_mark_wake(sem, RWSEM_WAKE_ANY, wake_q);

Even if we use a nice enum instead of 0/1/2 for the return value, I don't think this is an improvement. I played around with a couple of other ways to refactor this and didn't come up with anything pretty.

#9 lore raw

Re: [RFC 1/1] rwsem: Shrink rwsem by one pointer

In reply to Matthew Wilcox (Oracle)
Quoted 8 lines · Matthew Wilcox (Oracle)

Instead of embedding a list_head in struct rw_semaphore, store a pointer to the first waiter. The list of waiters remains a doubly linked list so we can efficiently add to the tail of the list, remove from the front (or middle) of the list.

Some of the list manipulation becomes more complicated, but it's a reasonable tradeoff on the slow paths to shrink some core data structures like struct inode.

If the goal is to use only one pointer for the rwsem structure, would it make sense to change list_head to hlist_head for instance? At least we have existing helpers that can be used instead of making our own coding convention here.

Cheers,
Longman

#10 lore raw

Re: [RFC 1/1] rwsem: Shrink rwsem by one pointer

Quoted 6 lines · Waiman Long
Quoted 5 lines · Matthew Wilcox (Oracle)

Instead of embedding a list_head in struct rw_semaphore, store a pointer to the first waiter. The list of waiters remains a doubly linked list so we can efficiently add to the tail of the list, remove from the front (or middle) of the list.

Quoted 8 lines
Quoted 3 lines

Some of the list manipulation becomes more complicated, but it's a reasonable tradeoff on the slow paths to shrink some core data structures like struct inode.

If the goal is to use only one pointer for the rwsem structure, would it make sense to change list_head to hlist_head for instance? At least we have existing helpers that can be used instead of making our own coding convention here.

There's no hlist_add_tail(), and obviously there can't be.

hlist_head.first = node1
node1.pprev = hlist_head.first
node1.next = node2
node2.pprev = node1
node2.next = NULL

now we want to add node3 to the tail. there's no pointer to it, we have to walk the entire chain to find out where to put it.

Whereas with this scheme, we can put it at ->first_waiter.prev. If you want to generalise this way to use list_head, be my guest, but I don't want to do that work (and I don't want this patch to get held up behind a "boil the ocean" approach).