From patchwork Fri Apr 1 11:04:55 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Hocko X-Patchwork-Id: 8722701 Return-Path: X-Original-To: patchwork-linux-sh@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id D1173C0553 for ; Fri, 1 Apr 2016 11:06:13 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F417D203E5 for ; Fri, 1 Apr 2016 11:06:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 11562203C0 for ; Fri, 1 Apr 2016 11:06:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759070AbcDALF6 (ORCPT ); Fri, 1 Apr 2016 07:05:58 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:35199 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758935AbcDALFT (ORCPT ); Fri, 1 Apr 2016 07:05:19 -0400 Received: by mail-wm0-f65.google.com with SMTP id 139so3700102wmn.2; Fri, 01 Apr 2016 04:05:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=qOOSoLcP2ZSjEuJOlTXQEHGfZFJT528ByWpGKgt+LMQ=; b=V+nK2Btw6Xvd69zp0pH1DlvCkztXZB9yWFXkwm3yCkr5ifJ9E18RSiGp/JACO5/oEu r8/GzIk/fNmIVPetBYtyqxAnH2CjgooWbL37FQVFm9EnYMKWVpB6rifki8OfjMOtiIm7 HYsx+749jlUsaJ6gbDz83TdccQerZR2MDqv5gOIP2jLh0oXzaGzjLMdrfQCkwnQZ9Nxw VvQaEHgsF+3Sj1LLKexIs+PesnRceHWZHP6o8z/RLWHkcIF9EAc/LnCC1z1hYr53BjW7 UjL2k7AFItwM+DSdl+MoXOwbFv3UREo2NLuTEO9g+D1TAWy6zOeELEIlgX4QilpltSMA WuTQ== X-Gm-Message-State: AD7BkJLUOVabcRIPrbmvRRZxMKUDG2l9gHyBK/SsCRbWg+NW5A02OWV5oCoXsiKGmQy64w== X-Received: by 10.194.246.194 with SMTP id xy2mr9461214wjc.65.1459508717477; Fri, 01 Apr 2016 04:05:17 -0700 (PDT) Received: from tiehlicka.suse.cz (nat1.scz.suse.com. [213.151.88.250]) by smtp.gmail.com with ESMTPSA id ys9sm13434670wjc.35.2016.04.01.04.05.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 01 Apr 2016 04:05:16 -0700 (PDT) From: Michal Hocko To: LKML Cc: Peter Zijlstra , Ingo Molnar , Thomas Gleixner , "H. Peter Anvin" , "David S. Miller" , Tony Luck , Andrew Morton , Chris Zankel , Max Filippov , x86@kernel.org, linux-alpha@vger.kernel.org, linux-ia64@vger.kernel.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-xtensa@linux-xtensa.org, linux-arch@vger.kernel.org, Michal Hocko Subject: [PATCH 11/11] locking, rwsem: provide down_write_killable Date: Fri, 1 Apr 2016 13:04:55 +0200 Message-Id: <1459508695-14915-12-git-send-email-mhocko@kernel.org> X-Mailer: git-send-email 2.8.0.rc3 In-Reply-To: <1459508695-14915-1-git-send-email-mhocko@kernel.org> References: <1459508695-14915-1-git-send-email-mhocko@kernel.org> Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Michal Hocko Now that all the architectures implement the necessary glue code we can introduce down_write_killable. The only difference wrt. regular down_write is that the slow path waits in TASK_KILLABLE state and the interruption by the fatal signal is reported as -EINTR to the caller. Signed-off-by: Michal Hocko --- include/linux/lockdep.h | 15 +++++++++++++++ include/linux/rwsem.h | 1 + kernel/locking/rwsem.c | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h index 4dca42fd32f5..13a80779b6da 100644 --- a/include/linux/lockdep.h +++ b/include/linux/lockdep.h @@ -446,6 +446,18 @@ do { \ lock_acquired(&(_lock)->dep_map, _RET_IP_); \ } while (0) +#define LOCK_CONTENDED_RETURN(_lock, try, lock) \ +({ \ + int ____err = 0; \ + if (!try(_lock)) { \ + lock_contended(&(_lock)->dep_map, _RET_IP_); \ + ____err = lock(_lock); \ + } \ + if (!____err) \ + lock_acquired(&(_lock)->dep_map, _RET_IP_); \ + ____err; \ +}) + #else /* CONFIG_LOCK_STAT */ #define lock_contended(lockdep_map, ip) do {} while (0) @@ -454,6 +466,9 @@ do { \ #define LOCK_CONTENDED(_lock, try, lock) \ lock(_lock) +#define LOCK_CONTENDED_RETURN(_lock, try, lock) \ + lock(_lock) + #endif /* CONFIG_LOCK_STAT */ #ifdef CONFIG_LOCKDEP diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h index 7d7ae029dac5..d1c12d160ace 100644 --- a/include/linux/rwsem.h +++ b/include/linux/rwsem.h @@ -118,6 +118,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_killable(struct rw_semaphore *sem); /* * trylock for writing -- returns 1 if successful, 0 if contention diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index 205be0ce34de..c817216c1615 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -55,6 +55,25 @@ void __sched down_write(struct rw_semaphore *sem) EXPORT_SYMBOL(down_write); /* + * lock for writing + */ +int __sched down_write_killable(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_killable)) { + rwsem_release(&sem->dep_map, 1, _RET_IP_); + return -EINTR; + } + + rwsem_set_owner(sem); + return 0; +} + +EXPORT_SYMBOL(down_write_killable); + +/* * trylock for writing -- returns 1 if successful, 0 if contention */ int down_write_trylock(struct rw_semaphore *sem)