From nobody Mon Feb 9 19:31:06 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20AFE1DFE14 for ; Wed, 29 Jan 2025 11:54:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738151693; cv=none; b=fT7XuFglssNUf8dsJsxQqH94xAjm6N7vjnrPOxIKw5cY+Kixr90eRUpjXomempUw9Da6ikyC3Dcc8hUS4pzHM5g+5oIX4hKwN9Py2HGSsCqfLGtNzTCnygLrw41Vo4HfZ8atenfbDtE8V33iiW2TYQj4dFT+a7whnVss0UEipdE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738151693; c=relaxed/simple; bh=Y0+EdNoTZ7KKt/QbFsaS3wPTCnj2OeBbOL4l6HpHEm0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oyNrneyp49jUyU2rtcTsDXW/3FZg3i6SzPOJi1vF4FJJXuQU0r3ETRzE4z6yHwHL+1g/RIFS/mHx78b8zN61aQPaO2qc0qJFljaaQ3qmpYhcNtbKZGOgZqgMKgEiUdSb9xCMD042V4Baw+uXxxDjxA1ftd9Z+xu0ExXV8Qjt5sM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=XsN8R1by; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XsN8R1by" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738151691; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8FdUGdC2w/N5TeOcJ3tL/h1ogRWBFVvHy4t3b3m6isA=; b=XsN8R1bylwEDAh3+TloxaWwwdbAcysYbloaY9Y0+R4Qd0KQbC7gnnccErNiT2aPSX7+d3h RgATNATUgBPG7HbYoOpuJjlFKY8KwKTvCjK+Drf8FwF5/tKTyrS7zTFCA74XS6jdCCp2W+ DmOWL+KiLw/Y1y+GZnLsEyTtsUSyDrk= 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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-290-ybOxRQowP5SBwQ2QcdWytw-1; Wed, 29 Jan 2025 06:54:50 -0500 X-MC-Unique: ybOxRQowP5SBwQ2QcdWytw-1 X-Mimecast-MFC-AGG-ID: ybOxRQowP5SBwQ2QcdWytw Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-385ded5e92aso2765289f8f.3 for ; Wed, 29 Jan 2025 03:54:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738151689; x=1738756489; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8FdUGdC2w/N5TeOcJ3tL/h1ogRWBFVvHy4t3b3m6isA=; b=ippF6QHVdR6zFeG0GizBISEYqFpmIVehIN4qh7vNadFagP/sssekMOrHuf6B6x9tPl jBMR8hVAJ6sJbTNyyVlXjsoed6u44/2finpfJrrjF67TokPLuS15eOKOiJ/K4YaxnD/e /qnTC97J1zU0nkMt00EmfN06hiWBKrFl+0KX1ipdv+vKLV4FE14O6XJYU4uaeU9250bH eDg8bg57AqCxrnwHMLyZIlFaptfv8pgGYUxBv7INyZDhhUo5pktk1R123U2mB3ve1Tkp HweeNAKYERTfLn7Jq1oyQwx8dRT6k6b7lE3IK+4LvKpj90cljZCKO5NvNRutDWEptrIk Ofiw== X-Gm-Message-State: AOJu0YzJMnXfoUbu4dQUfGLmxWwxPd38VsQKz1GQDOMorsKzT5KrGVeL 0YcLQWKC6VIBx///f5c+g9WnmStIQD6/78NofKumdIa974uBfaq0bhlvdRg4jAlVB8UJqDM9FP4 bq9kdE4Z5YomW7utAri6dFkoTbDk2PIUDDTSHZQGzwan+55XwFP8I6Wd4NGIfUQq0p1gRdX9ayw BVm0DHciWnsXIMCRY3fujQ+JfL60OAr/uSvHVAYTTSLE/F X-Gm-Gg: ASbGnctuCDph5j/IIwtt28bSeqLhy0E66qq+2Vv1vaVoHZywY5VDHSD26754uS6YqXK NCk9v7cC1IqLS7V/tJsnc1qlPoJWf8db2C9eHDWufMvnCh/fqgv2HhhVDAKrKJQM3+yLfibfJxf dAUNedALNNthUcMgbuH8gBJ88zG2vAwT3RTR1hVgqMdQk7izXs3dVD2dNbBBvFyUjuwPkdPfoSJ zSHu0jNrRVTouTkJ1pJ1dQyZlbqPX658fwRRL+k6xFtrivr+yYBO2Ejd/iAXr9St2qtK5PYkyF1 GDhEzikfZ+bRDeMJMWjbvXHmZbzTcEx21Mi5CtLkELeh5L/icdJ0jv1n7oMtzyrrGQ== X-Received: by 2002:a5d:5384:0:b0:38a:8ed1:c5c7 with SMTP id ffacd0b85a97d-38c520bdb45mr1846677f8f.46.1738151688884; Wed, 29 Jan 2025 03:54:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IEQObdLJ4S/sxdMO/dY9vI6fd5p4fQOJgmwgX26yitgXbCNTkIYH7aPDZgCkfY36quLJ2o2xA== X-Received: by 2002:a5d:5384:0:b0:38a:8ed1:c5c7 with SMTP id ffacd0b85a97d-38c520bdb45mr1846636f8f.46.1738151688502; Wed, 29 Jan 2025 03:54:48 -0800 (PST) Received: from localhost (p200300cbc7053b0064b867195794bf13.dip0.t-ipconnect.de. [2003:cb:c705:3b00:64b8:6719:5794:bf13]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38c2a1c4006sm17020800f8f.94.2025.01.29.03.54.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 03:54:47 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, nouveau@lists.freedesktop.org, David Hildenbrand , Andrew Morton , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Jonathan Corbet , Alex Shi , Yanteng Si , Karol Herbst , Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Pasha Tatashin , Peter Xu , Alistair Popple , Jason Gunthorpe Subject: [PATCH v1 12/12] mm/rmap: keep mapcount untouched for device-exclusive entries Date: Wed, 29 Jan 2025 12:54:10 +0100 Message-ID: <20250129115411.2077152-13-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250129115411.2077152-1-david@redhat.com> References: <20250129115411.2077152-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that conversion to device-exclusive does no longer perform an rmap walk and the main page_vma_mapped_walk() users were taught to properly handle nonswap entries, let's treat device-exclusive entries just as if they would be present, similar to how we handle device-private entries already. This fixes swapout/migration of folios with device-exclusive entries. Likely there are still some page_vma_mapped_walk() callers that are not fully prepared for these entries, and where we simply want to refuse !pte_present() entries. They have to be fixed independently; the ones in mm/rmap.c are prepared. Fixes: b756a3b5e7ea ("mm: device exclusive memory access") Signed-off-by: David Hildenbrand --- mm/memory.c | 17 +---------------- mm/rmap.c | 7 ------- 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index db38d6ae4e74..cd689cd8a7c8 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -743,20 +743,6 @@ static void restore_exclusive_pte(struct vm_area_struc= t *vma, =20 VM_BUG_ON_FOLIO(pte_write(pte) && (!folio_test_anon(folio) && PageAnonExclusive(page)), folio); - - /* - * No need to take a page reference as one was already - * created when the swap entry was made. - */ - if (folio_test_anon(folio)) - folio_add_anon_rmap_pte(folio, page, vma, address, RMAP_NONE); - else - /* - * Currently device exclusive access only supports anonymous - * memory so the entry shouldn't point to a filebacked page. - */ - WARN_ON_ONCE(1); - set_pte_at(vma->vm_mm, address, ptep, pte); =20 /* @@ -1628,8 +1614,7 @@ static inline int zap_nonpresent_ptes(struct mmu_gath= er *tlb, */ WARN_ON_ONCE(!vma_is_anonymous(vma)); rss[mm_counter(folio)]--; - if (is_device_private_entry(entry)) - folio_remove_rmap_pte(folio, page, vma); + folio_remove_rmap_pte(folio, page, vma); folio_put(folio); } else if (!non_swap_entry(entry)) { /* Genuine swap entries, hence a private anon pages */ diff --git a/mm/rmap.c b/mm/rmap.c index 9e2002d97d6f..4acc9f6d743a 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2495,13 +2495,6 @@ struct page *make_device_exclusive(struct mm_struct = *mm, unsigned long addr, /* The pte is writable, uffd-wp does not apply. */ set_pte_at(mm, addr, fw.ptep, swp_pte); =20 - /* - * TODO: The device-exclusive non-swap PTE holds a folio reference but - * does not count as a mapping (mapcount), which is wrong and must be - * fixed, otherwise RMAP walks don't behave as expected. - */ - folio_remove_rmap_pte(folio, page, vma); - folio_walk_end(&fw, vma); *foliop =3D folio; return page; --=20 2.48.1