Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3919663pxv; Mon, 28 Jun 2021 16:40:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxPd/IYZXHAUWWa/8jVNfDIGF7assZM/a8jH7G8b/6B6+htmT0Sshm/+f3k5DD4Nc69xQa3 X-Received: by 2002:a92:7312:: with SMTP id o18mr18913082ilc.289.1624923643056; Mon, 28 Jun 2021 16:40:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624923643; cv=none; d=google.com; s=arc-20160816; b=CdLKxyl6xcw4xZgvaZF1V3kGGpWakHFAagZf8jLFN61QYcQZYegAWO8RnKI59A+0ou +4Xh8yKYW/28UYs3wqn9v0sSc2muNTtH5NRbpajHwZKo1hIoEDOLnrHHcqno145vm5id ubWO2Gc/iL6M7tOUhyO+fSVie6f0tsveqfyYPvO+plA9sYF50ohnoshxXT5LIZC7AAIw MQMsuRw10m5R+PPWAGhNmrOpg1ycI9sE49ktib4BVgTvB0n2LWm3tBNbLTXbnXfi1XnS ShMHj5JA3e/QsOPuCqejz3L/Q/mLB6gvLvW6PrrqQL6GfFZJAi37FG9z8fc1yDvoA2Gs 7eMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:subject:from:dkim-signature; bh=9WGSZJwLu54dZ1SXYRBBCBs+9QaI0qgizM8sbdetDwM=; b=os6ZIVIKp6eQLdFag6jfbg5kvqySV8j/Kb8Sis4e4FpMR07Jb/H1SUo9/AlL436Gqt zx2DtWS+3T4YBZi4F2gZtxd0MXojXbz3BN/Pc9+0PTLsAbikBTEHq0qxjohxOnfhJGhl 8Cxnk23hJjveJ6ocszfMCrxOFQ7PbPwnnLyOfAvgm1g2aCaWf/aa9hNptpgbi/VjGSnl GPtar6vIu5VT3bP8HFbLbNExgjtrFVGkBT7YVZ1n+cE+g1komiQvep6PcF6z/Yz7zQzm FFTaH2aXMfdKnUZf7skbp57KyIf+XX52KqIWuHCgMFcx/ENWuw9aFuF1Nm0dl+5AHU9+ OvEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Itx2qoTu; 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 a10si4862947ion.48.2021.06.28.16.40.31; Mon, 28 Jun 2021 16:40:43 -0700 (PDT) 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=Itx2qoTu; 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 S234044AbhF1SAM (ORCPT + 99 others); Mon, 28 Jun 2021 14:00:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:35963 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231950AbhF1SAK (ORCPT ); Mon, 28 Jun 2021 14:00:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1624903064; 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=9WGSZJwLu54dZ1SXYRBBCBs+9QaI0qgizM8sbdetDwM=; b=Itx2qoTuAK/jxRKqcWnNB7Y3tJyxFw7ctl/6iv3RyiIcSsBSMnmvcccEtM7d/a8TiuM50L EnP8B6gx9V32PcbrTXTnNqLqv4ht1TGsJUAlyD2OW+P1BOQ6XDzJweryCe0exPvSlUEhqe zPpLwwP+pgzb9vSJlEFufz6boq0V5q8= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-507-fXnrd-PKPfWtA78NRvZw0Q-1; Mon, 28 Jun 2021 13:57:42 -0400 X-MC-Unique: fXnrd-PKPfWtA78NRvZw0Q-1 Received: by mail-qt1-f199.google.com with SMTP id l14-20020ac84a8e0000b0290250ca4407e3so1247281qtq.0 for ; Mon, 28 Jun 2021 10:57:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=9WGSZJwLu54dZ1SXYRBBCBs+9QaI0qgizM8sbdetDwM=; b=menieuJmCDlAnct19cXJK8yokvn6ioO/zUIL1jlbnC+Q9yPGQMzuBqW76WQgNQrsPt S2r5eMS3wlAmIqbzFK5y23G8/J8USiyKbJ+IRPlZSlz1IjJSFvKAZQWfLc1CUNdekEma DbwhEKofLUj243sYMPiXdLI7bxU3ThUludi18TtmPH3mb50N25eEYQJKJCkb4hwU6All Mo8kZtsb0E/rdt5wusbqNnLwZlF7wtVQf1lWRMDXaYRq8QPrFGbzvyqB9yNGaZ5e5JUD CL2dw1yzbO6wuvuP+Ya/zDa1XU2z3LQR3Znpt3r/9j/CN1VMcbFwX0PjEtwXYQUXwzFF V1Rw== X-Gm-Message-State: AOAM533q3czrfq1WmVpebuZk7lIW1+B1gq9Hmz8Qdn3XXG1Wic6BHHdG Y7VqbYjJv8zyS+U8R2brPQGwMlOmd0G2jHW0eN6GxBA3hCMnAIEBgGPVzDafyLozOAUvpX/Mkwf haGhyZ9CZWNt/Pjh7CyJA3t605Nf6wwGMbtBpfr6Ns7iRmKFj6zQw+fOEZlAc1ACvBWsUXqc4 X-Received: by 2002:ac8:5a44:: with SMTP id o4mr18771916qta.105.1624903062072; Mon, 28 Jun 2021 10:57:42 -0700 (PDT) X-Received: by 2002:ac8:5a44:: with SMTP id o4mr18771899qta.105.1624903061750; Mon, 28 Jun 2021 10:57:41 -0700 (PDT) Received: from llong.remote.csb ([2601:191:8500:76c0::cdbc]) by smtp.gmail.com with ESMTPSA id b66sm10885314qkc.4.2021.06.28.10.57.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 28 Jun 2021 10:57:41 -0700 (PDT) From: Waiman Long X-Google-Original-From: Waiman Long Subject: Re: [PATCH] locking/mutex: fix the MUTEX_FLAG_HANDOFF bit is cleared unexpected To: Yanfei Xu , peterz@infradead.org, mingo@redhat.com, boqun.feng@gmail.com Cc: linux-kernel@vger.kernel.org References: <20210628155155.11623-1-yanfei.xu@windriver.com> Message-ID: Date: Mon, 28 Jun 2021 13:57:40 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 In-Reply-To: <20210628155155.11623-1-yanfei.xu@windriver.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 6/28/21 11:51 AM, Yanfei Xu wrote: > When the mutex unlock path is excuted with WAITERS bit and without > HANDOFF bit set, it will wake up the first task in wait_list. If > there are some tasks not in wait_list are stealing lock, it is very > likely successfully due to the task field of lock is NULL and flags > field is non-NULL. Then the HANDOFF bit will be cleared. But if the > HANDOFF bit was just set by the waked task in wait_list, this clearing > is unexcepted. I think you mean "unexpected". Right? Anyway, all the setting and clearing of the HANDOFF bit are atomic. There shouldn't be any unexpected clearing. > __mutex_lock_common __mutex_lock_common > __mutex_trylock schedule_preempt_disabled > /*steal lock successfully*/ __mutex_set_flag(lock,MUTEX_FLAG_HANDOFF) > __mutex_trylock_or_owner > if (task==NULL) > flags &= ~MUTEX_FLAG_HANDOFF > atomic_long_cmpxchg_acquire > __mutex_trylock //failed > mutex_optimistic_spin //failed > schedule_preempt_disabled //sleep without HANDOFF bit > > So the HANDOFF bit should be set as late as possible, here we defer > it util the task is going to be scheduled. > Signed-off-by: Yanfei Xu > --- > > Hi maintainers, > > I am not very sure if I missed or misunderstanded something, please help > to review. Many thanks! > > kernel/locking/mutex.c | 8 ++++---- > 1 file changed, 4 insertions(+), 4 deletions(-) > > diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c > index 013e1b08a1bf..e57d920e96bf 100644 > --- a/kernel/locking/mutex.c > +++ b/kernel/locking/mutex.c > @@ -1033,17 +1033,17 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, > } > > spin_unlock(&lock->wait_lock); > + > + if (first) > + __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF); > schedule_preempt_disabled(); > > /* > * ww_mutex needs to always recheck its position since its waiter > * list is not FIFO ordered. > */ > - if (ww_ctx || !first) { > + if (ww_ctx || !first) > first = __mutex_waiter_is_first(lock, &waiter); > - if (first) > - __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF); > - } > > set_current_state(state); > /* In general, I don't mind setting the HANDOFF bit later, but mutex_optimistic_spin() at the end of the loop should only be called after the HANDOFF bit is set. So the logic isn't quite right yet. Cheers, Longman