All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] prctl: Get private anonymous memory region name
@ 2023-11-25 14:04 Rong Tao
  2023-11-25 15:56 ` Oleg Nesterov
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Rong Tao @ 2023-11-25 14:04 UTC (permalink / raw)
  To: Andrew Morton, Catalin Marinas, Florent Revest, Kees Cook,
	David Hildenbrand, Stefan Roesch, Andy Chiu, Josh Triplett,
	Joey Gouly, Oleg Nesterov, Miguel Ojeda, Ondrej Mosnacek,
	open list:MEMORY MANAGEMENT, open list
  Cc: rtoax, rongtao

From: Rong Tao <rongtao@cestc.cn>

In commit 9a10064f5625 ("mm: add a field to store names for private anony-
mous memory") add PR_SET_VMA options and PR_SET_VMA_ANON_NAME for the prctl
system call, then the PR_GET_VMA interface should be provided accordingly,
which is necessary, as the userspace program usually wants to know what
VMA name it has configured for the anonymous page.

Userspace can set the name for a region of memory by calling:

    prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, start, len, (unsigned long)name);

Then, Userspace can get the name of a memory region by calling:

    char buf[80];
    prctl(PR_GET_VMA, PR_GET_VMA_ANON_NAME, start, buf, 0);

Changes for prctl(2) manual page (in the options section):

PR_GET_VMA
        Gets an attribute specified in arg2 for virtual memory areas
        starting from the address specified in arg3 and spanning the
        size specified in arg4. arg5 specifies the value of the attribute
        to be set.

        Currently, arg2 must be one of:

        PR_GET_VMA_ANON_NAME
                Get name of anonymous virtual memory areas. arg4 should be
                a buffer in the user's program, and the size of the buffer
                should not be less than 80 bytes, otherwise it is possible
                that the prctl return will fail due to a copy failure
                (unless you know the length of the name you set through
                the PR_SET_VMA_ANON_NAME).

                This feature is available only if the kernel is built with
                the CONFIG_ANON_VMA_NAME option enabled.

Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
 include/linux/mm.h         |  7 ++++++
 include/uapi/linux/prctl.h |  3 +++
 kernel/sys.c               | 44 ++++++++++++++++++++++++++++++++++++++
 mm/madvise.c               | 18 ++++++++++++++++
 4 files changed, 72 insertions(+)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 418d26608ece..2981c94c52f8 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -4106,6 +4106,8 @@ static inline int seal_check_write(int seals, struct vm_area_struct *vma)
 }
 
 #ifdef CONFIG_ANON_VMA_NAME
+const struct anon_vma_name *madvise_get_anon_name(struct mm_struct *mm,
+						  unsigned long start);
 int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
 			  unsigned long len_in,
 			  struct anon_vma_name *anon_name);
@@ -4115,6 +4117,11 @@ madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
 		      unsigned long len_in, struct anon_vma_name *anon_name) {
 	return 0;
 }
+static inline
+const struct anon_vma_name *madvise_get_anon_name(struct mm_struct *mm,
+						  unsigned long start) {
+	return NULL;
+}
 #endif
 
 #ifdef CONFIG_UNACCEPTED_MEMORY
diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
index 370ed14b1ae0..8ba0016d77de 100644
--- a/include/uapi/linux/prctl.h
+++ b/include/uapi/linux/prctl.h
@@ -291,6 +291,9 @@ struct prctl_mm_map {
 #define PR_SET_VMA		0x53564d41
 # define PR_SET_VMA_ANON_NAME		0
 
+#define PR_GET_VMA		0x53564d42
+# define PR_GET_VMA_ANON_NAME		0
+
 #define PR_GET_AUXV			0x41555856
 
 #define PR_SET_MEMORY_MERGE		67
diff --git a/kernel/sys.c b/kernel/sys.c
index e219fcfa112d..f11eadd666d0 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -2359,12 +2359,53 @@ static int prctl_set_vma(unsigned long opt, unsigned long addr,
 	return error;
 }
 
+static int prctl_get_vma(unsigned long opt, unsigned long addr,
+			 unsigned long buf, unsigned long arg)
+{
+	struct mm_struct *mm = current->mm;
+	const char __user *u_buf;
+	int error;
+
+	switch (opt) {
+	case PR_GET_VMA_ANON_NAME:
+		const struct anon_vma_name *anon_name = NULL;
+
+		u_buf = (const char __user *)buf;
+		error = 0;
+
+		mmap_read_lock(mm);
+		anon_name = madvise_get_anon_name(mm, addr);
+		if (!anon_name) {
+			mmap_read_unlock(mm);
+			error = -EFAULT;
+			break;
+		}
+
+		if (copy_to_user((char __user *)u_buf, anon_name->name,
+				 strlen(anon_name->name) + 1))
+			error = -EFAULT;
+
+		mmap_read_unlock(mm);
+		anon_vma_name_put(anon_name);
+		break;
+	default:
+		error = -EINVAL;
+	}
+	return error;
+}
+
 #else /* CONFIG_ANON_VMA_NAME */
 static int prctl_set_vma(unsigned long opt, unsigned long start,
 			 unsigned long size, unsigned long arg)
 {
 	return -EINVAL;
 }
+
+static int prctl_get_vma(unsigned long opt, unsigned long start,
+			 unsigned long u_buf, unsigned long arg)
+{
+	return -EINVAL;
+}
 #endif /* CONFIG_ANON_VMA_NAME */
 
 static inline unsigned long get_current_mdwe(void)
@@ -2712,6 +2753,9 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 	case PR_SET_VMA:
 		error = prctl_set_vma(arg2, arg3, arg4, arg5);
 		break;
+	case PR_GET_VMA:
+		error = prctl_get_vma(arg2, arg3, arg4, arg5);
+		break;
 	case PR_GET_AUXV:
 		if (arg4 || arg5)
 			return -EINVAL;
diff --git a/mm/madvise.c b/mm/madvise.c
index cf4d694280e9..a68f7a7d6389 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -1287,6 +1287,24 @@ static int madvise_vma_anon_name(struct vm_area_struct *vma,
 	return error;
 }
 
+const struct anon_vma_name *madvise_get_anon_name(struct mm_struct *mm,
+						  unsigned long start)
+{
+	struct vm_area_struct *vma;
+	struct anon_vma_name *anon_name;
+
+	vma = find_vma(mm, start);
+	if (vma) {
+		anon_name = anon_vma_name(vma);
+		if (anon_name) {
+			anon_vma_name_get(anon_name);
+			return anon_name;
+		}
+	}
+
+	return NULL;
+}
+
 int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
 			  unsigned long len_in, struct anon_vma_name *anon_name)
 {
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 7+ messages in thread
* Re: [PATCH] prctl: Get private anonymous memory region name
@ 2023-11-25 21:26 kernel test robot
  0 siblings, 0 replies; 7+ messages in thread
From: kernel test robot @ 2023-11-25 21:26 UTC (permalink / raw)
  To: oe-kbuild; +Cc: lkp

:::::: 
:::::: Manual check reason: "low confidence static check warning: kernel/sys.c:2371:17: sparse: sparse: typename in expression"
:::::: 

BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <tencent_977CBF8E8CA6234A1B740A35655D5D7EAA0A@qq.com>
References: <tencent_977CBF8E8CA6234A1B740A35655D5D7EAA0A@qq.com>
TO: Rong Tao <rtoax@foxmail.com>
TO: Andrew Morton <akpm@linux-foundation.org>
CC: Linux Memory Management List <linux-mm@kvack.org>
TO: Catalin Marinas <catalin.marinas@arm.com>
TO: Florent Revest <revest@chromium.org>
TO: Kees Cook <keescook@chromium.org>
TO: David Hildenbrand <david@redhat.com>
TO: Stefan Roesch <shr@devkernel.io>
TO: Andy Chiu <andy.chiu@sifive.com>
TO: Josh Triplett <josh@joshtriplett.org>
TO: Joey Gouly <joey.gouly@arm.com>
TO: Oleg Nesterov <oleg@redhat.com>
TO: Miguel Ojeda <ojeda@kernel.org>
TO: Ondrej Mosnacek <omosnace@redhat.com>
TO: linux-kernel@vger.kernel.org
CC: rtoax@foxmail.com
CC: rongtao@cestc.cn

Hi Rong,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on linus/master v6.7-rc2 next-20231124]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rong-Tao/prctl-Get-private-anonymous-memory-region-name/20231125-220925
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/tencent_977CBF8E8CA6234A1B740A35655D5D7EAA0A%40qq.com
patch subject: [PATCH] prctl: Get private anonymous memory region name
:::::: branch date: 7 hours ago
:::::: commit date: 7 hours ago
config: arm64-randconfig-r133-20231126 (https://download.01.org/0day-ci/archive/20231126/202311260555.H8SrEcwc-lkp@intel.com/config)
compiler: aarch64-linux-gcc (GCC) 13.2.0
reproduce: (https://download.01.org/0day-ci/archive/20231126/202311260555.H8SrEcwc-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/r/202311260555.H8SrEcwc-lkp@intel.com/

sparse warnings: (new ones prefixed by >>)
>> kernel/sys.c:2371:17: sparse: sparse: typename in expression
   kernel/sys.c:2371:23: sparse: sparse: Expected ; at end of statement
   kernel/sys.c:2371:23: sparse: sparse: got struct
   kernel/sys.c:2371:17: sparse: sparse: undefined identifier 'const'
   kernel/sys.c:2377:17: sparse: sparse: undefined identifier 'anon_name'
   kernel/sys.c:2378:22: sparse: sparse: undefined identifier 'anon_name'
   kernel/sys.c:2384:56: sparse: sparse: undefined identifier 'anon_name'
   kernel/sys.c:2389:35: sparse: sparse: undefined identifier 'anon_name'

vim +2371 kernel/sys.c

9a10064f5625d5 Colin Cross 2022-01-14  2361  
e5c636bb5ceadb Rong Tao    2023-11-25  2362  static int prctl_get_vma(unsigned long opt, unsigned long addr,
e5c636bb5ceadb Rong Tao    2023-11-25  2363  			 unsigned long buf, unsigned long arg)
e5c636bb5ceadb Rong Tao    2023-11-25  2364  {
e5c636bb5ceadb Rong Tao    2023-11-25  2365  	struct mm_struct *mm = current->mm;
e5c636bb5ceadb Rong Tao    2023-11-25  2366  	const char __user *u_buf;
e5c636bb5ceadb Rong Tao    2023-11-25  2367  	int error;
e5c636bb5ceadb Rong Tao    2023-11-25  2368  
e5c636bb5ceadb Rong Tao    2023-11-25  2369  	switch (opt) {
e5c636bb5ceadb Rong Tao    2023-11-25  2370  	case PR_GET_VMA_ANON_NAME:
e5c636bb5ceadb Rong Tao    2023-11-25 @2371  		const struct anon_vma_name *anon_name = NULL;
e5c636bb5ceadb Rong Tao    2023-11-25  2372  
e5c636bb5ceadb Rong Tao    2023-11-25  2373  		u_buf = (const char __user *)buf;
e5c636bb5ceadb Rong Tao    2023-11-25  2374  		error = 0;
e5c636bb5ceadb Rong Tao    2023-11-25  2375  
e5c636bb5ceadb Rong Tao    2023-11-25  2376  		mmap_read_lock(mm);
e5c636bb5ceadb Rong Tao    2023-11-25  2377  		anon_name = madvise_get_anon_name(mm, addr);
e5c636bb5ceadb Rong Tao    2023-11-25  2378  		if (!anon_name) {
e5c636bb5ceadb Rong Tao    2023-11-25  2379  			mmap_read_unlock(mm);
e5c636bb5ceadb Rong Tao    2023-11-25  2380  			error = -EFAULT;
e5c636bb5ceadb Rong Tao    2023-11-25  2381  			break;
e5c636bb5ceadb Rong Tao    2023-11-25  2382  		}
e5c636bb5ceadb Rong Tao    2023-11-25  2383  
e5c636bb5ceadb Rong Tao    2023-11-25  2384  		if (copy_to_user((char __user *)u_buf, anon_name->name,
e5c636bb5ceadb Rong Tao    2023-11-25  2385  				 strlen(anon_name->name) + 1))
e5c636bb5ceadb Rong Tao    2023-11-25  2386  			error = -EFAULT;
e5c636bb5ceadb Rong Tao    2023-11-25  2387  
e5c636bb5ceadb Rong Tao    2023-11-25  2388  		mmap_read_unlock(mm);
e5c636bb5ceadb Rong Tao    2023-11-25  2389  		anon_vma_name_put(anon_name);
e5c636bb5ceadb Rong Tao    2023-11-25  2390  		break;
e5c636bb5ceadb Rong Tao    2023-11-25  2391  	default:
e5c636bb5ceadb Rong Tao    2023-11-25  2392  		error = -EINVAL;
e5c636bb5ceadb Rong Tao    2023-11-25  2393  	}
e5c636bb5ceadb Rong Tao    2023-11-25  2394  	return error;
e5c636bb5ceadb Rong Tao    2023-11-25  2395  }
e5c636bb5ceadb Rong Tao    2023-11-25  2396  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-11-26  3:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-25 14:04 [PATCH] prctl: Get private anonymous memory region name Rong Tao
2023-11-25 15:56 ` Oleg Nesterov
2023-11-26  3:04   ` Rong Tao
2023-11-25 20:00 ` kernel test robot
2023-11-25 20:00 ` kernel test robot
2023-11-25 22:26 ` kernel test robot
  -- strict thread matches above, loose matches on Subject: below --
2023-11-25 21:26 kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.