Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1775601pxp; Mon, 7 Mar 2022 02:02:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJwCAVmR/Fk69qLEatJnk979HsxyUdXSeFBl+OXEufH7y6z4w9m3XvijIVxnyat+2AslgzHv X-Received: by 2002:a63:1d59:0:b0:380:6a04:89da with SMTP id d25-20020a631d59000000b003806a0489damr985094pgm.9.1646647364257; Mon, 07 Mar 2022 02:02:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646647364; cv=none; d=google.com; s=arc-20160816; b=JW/P46OJ4F6xxR8q1mRcJTR1QB+XL1G2LJfruFtsbESyeoT1pvSKCJ8bv5U660CweR CjhW0MI4pgnOFAeDEdILWiHEPwiafiCldHirfrIRWE6vp/kOIgYF+ezWlJMgyJdNvP4U HWSpWikcdT/O7Dx+4trGt8zxCOUDm+Sbn4dx8Bpkd1ugbVIr+Jsuoh2l284hC1eYFfwM G1riYbnDtTeDnJShzm0CYPV4ls8GfgY0+8r/loSAL9HbHl6T4847hsrFB9+PNq2MbR8n Tt9fr5S9G1k94vrJos9m3F8RIO0fOGrT8oOEBCBQmnewjw2DcHZCWG2p4xDFxdSGetlX m/iA== 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=XOKT7Gydmoi8+8TZUvO7UjIdr4W/rXm7m71ZoidEa/M=; b=TwIqULwS0ax2PYuVhi6uKVh5G0t2AGT2yTEm0uOVo5ftsZyjxqZFkD7XPvIXtOaZpn dwTdRShN3dpVwwIQ/gJrtxLCP3uo/GDibQWJSS6I0/5b7HOa+/6hJWVH7lplb+uIJud2 8bd6cedVsPdaMtKhMpBET5gC7Vk+mJCel5NDp56GJpmboLCHsdZqZsD/tb3vZVhb3yce 22VV3Kxu1FDmbQRFBtdo5I5Ve66GBZ9QdGhuDFtygpfb82tWJW2VsYzEjH0B3w962hai hgykUtxx8ZEl0lWLvkWc8JccfOMZPfFvWe8+rYlVwuAbgvFw+56COOsMWoiegzl1m3sS eaPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TdmY04xY; 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 g3-20020a056a001a0300b004f7090d5e40si1641007pfv.115.2022.03.07.02.02.30; Mon, 07 Mar 2022 02:02:44 -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=TdmY04xY; 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 S236646AbiCGJYk (ORCPT + 99 others); Mon, 7 Mar 2022 04:24:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237385AbiCGJXz (ORCPT ); Mon, 7 Mar 2022 04:23:55 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C64654F89; Mon, 7 Mar 2022 01:23:01 -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 48749B80E70; Mon, 7 Mar 2022 09:23:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7747BC340E9; Mon, 7 Mar 2022 09:22:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646644979; bh=9k9TnfFJxozCmdGaI4LQIZC5djFmfCDCM4ic8RTYkgs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TdmY04xYnW6AyTlJuJHq6eyNKYR9jVYEHG13iLjHc5nZMqMn55R63uCYiu7eF3T1e fiHMhcnAJWQoPDc3uxfspxxLfFTkjVCI7EkcJfCon0VuokGG8O50ro7JdHZPq8cUJj sP52WxhiQ6QvLgF4lYqBtE1a1yRyzrZu6lqjl9m0= 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.14 41/42] memfd: fix F_SEAL_WRITE after shmem huge page allocated Date: Mon, 7 Mar 2022 10:19:15 +0100 Message-Id: <20220307091637.348617653@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220307091636.146155347@linuxfoundation.org> References: <20220307091636.146155347@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/shmem.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2689,7 +2689,8 @@ static void shmem_tag_pins(struct addres slot = radix_tree_iter_retry(&iter); continue; } - } else if (page_count(page) - page_mapcount(page) > 1) { + } else if (!PageTail(page) && page_count(page) != + hpage_nr_pages(page) + total_mapcount(page)) { radix_tree_tag_set(&mapping->page_tree, iter.index, SHMEM_TAG_PINNED); } @@ -2749,8 +2750,8 @@ static int shmem_wait_for_pins(struct ad page = NULL; } - if (page && - page_count(page) - page_mapcount(page) != 1) { + if (page && page_count(page) != + hpage_nr_pages(page) + total_mapcount(page)) { if (scan < LAST_SCAN) goto continue_resched;