Skip to contentSkip to conversation

Anonymous shared memory (ashmem) subsystem

RFC PATCH In review John Stultz 6 participants 9 messages Jul 18, 2011 – Jul 19, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Anonymous shared memory (ashmem) subsystem

From: Robert Love <[email protected]>

Recently, Robert Love updated his ashmem patch and posted it here:
http://www.kernel.org/pub/linux/kernel/people/rml/ashmem/

This reminded me that in all the conversations around the Android patches, I've not seen any discussion on lkml about the ashmem functionality. Coming from Robert, its very clean and self-contained and seems pretty interesting. So I wanted to send it out for review and comments.

thanks
-john

The anonymous shared memory (ashmem) subsystem provides a Unix-y,file-based shared memory interface to user-space. It works like anonymous memory (e.g. mmapping fd=0) except if you share the file descriptor via the usual means, you will share the mapping. The shared memory can be accessed via both mmap or file I/O. The backing store is a simple shmem file.

Additionally, ashmem introduces the concept of page pinning. Pinned pages (the default) behave like any anonymous memory. Unpinned pages are available to the kernel for eviction during VM pressure. When repinning the pages, the return value instructs user-space as to any eviction. In this manner, user-space processes may implement caching and similar resource management that efficiently integrates with kernel memory management.

CC Andrew Morton <[email protected]>
CC Hugh Dickins <[email protected]>
CC Mel Gorman <[email protected]>
CC Dave Hansen <[email protected]>
CC Rik van Riel <[email protected]>
CC Eric Anholt <[email protected]>
CC Jesse Barnes <[email protected]>
[jstultz: minor whitespace fixes for checkpatch.pl]
---
 include/linux/ashmem.h |   48 +++
 include/linux/mm.h     |    1 +
 init/Kconfig           |   12 +
 mm/Makefile            |    1 +
 mm/ashmem.c            |  747 ++++++++++++++++++++++++++++++++++++++++++++++++
 mm/shmem.c             |   14 +-
 6 files changed, 818 insertions(+), 5 deletions(-)
 create mode 100644 include/linux/ashmem.h
 create mode 100644 mm/ashmem.c
include/linux/ashmem.h new file +48 -0
new file mode 100644
index 0000000..1976b10
@@ -0,0 +1,48 @@
+/*
+ * include/linux/ashmem.h
+ *
+ * Copyright 2008 Google Inc.
+ * Author: Robert Love
+ *
+ * This file is dual licensed.  It may be redistributed and/or modified
+ * under the terms of the Apache 2.0 License OR version 2 of the GNU
+ * General Public License.
+ */
+
+#ifndef _LINUX_ASHMEM_H
+#define _LINUX_ASHMEM_H
+
+#include <linux/limits.h>
+#include <linux/ioctl.h>
+
+#define ASHMEM_NAME_LEN		256
+
+#define ASHMEM_NAME_DEF		"dev/ashmem"
+
+/* Return values from ASHMEM_PIN: Was the mapping purged while unpinned? */
+#define ASHMEM_NOT_PURGED	0
+#define ASHMEM_WAS_PURGED	1
+
+/* Return values from ASHMEM_GET_PIN_STATUS: Is the mapping pinned? */
+#define ASHMEM_IS_UNPINNED	0
+#define ASHMEM_IS_PINNED	1
+
+struct ashmem_pin {
+	__u32 offset;	/* offset into region, in bytes, page-aligned */
+	__u32 len;	/* length forward from offset, in bytes, page-aligned */
+};
+
+#define __ASHMEMIOC		0x77
+
+#define ASHMEM_SET_NAME		_IOW(__ASHMEMIOC, 1, char[ASHMEM_NAME_LEN])
+#define ASHMEM_GET_NAME		_IOR(__ASHMEMIOC, 2, char[ASHMEM_NAME_LEN])
+#define ASHMEM_SET_SIZE		_IOW(__ASHMEMIOC, 3, size_t)
+#define ASHMEM_GET_SIZE		_IO(__ASHMEMIOC, 4)
+#define ASHMEM_SET_PROT_MASK	_IOW(__ASHMEMIOC, 5, unsigned long)
+#define ASHMEM_GET_PROT_MASK	_IO(__ASHMEMIOC, 6)
+#define ASHMEM_PIN		_IOW(__ASHMEMIOC, 7, struct ashmem_pin)
+#define ASHMEM_UNPIN		_IOW(__ASHMEMIOC, 8, struct ashmem_pin)
+#define ASHMEM_GET_PIN_STATUS	_IO(__ASHMEMIOC, 9)
+#define ASHMEM_PURGE_ALL_CACHES	_IO(__ASHMEMIOC, 10)
+
+#endif	/* _LINUX_ASHMEM_H */
include/linux/mm.h +1 -0
index 9670f71..de20025 100644
@@ -871,6 +871,7 @@ extern bool skip_free_areas_node(unsigned int flags, int nid);
 
 int shmem_lock(struct file *file, int lock, struct user_struct *user);
 struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags);
