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 6776AECAAA1 for ; Tue, 6 Sep 2022 14:44:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D628680294; Tue, 6 Sep 2022 10:44:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CEA8980224; Tue, 6 Sep 2022 10:44:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B648280294; Tue, 6 Sep 2022 10:44:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A013580224 for ; Tue, 6 Sep 2022 10:44:25 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7524A1618C9 for ; Tue, 6 Sep 2022 14:44:25 +0000 (UTC) X-FDA: 79881931290.27.CC84DBD Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf10.hostedemail.com (Postfix) with ESMTP id DA348C0069 for ; Tue, 6 Sep 2022 14:44:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1662475464; 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=zk4tT/sTQIn6h0bFOLVjj/y+xayeBLMBAFWx6yVN1pM=; b=JT0diHb59PWzKCS7pnZfn2RiQW4wHJcuy8rby8tT1NY4qEfakXTb7xS5K3kC9gljSPzDPZ oAMZ5WTvmsVk/A7HKM6iXuaDqwbSAlE0zylhMfky9UOjusACtHMBeKAVoMvBXDHrl3x+uD +6Sp8HoOcqsIrYG8iZPVBNMl/veFGGU= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-562-kwvqMJVoPYa4cNt0blK0Rw-1; Tue, 06 Sep 2022 10:44:23 -0400 X-MC-Unique: kwvqMJVoPYa4cNt0blK0Rw-1 Received: by mail-wm1-f71.google.com with SMTP id a17-20020a05600c349100b003a545125f6eso8881064wmq.4 for ; Tue, 06 Sep 2022 07:44:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date; bh=zk4tT/sTQIn6h0bFOLVjj/y+xayeBLMBAFWx6yVN1pM=; b=ofSxg8Yv0/sW40nO9RufRF1sznL93AbdhX/FIbIgsReGcIBHcSQGBI5mw5P0yFTbE/ u8NHtnSRC4Nw1JGs8MuRuuUAOZevMlEkXF+P2mQbwZZwDQ4k0vnpyqDjV5w2Q4PvwIjP 5LwntJ8SkmV4/fXiGA4mybiq3WHFbkOlS+GQGiqT0Lwq5eUO9KcPyhf8ANDI2vqiFfWh Tp814bGhJIb1TdUYGk2fBVfYQhoduWOkROsOg2HRqmhgv8vjt158Uhqx9jiPWpcpS6nB xSgMjK8OjU4T+neAPiah1bYPAKJAsghYkaCKG5DHSXwdFT5pzzGW5FqDoVrRPeF5lkrr gtZw== X-Gm-Message-State: ACgBeo2C6zzrUc47+lxwR0HeyEShlNBeAv6tOdTzpMDECSchtTXzHGtt DI10SDk6ygjdst29c111pyahKZspwArZeuWlnsBZ8NrSfN3NgrvwfMTCSVzQugFaxS+BmWxppVF k/UUGuxWm/4E= X-Received: by 2002:a5d:6d03:0:b0:226:eb13:c906 with SMTP id e3-20020a5d6d03000000b00226eb13c906mr17744625wrq.325.1662475462120; Tue, 06 Sep 2022 07:44:22 -0700 (PDT) X-Google-Smtp-Source: AA6agR4uz7+OPx4RNqBc8NRUWHOejnV6UayRVD4MQwgmIzSYf9kiR3yQYUlSX7gzenUhLQyreN2PgA== X-Received: by 2002:a5d:6d03:0:b0:226:eb13:c906 with SMTP id e3-20020a5d6d03000000b00226eb13c906mr17744608wrq.325.1662475461832; Tue, 06 Sep 2022 07:44:21 -0700 (PDT) Received: from ?IPV6:2003:d8:2f0d:ba00:c951:31d7:b2b0:8ba0? (p200300d82f0dba00c95131d7b2b08ba0.dip0.t-ipconnect.de. [2003:d8:2f0d:ba00:c951:31d7:b2b0:8ba0]) by smtp.gmail.com with ESMTPSA id e18-20020a5d5012000000b00226dedf1ab7sm7619868wrt.76.2022.09.06.07.44.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 06 Sep 2022 07:44:21 -0700 (PDT) Message-ID: <40486dbb-9f19-6fa6-d46d-99d2b033883d@redhat.com> Date: Tue, 6 Sep 2022 16:44:20 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.0 To: Jason Gunthorpe Cc: John Hubbard , Yang Shi , peterx@redhat.com, kirill.shutemov@linux.intel.com, hughd@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20220901222707.477402-1-shy828301@gmail.com> <4516a349-49cb-fd7b-176a-f1a9479906d9@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH] mm: gup: fix the fast GUP race against THP collapse 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 ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JT0diHb5; spf=pass (imf10.hostedemail.com: domain of david@redhat.com designates 170.10.133.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=1662475465; a=rsa-sha256; cv=none; b=53wL9nM3nHFDMz55WRBrUrrfXwuEBo3vdhPscugL/+H8WuZKJ709iH1+oPX4u7AtZZnO29 C1pzPTTe897H88ynTgXlCbWbjOGoaSUZuskusx4YAah4ON40rEUxaDxrQyCxcNzm/rRH5R wlKjEhTpNGkqAUETrDwa/XiYpl+mRmA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662475465; 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=zk4tT/sTQIn6h0bFOLVjj/y+xayeBLMBAFWx6yVN1pM=; b=KuBOhBHx17Vi+l6CZVZKmHBdCsjROGIVsqfvedA82COSsP6TNZFhpja/dGcrZ7ayvSjQ9r FM+0bM8lt3c9q07EODtjPa6bGd+iAQcGbEY3yrSPRJzBn/N7HWDOz/xWsdooJLasoNgcQ3 8sOFztC2z96Z4SGCM8/VUCh+grcTtAs= Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JT0diHb5; spf=pass (imf10.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: rpt6yk6my3i44wgg61tj5yh3ggjef9nj X-Rspamd-Queue-Id: DA348C0069 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1662475464-239002 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 06.09.22 16:30, Jason Gunthorpe wrote: > On Tue, Sep 06, 2022 at 03:57:30PM +0200, David Hildenbrand wrote: > >>> READ_ONCE primarily is a marker that the data being read is unstable >>> and that the compiler must avoid all instability when reading it. eg >>> in this case the compiler could insanely double read the value, even >>> though the 'if' requires only a single read. This would result in >>> corrupt calculation. >> >> As we have a full memory barrier + compile barrier, the compiler might >> indeed do double reads and all that stuff. BUT, it has to re-read after we >> incremented the refcount, and IMHO that's the important part to detect the >> change. > > Yes, it is important, but it is not the only important part. > > The compiler still has to exectute "if (*a != b)" *correctly*. > > This is what READ_ONCE is for. It doesn't set order, it doesn't > implement a barrier, it tells the compiler that '*a' is unstable data > and the compiler cannot make assumptions based on the idea that > reading '*a' multiple times will always return the same value. > > If the compiler makes those assumptions then maybe even though 'if (*a > != b)' is the reality, it could mis-compute '*a == b'. You enter into > undefined behavior here. > > Though it is all very unlikely, the general memory model standard is > to annotate with READ_ONCE. The only thing I could see going wrong in the comparison once the stars alingn would be something like the following: if (*a != b) implemented as if ((*a).lower != b.lower && (*a).higher != b.higher) This could only go wrong if we have more than one change such that: Original: *a = 0x00000000ffffffffull; First modification: *a = 0xffffffffffffffffull; Second modification: *a = 0x00000000eeeeeeeeull; If we race with both modifications, we could see that ffffffff matches, and could see that 00000000 matches as well. So I agree that we should change it, but not necessarily as an urgent fix and not necessarily in this patch. It's best to adjust all gup_* functions in one patch. ... I do wonder if we want to reuse ptep_get_lockless() instead of the READ_ONCE(). CONFIG_GUP_GET_PTE_LOW_HIGH is confusing. -- Thanks, David / dhildenb