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 754D7C05027 for ; Thu, 26 Jan 2023 09:15:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0A9688E0001; Thu, 26 Jan 2023 04:15:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 07F856B0072; Thu, 26 Jan 2023 04:15:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E62758E0001; Thu, 26 Jan 2023 04:15:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D7FC06B0071 for ; Thu, 26 Jan 2023 04:15:52 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id AB8FB1402BA for ; Thu, 26 Jan 2023 09:15:52 +0000 (UTC) X-FDA: 80396392944.22.358CF1A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 76AFD140002 for ; Thu, 26 Jan 2023 09:15:49 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iccx0k15; spf=pass (imf09.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674724549; 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=xdoN5yzxVJEREZqW/URQXy+ilieYA2ZDeF7R/orA9d4=; b=wTwZFQ+XH+gfxxtFuAD/fyZRCwOths+bT9JEtasd3z3lDME1XT1/NTS6vtKZx30crPRgdT k8Q6J7WUAMvibd6akqBSlhqMy709/yIBi2LITpafW/MkI2Nv6QCJXbh01RDMxu4nrix5ax RMOQXUysN6sK/fRqKe4s0UtUevg9nnU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iccx0k15; spf=pass (imf09.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674724549; a=rsa-sha256; cv=none; b=MExzJhX+XC2n9eD4vg84RJzr02h1H7lXluwm6bW2RaKowIju4Yb/RMePBjN63sNqlHC0vv 83OwYpRp2iYVVfVhCI72G8XBqqrl80m1nheQxMVfT7pKCDO2xPNf+8pm945K6+tsTIjkTe vPyCq/59XqGZmzcKTMlW4+zK6pDzbzA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674724548; 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=xdoN5yzxVJEREZqW/URQXy+ilieYA2ZDeF7R/orA9d4=; b=iccx0k15fyNp+doMKE9r3/l4mGiuA+d3lKgte0+Z9GVPP5wIx69h79mN68UmmwUYJY2K39 2iHbwpMLW5wNs+CpQfHNrPo0GQ7pGK+h6tE7xIUDqpWwfh/u/VO2bGz7OVWpaZlcgygUIR CcMPFTJspJJno/GcInKNLhFCxPh8fZY= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-615-a3TGFL4DP0Wy26KCBmTA6A-1; Thu, 26 Jan 2023 04:15:45 -0500 X-MC-Unique: a3TGFL4DP0Wy26KCBmTA6A-1 Received: by mail-wm1-f72.google.com with SMTP id bg25-20020a05600c3c9900b003da1f6a7b2dso2555770wmb.1 for ; Thu, 26 Jan 2023 01:15:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xdoN5yzxVJEREZqW/URQXy+ilieYA2ZDeF7R/orA9d4=; b=0OKjzRAPz76oFvsUoUO5PGjwE9e28nqA99D2/+pWsUDVfDvNQqgKyo7jt4Rt0Hky7Z +7qiV9D+H4bI+RQKAtOyCldQRMVvKtHyEe2NrHPGkko6uCqqDRWyINOFOIY+VMiaFz+O QwhqQdD/nd16YG70438G2axsqVIKh5V4rBG8AyMIkXC08kQFIcW/ybryNSznQQenNgR/ QpPU7bOERajt0xloZo+SzO3k2AELBMEg381az9ANfx8fKrROmboOlQF9npk9Qu4DJt0B I3qKi+Jt423lri21ZgwJ+kwVnAsHoTzmIvoc/fDwFQWc4IWnBbZKpBlBFRwI7dqwsLrf QhVA== X-Gm-Message-State: AO0yUKVsvZLrUqd53pWRU+RLcV6PPwl8jrBE90PPXhU2HzZI/EiML8D5 nJKSXq8eDze6X8MwgpLj73njRZ29CR8MsVMZbhy/PTWEm8PDnIzqekgmpa9ZWIGV2uNot1os/R7 zCRLaFKRlsmg= X-Received: by 2002:a5d:6dd2:0:b0:2bf:b0e6:f463 with SMTP id d18-20020a5d6dd2000000b002bfb0e6f463mr7469397wrz.13.1674724544334; Thu, 26 Jan 2023 01:15:44 -0800 (PST) X-Google-Smtp-Source: AK7set8Gm1mIzKlUmDps74fzfAh6ZmGmXPK8wFRxtHRNR5sPzRfm6UryQ5Qc1w14saHRBv9MwvmN2Q== X-Received: by 2002:a5d:6dd2:0:b0:2bf:b0e6:f463 with SMTP id d18-20020a5d6dd2000000b002bfb0e6f463mr7469383wrz.13.1674724544007; Thu, 26 Jan 2023 01:15:44 -0800 (PST) Received: from ?IPV6:2a09:80c0:192:0:5dac:bf3d:c41:c3e7? ([2a09:80c0:192:0:5dac:bf3d:c41:c3e7]) by smtp.gmail.com with ESMTPSA id j2-20020a5d4482000000b002be063f6820sm898614wrq.81.2023.01.26.01.15.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 26 Jan 2023 01:15:43 -0800 (PST) Message-ID: <394ed0b3-273d-6071-d71d-ddcef6ea72e9@redhat.com> Date: Thu, 26 Jan 2023 10:15:42 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: A mapcount riddle To: James Houghton , Peter Xu Cc: Mike Kravetz , linux-mm@kvack.org, Naoya Horiguchi , David Rientjes , Michal Hocko , Matthew Wilcox , Muchun Song References: From: David Hildenbrand Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 76AFD140002 X-Stat-Signature: hxfnydnyptd31kkbyedkt9x69p8yxbta X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1674724549-10505 X-HE-Meta: U2FsdGVkX18rBKdR8ewEP+xat02deNy3wX5oj1TPT4X/6A97vPFDcXSNii8GPTfJhR0XB8Gt7AFhepGhTxAkffi38b6alHRE8e30W8fSTVOJ+xoMeNbIOIIN/ki6SHCEHxVeSEKrRLTL+fDt4k36jUrI9vRJ7RBv/lmv2vq1TacRg9R4vo0o07u5UgbpR2yu3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a7FpJi/CgsJ8yuHHwm8pLoZPfmOGFcFIB69XljZ5h6PnvnVcWOuR2+xb4zyig/AlRzNUWzXe56NA00Mrhb/YE+3Wl5l2J7RIsfB+voKWcyZT58+G0yCqfgl5bAXxPGchgKaEaV1C7+HKw2Nb6Nkoo= 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 25.01.23 17:22, James Houghton wrote: > On Wed, Jan 25, 2023 at 7:54 AM Peter Xu wrote: >> >> On Wed, Jan 25, 2023 at 07:26:49AM -0800, James Houghton wrote: >>>> At first thought this seems bad. However, I believe this has been the >>>> behavior since hugetlb PMD sharing was introduced in 2006 and I am >>>> unaware of any reported issues. I did a audit of code looking at >>>> mapcount. In addition to the above issue with smaps, there appears >>>> to be an issue with 'migrate_pages' where shared pages could be migrated >>>> without appropriate privilege. >>>> >>>> /* With MPOL_MF_MOVE, we migrate only unshared hugepage. */ >>>> if (flags & (MPOL_MF_MOVE_ALL) || >>>> (flags & MPOL_MF_MOVE && page_mapcount(page) == 1)) { >>>> if (isolate_hugetlb(page, qp->pagelist) && >>>> (flags & MPOL_MF_STRICT)) >>>> /* >>>> * Failed to isolate page but allow migrating pages >>>> * which have been queued. >>>> */ >>>> ret = 1; >>>> } >>> >>> This isn't the exact same problem you're fixing Mike, but I want to >>> point out a related problem. >>> >>> This is the generic-mm-equivalent of the hugetlb code above: >>> >>> static int migrate_page_add(struct page *page, struct list_head >>> *pagelist, unsigned long flags) >>> { >>> struct page *head = compound_head(page); >>> /* >>> * Avoid migrating a page that is shared with others. >>> */ >>> if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(head) == 1) { >>> if (!isolate_lru_page(head)) { >>> list_add_tail(&head->lru, pagelist); >>> mod_node_page_state(page_pgdat(head), >>> NR_ISOLATED_ANON + page_is_file_lru(head), >>> thp_nr_pages(head)); >>> ... >>> } >>> >>> If you have a partially PTE-mapped THP, page_mapcount(head) will not >>> accurately determine if a page is mapped in multiple VMAs or not (it >>> only tells you how many times the head page is mapped). >>> >>> For example... >>> 1) You could have the THP PMD-mapped in one VMA, and then one tail >>> page of the THP can be mapped in another. page_mapcount(head) will be >>> 1. >>> 2) You could have two VMAs map two separate tail pages of the THP, in >>> which case page_mapcount(head) will be 0. >>> >>> I bring this up because we have the same problem with HugeTLB >>> high-granularity mapping. >> >> Maybe a better match here is total_mapcount() rather than page_mapcount() >> (despite the overheads on the sub-page loop)? IIRC, total_mapcount() would also not be what we want: for a PTE-mapped THP it would be e.g. 512 instead of one. [unless I am confused again about mapcounts] See my other comment, I believe this is supposed to be a guesstimate whether "this page is shared". And we use the first subpage to make a guess here ... Of course, we could try harder, by looking at > 1 subpage, to test if any of these subpages has a mapcount > 1. But it still wouldn't be accurate .... -- Thanks, David / dhildenb