+void shmem_set_file(struct vm_area_struct *vma, struct file *file);
 int shmem_zero_setup(struct vm_area_struct *);
 
 extern int can_do_mlock(void);
init/Kconfig +12 -0
index 412c21b..e9f7058 100644
@@ -1081,6 +1081,18 @@ config SHMEM
 	  option replaces shmem and tmpfs with the much simpler ramfs code,
 	  which may be appropriate on small systems without swap.
 
+config ASHMEM
+	bool "Enable the Anonymous Shared Memory Subsystem"
+	default n
+	depends on SHMEM || TINY_SHMEM
+	help
+	  The ashmem subsystem is a new shared memory allocator available to
+	  user-space, similar to POSIX SHM but with different behavior and a
+	  more Unix-y, file-based API. The ashmem subsystem also supports
+	  the eviction of unpinned pages during VM pressure, allowing
+	  user-space to implement caches that are automatically shrunk when
+	  memory is low.
+
 config AIO
 	bool "Enable AIO support" if EXPERT
 	default y
mm/Makefile +1 -0
index 836e416..cd41f09 100644
@@ -30,6 +30,7 @@ obj-$(CONFIG_HUGETLBFS)	+= hugetlb.o
 obj-$(CONFIG_NUMA) 	+= mempolicy.o
 obj-$(CONFIG_SPARSEMEM)	+= sparse.o
 obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o
+obj-$(CONFIG_ASHMEM)	+= ashmem.o
 obj-$(CONFIG_SLOB) += slob.o
 obj-$(CONFIG_COMPACTION) += compaction.o
 obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o
mm/ashmem.c new file +747 -0

747 lines not on this page · raw message

mm/shmem.c +9 -5
index fcedf54..f6de1b2 100644
@@ -3015,6 +3015,14 @@ put_memory:
 }
 EXPORT_SYMBOL_GPL(shmem_file_setup);
 
