Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3661795pxv; Mon, 19 Jul 2021 05:57:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyXZcmqs/za6Hv5HfWxse1kF9TDx5IXY+bX4rDKnZQGHqLNKwh8SWa3SpkuKZlF3SLMyFgG X-Received: by 2002:a02:cc19:: with SMTP id n25mr21747188jap.140.1626699434095; Mon, 19 Jul 2021 05:57:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626699434; cv=none; d=google.com; s=arc-20160816; b=SoG9o/t/kG6RkjmD49n6+hqEk3wIvK3BKDAnaFoP0Qs7fQQt6Ew9anaHt2RfwwPt7P rklUVjd2vnYMs9Uu5Yqxw43MMYRxc3rhH2oRHvJfJg07M/3PWKQ7iztvRzomQSq1eiRg qkb0OSoxcCNYj1suRzRIZv1GhQkoDO+UOLmyRyOWBjN/TDsjbDfsOdFPvMWEHcmqIJ8u LjAM38tUmuTjkIObTMoIJgcy2gUdqxvsQfe4E5RrGlT3kuF7+NpPkdWcc5sY/Zve1hTB jh5TVSC8mn8LkmQQmVEPAFu4FUZbhPpBryQSUuEqB0591op1c8m8FT/XWKCKuoXwc72c M8JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject:dkim-signature; bh=pJ4knqJr8MCDWuPshU9F5K+EeAL4qKb9eJnyIRFa9hM=; b=gKRtFPNBrng35WwqKWVoKQgR6AWGvzrnRPKwUnnjy11XColQedGhJeXibDpmfhRVLb VKgEargnccXWxgQsf87jaJidKoAEHocs7/28V9XowZpDKfGROsQSOih/SW+xV2wL92hz 2Nf8qtK27WIGbJMv+3/7URtP/7F/3BjO9s0NWhTT9p1xCm/LZTwkGc6reGzQc8RMzUUH 2hbvmg2i9VBhg1V5tq6lBmN4HoiG/S43EEze1h2fVmeHDOAiODApUuAb2IZgM10x180C DsIqkf75KsmGvNJZcCbnDR4lT7zbFjs5OUsZubhLH5kBpnT6x2pnVjj1tFJ6rFFbjU5Q bxYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=Gie9aHoU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d9si2824124ila.112.2021.07.19.05.57.02; Mon, 19 Jul 2021 05:57:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=Gie9aHoU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237111AbhGSMP2 (ORCPT + 99 others); Mon, 19 Jul 2021 08:15:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231282AbhGSMP1 (ORCPT ); Mon, 19 Jul 2021 08:15:27 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CA95C061767 for ; Mon, 19 Jul 2021 05:14:43 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id b2so4967417plx.1 for ; Mon, 19 Jul 2021 05:56:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=pJ4knqJr8MCDWuPshU9F5K+EeAL4qKb9eJnyIRFa9hM=; b=Gie9aHoUo6wyrEfk0yUJ4HChlgLtj36K7r+9gR1sk6mMB9vtxtnoQemP4/b1OCld6s 0ks63IT2NW5TAw2oeEN2C62RG39VUbz1jixYLXxoTLLdpDBRUSuUSVWEV/IXfZDPYk6R +WT2bHmI8w3Xdz57wKU9ne22rjwox+48hsrTCsPwyrCvqWae5EgUQ9ti//amKiqMzDRZ ORYN2icrfBc3L/CaX80twbLD4z6htpBjscOdCZYWHsVKe4MUhE2zpsi36tcNPwSA/YSG CRFmnEkU4qwSOdpVdbJLz+1DPJhu5P+7SWpAd4Uo0KoPCx4I7yXtxp2tfmo9ljTQOm5p Bp/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=pJ4knqJr8MCDWuPshU9F5K+EeAL4qKb9eJnyIRFa9hM=; b=j0HO2tZlIzi4PRhLzWVWCarL1skegz1elmYb/yCGyWOwhsJnJ/oAV8SEQAaYApwB/D ivx3/p1FXUwn+wEdlhYuw91dwqj7p0KwXoNtY10OBwRCtadWGo3qZsh0rSMRWqev7CYW djwsxFfIhGX8D0fT1Nof94LbnhFquLX5UGl2EGNDKQs/lY51IsCNCHNNubdgkkEUtC3J aQZ1pQRAd+kwXdpUFtjocdbYoct5w6aU8X6BvXbi52AGxuOjcRIduRxYACu0yjN2rvtC HhK7x93Ts4jNPvzL696KLLazcBp7c5fmyrRK0RapDUTuztoXWVcu0cG1qWWgk5AzYtm2 syNQ== X-Gm-Message-State: AOAM5311nQTQUj7ZiUOF3O1WRDxuDcXZad0BOTJ8lEMNIaq9kY8yi/ez Bmj5Zorvj7S6mpT6N9zyrX25Tw== X-Received: by 2002:a17:90b:1041:: with SMTP id gq1mr29619799pjb.222.1626699366674; Mon, 19 Jul 2021 05:56:06 -0700 (PDT) Received: from [10.200.196.235] ([139.177.225.251]) by smtp.gmail.com with ESMTPSA id a18sm19970441pfi.6.2021.07.19.05.56.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 19 Jul 2021 05:56:06 -0700 (PDT) Subject: Re: [PATCH 5/7] mm: free user PTE page table pages To: =?UTF-8?Q?Mika_Penttil=c3=a4?= , akpm@linux-foundation.org, tglx@linutronix.de, hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, songmuchun@bytedance.com References: <20210718043034.76431-1-zhengqi.arch@bytedance.com> <20210718043034.76431-6-zhengqi.arch@bytedance.com> <9c3c87d5-e64e-f13f-ef36-b438e4de1e66@nextfour.com> From: Qi Zheng Message-ID: <80b7d7fc-9d6d-0d1b-a333-b0ccd856e7c1@bytedance.com> Date: Mon, 19 Jul 2021 20:56:01 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: <9c3c87d5-e64e-f13f-ef36-b438e4de1e66@nextfour.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 7/18/21 2:19 PM, Mika Penttilä wrote: >> + >> +/* >> + * returns true if the pmd has been populated with PTE page table, >> + * or false for all other cases. >> + */ >> +bool pte_install_try_get(struct mm_struct *mm, pmd_t *pmd, pgtable_t >> *pte) >> +{ >> +    spinlock_t *ptl; >> +    bool retval = true; >> + >> +retry: >> +    ptl = pmd_lock(mm, pmd); >> +    if (likely(pmd_none(*pmd))) { >> +        __pte_install(mm, pmd, pte); >> +    } else if (pmd_leaf(*pmd) || !pmd_present(*pmd)) { >> +        retval = false; >> +    } else if (!pte_get_unless_zero(pmd)) { >> +        spin_unlock(ptl); >> +        goto retry; >> +    } >> +    spin_unlock(ptl); >> +    return retval; >> +} >> + > > Can pte_get_unless_zero() return true above? Can the pmd have been by > populated by others? In that case the ref count is wrongly incremented. > Here we only have mmap_read_lock(mm), so the pmd can be populated with other PTE page table page after a page fault in a different thread B of this mm. In this case, thread B already hold a pte_refcount of the PTE page table page populated in the pmd, so pte_get_unless_zero() can return true above. Similarly, if THP is enabled, the pmd also can be populated with a THP page, we can see more detail in comment in handle_pte_fault(). The pmd_leaf() above is to detect this situation.