From patchwork Fri Nov 18 01:10:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13047539 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 5F651C433FE for ; Fri, 18 Nov 2022 01:10:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 149A18E0007; Thu, 17 Nov 2022 20:10:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0FB1B8E0005; Thu, 17 Nov 2022 20:10:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DF1EB8E0007; Thu, 17 Nov 2022 20:10:40 -0500 (EST) 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 C87D28E0005 for ; Thu, 17 Nov 2022 20:10:40 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A4071A0290 for ; Fri, 18 Nov 2022 01:10:40 +0000 (UTC) X-FDA: 80144783040.24.DDED779 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 4F8122000C for ; Fri, 18 Nov 2022 01:10:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668733839; 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=ek0v3IZFacfv4QAamvdXXDg+rLS9iFnyxQu22HNSG2w=; b=cUNduIw9p51c1oOsfMI+bUWHghF9FF2t4a8jDrG+pEkKu6xK8I6ie26PDp0Dud4vEgODm+ r4YxtMFaUKQv1244x/zk0gNMdEQjG+hdabLjTZxjSsZ1/wp93fpxN1Spt6wcMFB2lIQoh3 d5FxLy9hJSkscqeA3Qtk5/2sAqXWFfo= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-267-cwQ99g8VN02nXnA_Ot8YCg-1; Thu, 17 Nov 2022 20:10:38 -0500 X-MC-Unique: cwQ99g8VN02nXnA_Ot8YCg-1 Received: by mail-qk1-f199.google.com with SMTP id w4-20020a05620a444400b006fa24b2f394so4362556qkp.15 for ; Thu, 17 Nov 2022 17:10:38 -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=ek0v3IZFacfv4QAamvdXXDg+rLS9iFnyxQu22HNSG2w=; b=CVIRLIxQKXvUUp3Kc6YU3etWzNp18sCNDhkrT6JsFey4pfpgPdcQLUASM8IJkTHyE+ twQVrQ+vEmySz9dR1MKSPHgJKpvxMjdHdhqwy2Ay12Y6fVMQT8vKuRcYumT4jrYjtVuu JooKXMOYOvYqte63fN5d66f0TdydjoF5bS0sIOYbPn2t8OsGrfFVPjL4gNUxW0sbEENd 44H3DgrHqlDlB4yazlR0yDRO5mPsd1W8gDMN8c2NDYHBGvKvCPtO53ggPdLtAOQv4MLp uG27NFrnASiWqhuOb217RIDy3HqsCPa1sKmTClmFOu6uSgCWjbQE3wtED3JizN9ALac7 +gCg== X-Gm-Message-State: ANoB5pkqWxRjrDjGRXOx8Wz1tukxGJWHMCKnpRPFhZtqFky3Cnd9q8k5 5oXOtt2LgBNsLw//3dTyvI3uJSCane4BAZ11gN7YJ4XzE23ALiDPTfAX5AUxQ4tDQwYJrCC66/z VC0vbkxR4cycXVlyfUhx1pprS+ti4L5+7QdU3I6e7jZT4aqWBwtgmZRcttySW X-Received: by 2002:ac8:7608:0:b0:3a5:6797:c57a with SMTP id t8-20020ac87608000000b003a56797c57amr4788577qtq.34.1668733838017; Thu, 17 Nov 2022 17:10:38 -0800 (PST) X-Google-Smtp-Source: AA0mqf4YUHL//KLcKTO3eXesn/AhPVyYy7y3vCnFwX7NxkFnL7BxzMVqhjH97L9Gr/XOknNuKwkO9w== X-Received: by 2002:ac8:7608:0:b0:3a5:6797:c57a with SMTP id t8-20020ac87608000000b003a56797c57amr4788551qtq.34.1668733837733; Thu, 17 Nov 2022 17:10:37 -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.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 17:10:37 -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 06/12] mm/hugetlb: Protect huge_pmd_share() with walker lock Date: Thu, 17 Nov 2022 20:10:19 -0500 Message-Id: <20221118011025.2178986-7-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-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cUNduIw9; spf=pass (imf03.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=1668733840; a=rsa-sha256; cv=none; b=pjlx/U/hjAIhpPckIDAn8CJtqC6PjiJ/h3EOVT35UDHs4ontI5EQHiURxsvR29IkQpeMk3 lB0KMVcsqkVPBh8NtSGZjW14I+I483hdtUbL1lfc33Dw3YvuPZ2bM+pHu9VgJFP8O2MN0x wripCyIkeyEyD5wx+Gq07WcOef4LSlg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668733840; 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=ek0v3IZFacfv4QAamvdXXDg+rLS9iFnyxQu22HNSG2w=; b=o1VuqzyKqu1zcRm4TnMDBaxcQzlYvsJlK2W/+mPUIpY8GrWfGNfxNXo/5fb+NmNAqjEQ8H +fPqi/oYiCH7JYrED1892Xze6zsZSku/u/L2EVjXBh5SeUoRuHfn+Hzjj9zK6u6Y2Rqice WNV49dus4KIOWxc85yOaPPPZC1Vek3w= Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cUNduIw9; spf=pass (imf03.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: pe7zzzex1dhgetehpa64mw3jbduhksje X-Rspamd-Queue-Id: 4F8122000C X-HE-Tag: 1668733840-233843 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: 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;