+void shmem_set_file(struct vm_area_struct *vma, struct file *file)
+{
+	if (vma->vm_file)
+		fput(vma->vm_file);
+	vma->vm_file = file;
+	vma->vm_ops = &shmem_vm_ops;
+}
+
 /**
  * shmem_zero_setup - setup a shared anonymous mapping
  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
@@ -3027,11 +3035,7 @@ int shmem_zero_setup(struct vm_area_struct *vma)
 	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
 	if (IS_ERR(file))
 		return PTR_ERR(file);
-
-	if (vma->vm_file)
-		fput(vma->vm_file);
-	vma->vm_file = file;
-	vma->vm_ops = &shmem_vm_ops;
+	shmem_set_file(vma, file);
 	vma->vm_flags |= VM_CAN_NONLINEAR;
 	return 0;
 }
Signature · 1 line
1.7.3.2.146.gca209
#1 lore raw
Quoted 9 lines · John Stultz

From: Robert Love <[email protected]>

Recently, Robert Love updated his ashmem patch and posted it here:
http://www.kernel.org/pub/linux/kernel/people/rml/ashmem/

This reminded me that in all the conversations around the Android patches, I've not seen any discussion on lkml about the ashmem functionality. Coming from Robert, its very clean and self-contained and seems pretty interesting. So I wanted to send it out for review and comments.

And utterly stupid, just use tmpfs and /dev/zero for the base functionality.

And the pinning crap really needs a much better rationale.

#2 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · John Stultz

From: Robert Love <[email protected]>

Recently, Robert Love updated his ashmem patch and posted it here:
      http://www.kernel.org/pub/linux/kernel/people/rml/ashmem/

Quoted 8 lines
Quoted 4 lines

This reminded me that in all the conversations around the Android patches, I've not seen any discussion on lkml about the ashmem functionality. Coming from Robert, its very clean and self-contained and seems pretty interesting. So I wanted to send it out for review and comments.

And utterly stupid, just use tmpfs and /dev/zero for the base functionality.

And the pinning crap really needs a much better rationale.

The pinning stuff seems like a reasonable idea to me, but why should it be limited to ashmem using a private ioctl? If it were a proper syscall, I could see automatic discarding being useful in on-disk filesystems as well, for managing persistent caches. But yes, tmpfs already solved the problem of getting a shared-memory file descriptor
- just open a random-name file in tmpfs, unlink it, and start passing it around.

#3 lore raw
Quoted 4 lines · Bryan Donlan

The pinning stuff seems like a reasonable idea to me, but why should it be limited to ashmem using a private ioctl? If it were a proper syscall, I could see automatic discarding being useful in on-disk filesystems as well, for managing persistent caches.

Exactly.

Quoted 4 lines

But yes, tmpfs
already solved the problem of getting a shared-memory file descriptor
- just open a random-name file in tmpfs, unlink it, and start passing it around.

Indeed. And as mentioned before using /dev/zero as the file will get you a pre-unlinked tmpfs fd without any races, and without the need of actually mounting shmem into any namespace.

#4 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines

But yes, tmpfs
already solved the problem of getting a shared-memory file descriptor
- just open a random-name file in tmpfs, unlink it, and start passing it around.

Indeed. And as mentioned before using /dev/zero as the file will get you a pre-unlinked tmpfs fd without any races, and without the need of actually mounting shmem into any namespace.

"Why not use tmpfs?" was my gut reaction, too. The patch is named a bit unfortunately, but I think the key bit in them is the "volatile" pages, not the "anonymous" or "shared memory" features.

Those let userspace allocate things like caches, and lets the kernel destructively (but safely) reclaim them when there's memory pressure (implemented as a slab shrinker here). It's kinda like a kernel-driven garbage collector, or even like a distant relative of those CMM2 patches from the s390 folks a few years ago.

This implementation has some real show-stopper things in it, like its own LRU. But, the concept seems interesting at least.

-- Dave

#5 lore raw
Quoted 2 lines · Dave Hansen

This implementation has some real show-stopper things in it, like its own LRU. But, the concept seems interesting at least.

So if you care about it implement it sanely and not like a monkey on crack would do it. Having a madvice flag to allow reclaiming tmpfs pages, and using the normal VM infrastructure sounds like a perfectly fine feature.

#6 lore raw
Quoted 3 lines · Christoph Hellwig
Quoted 2 lines · Dave Hansen

This implementation has some real show-stopper things in it, like its own LRU. But, the concept seems interesting at least.

[snip]

Quoted 3 lines

Having a madvice flag to allow reclaiming tmpfs
pages, and using the normal VM infrastructure sounds like a perfectly fine feature.

Ok. Thanks all for the review and comments. I'll see if something like what was suggested can be worked out.

thanks again,
-john

#7 lore raw
In reply to John Stultz
Quoted 6 lines · John Stultz

Additionally, ashmem introduces the concept of page pinning. Pinned pages (the default) behave like any anonymous memory. Unpinned pages are available to the kernel for eviction during VM pressure. When repinning the pages, the return value instructs user-space as to any eviction. In this manner, user-space processes may implement caching and similar resource management that efficiently integrates with kernel memory management.

Memory locking (mlock) may be a better term to use here. mlock prevents eviction. Page pinning also prevents the movement of the pages in memory (as needed by page migration, compaction etc).

#8 lore raw
In reply to John Stultz
Quoted 10 lines · John Stultz
include/linux/ashmem.h new file +10 -0
+
+#define ASHMEM_NAME_DEF		"dev/ashmem"
+
+/* Return values from ASHMEM_PIN: Was the mapping purged while unpinned? */
+#define ASHMEM_NOT_PURGED	0
+#define ASHMEM_WAS_PURGED	1
+
+/* Return values from ASHMEM_GET_PIN_STATUS: Is the mapping pinned? */
+#define ASHMEM_IS_UNPINNED	0
+#define ASHMEM_IS_PINNED	1

