git: cc876e9fa5d1 - stable/13 - cuse(3): Remove PAGE_SIZE from libcuse.

From: Hans Petter Selasky <hselasky_at_FreeBSD.org>
Date: Wed, 27 Jul 2022 09:36:44 UTC
The branch stable/13 has been updated by hselasky:

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

commit cc876e9fa5d15aa07271cdf609946bf7c8c7fb9f
Author:     Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2022-06-23 20:34:45 +0000
Commit:     Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2022-07-27 09:33:36 +0000

    cuse(3): Remove PAGE_SIZE from libcuse.
    
    To allow for a dynamic page size on arm64 remove the static value from libcuse.
    
    Differential Revision:  https://reviews.freebsd.org/D35585
    Sponsored by:   NVIDIA Networking
    
    (cherry picked from commit 2c28cd09d9f7ac266a8c7b283384712aba0af93a)
---
 lib/libcuse/cuse_lib.c   | 20 +++++++++++---------
 sys/fs/cuse/cuse.c       |  9 ++++++++-
 sys/fs/cuse/cuse_ioctl.h |  6 +++---
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/lib/libcuse/cuse_lib.c b/lib/libcuse/cuse_lib.c
index 436b5db728bd..fda0c96d0d70 100644
--- a/lib/libcuse/cuse_lib.c
+++ b/lib/libcuse/cuse_lib.c
@@ -1,6 +1,6 @@
 /* $FreeBSD$ */
 /*-
- * Copyright (c) 2010-2012 Hans Petter Selasky. All rights reserved.
+ * Copyright (c) 2010-2022 Hans Petter Selasky. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -142,7 +142,7 @@ cuse_vmoffset(void *_ptr)
 	uint8_t *ptr_max;
 	uint8_t *ptr = _ptr;
 	unsigned long remainder;
-	int n;
+	unsigned long n;
 
 	CUSE_LOCK();
 	for (n = 0; n != CUSE_ALLOC_UNIT_MAX; n++) {
@@ -158,9 +158,10 @@ cuse_vmoffset(void *_ptr)
 
 			remainder = (ptr - ptr_min);
 
-			remainder -= remainder % PAGE_SIZE;
+			remainder -= remainder %
+			    (unsigned long)getpagesize();
 
-			return ((n * PAGE_SIZE * CUSE_ALLOC_PAGES_MAX) + remainder);
+			return ((n * CUSE_ALLOC_BYTES_MAX) + remainder);
 		}
 	}
 	CUSE_UNLOCK();
@@ -172,9 +173,10 @@ void   *
 cuse_vmalloc(int size)
 {
 	struct cuse_alloc_info info;
+	unsigned long pgsize;
+	unsigned long n;
 	void *ptr;
 	int error;
-	int n;
 
 	if (f_cuse < 0)
 		return (NULL);
@@ -184,7 +186,8 @@ cuse_vmalloc(int size)
 	if (size < 1)
 		return (NULL);
 
-	info.page_count = howmany(size, PAGE_SIZE);
+	pgsize = getpagesize();
+	info.page_count = howmany(size, pgsize);
 
 	CUSE_LOCK();
 	for (n = 0; n != CUSE_ALLOC_UNIT_MAX; n++) {
@@ -212,10 +215,9 @@ cuse_vmalloc(int size)
 			else
 				break;
 		}
-		ptr = mmap(NULL, info.page_count * PAGE_SIZE,
+		ptr = mmap(NULL, info.page_count * pgsize,
 		    PROT_READ | PROT_WRITE,
-		    MAP_SHARED, f_cuse, CUSE_ALLOC_PAGES_MAX *
-		    PAGE_SIZE * n);
+		    MAP_SHARED, f_cuse, CUSE_ALLOC_BYTES_MAX * n);
 
 		if (ptr == MAP_FAILED) {
 
diff --git a/sys/fs/cuse/cuse.c b/sys/fs/cuse/cuse.c
index a893250043c4..7c7d8ec20493 100644
--- a/sys/fs/cuse/cuse.c
+++ b/sys/fs/cuse/cuse.c
@@ -1,6 +1,6 @@
 /* $FreeBSD$ */
 /*-
- * Copyright (c) 2010-2020 Hans Petter Selasky. All rights reserved.
+ * Copyright (c) 2010-2022 Hans Petter Selasky. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -64,6 +64,13 @@
 #include <fs/cuse/cuse_defs.h>
 #include <fs/cuse/cuse_ioctl.h>
 
+#define	CUSE_ALLOC_PAGES_MAX \
+	(CUSE_ALLOC_BYTES_MAX / PAGE_SIZE)
+
+#if (CUSE_ALLOC_PAGES_MAX == 0)
+#error "PAGE_SIZE is too big!"
+#endif
+
 static int
 cuse_modevent(module_t mod, int type, void *data)
 {
diff --git a/sys/fs/cuse/cuse_ioctl.h b/sys/fs/cuse/cuse_ioctl.h
index 322ff28a4131..44e3c122979d 100644
--- a/sys/fs/cuse/cuse_ioctl.h
+++ b/sys/fs/cuse/cuse_ioctl.h
@@ -1,6 +1,6 @@
 /* $FreeBSD$ */
 /*-
- * Copyright (c) 2014 Hans Petter Selasky. All rights reserved.
+ * Copyright (c) 2014-2022 Hans Petter Selasky. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,13 +30,13 @@
 #include <sys/ioccom.h>
 #include <sys/types.h>
 
-#define	CUSE_BUFFER_MAX		PAGE_SIZE
+#define	CUSE_BUFFER_MAX		(1 << 12)	/* bytes */
 #define	CUSE_DEVICES_MAX	64	/* units */
 #define	CUSE_BUF_MIN_PTR	0x10000UL
 #define	CUSE_BUF_MAX_PTR	0x20000UL
 #define	CUSE_ALLOC_UNIT_MAX	128	/* units */
 /* All memory allocations must be less than the following limit */
-#define	CUSE_ALLOC_PAGES_MAX	(((16UL * 1024UL * 1024UL) + PAGE_SIZE - 1) / PAGE_SIZE)
+#define	CUSE_ALLOC_BYTES_MAX	(1UL << 24)	/* bytes */
 
 struct cuse_dev;