public inbox for linux-s390@vger.kernel.org
 help / color / mirror / Atom feed
From: Kirill Tkhai <ktkhai@virtuozzo.com>
To: linux-ia64@vger.kernel.org, avagin@virtuozzo.com,
	peterz@infradead.org, heiko.carstens@de.ibm.com, hpa@zytor.com,
	gorcunov@virtuozzo.com, linux-arch@vger.kernel.org,
	linux-s390@vger.kernel.org, x86@kernel.org, mingo@redhat.com,
	mattst88@gmail.com, fenghua.yu@intel.com, arnd@arndb.de,
	ktkhai@virtuozzo.com, ink@jurassic.park.msu.ru,
	tglx@linutronix.de, rth@twiddle.net, tony.luck@intel.com,
	linux-kernel@vger.kernel.org, linux-alpha@vger.kernel.org,
	schwidefsky@de.ibm.com, davem@davemloft.net
Subject: [PATCH 1/7] rwsem-spinlock: Add killable versions of __down_read()
Date: Mon, 19 Jun 2017 21:02:12 +0300	[thread overview]
Message-ID: <149789533283.9059.9829416940494747182.stgit@localhost.localdomain> (raw)
In-Reply-To: <149789463636.9059.16943955939303454611.stgit@localhost.localdomain>

Rename __down_read() in __down_read_common() and teach it
to abort waiting in case of pending signals and killable
state argument passed.

Note, that we shouldn't wake anybody up in EINTR path, as:

We check for signal_pending_state() after (!waiter.task)
test and under spinlock. So, current task wasn't able to
be woken up. It may be in two cases: a writer is owner
of the sem, or a writer is a first waiter of the sem.

If a writer is owner of the sem, no one else may work
with it in parallel. It will wake somebody, when it
call up_write() or downgrade_write().

If a writer is the first waiter, it will be woken up,
when the last active reader releases the sem, and
sem->count became 0.

Also note, that set_current_state() may be moved down
to schedule() (after !waiter.task check), as all
assignments in this type of semaphore (including wake_up),
occur under spinlock, so we can't miss anything.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 include/linux/rwsem-spinlock.h  |    1 +
 kernel/locking/rwsem-spinlock.c |   37 ++++++++++++++++++++++++++++---------
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/include/linux/rwsem-spinlock.h b/include/linux/rwsem-spinlock.h
index ae0528b834cd..e784761a4443 100644
--- a/include/linux/rwsem-spinlock.h
+++ b/include/linux/rwsem-spinlock.h
@@ -32,6 +32,7 @@ struct rw_semaphore {
 #define RWSEM_UNLOCKED_VALUE		0x00000000
 
 extern void __down_read(struct rw_semaphore *sem);
+extern int __must_check __down_read_killable(struct rw_semaphore *sem);
 extern int __down_read_trylock(struct rw_semaphore *sem);
 extern void __down_write(struct rw_semaphore *sem);
 extern int __must_check __down_write_killable(struct rw_semaphore *sem);
diff --git a/kernel/locking/rwsem-spinlock.c b/kernel/locking/rwsem-spinlock.c
index 20819df98125..0848634c5512 100644
--- a/kernel/locking/rwsem-spinlock.c
+++ b/kernel/locking/rwsem-spinlock.c
@@ -126,7 +126,7 @@ __rwsem_wake_one_writer(struct rw_semaphore *sem)
 /*
  * get a read lock on the semaphore
  */
-void __sched __down_read(struct rw_semaphore *sem)
+int __sched __down_read_common(struct rw_semaphore *sem, int state)
 {
 	struct rwsem_waiter waiter;
 	unsigned long flags;
@@ -140,8 +140,6 @@ void __sched __down_read(struct rw_semaphore *sem)
 		goto out;
 	}
 
-	set_current_state(TASK_UNINTERRUPTIBLE);
-
 	/* set up my own style of waitqueue */
 	waiter.task = current;
 	waiter.type = RWSEM_WAITING_FOR_READ;
@@ -149,20 +147,41 @@ void __sched __down_read(struct rw_semaphore *sem)
 
 	list_add_tail(&waiter.list, &sem->wait_list);
 
-	/* we don't need to touch the semaphore struct anymore */
-	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
-
 	/* wait to be given the lock */
 	for (;;) {
 		if (!waiter.task)
 			break;
+		if (signal_pending_state(state, current))
+			goto out_nolock;
+		set_current_state(state);
+		raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
 		schedule();
-		set_current_state(TASK_UNINTERRUPTIBLE);
+		raw_spin_lock_irqsave(&sem->wait_lock, flags);
 	}
 
-	__set_current_state(TASK_RUNNING);
+	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  out:
-	;
+	return 0;
+
+out_nolock:
+	/*
+	 * We didn't take the lock, so that there is a writer, which
+	 * is owner or the first waiter of the sem. If it's a waiter,
+	 * it will be woken by current owner. Not need to wake anybody.
+	 */
+	list_del(&waiter.list);
+	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
+	return -EINTR;
+}
+
+void __sched __down_read(struct rw_semaphore *sem)
+{
+	__down_read_common(sem, TASK_UNINTERRUPTIBLE);
+}
+
+int __sched __down_read_killable(struct rw_semaphore *sem)
+{
+	return __down_read_common(sem, TASK_KILLABLE);
 }
 
 /*

  reply	other threads:[~2017-06-19 18:02 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20170619180200.9jzmDGDBKZHdfFKf3iCHe7N7UhqaxG-FfpPV_v0g52U@z>
2017-06-19 18:02 ` [PATCH 0/7] rwsem: Implement down_read_killable() Kirill Tkhai
2017-06-19 18:02   ` Kirill Tkhai [this message]
2017-06-19 18:02   ` [PATCH 2/7] rwsem-spinlock: Add killable versions of rwsem_down_read_failed() Kirill Tkhai
2017-07-06  8:04     ` Peter Zijlstra
2017-07-06  9:45       ` Kirill Tkhai
2017-06-19 18:02   ` [PATCH 3/7] alpha: Add __down_read_killable() Kirill Tkhai
2017-06-19 18:02   ` [PATCH 4/7] ia64: " Kirill Tkhai
2017-06-19 18:03   ` [PATCH 5/7] s390: " Kirill Tkhai
2017-06-19 18:03   ` [PATCH 6/7] x86: " Kirill Tkhai
2017-06-19 18:03   ` [PATCH 7/7] rwsem: Add down_read_killable() Kirill Tkhai
2017-06-19 20:27   ` [PATCH 0/7] rwsem: Implement down_read_killable() David Rientjes
2017-06-20  8:30     ` David Howells
2017-06-20 10:36       ` Kirill Tkhai

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=149789533283.9059.9829416940494747182.stgit@localhost.localdomain \
    --to=ktkhai@virtuozzo.com \
    --cc=arnd@arndb.de \
    --cc=avagin@virtuozzo.com \
    --cc=davem@davemloft.net \
    --cc=fenghua.yu@intel.com \
    --cc=gorcunov@virtuozzo.com \
    --cc=heiko.carstens@de.ibm.com \
    --cc=hpa@zytor.com \
    --cc=ink@jurassic.park.msu.ru \
    --cc=linux-alpha@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-ia64@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mattst88@gmail.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rth@twiddle.net \
    --cc=schwidefsky@de.ibm.com \
    --cc=tglx@linutronix.de \
    --cc=tony.luck@intel.com \
    --cc=x86@kernel.org \
    /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