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 3D7C3C433FE for ; Fri, 28 Jan 2022 08:02:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B45FB6B0072; Fri, 28 Jan 2022 03:02:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AF4806B0073; Fri, 28 Jan 2022 03:02:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 995C16B0074; Fri, 28 Jan 2022 03:02:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0027.hostedemail.com [216.40.44.27]) by kanga.kvack.org (Postfix) with ESMTP id 898416B0072 for ; Fri, 28 Jan 2022 03:02:15 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 44C81181A5FE3 for ; Fri, 28 Jan 2022 08:02:15 +0000 (UTC) X-FDA: 79078953030.26.4F3C636 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf26.hostedemail.com (Postfix) with ESMTP id AC64A140006 for ; Fri, 28 Jan 2022 08:02:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643356934; 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=HrLvSZIR36NorBIqDb36MQSYEjpSxjJoIGRv0e2Vkb8=; b=hX1/HM4KGbcqmA4WLnW92SujFOg7jX/7m1INhu+KvHukY1CO7wbBdq8NQnIj3VbMsLGPY4 66FlcweOaaaPO4g++WBuOeFNPdK/rK0a8d7BMJ5mNaravIHkKueKbm6kF2dHGcY/dtC5Se gEo48vzJh4jMyAuSaxPQkWqARSj3KoA= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-456-RCeKmzmkNJazws7oC4agVw-1; Fri, 28 Jan 2022 03:02:11 -0500 X-MC-Unique: RCeKmzmkNJazws7oC4agVw-1 Received: by mail-wr1-f70.google.com with SMTP id m17-20020adfa3d1000000b001dd66c10c0cso1935934wrb.19 for ; Fri, 28 Jan 2022 00:02:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=HrLvSZIR36NorBIqDb36MQSYEjpSxjJoIGRv0e2Vkb8=; b=P0eDrTJgEC60PvLoXbiHKPEeITkigIgEgD6R3il/I2dwsIKro1dggp4gqaj+pdkU29 QtCBaHeWvMLMrEjaPa+qomxGprI+kjiM4Pq+Vx4hx0xPIYC/Q/yfEsYdBDQO9nApo5M5 VMkQ7sHVLasYzkRHLHFagczK4OLuGhskg1AXlFSxsj7MRshvG4yqS1cz4iyftd1PAYON 7OfnZOMKXdMj+KYtAdFGa9bFghBqP1BqCpVhaZhHqQ1MZoehf1InyHXwddcruNlt6+LX ihMOsmsc/eToABtmuHLEGVWNJ/wO/pMRaLJrgb+dNn9gCeY3vwmZ/TemhjvkZYL9atoe zLrA== X-Gm-Message-State: AOAM531675LKNz2EoxLKL6x2owzhcL/vIbjdJ4VBlf8n2LEQcXzTZsex Uweky42lxuOvwEzcB46vF4hxrWSouDHIdBkOjieCx7G1+tD2ok08ZcxfH3JPdwnBqowhX1oy5Q3 7CoKMCgcsrls= X-Received: by 2002:a05:600c:ac7:: with SMTP id c7mr1161835wmr.61.1643356930394; Fri, 28 Jan 2022 00:02:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJxJ7HYlNONpmT6c6aBVaZKnXYE6GGvxfmIczVldqWZrOLYQq8sPpaKzRiokTtE3ZubTa8rfYg== X-Received: by 2002:a05:600c:ac7:: with SMTP id c7mr1161809wmr.61.1643356930170; Fri, 28 Jan 2022 00:02:10 -0800 (PST) Received: from ?IPV6:2003:cb:c70e:5c00:522f:9bcd:24a0:cd70? (p200300cbc70e5c00522f9bcd24a0cd70.dip0.t-ipconnect.de. [2003:cb:c70e:5c00:522f:9bcd:24a0:cd70]) by smtp.gmail.com with ESMTPSA id i9sm4914187wry.32.2022.01.28.00.02.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Jan 2022 00:02:09 -0800 (PST) Message-ID: <8c0430e5-fecb-3eda-3d40-e94caa8cbd78@redhat.com> Date: Fri, 28 Jan 2022 09:02:08 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [v2 PATCH] fs/proc: task_mmu.c: don't read mapcount for migration entry To: Yang Shi Cc: Jann Horn , "Kirill A. Shutemov" , Matthew Wilcox , Andrew Morton , Linux MM , Linux Kernel Mailing List , stable References: <20220120202805.3369-1-shy828301@gmail.com> <5b4e2c29-8f1a-5a68-d243-a30467cc02d4@redhat.com> <5a565d5a-0540-4041-ce63-a8fd5d1bb340@redhat.com> <2a1c5bd2-cb8c-b93b-68af-de620438d19a@redhat.com> 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 Content-Transfer-Encoding: 7bit X-Rspam-User: nil X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: AC64A140006 X-Stat-Signature: rcxywxg6om1o4gapnr7f8jok71wkmpsf Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="hX1/HM4K"; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf26.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com X-HE-Tag: 1643356934-374678 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 27.01.22 22:16, Yang Shi wrote: > On Wed, Jan 26, 2022 at 10:54 AM David Hildenbrand wrote: >> >>>>> Just page lock or elevated page refcount could serialize against THP >>>>> split AFAIK. >>>>> >>>>>> >>>>>> But yeah, using the mapcount of a page that is not even mapped >>>>>> (migration entry) is clearly wrong. >>>>>> >>>>>> To summarize: reading the mapcount on an unlocked page will easily >>>>>> return a wrong result and the result should not be relied upon. reading >>>>>> the mapcount of a migration entry is dangerous and certainly wrong. >>>>> >>>>> Depends on your usecase. Some just want to get a snapshot, just like >>>>> smaps, they don't care. >>>> >>>> Right, but as discussed, even the snapshot might be slightly wrong. That >>>> might be just fine for smaps (and I would have enjoyed a comment in the >>>> code stating that :) ). >>> >>> I think that is documented already, see Documentation/filesystems/proc.rst: >>> >>> Note: reading /proc/PID/maps or /proc/PID/smaps is inherently racy (consistent >>> output can be achieved only in the single read call). >> >> Right, but I think there is a difference between >> >> * Atomic values that change immediately afterwards ("this value used to >> be true at one point in time") >> * Values that are unstable because we cannot read them atomically ("this >> value never used to be true") >> >> I'd assume with the documented race we actually talk about the first >> point, but I might be just wrong. >> >>> >>> Of course, if the extra note is preferred in the code, I could try to >>> add some in a separate patch. >> >> When staring at the (original) code I would have hoped to find something >> like: >> >> /* >> * We use page_mapcount() to get a snapshot of the mapcount. Without >> * holding the page lock this snapshot can be slightly wrong as we >> * cannot always read the mapcount atomically. As long we hold the PT >> * lock, the page cannot get unmapped and it's at safe to call >> * page_mapcount(). >> */ >> >> With the addition of >> >> "... For unmapped pages (e.g., migration entries) we cannot guarantee >> that, so treat the mapcount as being 1." > > It seems a little bit confusing to me, it is not safe to call with PTL > held either, right? I'd like to rephrase the note to: The implication that could have been spelled out is that only a mapped page can get unmapped. (I know, there are some weird migration entries nowadays ...) /* * We use page_mapcount() to get a snapshot of the mapcount. Without * holding the page lock this snapshot can be slightly wrong as we * cannot always read the mapcount atomically. As long we hold the PT * lock, a mapped page cannot get unmapped and it's at safe to call * page_mapcount(). Especially for migration entries, it's not safe to * call page_mapcount(), so we treat the mapcount as being 1. */ -- Thanks, David / dhildenb