Skip to contentSkip to conversation

staging, android, ashmem: invalidate pin/unpin ioctl for private map

RFC PATCH In review Joonsoo Kim 2 participants 5 messages Nov 30, 2012 – Dec 3, 2012 Staging

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 staging, android, ashmem: invalidate pin/unpin ioctl for private map

Private mapped ashmem doesn't install vm fault handler.
So, do_anonymous_page is called for handling fault in handle_pte_fault(). This type of backed memory isn't related to asma->file which is used by ashmem shrinker. Shrinking unpinned area for this mapping will not shrink memory actually.
Although it's memory doesn't actually removed, pin status will return ASHMEM_WAS_PURGED. So application will re-load content. This is needless overhead, so invalidate pin/unpin ioctl behavior for private map.

CC John Stultz <[email protected]>
CC Brian Swetland <[email protected]>
CC Colin Cross <[email protected]>
CC Arve Hjønnevåg <[email protected]>
CC Dima Zavin <[email protected]>
CC Robert Love <[email protected]>
drivers/staging/android/ashmem.c +29 -13
index 634b9ae..2fde9df 100644
@@ -49,6 +49,7 @@ struct ashmem_area {
 	struct file *file;		 /* the shmem-based backing file */
 	size_t size;			 /* size of the mapping, in bytes */
 	unsigned long prot_mask;	 /* allowed prot bits, as vm_flags */
+	bool shared_mapping;		 /* mapping type */
 };
 
 /*
@@ -327,6 +328,7 @@ static int ashmem_mmap(struct file *file, struct vm_area_struct *vma)
 			fput(asma->file);
 			goto out;
 		}
+		asma->shared_mapping = 1;
 	}
 
 	if (vma->vm_file)
@@ -614,21 +616,35 @@ static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd,
 	pgstart = pin.offset / PAGE_SIZE;
 	pgend = pgstart + (pin.len / PAGE_SIZE) - 1;
 
-	mutex_lock(&ashmem_mutex);
+	if (asma->shared_mapping) {
+		mutex_lock(&ashmem_mutex);
 
-	switch (cmd) {
-	case ASHMEM_PIN:
-		ret = ashmem_pin(asma, pgstart, pgend);
-		break;
-	case ASHMEM_UNPIN:
-		ret = ashmem_unpin(asma, pgstart, pgend);
-		break;
-	case ASHMEM_GET_PIN_STATUS:
-		ret = ashmem_get_pin_status(asma, pgstart, pgend);
-		break;
-	}
+		switch (cmd) {
+		case ASHMEM_PIN:
+			ret = ashmem_pin(asma, pgstart, pgend);
+			break;
+		case ASHMEM_UNPIN:
+			ret = ashmem_unpin(asma, pgstart, pgend);
+			break;
+		case ASHMEM_GET_PIN_STATUS:
+			ret = ashmem_get_pin_status(asma, pgstart, pgend);
+			break;
+		}
 
-	mutex_unlock(&ashmem_mutex);
+		mutex_unlock(&ashmem_mutex);
+
+	} else {
+		switch (cmd) {
+		/* if it is private map, pin/unpin have no impact to vm */
+		case ASHMEM_PIN:
+		case ASHMEM_UNPIN:
+			ret = ASHMEM_NOT_PURGED;
+			break;
+		case ASHMEM_GET_PIN_STATUS:
+			ret = ASHMEM_IS_PINNED;
+			break;
+		}
+	}
 
 	return ret;
 }
Signature · 1 line
1.7.9.5
#1 lore raw

drivers/staging/android/ashmem.c static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd, · line 620

Quoted 7 lines · Joonsoo Kim
drivers/staging/android/ashmem.c +2 -1
@@ -614,21 +616,35 @@ static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd,
 	pgstart = pin.offset / PAGE_SIZE;
 	pgend = pgstart + (pin.len / PAGE_SIZE) - 1;
 
