Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1017529ybe; Wed, 11 Sep 2019 08:09:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqxPIHSmcWXPrsmrWiMq4ZX+AtNgPU5xXZKsSANKgZ3t1sQqDpxEd85t1O32driZ3M7NwX2w X-Received: by 2002:aa7:c1c5:: with SMTP id d5mr36789592edp.46.1568214585126; Wed, 11 Sep 2019 08:09:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568214585; cv=none; d=google.com; s=arc-20160816; b=idH5Hx86eL3dUPt4w6c8biTmQuT3KwjtIMknRf8pkWc2RREjihWux0xYvCYFtd2PTP hLimFyJTafhvlgptiYUOfaEn9tboXVGW2iYxsWOLq6KBiB2hlNcSwuVCtFWhNEDV0LVa f+zaPQCMnFM91SPmVyCCiWITQMNf3Y7lyuVd9NfHVabyqXPnEKXqfUQasmcI/gH/O4I2 KKgK9g+EwuDALPOQXKxdAHGLWzeewY59eVnaJV5fQYOrAOUQHEI0ejiiwfj2YXd317y1 CKJ+gDjbuUtNWwLuiT3qTJJ6QE4FQkdq1ToGnA1AA0vBu7WdmRNnS8Mb/N/Ojmnc6J2F Iafw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=Di3EugS0/TsUQQWDg5N+RSHNQoEZl7T/F2OiGRCskYY=; b=AsCkL7bfkNv3u3xusK3lozy6OKLG1klyE9BI/Uq/RYBBx51XLV1ZgeT+l8CS2Od+vh VditJmo9Tw5YCjtFFnvuPgQNiYV4z8aTg1izANKYOF/PyKryjyQ6Xb/pPDxZHg9JOL8y tdcNh0vgD1q1x0r7a9u2/OC342jq+n0zO3fFC4DwVjjcZ8ymX5nYM0S8Yn5rHOeDKEh1 TwUzSaxyl6vFVG2eIFy5WILUF84kT4gLgPWXH2X8VWw0dXDBXcwvsKGaoq4yC8Jik1Lk Vh0QNEyET+U5+HCncqC196kKVzavPtkq1E8so3V6SMdwIp5y+zHqK3xcMQP56vNumn9N SLew== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d20si11506276ejc.302.2019.09.11.08.09.20; Wed, 11 Sep 2019 08:09:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728521AbfIKPGX (ORCPT + 99 others); Wed, 11 Sep 2019 11:06:23 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45394 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727581AbfIKPGW (ORCPT ); Wed, 11 Sep 2019 11:06:22 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 63CF5307D925; Wed, 11 Sep 2019 15:06:22 +0000 (UTC) Received: from llong.com (ovpn-125-196.rdu2.redhat.com [10.10.125.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id D361D5D9E2; Wed, 11 Sep 2019 15:06:19 +0000 (UTC) From: Waiman Long To: Peter Zijlstra , Ingo Molnar , Will Deacon , Alexander Viro , Mike Kravetz Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Davidlohr Bueso , Waiman Long Subject: [PATCH 4/5] locking/rwsem: Enable timeout check when staying in the OSQ Date: Wed, 11 Sep 2019 16:05:36 +0100 Message-Id: <20190911150537.19527-5-longman@redhat.com> In-Reply-To: <20190911150537.19527-1-longman@redhat.com> References: <20190911150537.19527-1-longman@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Wed, 11 Sep 2019 15:06:22 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use the break function allowed by the new osq_lock() to enable early break from the OSQ when a timeout value is specified and expiration time has been reached. Signed-off-by: Waiman Long --- kernel/locking/rwsem.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index c15926ecb21e..78708097162a 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -794,23 +794,50 @@ static inline u64 rwsem_rspin_threshold(struct rw_semaphore *sem) return sched_clock() + delta; } +struct rwsem_break_arg { + u64 timeout; + int loopcnt; +}; + +static bool rwsem_osq_break(void *brk_arg) +{ + struct rwsem_break_arg *arg = brk_arg; + + arg->loopcnt++; + /* + * Check sched_clock() only once every 256 iterations. + */ + if (!(arg->loopcnt++ & 0xff) && (sched_clock() >= arg->timeout)) + return true; + return false; +} + static bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock, ktime_t timeout) { - bool taken = false; + bool taken = false, locked; int prev_owner_state = OWNER_NULL; int loop = 0; u64 rspin_threshold = 0, curtime; + struct rwsem_break_arg break_arg; unsigned long nonspinnable = wlock ? RWSEM_WR_NONSPINNABLE : RWSEM_RD_NONSPINNABLE; preempt_disable(); /* sem->wait_lock should not be held when doing optimistic spinning */ - if (!osq_lock(&sem->osq, NULL, NULL)) - goto done; + if (timeout) { + break_arg.timeout = ktime_to_ns(timeout); + break_arg.loopcnt = 0; + locked = osq_lock(&sem->osq, rwsem_osq_break, &break_arg); + curtime = sched_clock(); + } else { + locked = osq_lock(&sem->osq, NULL, NULL); + curtime = 0; + } - curtime = timeout ? sched_clock() : 0; + if (!locked) + goto done; /* * Optimistically spin on the owner field and attempt to acquire the -- 2.18.1