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 X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=DATE_IN_PAST_03_06, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DDFAC47E49 for ; Tue, 29 Oct 2019 17:20:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 35A6320830 for ; Tue, 29 Oct 2019 17:20:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kMvDAfqN" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 35A6320830 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D8A216B000C; Tue, 29 Oct 2019 13:19:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D60E46B000D; Tue, 29 Oct 2019 13:19:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C76E76B000E; Tue, 29 Oct 2019 13:19:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0064.hostedemail.com [216.40.44.64]) by kanga.kvack.org (Postfix) with ESMTP id A328D6B000C for ; Tue, 29 Oct 2019 13:19:59 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with SMTP id 488B95850 for ; Tue, 29 Oct 2019 17:19:59 +0000 (UTC) X-FDA: 76097484918.06.lead33_571e1c5a5af59 X-HE-Tag: lead33_571e1c5a5af59 X-Filterd-Recvd-Size: 9136 Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Tue, 29 Oct 2019 17:19:58 +0000 (UTC) Received: by mail-wr1-f65.google.com with SMTP id e11so14545887wrv.4 for ; Tue, 29 Oct 2019 10:19:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=reply-to:subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=+7k6Tgo7zdAI3sdkV2vUab4Qg2cErY2tDGH+QpqLC8g=; b=kMvDAfqNMsgwLVSlQz2HGv5SXkrMFIA/sC5PQVA0qHCk2uNpr3vgteNY32KO6XolN9 jrYaAZAZBspll0O/IT//xBGYiWDFZD8KU1uPmY2vK3KPsyhTb7mzyGHCGTLByQgbXpOx OeloXmN2vq/6bG5ZHNwG845uL6ZvA2Sdmd4uYMZ4E+4NJ7wccdnycw840yvcV4FBt6VL /xrA8JLUiBY/5hX+ugxid7ZLni4gqfgl/j7jRF06f5phYKlyiq6OI+yIzaRotZfKsIyc R7JU0GMg+/jruClJEMts6SYQJZeL4C+Qp1GS1gxReNthE6HvcZsnnoBEcT3avouO/n1H WYQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:subject:to:cc:references:from :message-id:date:user-agent:mime-version:in-reply-to :content-transfer-encoding:content-language; bh=+7k6Tgo7zdAI3sdkV2vUab4Qg2cErY2tDGH+QpqLC8g=; b=SqNWUJlchodxdF/+PwJvLhpjGudqJc9cAf2AV9lHvs3WAMtGj5nVBlScIskzKFlVae L8ey1+CMYI+gfaU0JgkBZHwC2CFQbLoudQY3Gxuofhu3YiwoqlXivrfBIHCc+XFuT0/o sbTWwApd+uaCR6utN0vtagqtIa3nBuAhXvLtKWtLCMncrud7824G1+QgOut4Kz1VqygI RhBvB7I9lX7fRXYcIWhVlAD68zgG7lyeIghB+gQKgaIu4TgZuM+KuDKugFDkI/tifW2t GkmTLNBoiurzzNAjNNIp3zOUNZztK1gMbuCH/8bxNnRjuj4nvkf6XVKBklpuXa2BNvWx /ymw== X-Gm-Message-State: APjAAAUswZlHdNp5Pdwq9A+5Mcp3bw9rGkePgA4JAPChxK9inbp7TXrl Gmlt+6JKlrZ0DFWfdNFaJvg= X-Google-Smtp-Source: APXvYqyE7qwpgL7KV62AOaDM+oFRQdWcTDjdZKuljhwWYaRRqCfiSc2s9/fNi7ZWZZwXl8hUkkgUgA== X-Received: by 2002:adf:ef0a:: with SMTP id e10mr20205293wro.234.1572369597497; Tue, 29 Oct 2019 10:19:57 -0700 (PDT) Received: from ?IPv6:2a02:908:1252:fb60:be8a:bd56:1f94:86e7? ([2a02:908:1252:fb60:be8a:bd56:1f94:86e7]) by smtp.gmail.com with ESMTPSA id f8sm3544088wmb.37.2019.10.29.10.19.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Oct 2019 10:19:56 -0700 (PDT) Reply-To: christian.koenig@amd.com Subject: Re: [PATCH v2 12/15] drm/amdgpu: Call find_vma under mmap_sem To: "Kuehling, Felix" , Jason Gunthorpe , "linux-mm@kvack.org" , Jerome Glisse , Ralph Campbell , John Hubbard Cc: Juergen Gross , "Zhou, David(ChunMing)" , Mike Marciniszyn , Stefano Stabellini , Oleksandr Andrushchenko , "linux-rdma@vger.kernel.org" , "nouveau@lists.freedesktop.org" , Dennis Dalessandro , "amd-gfx@lists.freedesktop.org" , Christoph Hellwig , Jason Gunthorpe , "dri-devel@lists.freedesktop.org" , "Deucher, Alexander" , "xen-devel@lists.xenproject.org" , Boris Ostrovsky , Petr Cvek , "Koenig, Christian" , Ben Skeggs References: <20191028201032.6352-1-jgg@ziepe.ca> <20191028201032.6352-13-jgg@ziepe.ca> From: =?UTF-8?Q?Christian_K=c3=b6nig?= Message-ID: Date: Tue, 29 Oct 2019 14:07:37 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Am 29.10.19 um 17:28 schrieb Kuehling, Felix: > On 2019-10-28 4:10 p.m., Jason Gunthorpe wrote: >> From: Jason Gunthorpe >> >> find_vma() must be called under the mmap_sem, reorganize this code to >> do the vma check after entering the lock. >> >> Further, fix the unlocked use of struct task_struct's mm, instead use >> the mm from hmm_mirror which has an active mm_grab. Also the mm_grab >> must be converted to a mm_get before acquiring mmap_sem or calling >> find_vma(). >> >> Fixes: 66c45500bfdc ("drm/amdgpu: use new HMM APIs and helpers") >> Fixes: 0919195f2b0d ("drm/amdgpu: Enable amdgpu_ttm_tt_get_user_pages = in worker threads") >> Cc: Alex Deucher >> Cc: Christian K=C3=B6nig >> Cc: David (ChunMing) Zhou >> Cc: amd-gfx@lists.freedesktop.org >> Signed-off-by: Jason Gunthorpe > One question inline to confirm my understanding. Otherwise this patch i= s > > Reviewed-by: Felix Kuehling > > >> --- >> drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 37 ++++++++++++++--------= --- >> 1 file changed, 21 insertions(+), 16 deletions(-) >> >> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm= /amd/amdgpu/amdgpu_ttm.c >> index dff41d0a85fe96..c0e41f1f0c2365 100644 >> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c >> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c >> @@ -35,6 +35,7 @@ >> #include >> #include >> #include >> +#include >> #include >> #include >> #include >> @@ -788,7 +789,7 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo = *bo, struct page **pages) >> struct hmm_mirror *mirror =3D bo->mn ? &bo->mn->mirror : NULL; >> struct ttm_tt *ttm =3D bo->tbo.ttm; >> struct amdgpu_ttm_tt *gtt =3D (void *)ttm; >> - struct mm_struct *mm =3D gtt->usertask->mm; >> + struct mm_struct *mm; >> unsigned long start =3D gtt->userptr; >> struct vm_area_struct *vma; >> struct hmm_range *range; >> @@ -796,25 +797,14 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_b= o *bo, struct page **pages) >> uint64_t *pfns; >> int r =3D 0; >> =20 >> - if (!mm) /* Happens during process shutdown */ >> - return -ESRCH; >> - >> if (unlikely(!mirror)) { >> DRM_DEBUG_DRIVER("Failed to get hmm_mirror\n"); >> - r =3D -EFAULT; >> - goto out; >> + return -EFAULT; >> } >> =20 >> - vma =3D find_vma(mm, start); >> - if (unlikely(!vma || start < vma->vm_start)) { >> - r =3D -EFAULT; >> - goto out; >> - } >> - if (unlikely((gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) && >> - vma->vm_file)) { >> - r =3D -EPERM; >> - goto out; >> - } >> + mm =3D mirror->hmm->mmu_notifier.mm; >> + if (!mmget_not_zero(mm)) /* Happens during process shutdown */ > This works because mirror->hmm->mmu_notifier holds an mmgrab reference > to the mm? So the MM will not just go away, but if the mmget refcount i= s > 0, it means the mm is marked for destruction and shouldn't be used any = more. Yes, exactly. That is a rather common pattern, one reference count for=20 the functionality and one for the structure. When the functionality is gone the structure might still be alive for=20 some reason. TTM and a couple of other structures use the same approach. Christian. > > >> + return -ESRCH; >> =20 >> range =3D kzalloc(sizeof(*range), GFP_KERNEL); >> if (unlikely(!range)) { >> @@ -847,6 +837,17 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo= *bo, struct page **pages) >> hmm_range_wait_until_valid(range, HMM_RANGE_DEFAULT_TIMEOUT); >> =20 >> down_read(&mm->mmap_sem); >> + vma =3D find_vma(mm, start); >> + if (unlikely(!vma || start < vma->vm_start)) { >> + r =3D -EFAULT; >> + goto out_unlock; >> + } >> + if (unlikely((gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) && >> + vma->vm_file)) { >> + r =3D -EPERM; >> + goto out_unlock; >> + } >> + >> r =3D hmm_range_fault(range, 0); >> up_read(&mm->mmap_sem); >> =20 >> @@ -865,15 +866,19 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_b= o *bo, struct page **pages) >> } >> =20 >> gtt->range =3D range; >> + mmput(mm); >> =20 >> return 0; >> =20 >> +out_unlock: >> + up_read(&mm->mmap_sem); >> out_free_pfns: >> hmm_range_unregister(range); >> kvfree(pfns); >> out_free_ranges: >> kfree(range); >> out: >> + mmput(mm); >> return r; >> } >> =20 > _______________________________________________ > amd-gfx mailing list > amd-gfx@lists.freedesktop.org > https://lists.freedesktop.org/mailman/listinfo/amd-gfx