-	mutex_lock(&ashmem_mutex);
+	if (asma->shared_mapping) {
+		mutex_lock(&ashmem_mutex);

Wouldn't we need to hold the mutex while we check the ->shared_mapping?

regards,
dan carpenter

#2 lore raw

Hello, Dan.

2012/12/2 Dan Carpenter <[email protected]>:

Quoted 11 lines · Joonsoo Kim

drivers/staging/android/ashmem.c static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd, · line 620

Quoted 7 lines
drivers/staging/android/ashmem.c +2 -1
@@ -614,21 +616,35 @@ static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd,
      pgstart = pin.offset / PAGE_SIZE;
      pgend = pgstart + (pin.len / PAGE_SIZE) - 1;
 
-     mutex_lock(&ashmem_mutex);
+     if (asma->shared_mapping) {
+             mutex_lock(&ashmem_mutex);

Wouldn't we need to hold the mutex while we check the ->shared_mapping?

I doesn't fully understand ashmem's lock semantic.
Code for retrieving some value of asma instance doesn't hold the mutex, now. For example, in ashmem_ioctl(), asma->size, asma->prot_mask.
And in ashmem_pin_unpin(), there is asma->file, asma->size which is retrieved without the mutex.
According to this semantic, the mutex doesn't need for checking asma->shared_mapping.

Thanks for review!

#3 lore raw
Quoted 15 lines · JoonSoo Kim

Hello, Dan.

2012/12/2 Dan Carpenter <[email protected]>:

Quoted 11 lines · Joonsoo Kim

drivers/staging/android/ashmem.c static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd, · line 620

Quoted 7 lines
drivers/staging/android/ashmem.c +2 -1
@@ -614,21 +616,35 @@ static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd,
      pgstart = pin.offset / PAGE_SIZE;
      pgend = pgstart + (pin.len / PAGE_SIZE) - 1;
 
-     mutex_lock(&ashmem_mutex);
+     if (asma->shared_mapping) {
+             mutex_lock(&ashmem_mutex);

Wouldn't we need to hold the mutex while we check the ->shared_mapping?

Quoted 7 lines

I doesn't fully understand ashmem's lock semantic.
Code for retrieving some value of asma instance doesn't hold the mutex, now. For example, in ashmem_ioctl(), asma->size, asma->prot_mask.
And in ashmem_pin_unpin(), there is asma->file, asma->size which is retrieved without the mutex.
According to this semantic, the mutex doesn't need for checking asma->shared_mapping.

The ashmem_ioctl() is clearly racy. :P asma->size can be modified and read at the same time. It's not an example to follow.

regards,
dan carpenter

#4 lore raw

2012/12/3 Dan Carpenter <[email protected]>:

Quoted 24 lines · JoonSoo Kim
Quoted 15 lines

Hello, Dan.

2012/12/2 Dan Carpenter <[email protected]>:

Quoted 11 lines · Joonsoo Kim

drivers/staging/android/ashmem.c static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd, · line 620

Quoted 7 lines
drivers/staging/android/ashmem.c +2 -1
@@ -614,21 +616,35 @@ static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd,
      pgstart = pin.offset / PAGE_SIZE;
      pgend = pgstart + (pin.len / PAGE_SIZE) - 1;
 
-     mutex_lock(&ashmem_mutex);
+     if (asma->shared_mapping) {
+             mutex_lock(&ashmem_mutex);

Wouldn't we need to hold the mutex while we check the ->shared_mapping?

Quoted 7 lines

I doesn't fully understand ashmem's lock semantic.
Code for retrieving some value of asma instance doesn't hold the mutex, now. For example, in ashmem_ioctl(), asma->size, asma->prot_mask.
And in ashmem_pin_unpin(), there is asma->file, asma->size which is retrieved without the mutex.
According to this semantic, the mutex doesn't need for checking asma->shared_mapping.

Quoted 2 lines

The ashmem_ioctl() is clearly racy. :P asma->size can be modified and read at the same time. It's not an example to follow.

Okay :)
I will insert a code for holding the mutex in next spin.

Thanks!