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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46E5BC4332F for ; Fri, 18 Nov 2022 01:17:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BF19C6B0071; Thu, 17 Nov 2022 20:17:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BA2076B0072; Thu, 17 Nov 2022 20:17:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A42D98E0001; Thu, 17 Nov 2022 20:17:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 92B026B0071 for ; Thu, 17 Nov 2022 20:17:55 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5C4BDC0B6B for ; Fri, 18 Nov 2022 01:17:55 +0000 (UTC) X-FDA: 80144801310.06.38D6C36 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf06.hostedemail.com (Postfix) with ESMTP id DFF7E180002 for ; Fri, 18 Nov 2022 01:17:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668734274; 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: in-reply-to:in-reply-to:references:references; bh=J2VQywQIWlw8LxZeusGSWX+0k1dWP0sWzZLRZxVp07M=; b=H0tQ3xqH+1e6flFVHtz3/ewhZHoUCgXQ6ltMaU6IzlfpjHqabGM8zhJY9daNztKaXl4xK5 yrrt43ecelhbfoyxpkLCUgWY99Re+bpV/Zrke8vfI/0oVGQb9EJrqOACGg4L35Yom2wzt0 BOEjQE4eOFW234/tEtSnuW9Owx3gxE0= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-657-xtHwF9Q5MYO7OI-DvzJIEw-1; Thu, 17 Nov 2022 20:17:53 -0500 X-MC-Unique: xtHwF9Q5MYO7OI-DvzJIEw-1 Received: by mail-qv1-f71.google.com with SMTP id u18-20020a0cec92000000b004c6912ea1baso1381481qvo.5 for ; Thu, 17 Nov 2022 17:17:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=J2VQywQIWlw8LxZeusGSWX+0k1dWP0sWzZLRZxVp07M=; b=FUqB3EyllswUqra1GeVkW9C77GX0o9thYeJdxAJD3/kWw4bJaL9KeUznslhcuzSD4N j1hamH7gbBa/PT1izi4YCKkc4LIRKXmwcHx+CK7tjmgJB5pGDGabF3EYHnFdY+APXCM7 ZisD/B+sh+X2dtDHehl29/hSeWdz+MskYz8p7HznXBjPL/NLgyXT/5BpeHPfeuaJKm1W sa/7wb+83gP2q0lj9ALoMm0j1tqLjmVwkfXs2A5zldacGp7luIqRdL6R4Ffo5GJHTc2j lnvggAYRvi45YPO8bCAC+SnIDSuaBAfGxzXEtwosTMHSyVgr15TuAvEg9Rgy2CVlgTkO csdA== X-Gm-Message-State: ANoB5pkBQpwBKFF1Vk9NTV5Gom1pooW33Qu7v3+stuoC0S1Y05p/8KEZ KHxLeOmaC1G5sG0TLxqmNusCpiNSZczNceahBSXHrAo/Pa/1546fXhgT4ILNbbyl5zaoPGb4whR 9JeZkkxGzeGs1y58nBhxu3s8md9XaQyyLcuamb+v/IJ4ZOa0KBSKcKzStUHV2 X-Received: by 2002:a37:603:0:b0:6fa:81:6aa4 with SMTP id 3-20020a370603000000b006fa00816aa4mr4309777qkg.148.1668734272555; Thu, 17 Nov 2022 17:17:52 -0800 (PST) X-Google-Smtp-Source: AA0mqf7Ms4iaoqqE6cRnnYtO9svOrWT9cOf9t6bK7uiuSOSBy2IQRImtO7tU2zGzqIWZhdMeP+8I5w== X-Received: by 2002:a37:603:0:b0:6fa:81:6aa4 with SMTP id 3-20020a370603000000b006fa00816aa4mr4309750qkg.148.1668734272253; Thu, 17 Nov 2022 17:17:52 -0800 (PST) Received: from x1n (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id k21-20020ac86055000000b003a50c9993e1sm1226791qtm.16.2022.11.17.17.17.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 17:17:51 -0800 (PST) Date: Thu, 17 Nov 2022 20:17:50 -0500 From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Rik van Riel , Muchun Song , Andrew Morton , James Houghton , Nadav Amit , Andrea Arcangeli , David Hildenbrand , Miaohe Lin , Mike Kravetz Subject: Re: [PATCH RFC v2 06/12] mm/hugetlb: Protect huge_pmd_share() with walker lock Message-ID: References: <20221118011025.2178986-1-peterx@redhat.com> <20221118011025.2178986-7-peterx@redhat.com> MIME-Version: 1.0 In-Reply-To: <20221118011025.2178986-7-peterx@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=H0tQ3xqH; spf=pass (imf06.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=1668734275; a=rsa-sha256; cv=none; b=6T4bLgqRI11e2kX4S+xpyKdCLyFH0DO69m8rkIkBtX1hwXVMObr+EdIKdTUzihVAj63OJO 5qHH9rEePkpkNQfKsyQXjE7/5tdMUZQk+Q7CTv4RepkIhhI9evM6urjlG92aY+hQBSF4a9 LXRHJmYNShpcGzs/Im/GvRj+vUB/Wa4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668734275; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=J2VQywQIWlw8LxZeusGSWX+0k1dWP0sWzZLRZxVp07M=; b=qTaXDkhCEcs3XU/wTEu86qIoAQ7uEJITyM3gyyJUBv/71Q3FL5Z8sjtazvIN13lyczOoQ1 HS4DJzdIfcx1sEZ2m6HWvgtJAC1qnSD4UqPZ5fR5CBoValXFLBOs7LXvvZFilZEthSYv5r Hwf4BGPldX8UBaX2VY5RMeDCZT3OjzU= Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=H0tQ3xqH; spf=pass (imf06.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 X-Rspamd-Server: rspam02 X-Rspam-User: X-Stat-Signature: shjzmmj4367jz4yoj7i47ofxjnqerkru X-Rspamd-Queue-Id: DFF7E180002 X-HE-Tag: 1668734274-855851 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: On Thu, Nov 17, 2022 at 08:10:19PM -0500, Peter Xu wrote: > huge_pmd_share() is normally called with vma lock held already, it lets us > feel like we don't really need the walker lock. But that's not true, > because we only took the vma lock for "current" vma, but not all the vma > pgtables we're going to walk upon. > > Taking each vma lock may lead to deadlock and hard to order. The safe > approach is just to use the walker lock which guarantees the pgtable page > being alive, then we should use get_page_unless_zero() rather than > get_page() only, to make sure the pgtable page is not being freed at the > meantime. > > Signed-off-by: Peter Xu > --- > mm/hugetlb.c | 16 +++++++++++++--- > 1 file changed, 13 insertions(+), 3 deletions(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 61a1fa678172..5ef883184885 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -7008,6 +7008,13 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, > spinlock_t *ptl; > > i_mmap_lock_read(mapping); > + > + /* > + * NOTE: even if we've got the vma read lock, here we still need to > + * take the walker lock, because we're not walking the current vma, > + * but some other mm's! > + */ > + hugetlb_walker_lock(); > vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { > if (svma == vma) > continue; > @@ -7016,12 +7023,15 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, > if (saddr) { > spte = huge_pte_offset(svma->vm_mm, saddr, > vma_mmu_pagesize(svma)); > - if (spte) { > - get_page(virt_to_page(spte)); > + /* > + * When page ref==0, it means it's probably being > + * freed; continue with the next one. > + */ > + if (spte && get_page_unless_zero(virt_to_page(spte))) > break; > - } > } > } > + hugetlb_walker_unlock(); > > if (!spte) > goto out; > -- > 2.37.3 > Will squash this in.. diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 6c77ae7a3d94..f9abede9de47 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -7045,6 +7045,8 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, */ if (spte && get_page_unless_zero(virt_to_page(spte))) break; + else + spte = NULL; } } hugetlb_walker_unlock(); -- Peter Xu