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 02A05C761A6 for ; Mon, 3 Apr 2023 12:02:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B9406B0072; Mon, 3 Apr 2023 08:02:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 769876B0074; Mon, 3 Apr 2023 08:02:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 60B9F6B0075; Mon, 3 Apr 2023 08:02:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4EAE76B0072 for ; Mon, 3 Apr 2023 08:02:40 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 113DF120529 for ; Mon, 3 Apr 2023 12:02:40 +0000 (UTC) X-FDA: 80639942880.20.B6A2E6E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf07.hostedemail.com (Postfix) with ESMTP id CD3AC40061 for ; Mon, 3 Apr 2023 12:02:37 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="IWxkp/Uc"; spf=pass (imf07.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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680523357; 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=GDB/hmJOcrYFtJnN1TBanTBM2RE49CIj8eveC48zPtU=; b=1dReAYdNmV+PRUuNRBfflBLtVvW0EjlIkAl0vMKy/RJAKEJmZ+PcCAfbcR30j2vGb87tQL yvLYkh9qo3wz0OPI7X8MmT392WQwWXTPv3FZJyp9P/MriQHqliVWPYFm1wtluWyC3zvEhD dzNWLFRZBtn3otreck92K5BGeZkTH9U= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="IWxkp/Uc"; spf=pass (imf07.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=1680523357; a=rsa-sha256; cv=none; b=UCe1Q5+I+cGlBGEa04KeAi1xKwFMYvxP0VW9SjbXOT1IhTjjg6Bm1nve8F84m1ZU60ekRA ykJ/iisLWtAUSE3dim202tNPoaZyToFhEE7pd6KQS+E4ZfaLFyFPD/1w+dAOUUcuMdRSam UwoX1Hhy2XIYiG/qXZADKII2eG7JKjQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680523357; 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=GDB/hmJOcrYFtJnN1TBanTBM2RE49CIj8eveC48zPtU=; b=IWxkp/Ucy8xVRHY29w6pFdNFAL0UKAn+fpZuP6Fjk5a85wNSzFx/uVjjp8UqeJQAS/aWs+ VY+wP5ZcB2b1t0ODls33mNCBqpIGZJiJJlrdFdP10MLyYXqpNw66wD705rqc4zib1rtRUL oVCpRpujiW2bUbwLiI8biBxVPEQqfRw= 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_256_GCM_SHA384) id us-mta-502-aH2F0trVPky5kwBAxCoR-w-1; Mon, 03 Apr 2023 08:02:36 -0400 X-MC-Unique: aH2F0trVPky5kwBAxCoR-w-1 Received: by mail-wm1-f71.google.com with SMTP id r11-20020a05600c458b00b003eea8d25f06so14641112wmo.1 for ; Mon, 03 Apr 2023 05:02:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680523355; 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=GDB/hmJOcrYFtJnN1TBanTBM2RE49CIj8eveC48zPtU=; b=Oo0/iAnLPPcrx2yFAGhnZYAGZsHFSjtXQGOzI4se0012eZAjWn/jUAQDtsvP0/eZQT h2jZIWcMB07W2F+H/8Ni0jXFkJYCTFWkkFfUZ8hk7vKNSy+zxc08jNW3yn1Zrdy6uMfq xOpVsmArhXtF5qCng8xBM3gN/lM8FUIkzKQTAfb0MKFcadL5hmKT2WCEGpEGuwIut1sP b0CeuvZc52cjPBL9Rriuxjziyq3bV1lhLarxR7vAnDRzc1Rmux+DIzWOG2TU4LqNyiLq C1Paquy9wVU5hdJ8FlpOvYd6+8G2idLSnJ12WKEYXRdNZqIhkDZlc2ahJeUwZdCYUhW9 hTxQ== X-Gm-Message-State: AAQBX9c+6s50yokWz03SKZbpW61KKlibcL0bOq7M0pyPt5LgT4pWKire HJIzEwpBWJYkSptxVBvrnZXoHhyUmyFXgrMPQ5XHTmcm+/h379USzmPU4Q2xNb2MQv/Q6B9yg1h NCvcxhS2jHdgjmPZyNi4= X-Received: by 2002:a7b:ce88:0:b0:3f0:3a5a:362e with SMTP id q8-20020a7bce88000000b003f03a5a362emr10037675wmj.39.1680523355005; Mon, 03 Apr 2023 05:02:35 -0700 (PDT) X-Google-Smtp-Source: AKy350bK74W1j6j/etoodmlgMY4hwEOB599KYyw+0fEjbGAAaXiONOHFfop9MJUYTe3lHtONcsh5ug== X-Received: by 2002:a7b:ce88:0:b0:3f0:3a5a:362e with SMTP id q8-20020a7bce88000000b003f03a5a362emr10037657wmj.39.1680523354652; Mon, 03 Apr 2023 05:02:34 -0700 (PDT) Received: from ?IPV6:2003:cb:c702:5e00:8e78:71f3:6243:77f0? (p200300cbc7025e008e7871f3624377f0.dip0.t-ipconnect.de. [2003:cb:c702:5e00:8e78:71f3:6243:77f0]) by smtp.gmail.com with ESMTPSA id p13-20020a05600c468d00b003ef7058ea02sm19072053wmo.29.2023.04.03.05.02.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Apr 2023 05:02:33 -0700 (PDT) Message-ID: Date: Mon, 3 Apr 2023 14:02:32 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH v2] mm: Take a page reference when removing device exclusive entries To: Alistair Popple , linux-mm@kvack.org, Andrew Morton Cc: Ralph Campbell , John Hubbard , nouveau@lists.freedesktop.org, Matthew Wilcox , stable@vger.kernel.org References: <20230330012519.804116-1-apopple@nvidia.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20230330012519.804116-1-apopple@nvidia.com> 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: CD3AC40061 X-Stat-Signature: bi3aaqg8jqeen4sm545nubng9mdrg8ij X-HE-Tag: 1680523357-922869 X-HE-Meta: U2FsdGVkX19TKiVPHXqVdCaPIQyp04lLw2YDPu4o8SqQPY4XE+hG6g2wL/mGfB75XW0NtSHBf5romUaWHey5cJ1/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H6Sv/Af4j4B5VoaTQ1LYTgmo9rzWkIPVJXN+v9IrNV1ApKykMXZF+4Ek2TbOk52Ttfq8YNMlq5NPDk7uHY/PbgMK3a12vTS2j2Yr1nzinXHQsJkphEaQ/hr4FoOmqOt3uqWEsjqy6KhvCk1jXsCuF77tC3afHH7n/t2vVVDcFNTAI4ofm2Si9kiGkhaC957dhZRwDxqStdIFZSrpNd9M9f5LMbxOYaZYjIy3VMwTCLKOKDbZ2BrtptekWpFHZj3ECbmzCOUIdeYc5/0/cRUL83j0nAo0mV5O+YT6my6uzumsfQJemckQoePa5Zn7Z1kIEfL8L/WvR6m9F1w== 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 30.03.23 03:25, Alistair Popple wrote: > Device exclusive page table entries are used to prevent CPU access to > a page whilst it is being accessed from a device. Typically this is > used to implement atomic operations when the underlying bus does not > support atomic access. When a CPU thread encounters a device exclusive > entry it locks the page and restores the original entry after calling > mmu notifiers to signal drivers that exclusive access is no longer > available. > > The device exclusive entry holds a reference to the page making it > safe to access the struct page whilst the entry is present. However > the fault handling code does not hold the PTL when taking the page > lock. This means if there are multiple threads faulting concurrently > on the device exclusive entry one will remove the entry whilst others > will wait on the page lock without holding a reference. > > This can lead to threads locking or waiting on a folio with a zero > refcount. Whilst mmap_lock prevents the pages getting freed via > munmap() they may still be freed by a migration. This leads to > warnings such as PAGE_FLAGS_CHECK_AT_FREE due to the page being locked > when the refcount drops to zero. > > Fix this by trying to take a reference on the folio before locking > it. The code already checks the PTE under the PTL and aborts if the > entry is no longer there. It is also possible the folio has been > unmapped, freed and re-allocated allowing a reference to be taken on > an unrelated folio. This case is also detected by the PTE check and > the folio is unlocked without further changes. > > Signed-off-by: Alistair Popple > Reviewed-by: Ralph Campbell > Reviewed-by: John Hubbard > Fixes: b756a3b5e7ea ("mm: device exclusive memory access") > Cc: stable@vger.kernel.org Acked-by: David Hildenbrand -- Thanks, David / dhildenb