Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2444487ybb; Mon, 30 Mar 2020 06:19:31 -0700 (PDT) X-Google-Smtp-Source: ADFU+vs4qbzOFsO2ImKvVb+Ylq9zOtc42/qcgogsNAjrfqXKzHhSg+cgASmz02vHaEbBu5mHIEtQ X-Received: by 2002:a05:6830:2314:: with SMTP id u20mr9234471ote.166.1585574371763; Mon, 30 Mar 2020 06:19:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585574371; cv=none; d=google.com; s=arc-20160816; b=FdVH+1/pC6KnRqj3hotBOagrqAvNhCKnA3yRQH/A1w0PeJgLM1BepcWmx9uQHn7PQn vOXhvUOGWecJmgKhKg1tNxZEqI+/tGK+ISAaHJM9Uk2c477drrNiXnVgefXbNsgRH/ih 7oRg2g0jyWtNlvUR7TlbbvI18VARcAKnjIoJ6QIBbYzOjXTkD2rRGFQZupeDaoFAnKyz MxuzT/z2Qx6AENNrMZW7QJiu6NRqSh1Q0kxhL0TnQH+t2rtQdECRuCXJK97IWrkp3UoJ T17WdHDRljl5Ly5tYwIxfGQ0UkbRjzBa1I5gtIMFp/Dd/xXoLTlAAOH/s9tdv/zi4odF f93w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=oyPhPDGxJ3faw+iF5XZmXUEt8GkiTJqHI3z+ZzyTC7w=; b=n97Wn43zsve1IqFAFavNLL8+lSznY+RReuRRFVxkW0SR/wm9IDmeQZm1LNQ6tVT1Oq DGsrfEb0xMuktH5CiSkCXjkvG74rx7d0xzQjvQ7iSDoL7VMW+v3vEBpKkp+HPN5Zm8Sn +ipnI7cNdRcP8RTCm/YVXHUlRfVn5io/aNbenEiNCDmlc+vfrJLK8wB+qfx3zIP3oB7J x5KEFc0ZyuDAcPmeYLEfo/bbsYkb1UX+O12SpReegiK1flpKSZP0gxAyjUijSqDyav70 KJqTMrw12u9tmMPkcmu6isoUE5LjWqFcwldr3nXZdgZKg5NKWiptCnKyttHNhWZBJgoo HWCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=cibQWKrg; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i16si6560705otk.112.2020.03.30.06.19.18; Mon, 30 Mar 2020 06:19:31 -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; dkim=pass header.i=@lca.pw header.s=google header.b=cibQWKrg; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730331AbgC3NSa (ORCPT + 99 others); Mon, 30 Mar 2020 09:18:30 -0400 Received: from mail-qv1-f68.google.com ([209.85.219.68]:37994 "EHLO mail-qv1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730290AbgC3NSa (ORCPT ); Mon, 30 Mar 2020 09:18:30 -0400 Received: by mail-qv1-f68.google.com with SMTP id p60so8845820qva.5 for ; Mon, 30 Mar 2020 06:18:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=oyPhPDGxJ3faw+iF5XZmXUEt8GkiTJqHI3z+ZzyTC7w=; b=cibQWKrgQA8fU2bFZGG3xQ73+wcPnvJ2uUr8S8YyBn37a8OOnOwOv7YoNnZ82+Gidz A5gBaiI7f9ps0Sl6vzNn+QzfRlx3dqhtElSyM0ojAq+SEmMKb871HdNsCVx5U8hUx30v 7wElIznvbinZ8cOkKwREAL/W9z7HorqZG4gpmDXJ/sTuekv7uVYKrsyTiemNeTMbwQOa vGEBVImrzweGpp+odvZy0IT40BiTZrvCKdIfImlFmTHNIl5Y043jUTp+gPGp9q/PwQMP Vg/xq19DIc504X/s29eVm3MfuGEVB8+VPtCk0Y3F25pF+cUOYzxhbpchRB94b4/VMBS0 bv7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=oyPhPDGxJ3faw+iF5XZmXUEt8GkiTJqHI3z+ZzyTC7w=; b=r6ymC8g6xztEUaRprjMUqgKhh4cAbrM16alJbcbUhoZECzLLWVT8m207pPs+ZGd7Eq yxKCOkDTl3vZ89rIGXwqmI1x8M7jnQwrfZGiqesM/nKAFUzBdpT7Owib5XkIEhooUwXf 1uiBbuh2bSoXEZvJCmUlUGrt0dd8MIgC6Kl0NRlKGfsKWmreS8+lUz5/CswIWK+iPvmq pzhWiSyxGTwSmk3scZMkwVuj3zppD/a90sjzuKyAEwyfwdoXqYqkf3NvDEvV2SUthep0 wP4QAWIfSNZ96y7yBBwJ3OwbfBy3aRX7Wk7ST52mEJONpr9E90QhBWcHKweLgaiPhSXV JaSA== X-Gm-Message-State: ANhLgQ2R3BscNRk48SUkaf5MkXLcx5vTWXyrjdlFW15jj4Nld1tPcHkn cbzOWm1v/6prY0y1G1QBqRAHlA== X-Received: by 2002:a0c:f709:: with SMTP id w9mr11648009qvn.159.1585574307110; Mon, 30 Mar 2020 06:18:27 -0700 (PDT) Received: from [192.168.1.153] (pool-71-184-117-43.bstnma.fios.verizon.net. [71.184.117.43]) by smtp.gmail.com with ESMTPSA id z18sm11460552qtz.77.2020.03.30.06.18.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Mar 2020 06:18:26 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.80.23.2.2\)) Subject: Re: [PATCH -next] locking/percpu-rwsem: fix a task_struct refcount From: Qian Cai In-Reply-To: <20200330111852.GH20696@hirez.programming.kicks-ass.net> Date: Mon, 30 Mar 2020 09:18:25 -0400 Cc: Ingo Molnar , Will Deacon , dbueso@suse.de, juri.lelli@redhat.com, Waiman Long , linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Message-Id: <37BFF03B-470A-46B4-91AB-8A8A64FEF7B8@lca.pw> References: <20200327093754.GS20713@hirez.programming.kicks-ass.net> <20200330111852.GH20696@hirez.programming.kicks-ass.net> To: Peter Zijlstra X-Mailer: Apple Mail (2.3608.80.23.2.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Mar 30, 2020, at 7:18 AM, Peter Zijlstra = wrote: >=20 > On Fri, Mar 27, 2020 at 06:19:37AM -0400, Qian Cai wrote: >>=20 >>=20 >>> On Mar 27, 2020, at 5:37 AM, Peter Zijlstra = wrote: >>>=20 >>> If the trylock fails, someone else got the lock and we remain on the >>> waitqueue. It seems like a very bad idea to put the task while it >>> remains on the waitqueue, no? >>=20 >> Interesting, I thought this was more straightforward to see, >=20 > It is indeed as straight forward as you explain; but when doing 10 > things at once, and having just dug through some low-level arch = assembly > code for the previous email, even obvious things might sometimes need > a little explaining :/ >=20 > So please, always try and err on the side of a little verbose when > writing Changelogs, esp. when concerning locking / concurrency, you > really can't be clear enough. >=20 >> but I may >> be wrong as always. At the beginning of percpu_rwsem_wake_function() >> it calls get_task_struct(), but if the trylock failed, it will remain >> in the waitqueue. However, it will run percpu_rwsem_wake_function() >> again with get_task_struct() to increase the refcount. Can you >> enlighten me where it will call put_task_struct() in waitqueue or >> elsewhere to balance the refcount in this case? >=20 > See, had that explaination been part of the Changelog, my brain = would've > probably been able to kick itself in gear and actually spot the = problem. >=20 > Yes, you're right. >=20 > That said, I wonder if we can just move the get_task_struct() call = like > below; after all the race we're guarding against is = percpu_rwsem_wait() > observing !private, terminating the wait and doing a quick exit() = while > percpu_rwsem_wake_function() then does wake_up_process(p) as a > use-after-free. Looks good to me. If no one has any objection, I=E2=80=99ll dust-out the = commit log and send out a v2 for it.=20 >=20 > Hmm? >=20 > diff --git a/kernel/locking/percpu-rwsem.c = b/kernel/locking/percpu-rwsem.c > index a008a1ba21a7..8bbafe3e5203 100644 > --- a/kernel/locking/percpu-rwsem.c > +++ b/kernel/locking/percpu-rwsem.c > @@ -118,14 +118,15 @@ static int percpu_rwsem_wake_function(struct = wait_queue_entry *wq_entry, > unsigned int mode, int wake_flags, > void *key) > { > - struct task_struct *p =3D get_task_struct(wq_entry->private); > bool reader =3D wq_entry->flags & WQ_FLAG_CUSTOM; > struct percpu_rw_semaphore *sem =3D key; > + struct task_struct *p; >=20 > /* concurrent against percpu_down_write(), can get stolen */ > if (!__percpu_rwsem_trylock(sem, reader)) > return 1; >=20 > + p =3D get_task_struct(wq_entry->private); > list_del_init(&wq_entry->entry); > smp_store_release(&wq_entry->private, NULL); >=20