linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH] staging, android, ashmem: invalidate pin/unpin ioctl for private map
@ 2012-11-30 17:45 Joonsoo Kim
  2012-12-01 18:39 ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: Joonsoo Kim @ 2012-11-30 17:45 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: linux-kernel, devel, Android Kernel Team, Joonsoo Kim,
	John Stultz, Brian Swetland, Colin Cross,
	Arve Hjønnevåg, Dima Zavin, Robert Love

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 <john.stultz@linaro.org>
CC: Brian Swetland <swetland@google.com>
CC: Colin Cross <ccross@android.com>
CC: Arve Hjønnevåg <arve@android.com>
CC: Dima Zavin <dima@android.com>
CC: Robert Love <rlove@google.com>
Signed-off-by: Joonsoo Kim <js1304@gmail.com>

diff --git a/drivers/staging/android/ashmem.c b/drivers/staging/android/ashmem.c
index 634b9ae..2fde9df 100644
--- a/drivers/staging/android/ashmem.c
+++ b/drivers/staging/android/ashmem.c
@@ -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;
 }
-- 
1.7.9.5


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

* Re: [RFC PATCH] staging, android, ashmem: invalidate pin/unpin ioctl for private map
  2012-11-30 17:45 [RFC PATCH] staging, android, ashmem: invalidate pin/unpin ioctl for private map Joonsoo Kim
@ 2012-12-01 18:39 ` Dan Carpenter
  2012-12-03  0:09   ` JoonSoo Kim
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2012-12-01 18:39 UTC (permalink / raw)
  To: Joonsoo Kim
  Cc: Greg Kroah-Hartman, devel, Robert Love, Brian Swetland,
	linux-kernel, Dima Zavin, Arve Hjønnevåg, John Stultz,
	Colin Cross, Android Kernel Team

On Sat, Dec 01, 2012 at 02:45:57AM +0900, Joonsoo Kim wrote:
> @@ -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


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

* Re: [RFC PATCH] staging, android, ashmem: invalidate pin/unpin ioctl for private map
  2012-12-01 18:39 ` Dan Carpenter
@ 2012-12-03  0:09   ` JoonSoo Kim
  2012-12-03  7:31     ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: JoonSoo Kim @ 2012-12-03  0:09 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Greg Kroah-Hartman, devel, Robert Love, Brian Swetland,
	linux-kernel, Dima Zavin, Arve Hjønnevåg, John Stultz,
	Colin Cross, Android Kernel Team

Hello, Dan.

2012/12/2 Dan Carpenter <dan.carpenter@oracle.com>:
> On Sat, Dec 01, 2012 at 02:45:57AM +0900, Joonsoo Kim wrote:
>> @@ -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!

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

* Re: [RFC PATCH] staging, android, ashmem: invalidate pin/unpin ioctl for private map
  2012-12-03  0:09   ` JoonSoo Kim
@ 2012-12-03  7:31     ` Dan Carpenter
  2012-12-03 14:52       ` JoonSoo Kim
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2012-12-03  7:31 UTC (permalink / raw)
  To: JoonSoo Kim
  Cc: devel, Robert Love, Greg Kroah-Hartman, Colin Cross,
	linux-kernel, Dima Zavin, Arve Hjønnevåg, John Stultz,
	Brian Swetland, Android Kernel Team

On Mon, Dec 03, 2012 at 09:09:59AM +0900, JoonSoo Kim wrote:
> Hello, Dan.
> 
> 2012/12/2 Dan Carpenter <dan.carpenter@oracle.com>:
> > On Sat, Dec 01, 2012 at 02:45:57AM +0900, Joonsoo Kim wrote:
> >> @@ -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.

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


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

* Re: [RFC PATCH] staging, android, ashmem: invalidate pin/unpin ioctl for private map
  2012-12-03  7:31     ` Dan Carpenter
@ 2012-12-03 14:52       ` JoonSoo Kim
  0 siblings, 0 replies; 5+ messages in thread
From: JoonSoo Kim @ 2012-12-03 14:52 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: devel, Robert Love, Greg Kroah-Hartman, Colin Cross,
	linux-kernel, Dima Zavin, Arve Hjønnevåg, John Stultz,
	Brian Swetland, Android Kernel Team

2012/12/3 Dan Carpenter <dan.carpenter@oracle.com>:
> On Mon, Dec 03, 2012 at 09:09:59AM +0900, JoonSoo Kim wrote:
>> Hello, Dan.
>>
>> 2012/12/2 Dan Carpenter <dan.carpenter@oracle.com>:
>> > On Sat, Dec 01, 2012 at 02:45:57AM +0900, Joonsoo Kim wrote:
>> >> @@ -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.
>
> 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!

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

end of thread, other threads:[~2012-12-03 14:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-30 17:45 [RFC PATCH] staging, android, ashmem: invalidate pin/unpin ioctl for private map Joonsoo Kim
2012-12-01 18:39 ` Dan Carpenter
2012-12-03  0:09   ` JoonSoo Kim
2012-12-03  7:31     ` Dan Carpenter
2012-12-03 14:52       ` JoonSoo Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).