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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03B62C4332F for ; Thu, 13 Oct 2022 15:38:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229771AbiJMPiH (ORCPT ); Thu, 13 Oct 2022 11:38:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229459AbiJMPiD (ORCPT ); Thu, 13 Oct 2022 11:38:03 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF409C58B1 for ; Thu, 13 Oct 2022 08:38:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665675481; 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=8FglV2hEiVF1FMKvRG3wYfrJNDdbdOZrWruHYlhJav4=; b=BE/qkphoQnAuuMRelozRHn5BX8Z82KKfsRNhQcRxrhUW5SwhIbawKWUwFrOmhUaES557aS FcKY0BjB87z+ykoJezr5HD/k8mRiaVl3BKWyWhjpt6bfFODU1HUTfWQaNa1zCNb1MWLty+ JBodw0ZqpvVIrLwBjSJBKj6qWvDjg2Y= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-571-JuWYSfdnPAyk3C62-oWJ9Q-1; Thu, 13 Oct 2022 11:37:59 -0400 X-MC-Unique: JuWYSfdnPAyk3C62-oWJ9Q-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 530E9802541; Thu, 13 Oct 2022 15:37:48 +0000 (UTC) Received: from [10.22.17.27] (unknown [10.22.17.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54454C47E15; Thu, 13 Oct 2022 15:37:44 +0000 (UTC) Message-ID: <70523e24-755f-f6ae-f665-78e6f5eef575@redhat.com> Date: Thu, 13 Oct 2022 11:37:43 -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 v2 1/2] locking/rwsem: Prevent non-first waiter from spinning in down_write() slowpath Content-Language: en-US From: Waiman Long To: Peter Zijlstra Cc: Ingo Molnar , Will Deacon , Boqun Feng , linux-kernel@vger.kernel.org, john.p.donnelly@oracle.com, Hillf Danton , Mukesh Ojha , =?UTF-8?B?VGluZzExIFdhbmcg546L5am3?= References: <20221012133333.1265281-1-longman@redhat.com> <20221012133333.1265281-2-longman@redhat.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/13/22 09:33, Waiman Long wrote: > On 10/13/22 06:02, Peter Zijlstra wrote: >> On Wed, Oct 12, 2022 at 09:33:32AM -0400, 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. >>> >> So why not do a better handoff? Specifically, have the owner set owner >> to first-waiter instead of NULL ? (like the normal mutex code) > > I understand your desire to make the rwsem handoff process more like > what mutex is currently doing. I certainly think it is doable and will > put this in my todo list. However, that needs to be done at unlock and > wakeup time. I expect that will require moderate amount of code > changes which will make it not that suitable for backporting to the > stable releases. > > I would like to see these simple fixes get merged first and then we > can work on a major revamp of the handoff code. What do you think? > I am planning to post additional patches on top to rework the handoff code sometimes next week, but I will keep these fix patches for the stable releases. Cheers, Longman