Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1782411pxp; Mon, 7 Mar 2022 02:11:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJzX6Kiy3UITyjd/q4mqSvbRCb8SxrmUVvGoWCQ/g7est6yj0Fjgq16Xy1n/ij8eXBcMPVjD X-Received: by 2002:a17:907:60d5:b0:6da:a62d:1845 with SMTP id hv21-20020a17090760d500b006daa62d1845mr8714175ejc.154.1646647891715; Mon, 07 Mar 2022 02:11:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646647891; cv=none; d=google.com; s=arc-20160816; b=zt/BqjZOoujMOtL74vuSlf05TIhZjdnRroeqA4V5Lbyx/tUuaAcuQYHlHrS/5ptiDB Bs4EmWpJAg0IIBdj5z4y9SSea7M+JR5aYKCmSYGu01GInYYghagciGOxqTpwEFL16w/I ql9oFYLUzpB+4JqfR2r2GvZ0+icfHYhwjvUad2nb6DkVprE3PZrhmmtyFUFXGzNe2ckR 9by8okTO8kR/bMA9Dc/ATe9YtrEpCYfwaWFWSiYwtBPAC1MA8gVkQ/hd/FX44kSxHM6/ XGDcJXpkZgjmK48CZXYpErA+TOv84qqwqWQ/+ePsUvY1kKJ2P3UVCUIp2YtaWDIasD+z ew8w== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wc7rh4iGdR4bHPFSdLtTtQu+cMPM2UNjGHscyWDwh8s=; b=Tk3tcmz1fHvKgPWJKIoQrPEf1bGAdk54eYu69kgCu9XH+OULOtpuqYLKKygtleC0iA r5AORwt6NYN7p6amtZFwiCU2uoz53YyESmw+UcvnJZ7csahugHnSV4v3sMx51i466lLp shlBVXzhR+Zj8sb4D2Om+42Y6bZD5phdqmHD3Jpg3FQM3hbiBTEHDwRZ5p38duCdBXJF u/LMEBhxpRXKYpbR7XGI5X5dYd3GPc40qxee2/pLFXByGlzvSayiqITtdbk/4Coj03cQ pH4/PkRFw3eMRVUmecFOXrKOH8hkvaxx6+2dihoAH+/MOf+ZzC8CbRQKpvqa5lLKDbS2 ZPLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OQSvIZgU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gs41-20020a1709072d2900b006da8ec5d385si8425879ejc.999.2022.03.07.02.11.09; Mon, 07 Mar 2022 02:11:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OQSvIZgU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237618AbiCGJfm (ORCPT + 99 others); Mon, 7 Mar 2022 04:35:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237539AbiCGJ2N (ORCPT ); Mon, 7 Mar 2022 04:28:13 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE96D6AA58; Mon, 7 Mar 2022 01:25:35 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 60E05B810B6; Mon, 7 Mar 2022 09:25:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 71D73C340E9; Mon, 7 Mar 2022 09:25:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646645133; bh=RA5MjAREVeLlyf8DR2wQTDiIpoO9krt7YPldP0t9j60=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OQSvIZgUUEKNnXo31o/+yosHBj2WYmbST0WyeLKBQHjiJt/uvfstbCbYVDJewymVr eAjBzPw3/M+3/UAzdTanTADIEEgOf5icAq04CiFVPOvexkarWTFGl+I9K53h6xwv5F +hIp74gFJ08kP0xIaCRp7UG87RwTMNo6JSFuYT60= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hugh Dickins , Zeal Robot , wangyong , Mike Kravetz , "Matthew Wilcox (Oracle)" , CGEL ZTE , "Kirill A. Shutemov" , Song Liu , Yang Yang , Andrew Morton , Linus Torvalds Subject: [PATCH 4.19 48/51] memfd: fix F_SEAL_WRITE after shmem huge page allocated Date: Mon, 7 Mar 2022 10:19:23 +0100 Message-Id: <20220307091638.357126249@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220307091636.988950823@linuxfoundation.org> References: <20220307091636.988950823@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hugh Dickins commit f2b277c4d1c63a85127e8aa2588e9cc3bd21cb99 upstream. Wangyong reports: after enabling tmpfs filesystem to support transparent hugepage with the following command: echo always > /sys/kernel/mm/transparent_hugepage/shmem_enabled the docker program tries to add F_SEAL_WRITE through the following command, but it fails unexpectedly with errno EBUSY: fcntl(5, F_ADD_SEALS, F_SEAL_WRITE) = -1. That is because memfd_tag_pins() and memfd_wait_for_pins() were never updated for shmem huge pages: checking page_mapcount() against page_count() is hopeless on THP subpages - they need to check total_mapcount() against page_count() on THP heads only. Make memfd_tag_pins() (compared > 1) as strict as memfd_wait_for_pins() (compared != 1): either can be justified, but given the non-atomic total_mapcount() calculation, it is better now to be strict. Bear in mind that total_mapcount() itself scans all of the THP subpages, when choosing to take an XA_CHECK_SCHED latency break. Also fix the unlikely xa_is_value() case in memfd_wait_for_pins(): if a page has been swapped out since memfd_tag_pins(), then its refcount must have fallen, and so it can safely be untagged. Link: https://lkml.kernel.org/r/a4f79248-df75-2c8c-3df-ba3317ccb5da@google.com Signed-off-by: Hugh Dickins Reported-by: Zeal Robot Reported-by: wangyong Cc: Mike Kravetz Cc: Matthew Wilcox (Oracle) Cc: CGEL ZTE Cc: Kirill A. Shutemov Cc: Song Liu Cc: Yang Yang Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/memfd.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) --- a/mm/memfd.c +++ b/mm/memfd.c @@ -34,26 +34,35 @@ static void memfd_tag_pins(struct addres void __rcu **slot; pgoff_t start; struct page *page; - unsigned int tagged = 0; + int latency = 0; + int cache_count; lru_add_drain(); start = 0; xa_lock_irq(&mapping->i_pages); radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) { + cache_count = 1; page = radix_tree_deref_slot_protected(slot, &mapping->i_pages.xa_lock); - if (!page || radix_tree_exception(page)) { + if (!page || radix_tree_exception(page) || PageTail(page)) { if (radix_tree_deref_retry(page)) { slot = radix_tree_iter_retry(&iter); continue; } - } else if (page_count(page) - page_mapcount(page) > 1) { - radix_tree_tag_set(&mapping->i_pages, iter.index, - MEMFD_TAG_PINNED); + } else { + if (PageTransHuge(page) && !PageHuge(page)) + cache_count = HPAGE_PMD_NR; + if (cache_count != + page_count(page) - total_mapcount(page)) { + radix_tree_tag_set(&mapping->i_pages, + iter.index, MEMFD_TAG_PINNED); + } } - if (++tagged % 1024) + latency += cache_count; + if (latency < 1024) continue; + latency = 0; slot = radix_tree_iter_resume(slot, &iter); xa_unlock_irq(&mapping->i_pages); @@ -79,6 +88,7 @@ static int memfd_wait_for_pins(struct ad pgoff_t start; struct page *page; int error, scan; + int cache_count; memfd_tag_pins(mapping); @@ -107,8 +117,12 @@ static int memfd_wait_for_pins(struct ad page = NULL; } - if (page && - page_count(page) - page_mapcount(page) != 1) { + cache_count = 1; + if (page && PageTransHuge(page) && !PageHuge(page)) + cache_count = HPAGE_PMD_NR; + + if (page && cache_count != + page_count(page) - total_mapcount(page)) { if (scan < LAST_SCAN) goto continue_resched;