From patchwork Mon Mar 8 20:54:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zack Rusin X-Patchwork-Id: 12123381 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04254C433E0 for ; Mon, 8 Mar 2021 20:55:02 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9F81E65272 for ; Mon, 8 Mar 2021 20:55:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9F81E65272 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=vmware.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B36C26E3D0; Mon, 8 Mar 2021 20:55:00 +0000 (UTC) Received: from EX13-EDG-OU-002.vmware.com (ex13-edg-ou-002.vmware.com [208.91.0.190]) by gabe.freedesktop.org (Postfix) with ESMTPS id BE2976E875 for ; Mon, 8 Mar 2021 20:54:59 +0000 (UTC) Received: from sc9-mailhost1.vmware.com (10.113.161.71) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Mon, 8 Mar 2021 12:54:57 -0800 Received: from vertex.localdomain (unknown [10.16.119.57]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id 2303F2040A; Mon, 8 Mar 2021 12:54:58 -0800 (PST) From: Zack Rusin To: Subject: [PATCH 1/2] locking/rwsem: Add down_write_interruptible Date: Mon, 8 Mar 2021 15:54:55 -0500 Message-ID: <20210308205456.1317366-2-zackr@vmware.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210308205456.1317366-1-zackr@vmware.com> References: <20210308205456.1317366-1-zackr@vmware.com> MIME-Version: 1.0 Received-SPF: None (EX13-EDG-OU-002.vmware.com: zackr@vmware.com does not designate permitted sender hosts) X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Zijlstra , Ingo Molnar , Will Deacon , dri-devel@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Add an interruptible version of down_write. It's the other side of the already implemented down_read_interruptible. It allows drivers which used custom locking code to support interruptible rw semaphores to switch over to rwsem. Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Will Deacon Cc: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Acked-by: Peter Zijlstra (Intel) --- include/linux/rwsem.h | 1 + kernel/locking/rwsem.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h index 4c715be48717..753ae2cb8677 100644 --- a/include/linux/rwsem.h +++ b/include/linux/rwsem.h @@ -135,6 +135,7 @@ extern int down_read_trylock(struct rw_semaphore *sem); * lock for writing */ extern void down_write(struct rw_semaphore *sem); +extern int __must_check down_write_interruptible(struct rw_semaphore *sem); extern int __must_check down_write_killable(struct rw_semaphore *sem); /* diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index abba5df50006..0eadd20347de 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -1270,6 +1270,11 @@ static inline void __down_write(struct rw_semaphore *sem) __down_write_common(sem, TASK_UNINTERRUPTIBLE); } +static inline int __down_write_interruptible(struct rw_semaphore *sem) +{ + return __down_write_common(sem, TASK_INTERRUPTIBLE); +} + static inline int __down_write_killable(struct rw_semaphore *sem) { return __down_write_common(sem, TASK_KILLABLE); @@ -1408,6 +1413,24 @@ void __sched down_write(struct rw_semaphore *sem) } EXPORT_SYMBOL(down_write); +/* + * interruptible lock for writing + */ +int __sched down_write_interruptible(struct rw_semaphore *sem) +{ + might_sleep(); + rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_); + + if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, + __down_write_interruptible)) { + rwsem_release(&sem->dep_map, _RET_IP_); + return -EINTR; + } + + return 0; +} +EXPORT_SYMBOL(down_write_interruptible); + /* * lock for writing */