From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 960F7C4332F for ; Wed, 12 Oct 2022 13:14:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB21F6B0071; Wed, 12 Oct 2022 09:14:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E62866B0073; Wed, 12 Oct 2022 09:14:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D2902900002; Wed, 12 Oct 2022 09:14:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BDDF86B0071 for ; Wed, 12 Oct 2022 09:14:52 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 94D1D1C5E4F for ; Wed, 12 Oct 2022 13:14:52 +0000 (UTC) X-FDA: 80012342424.29.38A1F4E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf20.hostedemail.com (Postfix) with ESMTP id 179731C0036 for ; Wed, 12 Oct 2022 13:14:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665580490; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sfVPErwGRUBoPEtS98XR3ZhtqLKvZ5tHOYIokn6dH28=; b=VavZnba0ipicQGgiSwu51rCS1GvxH0Yz6VH3025m+qGTX44oGyuBRVI/QbJ53U/izywAXD m7sylbTS5uMzhCMcISVAQ3IHcz7XYSFJHm2/UfFNZpw631SqBzTxXpnEt7z2504RkVrWvM 2y9UxJZUiHYP10ZrKUF8DF9zgQzMTB8= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-333-KGnqoq5UOvOnxcNp-2nzdg-1; Wed, 12 Oct 2022 09:14:40 -0400 X-MC-Unique: KGnqoq5UOvOnxcNp-2nzdg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B54D33C0ED58; Wed, 12 Oct 2022 13:14:39 +0000 (UTC) Received: from [10.22.33.120] (unknown [10.22.33.120]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54BE72166B2F; Wed, 12 Oct 2022 13:14:39 +0000 (UTC) Message-ID: <2cf44feb-cb41-84a9-171c-efc053651fc0@redhat.com> Date: Wed, 12 Oct 2022 09:14:38 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [PATCH] locking/rwsem: Prevent non-first waiter from spinning in down_write() slowpath Content-Language: en-US To: Hillf Danton , Mukesh Ojha Cc: linux-kernel@vger.kernel.org, john.p.donnelly@oracle.com, linux-mm@kvack.org, Peter Zijlstra , Will Deacon , Boqun Feng , Ingo Molnar References: <20221011104621.231-1-hdanton@sina.com> From: Waiman Long In-Reply-To: <20221011104621.231-1-hdanton@sina.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665580491; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=sfVPErwGRUBoPEtS98XR3ZhtqLKvZ5tHOYIokn6dH28=; b=j5Pn7s3HsP7YHhbGhB3fxcbpxFcEbiZkIv5+33SH7znC3bIYkmZgmCHyIixjdDSsmq7MU0 oY2/DQo9J6051/NXA7nxQW4qQkrcjD0fJ4U9DseHVrjHIjhU8B8RIANI0P4YmiQD1zVclf 2vrVCJ/bBALMhmKYeIt9i/glQKifgH4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VavZnba0; spf=pass (imf20.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665580491; a=rsa-sha256; cv=none; b=LtEAgoJritaPhsVk91RP6xFahhHH5usIwAcSoSQyMG1dixuFu8T7zT1eFpiRUs/td/W6Ku qaF7zRf82BrKjMvWqdTYwniL/fE+/G4x/kA6Pz8UgSiyxLGRH2oJYt//luB9jVB+6/8gb/ 4Bg/Ro8loGpe8ODtBOx5uh2fMX4h1r8= X-Stat-Signature: hqji7e4gr97c3fhq6tcnpzbaxiwregza X-Rspamd-Queue-Id: 179731C0036 Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VavZnba0; spf=pass (imf20.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1665580490-382269 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: On 10/11/22 06:46, Hillf Danton wrote: > On 10/10/22 06:24 Mukesh Ojha >> Hi Waiman, >> >> On 9/29/2022 11:36 PM, Waiman Long wrote: >>> On 9/29/22 14:04, Waiman Long wrote: >>>> A non-first waiter can potentially spin in the for loop of >>>> rwsem_down_write_slowpath() without sleeping but fail to acquire the >>>> lock even if the rwsem is free if the following sequence happens: >>>> >>>> Non-first waiter First waiter Lock holder >>>> ---------------- ------------ ----------- >>>> Acquire wait_lock >>>> rwsem_try_write_lock(): >>>> Set handoff bit if RT or >>>> wait too long >>>> Set waiter->handoff_set >>>> Release wait_lock >>>> Acquire wait_lock >>>> Inherit waiter->handoff_set >>>> Release wait_lock >>>> Clear owner >>>> Release lock >>>> if (waiter.handoff_set) { >>>> rwsem_spin_on_owner((); >>>> if (OWNER_NULL) >>>> goto trylock_again; >>>> } >>>> trylock_again: >>>> Acquire wait_lock >>>> rwsem_try_write_lock(): >>>> if (first->handoff_set && (waiter != first)) >>>> return false; >>>> Release wait_lock >>>> >>>> It is especially problematic if the non-first waiter is an RT task and >>>> it is running on the same CPU as the first waiter as this can lead to >>>> live lock. >>>> >>>> Fixes: d257cc8cb8d5 ("locking/rwsem: Make handoff bit handling more >>>> consistent") >>>> Signed-off-by: Waiman Long >>>> --- >>>> kernel/locking/rwsem.c | 13 ++++++++++--- >>>> 1 file changed, 10 insertions(+), 3 deletions(-) >>> Mukesh, can you test if this patch can fix the RT task lockup problem? >>> >> Looks like, There is still a window for a race. >> >> There is a chance when a reader who came first added it's BIAS and >> goes to slowpath and before it gets added to wait list it got >> preempted by RT task which goes to slowpath as well and being the >> first waiter gets its hand-off bit set and not able to get the lock >> due to following condition in rwsem_try_write_lock() >> >> 630 if (count & RWSEM_LOCK_MASK) { ==> reader has >> sets its bias >> .. >> ... >> >> 634 >> 635 new |= RWSEM_FLAG_HANDOFF; >> 636 } else { >> 637 new |= RWSEM_WRITER_LOCKED; >> >> >> ---------------------->----------------------->------------------------- >> >> First reader (1) writer(2) RT task Lock holder(3) >> >> It sets >> RWSEM_READER_BIAS. >> while it is going to >> slowpath(as the lock >> was held by (3)) and >> before it got added >> to the waiters list >> it got preempted >> by (2). >> RT task also takes >> the slowpath and add release the >> itself into waiting list rwsem lock >> and since it is the first clear the >> it is the next one to get owner. >> the lock but it can not >> get the lock as (count & >> RWSEM_LOCK_MASK) is set >> as (1) has added it but >> not able to remove its >> adjustment. >> > Hey Mukesh, > > Can you test the diff if it makes sense to you? > > It simply prevents the first waiter from spinning any longer after detecting > it barely makes any progress to spin without lock owner. > > Hillf > > --- mainline/kernel/locking/rwsem.c > +++ b/kernel/locking/rwsem.c > @@ -611,26 +611,15 @@ static inline bool rwsem_try_write_lock( > long count, new; > > lockdep_assert_held(&sem->wait_lock); > + waiter->handoff_set = false; > > count = atomic_long_read(&sem->count); > do { > bool has_handoff = !!(count & RWSEM_FLAG_HANDOFF); > > if (has_handoff) { > - /* > - * Honor handoff bit and yield only when the first > - * waiter is the one that set it. Otherwisee, we > - * still try to acquire the rwsem. > - */ > - if (first->handoff_set && (waiter != first)) > + if (waiter != first) > return false; > - > - /* > - * First waiter can inherit a previously set handoff > - * bit and spin on rwsem if lock acquisition fails. > - */ > - if (waiter == first) > - waiter->handoff_set = true; > } > > new = count; That is somewhat equivalent to allowing first waiter to spin only once after setting the handoff bit. It also remove the code to handle some of corner cases. Cheers, Longman