From patchwork Tue Feb 2 20:19:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Hocko X-Patchwork-Id: 577575 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 4BE99140784 for ; Wed, 3 Feb 2016 07:21:28 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933971AbcBBUUs (ORCPT ); Tue, 2 Feb 2016 15:20:48 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:34788 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933706AbcBBUTx (ORCPT ); Tue, 2 Feb 2016 15:19:53 -0500 Received: by mail-wm0-f66.google.com with SMTP id p63so4054969wmp.1; Tue, 02 Feb 2016 12:19:52 -0800 (PST) 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=vWPLipEgvQ39BTz3xJb6QRuM56boPNZa77SUhPwTeC8=; b=IMPXkuG0gSsbsB99GEVkGuzNOasDB+AEimSq0UHcT0HspzE0gMvqnhjSdxmtLGf/9L ucIXWgaUZMmrtFqIqlqDJXLiSuz3ZyZAqLI2jrD1CGDVkNcLWIb0NUbyli2qgJgcMPa/ hqDZ5MpBzdnRc/Z3i2f/n43ZuPSo0Hq0KUZK9uS4awEbrtQgQzvm7ak2o1ukYFzxRd+U PAVzvKiJhTJ1wZC/C6HDzDAXEP9ij+e7JiIGm67bAryPJyHKiaAXoGLGzGLg8wBMMUZ6 Uqb9cjPDQSnns/8/YxSr9dFRbz32IOZT1XkSkKZJNNzGRdPHABBCLlHl9n5TwGKaPoUh aaUA== X-Gm-Message-State: AG10YORJPggvBlKO1iXCeZu75SCaiABalgeXZRuxiCxGF59DOnVv8+d2nYZOLiCJt1cb6Q== X-Received: by 10.28.217.74 with SMTP id q71mr19560808wmg.68.1454444391905; Tue, 02 Feb 2016 12:19:51 -0800 (PST) Received: from tiehlicka.suse.cz (ip-86-49-65-8.net.upcbroadband.cz. [86.49.65.8]) by smtp.gmail.com with ESMTPSA id l7sm3099511wjx.14.2016.02.02.12.19.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 02 Feb 2016 12:19:51 -0800 (PST) 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: [RFC 12/12] locking, rwsem: provide down_write_killable Date: Tue, 2 Feb 2016 21:19:29 +0100 Message-Id: <1454444369-2146-13-git-send-email-mhocko@kernel.org> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1454444369-2146-1-git-send-email-mhocko@kernel.org> References: <1454444369-2146-1-git-send-email-mhocko@kernel.org> Sender: sparclinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: sparclinux@vger.kernel.org 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 c57e424d914b..f6bd57c0784b 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)