From patchwork Sun Oct 30 21:30:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13025221 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 654D4FA3744 for ; Sun, 30 Oct 2022 21:30:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 080F18E0001; Sun, 30 Oct 2022 17:30:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 00AE96B0074; Sun, 30 Oct 2022 17:30:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DED718E0001; Sun, 30 Oct 2022 17:30:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CC5BF6B0071 for ; Sun, 30 Oct 2022 17:30:46 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id B1F781A0227 for ; Sun, 30 Oct 2022 21:30:46 +0000 (UTC) X-FDA: 80078910492.29.995F166 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id 4CBF21A000B for ; Sun, 30 Oct 2022 21:30:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667165445; 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=IiwOf4LgCWPWNLGKe48rMbCAvlS3n6l4UUSg+FeDzrg=; b=cDUvgteG8ZYU8c8YrXhT6MRjbT9Nir68rz5SJgER12jRMIxPKGXLR/JneyKKuRtpXJPuU5 sVSUKM9afDwQ8s+GQGm2m5JSoCZjTb8vE5T17aMpJyWMLgOy8pmE5M52szXGpqxxenBKcR Kq82nuHdZBXAic4JgjXagXGfaFluW+U= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-164-qnY4Ntr9Mqu9hcdkXngXHQ-1; Sun, 30 Oct 2022 17:30:35 -0400 X-MC-Unique: qnY4Ntr9Mqu9hcdkXngXHQ-1 Received: by mail-qk1-f197.google.com with SMTP id bp10-20020a05620a458a00b006fa29f253dcso1142224qkb.11 for ; Sun, 30 Oct 2022 14:30:35 -0700 (PDT) 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=IiwOf4LgCWPWNLGKe48rMbCAvlS3n6l4UUSg+FeDzrg=; b=G7DG5JehskZUkVDgKOagJPqBQUR+pdT+oB2VPzWbqZpTJ1L7ptF7csVgeN4HHTWu3x AHG5hP6CHwBOi71OC9YcqRZ12TZ3e9aiHRY5lNG3QsuYy24IhKFyVtOjTsb4zAIlxcHv sVDAUXHvq/yVJ7DQhWNmgOAaBmpRFTgJKCkJd/w2olbiAkk5W2uBFXOct6CHkv0SVswk CdkXqAEoOWmOwC0bmaJbfyXOqL75vn7cdGdmDsOWpkGv35JYXcnMxu2wWe3wCxrJd6Jm zs+G0BM7cYXSCwkoz9lvpaliCjBSTatJWwljXWoe8shLHTHJx/tr3PBond/xku782+3A dM3A== X-Gm-Message-State: ACrzQf08cu8kiXBiLkdJyc2jTi1CQzttYQbg3vjr5k5zIW2EK2NSMnhI bIH1hCAMDhxX/9cvfUZHaZxZDmwTqqhAVkddqSNkVzl3LLPSPrzvCyrDw+r0TVgT914shXOJ1Dr 56N2K8IFb53c= X-Received: by 2002:a05:620a:16c7:b0:6fa:313b:f0e0 with SMTP id a7-20020a05620a16c700b006fa313bf0e0mr325877qkn.137.1667165434555; Sun, 30 Oct 2022 14:30:34 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5tfUshtQQUT1K81sgbv4hWXmjMUpQkS9187l/2AQBRJVYTUcvFXxGStsp5n6ll6i2p3uKefg== X-Received: by 2002:a05:620a:16c7:b0:6fa:313b:f0e0 with SMTP id a7-20020a05620a16c700b006fa313bf0e0mr325867qkn.137.1667165434343; Sun, 30 Oct 2022 14:30:34 -0700 (PDT) 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 bm29-20020a05620a199d00b006e42a8e9f9bsm3469443qkb.121.2022.10.30.14.30.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Oct 2022 14:30:33 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Andrew Morton , Miaohe Lin , Muchun Song , Rik van Riel , James Houghton , Nadav Amit , Mike Kravetz , David Hildenbrand , Andrea Arcangeli , peterx@redhat.com Subject: [PATCH RFC 08/10] mm/hugetlb: Make follow_hugetlb_page RCU-safe Date: Sun, 30 Oct 2022 17:30:32 -0400 Message-Id: <20221030213032.335589-1-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221030212929.335473-1-peterx@redhat.com> References: <20221030212929.335473-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667165446; a=rsa-sha256; cv=none; b=lZR5e0kk+OhKf26+N8IPdYzAoafUwnthUEXHQB32kqphDAvLNdaqw/NHNtjDnNs1v9o87R Yhyq3oiNaT/tyxPXjeoyTxsW0GXoeKn/ww7Heo/PK4zwxrAwoD9N+NJkrMnDK86eokjOkR IrDXtp6s+2LhDvc2wqFKs+sgD3xrTJE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cDUvgteG; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667165446; 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=IiwOf4LgCWPWNLGKe48rMbCAvlS3n6l4UUSg+FeDzrg=; b=cnXTIltF8zKIly3AahEF5toRItBpmVziJJYAo+WS9RengfdKWmSb+D84A9l/hcI2xP935+ f360O2tHuoLrvBkZNQnesaVFADjzfhOaisXf7/qF691lKrJ+13TC1zE6O8AZ6t7UbpbHYS eS6mVRIBdHNNP5TphAQuemGDIutLkIA= X-Stat-Signature: d7dyb6p7tq5qpq5sc39wzmxzfypd17cp X-Rspamd-Queue-Id: 4CBF21A000B X-Rspam-User: Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cDUvgteG; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com X-Rspamd-Server: rspam09 X-HE-Tag: 1667165446-915107 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: RCU makes sure the pte_t* won't go away from under us. Please refer to the comment above huge_pte_offset() for more information. Some small trick is used to release RCU slightly earlier, but that should be safe and just cleaner (with rich comment in code). Signed-off-by: Peter Xu --- mm/hugetlb.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 85214095fb85..5dc87e4e6780 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6300,6 +6300,9 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, break; } + /* For huge_pte_offset() */ + rcu_read_lock(); + /* * Some archs (sparc64, sh*) have multiple pte_ts to * each hugepage. We have to make sure we get the @@ -6324,6 +6327,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, !hugetlbfs_pagecache_present(h, vma, vaddr)) { if (pte) spin_unlock(ptl); + rcu_read_unlock(); remainder = 0; break; } @@ -6345,6 +6349,8 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, if (pte) spin_unlock(ptl); + rcu_read_unlock(); + if (flags & FOLL_WRITE) fault_flags |= FAULT_FLAG_WRITE; else if (unshare) @@ -6387,6 +6393,19 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, continue; } + /* + * When reach here, it means the pteval is not absent, so + * anyone who wants to free and invalidate the pgtable page + * (aka, pte*) should need to first unmap the entries which + * relies on the pgtable lock. Since we're holding it, + * we're safe even without RCU anymore. + * + * We can also just release RCU after each unlock of + * pgtable below, but this is just much cleaner, and also + * smaller critical section. + */ + rcu_read_unlock(); + pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; page = pte_page(huge_ptep_get(pte));