From patchwork Fri Nov 18 01:10:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13047541 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 E01FDC43217 for ; Fri, 18 Nov 2022 01:10:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 73F868E0009; Thu, 17 Nov 2022 20:10:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 651528E0005; Thu, 17 Nov 2022 20:10:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3923F8E0009; Thu, 17 Nov 2022 20:10:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 240D58E0005 for ; Thu, 17 Nov 2022 20:10:44 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id ED35B1202F1 for ; Fri, 18 Nov 2022 01:10:43 +0000 (UTC) X-FDA: 80144783166.26.26894B2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf13.hostedemail.com (Postfix) with ESMTP id A853220008 for ; Fri, 18 Nov 2022 01:10:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668733843; 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=qjnChz0h9yEvsDVybPnS+PQNkUKEm7fVo3IYCZg/FQ4=; b=g1Vpboepen4xbDn22+iuycZ5V1QEIwfomXwZs+IbsweuK5PGyQGONzXprAIvVj4Pu+X2MQ gFhC8uW0zrDKBpoad5fQffJgNOzSDcRqzTYPYcF1Zlwi3rFsWoVeZ4F2fBUP+SG+karOOC dafZzC8vfFbm+VjhDOxtO5WR5Hp9iVA= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-570-YwwT6QYPP_uLWAwQH8uxwQ-1; Thu, 17 Nov 2022 20:10:41 -0500 X-MC-Unique: YwwT6QYPP_uLWAwQH8uxwQ-1 Received: by mail-qt1-f198.google.com with SMTP id y19-20020a05622a121300b003a526e0ff9bso3387348qtx.15 for ; Thu, 17 Nov 2022 17:10:41 -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=qjnChz0h9yEvsDVybPnS+PQNkUKEm7fVo3IYCZg/FQ4=; b=4aGeFl3Yx1Pgo1l85QQDxfzJ6kOoaKdZPiPY0z43f5u/DobCs68eRIeRHpD0U82ibV uUh3f8HPgkl4zxch81rFsyLP/hYRcHOegH1i4coGsu2n5X8+IxCrwMEMMQ7kylQuhgSP XFrJEgIbD+/TPLCH14dbIYFVCYZ49ZYQuDHWtY7W4A509ilybQn2YwR2VY9S333UB84j yYH0xCC4lP1pkUlSq8SI7f5+vCmJEqkmx7KIjbq2KKa39D1XTGYz8003UDyhGpt0Bx1o gzjp6jDTdnAZRpTrgui4iXiXuhG74vTNSL+R4ElNCrVBFdwf5F3qqxb+LcBQf7L4YX69 p46w== X-Gm-Message-State: ANoB5ploweYqH+dWH/OUgJFAYZwW+PJZNRdhEqruA6lZMalB1K8ALNJM cbCyt7WX9xdcj7NGl8mDQZjxjUMiNWXNX1AVTbVOvfiDFRxeJoUT8x2S4zfE27ctkukTe3fwZQR +NECb3fVfTz3oQVaR8rm6nue6yYVlMm1rcEf4uixFGboM8w2PuFltfzQa1RjA X-Received: by 2002:a05:620a:c04:b0:6cf:7988:75a9 with SMTP id l4-20020a05620a0c0400b006cf798875a9mr4033260qki.613.1668733841079; Thu, 17 Nov 2022 17:10:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf54cqseGLR98vZZaTQtgNM/mQInkImtMr4ZFy3PyyVl91SKAmCECTcH2P+eiXPqv9u2h3rDQw== X-Received: by 2002:a05:620a:c04:b0:6cf:7988:75a9 with SMTP id l4-20020a05620a0c0400b006cf798875a9mr4033233qki.613.1668733840789; Thu, 17 Nov 2022 17:10:40 -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 u7-20020a05620a430700b006eed75805a2sm1491342qko.126.2022.11.17.17.10.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 17:10:40 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Rik van Riel , Muchun Song , Andrew Morton , peterx@redhat.com, James Houghton , Nadav Amit , Andrea Arcangeli , David Hildenbrand , Miaohe Lin , Mike Kravetz Subject: [PATCH RFC v2 08/12] mm/hugetlb: Use hugetlb walker lock in follow_hugetlb_page() Date: Thu, 17 Nov 2022 20:10:21 -0500 Message-Id: <20221118011025.2178986-9-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221118011025.2178986-1-peterx@redhat.com> References: <20221118011025.2178986-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=1668733843; a=rsa-sha256; cv=none; b=19i+7As+TOHHZAKKrkasx8fdfXmKfSBgRFyC1vLyZFiuHwTCCaZ7r2tBCSxbgQDflii8Pp TVSJdo1J9ciUdzKwekcqvq/CFv2D9yWQaX1ct/9eoTGLLrD440TY3RNjlaDeYv8CbfEuOy WmwQP3ss5AP/WNMeMdPxrpisU0ZKjLA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=g1Vpboep; spf=pass (imf13.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1668733843; 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=qjnChz0h9yEvsDVybPnS+PQNkUKEm7fVo3IYCZg/FQ4=; b=sW1OW+ISM2Nd8nYKWmCDzPXU9m+lzcPkr2aVEmpg791EYU/cGkLohp+j/XGQjZErK4GBSd X80iNnfGQJROvF8R86qnP8+K/nVVHIo/Bknwz8uL8fc4lJQjxu0eUVWIgxEaNd3Nj3aCpW XVT0K2N3c4sXZgKoyNJerk3hdJ9pIFo= X-Stat-Signature: ydai7i4ycnr8wcb9cgobqfns6aw7hj6i X-Rspamd-Queue-Id: A853220008 Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=g1Vpboep; spf=pass (imf13.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1668733843-182570 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: Hugetlb walker lock makes sure the pte_t* won't go away from under us. Some trick is used to release the walker lock slightly earlier when we found present pte. Signed-off-by: Peter Xu --- mm/hugetlb.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index fc49e3ca8acd..e81af6a46c59 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6280,6 +6280,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, break; } + hugetlb_walker_lock(); /* * Some archs (sparc64, sh*) have multiple pte_ts to * each hugepage. We have to make sure we get the @@ -6304,6 +6305,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); + hugetlb_walker_unlock(); remainder = 0; break; } @@ -6325,6 +6327,8 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, if (pte) spin_unlock(ptl); + hugetlb_walker_unlock(); + if (flags & FOLL_WRITE) fault_flags |= FAULT_FLAG_WRITE; else if (unshare) @@ -6367,6 +6371,15 @@ 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 the walker lock anymore. + */ + hugetlb_walker_unlock(); + pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; page = pte_page(huge_ptep_get(pte));