From patchwork Fri Dec 9 17:00:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13070020 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2532EC04FDE for ; Fri, 9 Dec 2022 17:01:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AED1B8E000B; Fri, 9 Dec 2022 12:01:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A7BA18E000C; Fri, 9 Dec 2022 12:01:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 875348E000B; Fri, 9 Dec 2022 12:01:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 74D878E0001 for ; Fri, 9 Dec 2022 12:01:36 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3B8A312099F for ; Fri, 9 Dec 2022 17:01:36 +0000 (UTC) X-FDA: 80223384192.27.96FD28F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id C0B6340043 for ; Fri, 9 Dec 2022 17:01:32 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TJDdxKJs; spf=pass (imf01.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670605293; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=SiCXWzCdYbU9QHTa4B1WTrTi25NBF8hxZPF60vWGZSs=; b=bYyrxEk5tcJDXphHqPb8m34DYXPjygBBm4zgpSn4C3K4IfWI0MSb6KpnZCWT83X83gynsK 7nrdHiT4N/0P7f9jza3qJ1uNufP1xjEGWyL96noe4ZfVxlkdEuIItwjXOyX4ciD/6rqCmR 9EAsF6hCZ//jDcBJvdgUktHYUXJU0kw= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TJDdxKJs; spf=pass (imf01.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670605293; a=rsa-sha256; cv=none; b=nHKJM6AKi5WWeu4cCIKO6Ei55G/7DVVwde2jzlNKNIn4ASlw1oy3C5IoPISXhXSgiIZFrA 2oU1NoDqRdlDJ9wAAM7Fiqq6jQBW1n8iUV2YzNZ0Rz/KG6EIsfKFUxXpWgRB7fxTC2H/4m zhJUqGkzNgHR5dLqBKPUHY/yJpxPZes= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670605292; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SiCXWzCdYbU9QHTa4B1WTrTi25NBF8hxZPF60vWGZSs=; b=TJDdxKJsKIsNSSdoJ+Zz9nypPwz0yALia0hIFQbRa+yl2yxi63utwuqJpL2aNA6bcQAvTo BbI2tutviRMFTTrDVjigpe9wskqxa3+nB88b15wSgfYBRQMxYsMIZAbMDH2WPuI28EGvw4 mFDaMbTyib8YvD0rfPLEtRBHRHyA9f4= Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-587-mf_N1Z8RM0ORnkk2KI8wYw-1; Fri, 09 Dec 2022 12:01:30 -0500 X-MC-Unique: mf_N1Z8RM0ORnkk2KI8wYw-1 Received: by mail-oo1-f69.google.com with SMTP id d16-20020a4a3c10000000b004a096d64cabso1559664ooa.16 for ; Fri, 09 Dec 2022 09:01:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SiCXWzCdYbU9QHTa4B1WTrTi25NBF8hxZPF60vWGZSs=; b=hfFsR0nR9JxM6AKr5UG0AoKeuacMBt3656QEEZwqYBS9IzdUW7Of90GGg3/vzwO2an RxJJFpZGpmQCv+9lLmgu3axOrpM2EUnGpgwY//XxOy0c5sJMkZKDBA/ngnSsrhEa6Fcs kvhfkvyx7i7elGtvFdk4Y0ZVvvleKaGlMVKHYkoHtErCNjBFT9pwJkBYGq983QDhKkEX B7tXcfJamGsw9gZ6hry3wvOsdc4o4XSPZQukC3W++PaY36qVvMPeTrrvvybDt/CvftJf 9AadRNAGIhqyTjj4tYlKjSln9K9xlXuxeRtH5312TnEjOBzqC5Ya8454hKUxH/l+25iz Wmzw== X-Gm-Message-State: ANoB5pkwQWTvCo2DjgHbhOP1PTLYKPUbH89dD56EVIxgvdQOzcB4gpZ1 yfsYFSwc8AvMq8zORl84chHeGlmmY2xeCFf90xFKD40Jix7F0V9/riqjyB0vQAWQCxO5mTrkVFp +bjD6uX09DD9M2Pylxg/4MMc+Dw0qu9iIFJ3J+joafOzxvds4qxfE5BBcjwJ3 X-Received: by 2002:a05:6808:141:b0:35a:640d:300e with SMTP id h1-20020a056808014100b0035a640d300emr2638008oie.19.1670605288865; Fri, 09 Dec 2022 09:01:28 -0800 (PST) X-Google-Smtp-Source: AA0mqf6z8153BBKEHldWTDZCjSK48fzRxQgu6nHidb9TN0qLtv3uxM5cswjC0U3ES+rGDimiirmJAQ== X-Received: by 2002:a05:6808:141:b0:35a:640d:300e with SMTP id h1-20020a056808014100b0035a640d300emr2637970oie.19.1670605288528; Fri, 09 Dec 2022 09:01:28 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id q7-20020a05620a0d8700b006cf38fd659asm178907qkl.103.2022.12.09.09.01.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Dec 2022 09:01:27 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Miaohe Lin , David Hildenbrand , Nadav Amit , peterx@redhat.com, Andrea Arcangeli , Jann Horn , John Hubbard , Mike Kravetz , James Houghton , Rik van Riel , Muchun Song Subject: [PATCH v3 8/9] mm/hugetlb: Make walk_hugetlb_range() safe to pmd unshare Date: Fri, 9 Dec 2022 12:00:59 -0500 Message-Id: <20221209170100.973970-9-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221209170100.973970-1-peterx@redhat.com> References: <20221209170100.973970-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain X-Rspamd-Queue-Id: C0B6340043 X-Stat-Signature: n3ihtgagnxgrwjs1y1b7d6rprwwn7btd X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1670605292-150471 X-HE-Meta: U2FsdGVkX19lwMI8/ydgpiRti+nPWwcutuOJQY6DCHbjG3deVa9qfD/mlc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eiSDj/BZVpCj48Qlg02h3A4kn1qJXQfP/RLJ6c5uKvaSIcgM3m+boZvZOzB1UT9GEXuJhc2xGUweADs/wuwnoyQHv24JCoZiqDAb8yEq38irc7sdWSWrTVI= 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: Since walk_hugetlb_range() walks the pgtable, it needs the vma lock to make sure the pgtable page will not be freed concurrently. Reviewed-by: Mike Kravetz Signed-off-by: Peter Xu Reviewed-by: John Hubbard --- include/linux/pagewalk.h | 11 ++++++++++- mm/hmm.c | 15 ++++++++++++++- mm/pagewalk.c | 2 ++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/linux/pagewalk.h b/include/linux/pagewalk.h index 959f52e5867d..27a6df448ee5 100644 --- a/include/linux/pagewalk.h +++ b/include/linux/pagewalk.h @@ -21,7 +21,16 @@ struct mm_walk; * depth is -1 if not known, 0:PGD, 1:P4D, 2:PUD, 3:PMD. * Any folded depths (where PTRS_PER_P?D is equal to 1) * are skipped. - * @hugetlb_entry: if set, called for each hugetlb entry + * @hugetlb_entry: if set, called for each hugetlb entry. This hook + * function is called with the vma lock held, in order to + * protect against a concurrent freeing of the pte_t* or + * the ptl. In some cases, the hook function needs to drop + * and retake the vma lock in order to avoid deadlocks + * while calling other functions. In such cases the hook + * function must either refrain from accessing the pte or + * ptl after dropping the vma lock, or else revalidate + * those items after re-acquiring the vma lock and before + * accessing them. * @test_walk: caller specific callback function to determine whether * we walk over the current vma or not. Returning 0 means * "do page table walk over the current vma", returning diff --git a/mm/hmm.c b/mm/hmm.c index 3850fb625dda..796de6866089 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -493,8 +493,21 @@ static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask, required_fault = hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags); if (required_fault) { + int ret; + spin_unlock(ptl); - return hmm_vma_fault(addr, end, required_fault, walk); + hugetlb_vma_unlock_read(vma); + /* + * Avoid deadlock: drop the vma lock before calling + * hmm_vma_fault(), which will itself potentially take and + * drop the vma lock. This is also correct from a + * protection point of view, because there is no further + * use here of either pte or ptl after dropping the vma + * lock. + */ + ret = hmm_vma_fault(addr, end, required_fault, walk); + hugetlb_vma_lock_read(vma); + return ret; } pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT); diff --git a/mm/pagewalk.c b/mm/pagewalk.c index 7f1c9b274906..d98564a7be57 100644 --- a/mm/pagewalk.c +++ b/mm/pagewalk.c @@ -302,6 +302,7 @@ static int walk_hugetlb_range(unsigned long addr, unsigned long end, const struct mm_walk_ops *ops = walk->ops; int err = 0; + hugetlb_vma_lock_read(vma); do { next = hugetlb_entry_end(h, addr, end); pte = huge_pte_offset(walk->mm, addr & hmask, sz); @@ -314,6 +315,7 @@ static int walk_hugetlb_range(unsigned long addr, unsigned long end, if (err) break; } while (addr = next, addr != end); + hugetlb_vma_unlock_read(vma); return err; }