Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp1105404rdh; Fri, 24 Nov 2023 05:30:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IG2crTo2fztfRVKTkKMdi2K52gjVsWMPm8auGlDRQ/hwp2rwG3IGEMhR0KJZF/7yEmVr8Zy X-Received: by 2002:a05:6a20:3c91:b0:187:3643:ecce with SMTP id b17-20020a056a203c9100b001873643eccemr3499098pzj.56.1700832600058; Fri, 24 Nov 2023 05:30:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700832600; cv=none; d=google.com; s=arc-20160816; b=TbVZWYX7kw1LFnHEXRvwJz0SnZ0glAZxmxv8e6U3uaiDIwexXJoLPwtbzEhU/qqclU xlQQz1uKyRwKhnCswnnpMSgra+Ejqtvd8V0pB+Co3Y3mMMF3vfkNeJHdJchsYI2PKYVI BQYrtJsXoR8KJ/GNf3AQlRk4TKsXha5XycnsduKZ46YkPJCXGTWGoWkyqmAJll+jp78N Vd+XT3YlMbb3bB5MFBxAdulY8r4ly7MWKjd7K6rIAIUstLduRxGTW4g61XRMG/BZ24yL p+EZt7PozCLohLVPhGCCzVZatoIQ3fmPZ33M5q67C4/+ICoFeWRvRujK2nDr1Qrv8/P0 uA0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8wkdTzr4ovr6JqvWUuBFMPrmTwPV2fAdlmREPfnLqjk=; fh=UVV9UP3jB+jR+DJP/Pn6IwXb1P9vDh5E3FRL6G+mlkU=; b=0j17+20a6N89IgfVXu9QnG9A5t37ulMUPSevUmleXgCfV5Lr3yR0xYKeJbMOAkU7hf MLf/ncO0NFHwGr5HvinP7+r2IFpPlh6Wp+lnLv0l6FG73SU4y4ZxcNNpq24e7+gbnb0h v5g+vMATeaTeKWV+YphnynV+jYLdqtEHniQiswvMtWkqiB4tf9GuTIwxP8ASeRkzcdCQ 0azMFPoCRh5QUgimLKQRilptcf71UjQNP/hKM2s1Okd6EphpKm2iB/Q1ExObuI5seNPo D39pQUvko/2lvxiHZX6GZ0u4hQVCZWXqOp95mH4MihD18RgyuuKeIGR7vTW1Gvc2U2YM z6dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=P3OryhCw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id p32-20020a056a000a2000b006cbb1347e8esi3683740pfh.257.2023.11.24.05.29.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 05:30:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=P3OryhCw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 65F758047543; Fri, 24 Nov 2023 05:29:47 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345427AbjKXN2m (ORCPT + 99 others); Fri, 24 Nov 2023 08:28:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230104AbjKXN2S (ORCPT ); Fri, 24 Nov 2023 08:28:18 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CA4F2105 for ; Fri, 24 Nov 2023 05:27:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700832451; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8wkdTzr4ovr6JqvWUuBFMPrmTwPV2fAdlmREPfnLqjk=; b=P3OryhCwrrW3/y4KdyQKeVXl4ZsGXGCqICZDVyfWOr3qPxyr9OAPSTyhSzJPwo15ndIdhf M9IMRawBLym9dumKCK8uEAQfKEZt9+p2UK+VeGksUrqDmGNIoljzhaRSXa/a2ERoC8pkii QVZwgR8zn6qn35NAhZmGrVTX0kFoxO4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-631-Mzyg-U9HMR2q8VnSaYE9mw-1; Fri, 24 Nov 2023 08:27:23 -0500 X-MC-Unique: Mzyg-U9HMR2q8VnSaYE9mw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7F79C85A58C; Fri, 24 Nov 2023 13:27:22 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id E251F2166B2B; Fri, 24 Nov 2023 13:27:18 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Linus Torvalds , Ryan Roberts , Matthew Wilcox , Hugh Dickins , Yin Fengwei , Yang Shi , Ying Huang , Zi Yan , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , "Paul E. McKenney" Subject: [PATCH WIP v1 14/20] mm/huge_memory: avoid folio_refcount() < folio_mapcount() in __split_huge_pmd_locked() Date: Fri, 24 Nov 2023 14:26:19 +0100 Message-ID: <20231124132626.235350-15-david@redhat.com> In-Reply-To: <20231124132626.235350-1-david@redhat.com> References: <20231124132626.235350-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Fri, 24 Nov 2023 05:29:47 -0800 (PST) Currently, there is a short period in time where the refcount is smaller than the mapcount. Let's just make sure we obey the rules of refcount vs. mapcount: increment the refcount before incrementing the mapcount and decrement the refcount after decrementing the mapcount. While this could make code like can_split_folio() fail to detect other folio references, such code is (currently) racy already and this change shouldn't actually be considered a real fix but rather an improvement/ cleanup. The refcount vs. mapcount changes are now well balanced in the code, with the cost of one additional refcount change, which really shouldn't matter here that much -- we're usually touching >= 512 subpage mapcounts and much more after all. Found while playing with some sanity checks to detect such cases, which we might add at some later point. Signed-off-by: David Hildenbrand --- mm/huge_memory.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f47971d1afbf..9639b4edc8a5 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2230,7 +2230,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, if (!freeze) { rmap_t rmap_flags = RMAP_NONE; - folio_ref_add(folio, HPAGE_PMD_NR - 1); + folio_ref_add(folio, HPAGE_PMD_NR); if (anon_exclusive) rmap_flags = RMAP_EXCLUSIVE; folio_add_anon_rmap_range(folio, page, HPAGE_PMD_NR, @@ -2294,10 +2294,10 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, } pte_unmap(pte - 1); - if (!pmd_migration) + if (!pmd_migration) { page_remove_rmap(page, vma, true); - if (freeze) put_page(page); + } smp_wmb(); /* make pte visible before pmd */ pmd_populate(mm, pmd, pgtable); -- 2.41.0