From patchwork Mon Feb 10 20:46:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 11374051 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 357861395 for ; Mon, 10 Feb 2020 20:48:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EBC66214DB for ; Mon, 10 Feb 2020 20:47:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YUSJjiOi" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EBC66214DB Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2CB516B016C; Mon, 10 Feb 2020 15:47:59 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 27B736B016D; Mon, 10 Feb 2020 15:47:59 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 191D06B016E; Mon, 10 Feb 2020 15:47:59 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0239.hostedemail.com [216.40.44.239]) by kanga.kvack.org (Postfix) with ESMTP id F3E1E6B016C for ; Mon, 10 Feb 2020 15:47:58 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 7850E181AEF07 for ; Mon, 10 Feb 2020 20:47:58 +0000 (UTC) X-FDA: 76475404236.09.sort17_62b0b0fb4d624 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,longman@redhat.com,:peterz@infradead.org:mingo@redhat.com:will@kernel.org:cl@linux.com:penberg@kernel.org:rientjes@google.com:iamjoonsoo.kim@lge.com:akpm@linux-foundation.org:linux-kernel@vger.kernel.org::longman@redhat.com,RULES_HIT:30012:30054:30070:30090,0,RBL:205.139.110.120:@redhat.com:.lbl8.mailshell.net-62.18.0.100 66.10.201.10,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: sort17_62b0b0fb4d624 X-Filterd-Recvd-Size: 4217 Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by imf45.hostedemail.com (Postfix) with ESMTP for ; Mon, 10 Feb 2020 20:47:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581367677; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc; bh=yPQQ4pseeSMJlwHO9Wp9JJRVrfULoCXznNOttcSYqHw=; b=YUSJjiOiy8p8NaEpWEC2rWyQlOj3drGsM/HSYKfo5Z7iZoZEAvhU5T67Y9ru9aazvGzBsa wuiSUTT42v5L4NGXivaRKVseXuU15/s+ol70ckM+DUJoZfG7lInL62eviCHpkw4S/RZFDg Ogo5ONCj0PvUcI9MbO3Z4GKdqIbiaGc= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-298-YbUKrn-uN-2QeyOuRoMS9Q-1; Mon, 10 Feb 2020 15:47:55 -0500 X-MC-Unique: YbUKrn-uN-2QeyOuRoMS9Q-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EB5A610054E3; Mon, 10 Feb 2020 20:47:53 +0000 (UTC) Received: from llong.com (dhcp-17-59.bos.redhat.com [10.18.17.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7B92987B01; Mon, 10 Feb 2020 20:47:49 +0000 (UTC) From: Waiman Long To: Peter Zijlstra , Ingo Molnar , Will Deacon , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Waiman Long Subject: [PATCH 0/3] locking/mutex: Add mutex_timed_lock() to solve potential deadlock problems Date: Mon, 10 Feb 2020 15:46:48 -0500 Message-Id: <20200210204651.21674-1-longman@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: When writing to the some of the sysctl parameters or sysfs files, locks may be taken in an order that is different from other parts of the kernel. As a result, lockdep may complain about circular locking dependency indicating that deadlock may actually happen in some corner cases. Patch 3 shows an example of this in the sysfs files of the slub allocator. It is typically hard to change the locking order in many cases. One possible solution is to use a trylock loop. That is considered inelegant and it is hard to control the actual wait time. An alternative solution proposed by this patchset is to add a new mutex_timed_lock() call that allows an additional timeout argument. This function will return an error code if timeout happens. The use of this new API will prevent deadlock from happening while allowing the task to wait a sufficient period of time before giving up. The goal of this new API is to prevent deadlock from happening, so timeout accuracy is not high on the priority list. A coarse-grained and easily understood millisecond based integer timeout argument is used. That is somewhat different from the rt_mutex_timed_lock() function where a more precise but complex hrtimer_sleeper argument is used. On a 4-socket 128-thread x86-64 running a 128-thread mutex locking microbenchmark with 1ms timeout, the output of the microbenchmark were: Running locktest with mutex [runtime = 10s, load = 1] Threads = 128, Min/Mean/Max = 247,667/601,134/1,621,145 Threads = 128, Total Rate = 7,694 kop/s; Percpu Rate = 60 kop/s The corresponding mutex locking events were: mutex_handoff=2032 mutex_optspin=3486239 mutex_sleep=2047 mutex_slowpath=3626 mutex_timeout=294 Waiman Long (3): locking/mutex: Add mutex_timed_lock() locking/mutex: Enable some lock event counters mm/slub: Fix potential deadlock problem in slab_attr_store() include/linux/mutex.h | 3 + kernel/locking/lock_events_list.h | 9 +++ kernel/locking/mutex.c | 114 +++++++++++++++++++++++++++--- mm/slub.c | 7 +- 4 files changed, 123 insertions(+), 10 deletions(-)