[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250124235454.84587-6-anthony.yznaga@oracle.com>
Date: Fri, 24 Jan 2025 15:54:39 -0800
From: Anthony Yznaga <anthony.yznaga@...cle.com>
To: akpm@...ux-foundation.org, willy@...radead.org, markhemm@...glemail.com,
viro@...iv.linux.org.uk, david@...hat.com, khalid@...nel.org
Cc: anthony.yznaga@...cle.com, jthoughton@...gle.com, corbet@....net,
dave.hansen@...el.com, kirill@...temov.name, luto@...nel.org,
brauner@...nel.org, arnd@...db.de, ebiederm@...ssion.com,
catalin.marinas@....com, mingo@...hat.com, peterz@...radead.org,
liam.howlett@...cle.com, lorenzo.stoakes@...cle.com, vbabka@...e.cz,
jannh@...gle.com, hannes@...xchg.org, mhocko@...nel.org,
roman.gushchin@...ux.dev, shakeel.butt@...ux.dev,
muchun.song@...ux.dev, tglx@...utronix.de, cgroups@...r.kernel.org,
x86@...nel.org, linux-doc@...r.kernel.org, linux-arch@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-mm@...ck.org, mhiramat@...nel.org,
rostedt@...dmis.org, vasily.averin@...ux.dev, xhao@...ux.alibaba.com,
pcc@...gle.com, neilb@...e.de, maz@...nel.org
Subject: [PATCH 05/20] mm/mshare: Add ioctl support
From: Khalid Aziz <khalid@...nel.org>
Reserve a range of ioctls for msharefs and add the first two ioctls
to get and set the start address and size of an mshare region.
Signed-off-by: Khalid Aziz <khalid@...nel.org>
Signed-off-by: Anthony Yznaga <anthony.yznaga@...cle.com>
---
.../userspace-api/ioctl/ioctl-number.rst | 1 +
include/uapi/linux/msharefs.h | 29 ++++++++
mm/mshare.c | 68 +++++++++++++++++++
3 files changed, 98 insertions(+)
create mode 100644 include/uapi/linux/msharefs.h
diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
index 243f1f1b554a..aa22b5412e4d 100644
--- a/Documentation/userspace-api/ioctl/ioctl-number.rst
+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
@@ -303,6 +303,7 @@ Code Seq# Include File Comments
'v' 20-27 arch/powerpc/include/uapi/asm/vas-api.h VAS API
'v' C0-FF linux/meye.h conflict!
'w' all CERN SCI driver
+'x' 00-1F linux/msharefs.h msharefs filesystem
'y' 00-1F packet based user level communications
<mailto:zapman@...erlan.net>
'z' 00-3F CAN bus card conflict!
diff --git a/include/uapi/linux/msharefs.h b/include/uapi/linux/msharefs.h
new file mode 100644
index 000000000000..c7b509c7e093
--- /dev/null
+++ b/include/uapi/linux/msharefs.h
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * msharefs defines a memory region that is shared across processes.
+ * ioctl is used on files created under msharefs to set various
+ * attributes on these shared memory regions
+ *
+ *
+ * Copyright (C) 2024 Oracle Corp. All rights reserved.
+ * Author: Khalid Aziz <khalid@...nel.org>
+ */
+
+#ifndef _UAPI_LINUX_MSHAREFS_H
+#define _UAPI_LINUX_MSHAREFS_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+/*
+ * msharefs specific ioctl commands
+ */
+#define MSHAREFS_GET_SIZE _IOR('x', 0, struct mshare_info)
+#define MSHAREFS_SET_SIZE _IOW('x', 1, struct mshare_info)
+
+struct mshare_info {
+ __u64 start;
+ __u64 size;
+};
+
+#endif
diff --git a/mm/mshare.c b/mm/mshare.c
index 060292fb6a00..056cb5a82547 100644
--- a/mm/mshare.c
+++ b/mm/mshare.c
@@ -10,24 +10,91 @@
*
* Copyright (C) 2024 Oracle Corp. All rights reserved.
* Author: Khalid Aziz <khalid@...nel.org>
+ * Author: Matthew Wilcox <willy@...radead.org>
*
*/
#include <linux/fs.h>
#include <linux/fs_context.h>
+#include <linux/spinlock_types.h>
#include <uapi/linux/magic.h>
+#include <uapi/linux/msharefs.h>
const unsigned long mshare_align = P4D_SIZE;
struct mshare_data {
struct mm_struct *mm;
+ spinlock_t m_lock;
+ struct mshare_info minfo;
};
+static long
+msharefs_set_size(struct mm_struct *host_mm, struct mshare_data *m_data,
+ struct mshare_info *minfo)
+{
+ /*
+ * Validate alignment for start address and size
+ */
+ if (!minfo->size || ((minfo->start | minfo->size) & (mshare_align - 1))) {
+ spin_unlock(&m_data->m_lock);
+ return -EINVAL;
+ }
+
+ host_mm->mmap_base = minfo->start;
+ host_mm->task_size = minfo->size;
+
+ m_data->minfo.start = host_mm->mmap_base;
+ m_data->minfo.size = host_mm->task_size;
+ spin_unlock(&m_data->m_lock);
+
+ return 0;
+}
+
+static long
+msharefs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+ struct mshare_data *m_data = filp->private_data;
+ struct mm_struct *host_mm = m_data->mm;
+ struct mshare_info minfo;
+
+ switch (cmd) {
+ case MSHAREFS_GET_SIZE:
+ spin_lock(&m_data->m_lock);
+ minfo = m_data->minfo;
+ spin_unlock(&m_data->m_lock);
+
+ if (copy_to_user((void __user *)arg, &minfo, sizeof(minfo)))
+ return -EFAULT;
+
+ return 0;
+
+ case MSHAREFS_SET_SIZE:
+ if (copy_from_user(&minfo, (struct mshare_info __user *)arg,
+ sizeof(minfo)))
+ return -EFAULT;
+
+ /*
+ * If this mshare region has been set up once already, bail out
+ */
+ spin_lock(&m_data->m_lock);
+ if (m_data->minfo.size != 0) {
+ spin_unlock(&m_data->m_lock);
+ return -EINVAL;
+ }
+
+ return msharefs_set_size(host_mm, m_data, &minfo);
+
+ default:
+ return -ENOTTY;
+ }
+}
+
static const struct inode_operations msharefs_dir_inode_ops;
static const struct inode_operations msharefs_file_inode_ops;
static const struct file_operations msharefs_file_operations = {
.open = simple_open,
+ .unlocked_ioctl = msharefs_ioctl,
};
static int
@@ -51,6 +118,7 @@ msharefs_fill_mm(struct inode *inode)
goto err_free;
}
m_data->mm = mm;
+ spin_lock_init(&m_data->m_lock);
inode->i_private = m_data;
return 0;
--
2.43.5
Powered by blists - more mailing lists