Received: by 2002:a25:5b86:0:0:0:0:0 with SMTP id p128csp902600ybb; Thu, 28 Mar 2019 14:42:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqzqG92lAqBJAOKtCYs97OqIBCFbEC82WRL3b37PLaBHL8s3vweJDfmCBX695Ln3bnkXjcmZ X-Received: by 2002:a65:6108:: with SMTP id z8mr28334018pgu.106.1553809350278; Thu, 28 Mar 2019 14:42:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553809350; cv=none; d=google.com; s=arc-20160816; b=zlAIyvKs016HFwHNtOqUz6yeQL12KRXuX1dQsJLbgrj/NmdJGuKmQziHjyAz4p9cjs Na+xLgltuBw63AIlowTrIdo8a6FzHCF6M2xcsO88WYX5/zNfJacc7VXjqfbM8ZPoIwHJ zb9JOi+Q/WXn9idDz+oi4nWGfhMC2e6KR6YJGioFoZnqcDqn6/+AHVH2pHjyyiIXOKZ5 hRJoMNonNYr99dkpGkGhkvZkEwaTywxeTYO7uU91l7QcwKXIsZezayw1tV7tlG2vhO0o hID3yhs2/AGKVgQcKEAkJ5pJqCcYhtIp46rRrhJtVi+8NKr99J5FUbokdOADEJ/VyqPy gymg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:dkim-signature:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=i0yznMUZ3/E1QrRkk2+BDp44MTpSzcLV2/NZJ7ipR8Y=; b=QRdCx1N8khB1u832r6Hdb7EE2L7cd6xMzm29iKYKI8/IEQGKhczgTGCuyaNmvxz1DX gC60Fg6O1WJfr70PG0Wb8eGNWWQ23kbcbhkIeYwgBn0vMi45R9ZScQyIYLt/w4HLdrC8 tGUNzkH1QcVeJ4G3Mo1JWtFl0ImKB9ldLL3rYwwZPdb0eUsoX1jp5DFHWJj9AymVrhcb Nusyj4tC5wOUs/Cz2GSCiq1WBe7fzE2nu9iF20Hk+/tEzz60/q6Qgl+Uc0RvRlc2glhX VdV2goS5daG/JtnXAQoDEJ79PSTgcVXTsBJJoPVtcFz3Mf6F0HbGHbeNXNyXVVfpPl7L mSoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=hsLB4gq0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u6si181446pgr.456.2019.03.28.14.42.13; Thu, 28 Mar 2019 14:42:30 -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=@nvidia.com header.s=n1 header.b=hsLB4gq0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727901AbfC1VlF (ORCPT + 99 others); Thu, 28 Mar 2019 17:41:05 -0400 Received: from hqemgate15.nvidia.com ([216.228.121.64]:6712 "EHLO hqemgate15.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726611AbfC1VlE (ORCPT ); Thu, 28 Mar 2019 17:41:04 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate15.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Thu, 28 Mar 2019 14:40:56 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Thu, 28 Mar 2019 14:41:02 -0700 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Thu, 28 Mar 2019 14:41:02 -0700 Received: from [10.110.48.28] (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Thu, 28 Mar 2019 21:41:02 +0000 Subject: Re: [PATCH v2 10/11] mm/hmm: add helpers for driver to safely take the mmap_sem v2 To: Jerome Glisse CC: , , Andrew Morton , Dan Williams References: <20190325144011.10560-1-jglisse@redhat.com> <20190325144011.10560-11-jglisse@redhat.com> <9df742eb-61ca-3629-a5f4-8ad1244ff840@nvidia.com> <20190328213047.GB13560@redhat.com> From: John Hubbard X-Nvconfidentiality: public Message-ID: Date: Thu, 28 Mar 2019 14:41:02 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.3 MIME-Version: 1.0 In-Reply-To: <20190328213047.GB13560@redhat.com> X-Originating-IP: [10.124.1.5] X-ClientProxiedBy: HQMAIL108.nvidia.com (172.18.146.13) To HQMAIL101.nvidia.com (172.20.187.10) Content-Type: text/plain; charset="utf-8" Content-Language: en-US-large Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1553809256; bh=i0yznMUZ3/E1QrRkk2+BDp44MTpSzcLV2/NZJ7ipR8Y=; h=X-PGP-Universal:Subject:To:CC:References:From:X-Nvconfidentiality: Message-ID:Date:User-Agent:MIME-Version:In-Reply-To: X-Originating-IP:X-ClientProxiedBy:Content-Type:Content-Language: Content-Transfer-Encoding; b=hsLB4gq0cl32Xmjnr15hA+tqHT/Wm6mRjsQHjiYXOxb3EDWzhHb2doD30UpNQybfY mB4tQoX1AXWvQ5HPFUJRIchUWzThGP7EGYyFQMfgbFXZEsMPPzHYNNUuAEruEYAATZ Lx1tSjutTd930HwXjaKfkA6+RaQCjKkoNR/W+VNmHKa9OZN7vjgBCelyu04RN+TuE2 CH/WaP2gqDl6BJg18YbrQZDC/VCdKl64fJ+TEtVyDmK9eMd40yueJ92m8BK46Mtm7i 1bvhyC5cS2dd7jFW5U7miBWcM+ZuAHGV2IIHcxw8nAdImfwI3AWyKyVWMdGYKUhyf9 rKvF/tjoIl6NA== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/28/19 2:30 PM, Jerome Glisse wrote: > On Thu, Mar 28, 2019 at 01:54:01PM -0700, John Hubbard wrote: >> On 3/25/19 7:40 AM, jglisse@redhat.com wrote: >>> From: J=C3=A9r=C3=B4me Glisse >>> >>> The device driver context which holds reference to mirror and thus to >>> core hmm struct might outlive the mm against which it was created. To >>> avoid every driver to check for that case provide an helper that check >>> if mm is still alive and take the mmap_sem in read mode if so. If the >>> mm have been destroy (mmu_notifier release call back did happen) then >>> we return -EINVAL so that calling code knows that it is trying to do >>> something against a mm that is no longer valid. >>> >>> Changes since v1: >>> - removed bunch of useless check (if API is use with bogus argument >>> better to fail loudly so user fix their code) >>> >>> Signed-off-by: J=C3=A9r=C3=B4me Glisse >>> Reviewed-by: Ralph Campbell >>> Cc: Andrew Morton >>> Cc: John Hubbard >>> Cc: Dan Williams >>> --- >>> include/linux/hmm.h | 50 ++++++++++++++++++++++++++++++++++++++++++--- >>> 1 file changed, 47 insertions(+), 3 deletions(-) >>> >>> diff --git a/include/linux/hmm.h b/include/linux/hmm.h >>> index f3b919b04eda..5f9deaeb9d77 100644 >>> --- a/include/linux/hmm.h >>> +++ b/include/linux/hmm.h >>> @@ -438,6 +438,50 @@ struct hmm_mirror { >>> int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *m= m); >>> void hmm_mirror_unregister(struct hmm_mirror *mirror); >>> =20 >>> +/* >>> + * hmm_mirror_mm_down_read() - lock the mmap_sem in read mode >>> + * @mirror: the HMM mm mirror for which we want to lock the mmap_sem >>> + * Returns: -EINVAL if the mm is dead, 0 otherwise (lock taken). >>> + * >>> + * The device driver context which holds reference to mirror and thus = to core >>> + * hmm struct might outlive the mm against which it was created. To av= oid every >>> + * driver to check for that case provide an helper that check if mm is= still >>> + * alive and take the mmap_sem in read mode if so. If the mm have been= destroy >>> + * (mmu_notifier release call back did happen) then we return -EINVAL = so that >>> + * calling code knows that it is trying to do something against a mm t= hat is >>> + * no longer valid. >>> + */ >>> +static inline int hmm_mirror_mm_down_read(struct hmm_mirror *mirror) >> >> Hi Jerome, >> >> Let's please not do this. There are at least two problems here: >> >> 1. The hmm_mirror_mm_down_read() wrapper around down_read() requires a=20 >> return value. This is counter to how locking is normally done: callers d= o >> not normally have to check the return value of most locks (other than >> trylocks). And sure enough, your own code below doesn't check the return= value. >> That is a pretty good illustration of why not to do this. >=20 > Please read the function description this is not about checking lock > return value it is about checking wether we are racing with process > destruction and avoid trying to take lock in such cases so that driver > do abort as quickly as possible when a process is being kill. >=20 >> >> 2. This is a weird place to randomly check for semi-unrelated state, suc= h=20 >> as "is HMM still alive". By that I mean, if you have to detect a problem >> at down_read() time, then the problem could have existed both before and >> after the call to this wrapper. So it is providing a false sense of secu= rity, >> and it is therefore actually undesirable to add the code. >=20 > It is not, this function is use in device page fault handler which will > happens asynchronously from CPU event or process lifetime when a process > is killed or is dying we do want to avoid useless page fault work and > we do want to avoid blocking the page fault queue of the device. This > function reports to the caller that the process is dying and that it > should just abort the page fault and do whatever other device specific > thing that needs to happen. >=20 But it's inherently racy, to check for a condition outside of any lock, so = again, it's a false sense of security. >> >> If you insist on having this wrapper, I think it should have approximate= ly=20 >> this form: >> >> void hmm_mirror_mm_down_read(...) >> { >> WARN_ON(...) >> down_read(...) >> }=20 >=20 > I do insist as it is useful and use by both RDMA and nouveau and the > above would kill the intent. The intent is do not try to take the lock > if the process is dying. Could you provide me a link to those examples so I can take a peek? I am still convinced that this whole thing is a race condition at best. >=20 >=20 >> >>> +{ >>> + struct mm_struct *mm; >>> + >>> + /* Sanity check ... */ >>> + if (!mirror || !mirror->hmm) >>> + return -EINVAL; >>> + /* >>> + * Before trying to take the mmap_sem make sure the mm is still >>> + * alive as device driver context might outlive the mm lifetime. >> >> Let's find another way, and a better place, to solve this problem. >> Ref counting? >=20 > This has nothing to do with refcount or use after free or anthing > like that. It is just about checking wether we are about to do > something pointless. If the process is dying then it is pointless > to try to take the lock and it is pointless for the device driver > to trigger handle_mm_fault(). Well, what happens if you let such pointless code run anyway?=20 Does everything still work? If yes, then we don't need this change. If no, then we need a race-free version of this change. thanks, --=20 John Hubbard NVIDIA