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=-9.6 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,USER_AGENT_GIT 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 CB9D5ECE588 for ; Tue, 15 Oct 2019 18:16:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 81C4C222C2 for ; Tue, 15 Oct 2019 18:16:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="JuyClk7P" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 81C4C222C2 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C2C688E0011; Tue, 15 Oct 2019 14:16:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B68AF8E0001; Tue, 15 Oct 2019 14:16:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7CE18E0011; Tue, 15 Oct 2019 14:16:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0056.hostedemail.com [216.40.44.56]) by kanga.kvack.org (Postfix) with ESMTP id 84CD28E0001 for ; Tue, 15 Oct 2019 14:16:34 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with SMTP id 04E1D1808894B for ; Tue, 15 Oct 2019 18:16:34 +0000 (UTC) X-FDA: 76046824308.16.shade36_39407e56d062a X-HE-Tag: shade36_39407e56d062a X-Filterd-Recvd-Size: 7200 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf26.hostedemail.com (Postfix) with ESMTP for ; Tue, 15 Oct 2019 18:16:33 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id j11so9972728plk.3 for ; Tue, 15 Oct 2019 11:16:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cpXvWgqC++htg5t4tfaxCh8tm+v/06g7WiSmKM05mvw=; b=JuyClk7PKCaLwoQ+105chr3B4/YZVWePEMx44i+al/c0waHUPFVmcMJsk69iurM8xR TqIwh/dWh7/bezrzPA7KI4qy1RM9p4IWVXMg7fLRvoeynPIE47WqsqslPXSo0Nocyp8b S+/SYVDKWfxiXPCAFo7rRERONWIezL1LF4kOXzYqimZaG7E71vm9BsTxd6TJBsVwKqYW LBpwbsRaDkCFam+Aap+a3+qNpe/qW0b8h9rg7ONtiZhkEE9NnE0x3yb081fUXtFapyMg +2aW6MdXKqWTKPnnE77vavOkmbSBi9iBgcYZl0B6Q0XT/ieElII5zYsv38J1WN9BsZGN t/2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cpXvWgqC++htg5t4tfaxCh8tm+v/06g7WiSmKM05mvw=; b=fSWq9+w7Zs/7zNfdsXY+FdRbFf8Q5Lkxf4KTc7DXmOmEnNwdIWg4nxiWW6s/wtqjOG 2QgTkb0hUocop+S52iApgj0sSFpVSHoZAZl+myYW9Znakz1II+tyBhJi7KDYJ4U9UIXP 6uO/qVUUznmPkjT3AZnYiwoDs2W5dTLFiymwQ4B6P11WQx7F3AAlxnpBLa7xckXakg3+ tXUyAJ2gQMEcjzCxM5uPfSUrr1kmfGbxaKRbrIcoIku9F3bzzg0zO7S9OrqnPTJ7gI42 njVjzJBUPGcBCIuz73WmLweDD0X82iVZvdMfn4GC46yeV+rls2Xgq7Xo02c2d6dMg53m XHjA== X-Gm-Message-State: APjAAAV9Oow6kfrnSzM2sH9BqtSVdrdcdlwEAWv8A1cAz7cjMlq6vn95 eXWk8OLCjDsWg9J/Ok/RFfNybQ== X-Google-Smtp-Source: APXvYqylDFeyHP+orjZdFPigy5QgoSehDBF4HXwVxw56pNjg/wm3jT8eRKmoaCJljZuzzdfaqS/x2A== X-Received: by 2002:a17:902:820f:: with SMTP id x15mr37595901pln.288.1571163392460; Tue, 15 Oct 2019 11:16:32 -0700 (PDT) Received: from ziepe.ca ([24.114.26.129]) by smtp.gmail.com with ESMTPSA id u9sm19594pjb.4.2019.10.15.11.16.31 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 15 Oct 2019 11:16:32 -0700 (PDT) Received: from jgg by jggl.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1iKRJT-0002Bw-Ay; Tue, 15 Oct 2019 15:12:51 -0300 From: Jason Gunthorpe To: Jerome Glisse , Ralph Campbell , John Hubbard , Felix.Kuehling@amd.com Cc: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Andrea Arcangeli , dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org, Ben Skeggs , Jason Gunthorpe Subject: [PATCH hmm 03/15] mm/hmm: allow hmm_range to be used with a mmu_range_notifier or hmm_mirror Date: Tue, 15 Oct 2019 15:12:30 -0300 Message-Id: <20191015181242.8343-4-jgg@ziepe.ca> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191015181242.8343-1-jgg@ziepe.ca> References: <20191015181242.8343-1-jgg@ziepe.ca> MIME-Version: 1.0 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: From: Jason Gunthorpe hmm_mirror's handling of ranges does not use a sequence count which results in this bug: CPU0 CPU1 hmm_range_wait_until_valid(range) valid =3D=3D true hmm_range_fault(range) hmm_invalidate_range_start() range->valid =3D false hmm_invalidate_range_end() range->valid =3D true hmm_range_valid(range) valid =3D=3D true Where the hmm_range_valid should not have succeeded. Adding the required sequence count would make it nearly identical to the new mmu_range_notifier. Instead replace the hmm_mirror stuff with mmu_range_notifier. Co-existence of the two APIs is the first step. Signed-off-by: Jason Gunthorpe --- include/linux/hmm.h | 5 +++++ mm/hmm.c | 25 +++++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/include/linux/hmm.h b/include/linux/hmm.h index 3fec513b9c00f1..8ac1fd6a81af8f 100644 --- a/include/linux/hmm.h +++ b/include/linux/hmm.h @@ -145,6 +145,9 @@ enum hmm_pfn_value_e { /* * struct hmm_range - track invalidation lock on virtual address range * + * @notifier: an optional mmu_range_notifier + * @notifier_seq: when notifier is used this is the result of + * mmu_range_read_begin() * @hmm: the core HMM structure this range is active against * @vma: the vm area struct for the range * @list: all range lock are on a list @@ -159,6 +162,8 @@ enum hmm_pfn_value_e { * @valid: pfns array did not change since it has been fill by an HMM fu= nction */ struct hmm_range { + struct mmu_range_notifier *notifier; + unsigned long notifier_seq; struct hmm *hmm; struct list_head list; unsigned long start; diff --git a/mm/hmm.c b/mm/hmm.c index 902f5fa6bf93ad..22ac3595771feb 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -852,6 +852,14 @@ void hmm_range_unregister(struct hmm_range *range) } EXPORT_SYMBOL(hmm_range_unregister); =20 +static bool needs_retry(struct hmm_range *range) +{ + if (range->notifier) + return mmu_range_check_retry(range->notifier, + range->notifier_seq); + return !range->valid; +} + static const struct mm_walk_ops hmm_walk_ops =3D { .pud_entry =3D hmm_vma_walk_pud, .pmd_entry =3D hmm_vma_walk_pmd, @@ -892,18 +900,23 @@ long hmm_range_fault(struct hmm_range *range, unsig= ned int flags) const unsigned long device_vma =3D VM_IO | VM_PFNMAP | VM_MIXEDMAP; unsigned long start =3D range->start, end; struct hmm_vma_walk hmm_vma_walk; - struct hmm *hmm =3D range->hmm; + struct mm_struct *mm; struct vm_area_struct *vma; int ret; =20 - lockdep_assert_held(&hmm->mmu_notifier.mm->mmap_sem); + if (range->notifier) + mm =3D range->notifier->mm; + else + mm =3D range->hmm->mmu_notifier.mm; + + lockdep_assert_held(&mm->mmap_sem); =20 do { /* If range is no longer valid force retry. */ - if (!range->valid) + if (needs_retry(range)) return -EBUSY; =20 - vma =3D find_vma(hmm->mmu_notifier.mm, start); + vma =3D find_vma(mm, start); if (vma =3D=3D NULL || (vma->vm_flags & device_vma)) return -EFAULT; =20 @@ -933,7 +946,7 @@ long hmm_range_fault(struct hmm_range *range, unsigne= d int flags) start =3D hmm_vma_walk.last; =20 /* Keep trying while the range is valid. */ - } while (ret =3D=3D -EBUSY && range->valid); + } while (ret =3D=3D -EBUSY && !needs_retry(range)); =20 if (ret) { unsigned long i; @@ -991,7 +1004,7 @@ long hmm_range_dma_map(struct hmm_range *range, stru= ct device *device, continue; =20 /* Check if range is being invalidated */ - if (!range->valid) { + if (needs_retry(range)) { ret =3D -EBUSY; goto unmap; } --=20 2.23.0