git: 580340dbdaaf - main - vm_object: do not assume that un_pager.devp.dev is cdev

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 06 Nov 2024 00:23:48 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=580340dbdaaf372867e9ed3dd257430982753e5e

commit 580340dbdaaf372867e9ed3dd257430982753e5e
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-11-04 21:54:30 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-11-06 00:11:00 +0000

    vm_object: do not assume that un_pager.devp.dev is cdev
    
    It is subtype-specific handle.  Mark OBJT_DEVICE that do fill cdev into
    the handle, with a new object flag OBJ_CDEVH.
    
    PR:     282533
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D47443
---
 sys/kern/kern_proc.c | 5 +++--
 sys/vm/vm_mmap.c     | 3 +++
 sys/vm/vm_object.c   | 3 ++-
 sys/vm/vm_object.h   | 1 +
 4 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c
index 0d5b9a93d704..4cd13234a10b 100644
--- a/sys/kern/kern_proc.c
+++ b/sys/kern/kern_proc.c
@@ -2717,8 +2717,9 @@ kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
 
 			kve->kve_ref_count = obj->ref_count;
 			kve->kve_shadow_count = obj->shadow_count;
-			if (obj->type == OBJT_DEVICE ||
-			    obj->type == OBJT_MGTDEVICE) {
+			if ((obj->type == OBJT_DEVICE ||
+			    obj->type == OBJT_MGTDEVICE) &&
+			    (obj->flags & OBJ_CDEVH) != 0) {
 				cdev = obj->un_pager.devp.handle;
 				if (cdev != NULL) {
 					csw = dev_refthread(cdev, &ref);
diff --git a/sys/vm/vm_mmap.c b/sys/vm/vm_mmap.c
index 4f709b1b74e1..028175b88b4f 100644
--- a/sys/vm/vm_mmap.c
+++ b/sys/vm/vm_mmap.c
@@ -1407,6 +1407,9 @@ vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
 	    td->td_ucred);
 	if (obj == NULL)
 		return (EINVAL);
+	VM_OBJECT_WLOCK(obj);
+	vm_object_set_flag(obj, OBJ_CDEVH);
+	VM_OBJECT_WUNLOCK(obj);
 	*objp = obj;
 	*flagsp = flags;
 	return (0);
diff --git a/sys/vm/vm_object.c b/sys/vm/vm_object.c
index 6d29ddf71f19..e6324647e29e 100644
--- a/sys/vm/vm_object.c
+++ b/sys/vm/vm_object.c
@@ -2515,7 +2515,8 @@ vm_object_list_handler(struct sysctl_req *req, bool swap_only)
 			sp = swap_pager_swapped_pages(obj);
 			kvo->kvo_swapped = sp > UINT32_MAX ? UINT32_MAX : sp;
 		}
-		if (obj->type == OBJT_DEVICE || obj->type == OBJT_MGTDEVICE) {
+		if ((obj->type == OBJT_DEVICE || obj->type == OBJT_MGTDEVICE) &&
+		    (obj->flags & OBJ_CDEVH) != 0) {
 			cdev = obj->un_pager.devp.handle;
 			if (cdev != NULL) {
 				csw = dev_refthread(cdev, &ref);
diff --git a/sys/vm/vm_object.h b/sys/vm/vm_object.h
index 596f67b46e6f..fc39041d02d6 100644
--- a/sys/vm/vm_object.h
+++ b/sys/vm/vm_object.h
@@ -204,6 +204,7 @@ struct vm_object {
 #define	OBJ_PAGERPRIV2	0x00008000	/* Pager private */
 #define	OBJ_SYSVSHM	0x00010000	/* SysV SHM */
 #define	OBJ_POSIXSHM	0x00020000	/* Posix SHM */
+#define	OBJ_CDEVH	0x00040000	/* OBJT_DEVICE handle is cdev */
 
 /*
  * Helpers to perform conversion between vm_object page indexes and offsets.