Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1776956pxb; Fri, 20 Nov 2020 20:18:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJyDxtOZ3McMn5ooMOARBHuzvM6+slRgAzOWqxT0zBEOvjXKjWEOpJ9SqfdCELOopjtAYKgx X-Received: by 2002:a05:6402:cb4:: with SMTP id cn20mr37840788edb.235.1605932298219; Fri, 20 Nov 2020 20:18:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605932298; cv=none; d=google.com; s=arc-20160816; b=qxgLn7JpkSEjGCbNGfs0mjsZ/04AQeOYlPUg3tADD74b3KJNKoN8w0zO54L4rJrBv4 W1kkR0vvI9CeTi8TIr4OV1fdgDdZ/4VAEE5lb1rmTTp/3IJR4zlHKwOD6GjoaVWGwEZh t9sKsNNrYP2Ljlg2KrRKLwZ/R9XSaemEE99uapQ9DOo3SOapKRpS5ybbZOgRRHMm45Nj orFZJaA9EVvxfGJy1P4qeVkmosHukC1VmiTS+XxlPvrhWei9tP8a1Rfu+wzjWo75SbXQ EFoD5KZT0Dp58pw63DzKDp2T6m4ficYaQvGgj6QYSvQqX9bqaxWCg9Pp4OiRlHrZ6sml OXOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=ZUL8orKAEpAzaSF4r0ktk6KgtJ/ngdVfWUObCmncbxA=; b=VWb1ydJAFeoEfINdz20TXwbf9PwUulVNJYywuRMa25+tLz4ijKlf9v2ebhe+9NkgYw XeIKFZZrpRnF11fPWe3J3V+svmAeSjeKVw1Dn1qFu6EzTcpZ481a879FjoXDSc5bUdaG Sp62WIQV4zQJFSD/6h5WwtvDNijcpO1XNYYbFr0AOcIzO4Mg96tyX0GjL2wfKxK2jfE5 ATtndC4c06wgGkp/VdKXSVkPkkQMYICwBBwEBzEMZRs6vNws2ctdyhktinA7Ke04MGhY f6m3XwWIMLawj0+uOHwgbshrz82ME8fangVpnigy/7WcdRXuRpqYjd6GLpaXUgM+SQ7Q rHYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="ZdMhkq/O"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p19si2843182edu.169.2020.11.20.20.17.55; Fri, 20 Nov 2020 20:18:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="ZdMhkq/O"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727107AbgKUEOf (ORCPT + 99 others); Fri, 20 Nov 2020 23:14:35 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:52438 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726596AbgKUEOf (ORCPT ); Fri, 20 Nov 2020 23:14:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605932074; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=ZUL8orKAEpAzaSF4r0ktk6KgtJ/ngdVfWUObCmncbxA=; b=ZdMhkq/Ols7ViVJOaS6etCMgq+K5Xjf8qumOrC1T9jQSyubtymWyHCgUcuEvyavmP6PZW9 /SoUjDSbpYRyS1+ngZfzA97Zs1Fw7IFmNGghAgXnxFVbF4uluddqNweKPcUeEr5vkbDAD5 wOOzgQhwIq7pORkASttp1p25dvZcsIc= 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-94-5_FF4mBkOEmaDg55osem7w-1; Fri, 20 Nov 2020 23:14:32 -0500 X-MC-Unique: 5_FF4mBkOEmaDg55osem7w-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 07B588049C8; Sat, 21 Nov 2020 04:14:31 +0000 (UTC) Received: from llong.com (ovpn-119-225.rdu2.redhat.com [10.10.119.225]) by smtp.corp.redhat.com (Postfix) with ESMTP id 34A4460BD8; Sat, 21 Nov 2020 04:14:30 +0000 (UTC) From: Waiman Long To: Peter Zijlstra , Ingo Molnar , Will Deacon Cc: linux-kernel@vger.kernel.org, Davidlohr Bueso , Phil Auld , Waiman Long Subject: [PATCH v2 2/5] locking/rwsem: Prevent potential lock starvation Date: Fri, 20 Nov 2020 23:14:13 -0500 Message-Id: <20201121041416.12285-3-longman@redhat.com> In-Reply-To: <20201121041416.12285-1-longman@redhat.com> References: <20201121041416.12285-1-longman@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The lock handoff bit is added in commit 4f23dbc1e657 ("locking/rwsem: Implement lock handoff to prevent lock starvation") to avoid lock starvation. However, allowing readers to do optimistic spinning does introduce an unlikely scenario where lock starvation can happen. The lock handoff bit may only be set when a waiter is being woken up. In the case of reader unlock, wakeup happens only when the reader count reaches 0. If there is a continuous stream of incoming readers acquiring read lock via optimistic spinning, it is possible that the reader count may never reach 0 and so the handoff bit will never be asserted. One way to prevent this scenario from happening is to disallow optimistic spinning if the rwsem is currently owned by readers. If the previous or current owner is a writer, optimistic spinning will be allowed. If the previous owner is a reader but the reader count has reached 0 before, a wakeup should have been issued. So the handoff mechanism will be kicked in to prevent lock starvation. As a result, it should be OK to do optimistic spinning in this case. This patch may have some impact on reader performance as it reduces reader optimistic spinning especially if the lock critical sections are short the number of contending readers are small. Signed-off-by: Waiman Long --- kernel/locking/rwsem.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index 12761e02ab9b..a961c5c53b70 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -991,16 +991,27 @@ rwsem_spin_on_owner(struct rw_semaphore *sem, unsigned long nonspinnable) static struct rw_semaphore __sched * rwsem_down_read_slowpath(struct rw_semaphore *sem, int state, long count) { - long adjustment = -RWSEM_READER_BIAS; + long owner, adjustment = -RWSEM_READER_BIAS; + long rcnt = (count >> RWSEM_READER_SHIFT); struct rwsem_waiter waiter; DEFINE_WAKE_Q(wake_q); bool wake = false; + /* + * To prevent a constant stream of readers from starving a sleeping + * waiter, don't attempt optimistic spinning if the lock is currently + * owned by readers. + */ + owner = atomic_long_read(&sem->owner); + if ((owner & RWSEM_READER_OWNED) && (rcnt > 1) && + !(count & RWSEM_WRITER_LOCKED)) + goto queue; + /* * Save the current read-owner of rwsem, if available, and the * reader nonspinnable bit. */ - waiter.last_rowner = atomic_long_read(&sem->owner); + waiter.last_rowner = owner; if (!(waiter.last_rowner & RWSEM_READER_OWNED)) waiter.last_rowner &= RWSEM_RD_NONSPINNABLE; -- 2.18.1