include/linux/ashmem.h line 38

Quoted 10 lines
include/linux/ashmem.h new file +10 -0
+
+struct ashmem_pin {
+	__u32 offset;	/* offset into region, in bytes, page-aligned */
+	__u32 len;	/* length forward from offset, in bytes, page-aligned */
+};
+
+#define __ASHMEMIOC		0x77
+
+#define ASHMEM_SET_NAME		_IOW(__ASHMEMIOC, 1, char[ASHMEM_NAME_LEN])
+#define ASHMEM_GET_NAME		_IOR(__ASHMEMIOC, 2, char[ASHMEM_NAME_LEN])

Having a name for an 'anonymous shared memory' segment is rather counter-intuitive ;-)

include/linux/ashmem.h line 42

Quoted 4 lines
include/linux/ashmem.h new file +4 -0
+#define ASHMEM_SET_SIZE		_IOW(__ASHMEMIOC, 3, size_t)
+#define ASHMEM_GET_SIZE		_IO(__ASHMEMIOC, 4)
+#define ASHMEM_SET_PROT_MASK	_IOW(__ASHMEMIOC, 5, unsigned long)
+#define ASHMEM_GET_PROT_MASK	_IO(__ASHMEMIOC, 6)

size_t and unsigned long arguments in ioctl commands are harmful, because they require a compat_ioctl function. It's often better to just make these either __u32 or __u64.

mm/Makefile obj-$(CONFIG_HUGETLBFS) += hugetlb.o · line 36

Quoted 12 lines
mm/Makefile +1 -0
index 836e416..cd41f09 100644
@@ -30,6 +30,7 @@ obj-$(CONFIG_HUGETLBFS)	+= hugetlb.o
 obj-$(CONFIG_NUMA) 	+= mempolicy.o
 obj-$(CONFIG_SPARSEMEM)	+= sparse.o
 obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o
+obj-$(CONFIG_ASHMEM)	+= ashmem.o
 obj-$(CONFIG_SLOB) += slob.o
 obj-$(CONFIG_COMPACTION) += compaction.o
 obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o

Wouldn't this better live in drivers/char, next to mem.c or even merged into that file?

mm/Makefile

Quoted 10 lines
mm/Makefile +10 -0
+static const struct file_operations ashmem_fops = {
+	.owner = THIS_MODULE,
+	.open = ashmem_open,
+	.release = ashmem_release,
+	.read = ashmem_read,
+	.llseek = ashmem_llseek,
+	.mmap = ashmem_mmap,
+	.unlocked_ioctl = ashmem_ioctl,
+	.compat_ioctl = ashmem_ioctl,
+};

The compat_ioctl is wrong here, as described above.

Arnd