Received: by 2002:a25:5b86:0:0:0:0:0 with SMTP id p128csp961163ybb; Thu, 28 Mar 2019 16:06:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqxNG8PhPpCWFVV4W/6GCzVdBX6dQ/1Gv7IHU90Z/on4jrK7cIjO/BcHbgsMBO3RDNTP5oqU X-Received: by 2002:a17:902:54f:: with SMTP id 73mr45983347plf.210.1553814405386; Thu, 28 Mar 2019 16:06:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553814405; cv=none; d=google.com; s=arc-20160816; b=ZvNbZqdIBBkU4d73C5hleyTsD4cbzEl2ziuuIJ2as6ywwsVvZ6ZZslm1fLkoVtCVRM JwcBkqDSvE2F8DT3BeS8w8fogzUD6G7GK/5qnWbgjXPU8gPMYQprmHxwkxUDAqvjffjY DlPgqPsH7Gese/jh/8BzH8YzHkbGaiDN09C7yv4fO1vs4yZC0X7mcBZX93RvML1mYQfV QHrzXF0RkjBXoQHzfomMzgsRzOZh/WWXbJGOPhYo7P5n/lUDOgHRXZrtf8JJgV6iiMEy k46WMWxA2yOcB+iV5ZG3dIJ7T0cIrqTDlcZwEaU9XvPi50xBhjZA/BlunUB1ulSkkHAu muMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=kQgoMerodOMYcS1ac8dmbvH/WohoMn/PcftIRSm/UUE=; b=OJ6yQjqesqmThEHUk6vea0ru3dyUFlPbx0EtvSsPcOTb6AO/jCaU9tKvmU67RlIDBa FBhpn0YHMZ8MFGRIJuXOv+Bl4w21Ur0o36w2uwE+JmDbTeRi22lbdLil3uoA6BMTr0r1 JKQTvatMph2QgE64HOQtDHuYhbFcxQHeYN5yGf+7jTNqWeinbOngmtFIFZd3APYXBJXf XIFRvBmVJ0dP/L6YLK8i7UKfcug+4BGWDmFV9DAzK3HHFJ31+dfEpZ6HuRRgNGt/7P5z VYVGZKFvFYjq6RBX9ZLHTc+0r1ZEWzVZHJOr6U0lTDDFu+/kIOtES6XiTTbxXmkeWZb5 hFfQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n28si210927pfi.109.2019.03.28.16.06.27; Thu, 28 Mar 2019 16:06:45 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728478AbfC1XFr (ORCPT + 99 others); Thu, 28 Mar 2019 19:05:47 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49146 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728470AbfC1XFr (ORCPT ); Thu, 28 Mar 2019 19:05:47 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7522F86676; Thu, 28 Mar 2019 23:05:46 +0000 (UTC) Received: from redhat.com (ovpn-121-118.rdu2.redhat.com [10.10.121.118]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A31F45C29A; Thu, 28 Mar 2019 23:05:45 +0000 (UTC) Date: Thu, 28 Mar 2019 19:05:43 -0400 From: Jerome Glisse To: John Hubbard Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Dan Williams Subject: Re: [PATCH v2 10/11] mm/hmm: add helpers for driver to safely take the mmap_sem v2 Message-ID: <20190328230543.GI13560@redhat.com> References: <20190325144011.10560-1-jglisse@redhat.com> <20190325144011.10560-11-jglisse@redhat.com> <9df742eb-61ca-3629-a5f4-8ad1244ff840@nvidia.com> <20190328213047.GB13560@redhat.com> <20190328220824.GE13560@redhat.com> <068db0a8-fade-8ed1-3b9d-c29c27797301@nvidia.com> <20190328224032.GH13560@redhat.com> <0b698b36-da17-434b-b8e7-4a91ac6c9d82@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <0b698b36-da17-434b-b8e7-4a91ac6c9d82@nvidia.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 28 Mar 2019 23:05:46 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 28, 2019 at 03:43:33PM -0700, John Hubbard wrote: > On 3/28/19 3:40 PM, Jerome Glisse wrote: > > On Thu, Mar 28, 2019 at 03:25:39PM -0700, John Hubbard wrote: > >> On 3/28/19 3:08 PM, Jerome Glisse wrote: > >>> On Thu, Mar 28, 2019 at 02:41:02PM -0700, John Hubbard wrote: > >>>> 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?r?me Glisse > >> [...] > >>>> > >>>>>> > >>>>>> If you insist on having this wrapper, I think it should have approximately > >>>>>> this form: > >>>>>> > >>>>>> void hmm_mirror_mm_down_read(...) > >>>>>> { > >>>>>> WARN_ON(...) > >>>>>> down_read(...) > >>>>>> } > >>>>> > >>>>> 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. > >>> > >>> The race is fine and ok see: > >>> > >>> https://cgit.freedesktop.org/~glisse/linux/commit/?h=hmm-odp-v2&id=eebd4f3095290a16ebc03182e2d3ab5dfa7b05ec > >>> > >>> which has been posted and i think i provided a link in the cover > >>> letter to that post. The same patch exist for nouveau i need to > >>> cleanup that tree and push it. > >> > >> Thanks for that link, and I apologize for not keeping up with that > >> other review thread. > >> > >> Looking it over, hmm_mirror_mm_down_read() is only used in one place. > >> So, what you really want there is not a down_read() wrapper, but rather, > >> something like > >> > >> hmm_sanity_check() > >> > >> , that ib_umem_odp_map_dma_pages() calls. > > > > Why ? The device driver pattern is: > > if (hmm_is_it_dying()) { > > // handle when process die and abort the fault ie useless > > // to call within HMM > > } > > down_read(mmap_sem); > > > > This pattern is common within nouveau and RDMA and other device driver in > > the work. Hence why i am replacing it with just one helper. Also it has the > > added benefit that changes being discussed around the mmap sem will be easier > > to do as it avoid having to update each driver but instead it can be done > > just once for the HMM helpers. > > Yes, and I'm saying that the pattern is broken. Because it's racy. :) And i explained why it is fine, it just an optimization, in most case it takes time to tear down a process and the device page fault handler can be trigger while that happens, so instead of having it pile more work on we can detect that even if it is racy. It is just about avoiding useless work. There is nothing about correctness here. It does not need to identify dying process with 100% accuracy. The fact that the process is dying will be identified race free later on and it just means that in the meantime we are doing useless works, potential tons of useless works. They are hardware that can storm the page fault handler and we end up with hundred of page fault queued up against a process that might be dying. It is a big waste to go over all those fault and do works that will be trown on the floor later on. > > >>>>>>> +{ > >>>>>>> + 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? > >>>>> > >>>>> 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? > >>>> 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. > >>> > >>> Yes everything work, nothing bad can happen from a race, it will just > >>> do useless work which never hurt anyone. > >>> > >> > >> OK, so let's either drop this patch, or if merge windows won't allow that, > >> then *eventually* drop this patch. And instead, put in a hmm_sanity_check() > >> that does the same checks. > > > > RDMA depends on this, so does the nouveau patchset that convert to new API. > > So i do not see reason to drop this. They are user for this they are posted > > and i hope i explained properly the benefit. > > > > It is a common pattern. Yes it only save couple lines of code but down the > > road i will also help for people working on the mmap_sem patchset. > > > > It *adds* a couple of lines that are misleading, because they look like they > make things safer, but they don't actually do so. It is not about safety, sorry if it confused you but there is nothing about safety here, i can add a big fat comment that explains that there is no safety here. The intention is to allow the page fault handler that potential have hundred of page fault queue up to abort as soon as it sees that it is pointless to keep faulting on a dying process. Again if we race it is _fine_ nothing bad will happen, we are just doing use- less work that gonna be thrown on the floor and we are just slowing down the process tear down. Cheers, J?r?me