Replace list into lock-less list of ion page pool.
Measure how mutex lock contention latency on android.
1. the test is done under android 7.0
2. startup many applications circularly
3. find sample in trace log as below
cameraserver-625 [004] ...1 1891.952958: mutex_lock_enter: id=0
Binder:384_2-417 [005] ...1 1891.952958: mutex_lock_enter: id=0
Binder:384_2-417 [005] ...1 1891.952966: mutex_lock_enter: id=1
Binder:384_2-417 [005] ...1 1891.952970: mutex_lock_enter: id=0
Binder:384_2-417 [005] ...1 1891.952971: mutex_lock_enter: id=1
Binder:384_2-417 [005] ...1 1891.952982: mutex_lock_enter: id=0
Binder:384_2-417 [005] ...1 1891.952983: mutex_lock_enter: id=1
Binder:384_2-417 [005] ...1 1891.952989: mutex_lock_enter: id=0
Binder:384_2-417 [005] ...1 1891.952989: mutex_lock_enter: id=1
Binder:384_2-417 [005] ...1 1891.952995: mutex_lock_enter: id=0
cameraserver-625 [004] ...1 1891.952995: mutex_lock_enter: id=1
- id 0 is try to lock, id 1 is locked
Figure out how many latency reduction by this patch as below.
The test is startup 60 applications circularly (repeat 10cycles)
- lock contention count : 3717 -> 93
Signed-off-by: Bongkyu Kim <[email protected]>
Signed-off-by: Junil Lee <[email protected]>
---
drivers/staging/android/ion/ion_page_pool.c | 52 ++++++++++++++-------------
drivers/staging/android/ion/ion_priv.h | 8 ++---
drivers/staging/android/ion/ion_system_heap.c | 16 ++++-----
3 files changed, 40 insertions(+), 36 deletions(-)
diff --git a/drivers/staging/android/ion/ion_page_pool.c b/drivers/staging/android/ion/ion_page_pool.c
index aea89c1..1beb2c8 100644
--- a/drivers/staging/android/ion/ion_page_pool.c
+++ b/drivers/staging/android/ion/ion_page_pool.c
@@ -22,6 +22,7 @@
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/swap.h>
+#include <linux/llist.h>
#include "ion_priv.h"
static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool)
@@ -44,33 +45,36 @@ static void ion_page_pool_free_pages(struct ion_page_pool *pool,
static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
{
- mutex_lock(&pool->mutex);
if (PageHighMem(page)) {
- list_add_tail(&page->lru, &pool->high_items);
- pool->high_count++;
+ llist_add((struct llist_node *)&page->lru, &pool->high_items);
+ atomic_inc(&pool->high_count);
} else {
- list_add_tail(&page->lru, &pool->low_items);
- pool->low_count++;
+ llist_add((struct llist_node *)&page->lru, &pool->low_items);
+ atomic_inc(&pool->low_count);
}
- mutex_unlock(&pool->mutex);
+
return 0;
}
static struct page *ion_page_pool_remove(struct ion_page_pool *pool, bool high)
{
- struct page *page;
+ struct page *page = NULL;
+ struct llist_node *node;
if (high) {
- BUG_ON(!pool->high_count);
- page = list_first_entry(&pool->high_items, struct page, lru);
- pool->high_count--;
+ BUG_ON(!atomic_read(&pool->high_count));
+ node = llist_del_first(&pool->high_items);
+ if (node)
+ node = llist_entry((struct list_head *)node, struct page, lru);
+ atomic_dec(&pool->high_count);
} else {
- BUG_ON(!pool->low_count);
- page = list_first_entry(&pool->low_items, struct page, lru);
- pool->low_count--;
+ BUG_ON(!atomic_read(&pool->low_count));
+ node = llist_del_first(&pool->low_items);
+ if (node)
+ node = llist_entry((struct list_head *)node, struct page, lru);
+ atomic_dec(&pool->low_count);
}
- list_del(&page->lru);
return page;
}
@@ -81,9 +85,9 @@ struct page *ion_page_pool_alloc(struct ion_page_pool *pool)
BUG_ON(!pool);
mutex_lock(&pool->mutex);
- if (pool->high_count)
+ if (atomic_read(&pool->high_count))
page = ion_page_pool_remove(pool, true);
- else if (pool->low_count)
+ else if (atomic_read(&pool->low_count))
page = ion_page_pool_remove(pool, false);
mutex_unlock(&pool->mutex);
@@ -106,10 +110,10 @@ void ion_page_pool_free(struct ion_page_pool *pool, struct page *page)
static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
{
- int count = pool->low_count;
+ int count = atomic_read(&pool->low_count);
if (high)
- count += pool->high_count;
+ count += atomic_read(&pool->high_count);
return count << pool->order;
}
@@ -132,9 +136,9 @@ int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
struct page *page;
mutex_lock(&pool->mutex);
- if (pool->low_count) {
+ if (atomic_read(&pool->low_count)) {
page = ion_page_pool_remove(pool, false);
- } else if (high && pool->high_count) {
+ } else if (high && atomic_read(&pool->high_count)) {
page = ion_page_pool_remove(pool, true);
} else {
mutex_unlock(&pool->mutex);
@@ -155,10 +159,10 @@ struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order,
if (!pool)
return NULL;
- pool->high_count = 0;
- pool->low_count = 0;
- INIT_LIST_HEAD(&pool->low_items);
- INIT_LIST_HEAD(&pool->high_items);
+ atomic_set(&pool->high_count, 0);
+ atomic_set(&pool->low_count, 0);
+ init_llist_head(&pool->low_items);
+ init_llist_head(&pool->high_items);
pool->gfp_mask = gfp_mask | __GFP_COMP;
pool->order = order;
mutex_init(&pool->mutex);
diff --git a/drivers/staging/android/ion/ion_priv.h b/drivers/staging/android/ion/ion_priv.h
index 5b3059c..d4d5704 100644
--- a/drivers/staging/android/ion/ion_priv.h
+++ b/drivers/staging/android/ion/ion_priv.h
@@ -414,11 +414,11 @@ void ion_cma_heap_destroy(struct ion_heap *heap);
* on many systems
*/
struct ion_page_pool {
- int high_count;
- int low_count;
+ atomic_t high_count;
+ atomic_t low_count;
bool cached;
- struct list_head high_items;
- struct list_head low_items;
+ struct llist_head high_items;
+ struct llist_head low_items;
struct mutex mutex;
gfp_t gfp_mask;
unsigned int order;
diff --git a/drivers/staging/android/ion/ion_system_heap.c b/drivers/staging/android/ion/ion_system_heap.c
index 3ebbb75..8ee8d98 100644
--- a/drivers/staging/android/ion/ion_system_heap.c
+++ b/drivers/staging/android/ion/ion_system_heap.c
@@ -274,22 +274,22 @@ static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s,
pool = sys_heap->uncached_pools[i];
seq_printf(s, "%d order %u highmem pages uncached %lu total\n",
- pool->high_count, pool->order,
- (PAGE_SIZE << pool->order) * pool->high_count);
+ atomic_read(&pool->high_count), pool->order,
+ (PAGE_SIZE << pool->order) * atomic_read(&pool->high_count));
seq_printf(s, "%d order %u lowmem pages uncached %lu total\n",
- pool->low_count, pool->order,
- (PAGE_SIZE << pool->order) * pool->low_count);
+ atomic_read(&pool->low_count), pool->order,
+ (PAGE_SIZE << pool->order) * atomic_read(&pool->low_count));
}
for (i = 0; i < NUM_ORDERS; i++) {
pool = sys_heap->cached_pools[i];
seq_printf(s, "%d order %u highmem pages cached %lu total\n",
- pool->high_count, pool->order,
- (PAGE_SIZE << pool->order) * pool->high_count);
+ atomic_read(&pool->high_count), pool->order,
+ (PAGE_SIZE << pool->order) * atomic_read(&pool->high_count));
seq_printf(s, "%d order %u lowmem pages cached %lu total\n",
- pool->low_count, pool->order,
- (PAGE_SIZE << pool->order) * pool->low_count);
+ atomic_read(&pool->low_count), pool->order,
+ (PAGE_SIZE << pool->order) * atomic_read(&pool->low_count));
}
return 0;
}
--
2.6.2
On 03/14/2017 12:51 AM, Junil Lee wrote:
> Replace list into lock-less list of ion page pool.
>
> Measure how mutex lock contention latency on android.
>
> 1. the test is done under android 7.0
> 2. startup many applications circularly
> 3. find sample in trace log as below
>
> cameraserver-625 [004] ...1 1891.952958: mutex_lock_enter: id=0
> Binder:384_2-417 [005] ...1 1891.952958: mutex_lock_enter: id=0
> Binder:384_2-417 [005] ...1 1891.952966: mutex_lock_enter: id=1
> Binder:384_2-417 [005] ...1 1891.952970: mutex_lock_enter: id=0
> Binder:384_2-417 [005] ...1 1891.952971: mutex_lock_enter: id=1
> Binder:384_2-417 [005] ...1 1891.952982: mutex_lock_enter: id=0
> Binder:384_2-417 [005] ...1 1891.952983: mutex_lock_enter: id=1
> Binder:384_2-417 [005] ...1 1891.952989: mutex_lock_enter: id=0
> Binder:384_2-417 [005] ...1 1891.952989: mutex_lock_enter: id=1
> Binder:384_2-417 [005] ...1 1891.952995: mutex_lock_enter: id=0
> cameraserver-625 [004] ...1 1891.952995: mutex_lock_enter: id=1
>
> - id 0 is try to lock, id 1 is locked
>
> Figure out how many latency reduction by this patch as below.
>
> The test is startup 60 applications circularly (repeat 10cycles)
> - lock contention count : 3717 -> 93
>
We really need to finish up the work to move Ion out of staging before
looking at performance improvements so please help with that discussion.
Once that is finished up, we can look at performance improvements again.
That said, this is removing the lock from the free path. Is the
contention happening on the free path? Is the system heap deferring
frees? Does this have a notable affect on anything besides the count
of contention? None of this is necessarily a deal breaker but I'd
like a few more details.
Thanks,
Laura
> Signed-off-by: Bongkyu Kim <[email protected]>
> Signed-off-by: Junil Lee <[email protected]>
> ---
> drivers/staging/android/ion/ion_page_pool.c | 52 ++++++++++++++-------------
> drivers/staging/android/ion/ion_priv.h | 8 ++---
> drivers/staging/android/ion/ion_system_heap.c | 16 ++++-----
> 3 files changed, 40 insertions(+), 36 deletions(-)
>
> diff --git a/drivers/staging/android/ion/ion_page_pool.c b/drivers/staging/android/ion/ion_page_pool.c
> index aea89c1..1beb2c8 100644
> --- a/drivers/staging/android/ion/ion_page_pool.c
> +++ b/drivers/staging/android/ion/ion_page_pool.c
> @@ -22,6 +22,7 @@
> #include <linux/init.h>
> #include <linux/slab.h>
> #include <linux/swap.h>
> +#include <linux/llist.h>
> #include "ion_priv.h"
>
> static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool)
> @@ -44,33 +45,36 @@ static void ion_page_pool_free_pages(struct ion_page_pool *pool,
>
> static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
> {
> - mutex_lock(&pool->mutex);
> if (PageHighMem(page)) {
> - list_add_tail(&page->lru, &pool->high_items);
> - pool->high_count++;
> + llist_add((struct llist_node *)&page->lru, &pool->high_items);
> + atomic_inc(&pool->high_count);
> } else {
> - list_add_tail(&page->lru, &pool->low_items);
> - pool->low_count++;
> + llist_add((struct llist_node *)&page->lru, &pool->low_items);
> + atomic_inc(&pool->low_count);
> }
> - mutex_unlock(&pool->mutex);
> +
> return 0;
> }
>
> static struct page *ion_page_pool_remove(struct ion_page_pool *pool, bool high)
> {
> - struct page *page;
> + struct page *page = NULL;
> + struct llist_node *node;
>
> if (high) {
> - BUG_ON(!pool->high_count);
> - page = list_first_entry(&pool->high_items, struct page, lru);
> - pool->high_count--;
> + BUG_ON(!atomic_read(&pool->high_count));
> + node = llist_del_first(&pool->high_items);
> + if (node)
> + node = llist_entry((struct list_head *)node, struct page, lru);
> + atomic_dec(&pool->high_count);
> } else {
> - BUG_ON(!pool->low_count);
> - page = list_first_entry(&pool->low_items, struct page, lru);
> - pool->low_count--;
> + BUG_ON(!atomic_read(&pool->low_count));
> + node = llist_del_first(&pool->low_items);
> + if (node)
> + node = llist_entry((struct list_head *)node, struct page, lru);
> + atomic_dec(&pool->low_count);
> }
>
> - list_del(&page->lru);
> return page;
> }
>
> @@ -81,9 +85,9 @@ struct page *ion_page_pool_alloc(struct ion_page_pool *pool)
> BUG_ON(!pool);
>
> mutex_lock(&pool->mutex);
> - if (pool->high_count)
> + if (atomic_read(&pool->high_count))
> page = ion_page_pool_remove(pool, true);
> - else if (pool->low_count)
> + else if (atomic_read(&pool->low_count))
> page = ion_page_pool_remove(pool, false);
> mutex_unlock(&pool->mutex);
>
> @@ -106,10 +110,10 @@ void ion_page_pool_free(struct ion_page_pool *pool, struct page *page)
>
> static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
> {
> - int count = pool->low_count;
> + int count = atomic_read(&pool->low_count);
>
> if (high)
> - count += pool->high_count;
> + count += atomic_read(&pool->high_count);
>
> return count << pool->order;
> }
> @@ -132,9 +136,9 @@ int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
> struct page *page;
>
> mutex_lock(&pool->mutex);
> - if (pool->low_count) {
> + if (atomic_read(&pool->low_count)) {
> page = ion_page_pool_remove(pool, false);
> - } else if (high && pool->high_count) {
> + } else if (high && atomic_read(&pool->high_count)) {
> page = ion_page_pool_remove(pool, true);
> } else {
> mutex_unlock(&pool->mutex);
> @@ -155,10 +159,10 @@ struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order,
>
> if (!pool)
> return NULL;
> - pool->high_count = 0;
> - pool->low_count = 0;
> - INIT_LIST_HEAD(&pool->low_items);
> - INIT_LIST_HEAD(&pool->high_items);
> + atomic_set(&pool->high_count, 0);
> + atomic_set(&pool->low_count, 0);
> + init_llist_head(&pool->low_items);
> + init_llist_head(&pool->high_items);
> pool->gfp_mask = gfp_mask | __GFP_COMP;
> pool->order = order;
> mutex_init(&pool->mutex);
> diff --git a/drivers/staging/android/ion/ion_priv.h b/drivers/staging/android/ion/ion_priv.h
> index 5b3059c..d4d5704 100644
> --- a/drivers/staging/android/ion/ion_priv.h
> +++ b/drivers/staging/android/ion/ion_priv.h
> @@ -414,11 +414,11 @@ void ion_cma_heap_destroy(struct ion_heap *heap);
> * on many systems
> */
> struct ion_page_pool {
> - int high_count;
> - int low_count;
> + atomic_t high_count;
> + atomic_t low_count;
> bool cached;
> - struct list_head high_items;
> - struct list_head low_items;
> + struct llist_head high_items;
> + struct llist_head low_items;
> struct mutex mutex;
> gfp_t gfp_mask;
> unsigned int order;
> diff --git a/drivers/staging/android/ion/ion_system_heap.c b/drivers/staging/android/ion/ion_system_heap.c
> index 3ebbb75..8ee8d98 100644
> --- a/drivers/staging/android/ion/ion_system_heap.c
> +++ b/drivers/staging/android/ion/ion_system_heap.c
> @@ -274,22 +274,22 @@ static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s,
> pool = sys_heap->uncached_pools[i];
>
> seq_printf(s, "%d order %u highmem pages uncached %lu total\n",
> - pool->high_count, pool->order,
> - (PAGE_SIZE << pool->order) * pool->high_count);
> + atomic_read(&pool->high_count), pool->order,
> + (PAGE_SIZE << pool->order) * atomic_read(&pool->high_count));
> seq_printf(s, "%d order %u lowmem pages uncached %lu total\n",
> - pool->low_count, pool->order,
> - (PAGE_SIZE << pool->order) * pool->low_count);
> + atomic_read(&pool->low_count), pool->order,
> + (PAGE_SIZE << pool->order) * atomic_read(&pool->low_count));
> }
>
> for (i = 0; i < NUM_ORDERS; i++) {
> pool = sys_heap->cached_pools[i];
>
> seq_printf(s, "%d order %u highmem pages cached %lu total\n",
> - pool->high_count, pool->order,
> - (PAGE_SIZE << pool->order) * pool->high_count);
> + atomic_read(&pool->high_count), pool->order,
> + (PAGE_SIZE << pool->order) * atomic_read(&pool->high_count));
> seq_printf(s, "%d order %u lowmem pages cached %lu total\n",
> - pool->low_count, pool->order,
> - (PAGE_SIZE << pool->order) * pool->low_count);
> + atomic_read(&pool->low_count), pool->order,
> + (PAGE_SIZE << pool->order) * atomic_read(&pool->low_count));
> }
> return 0;
> }
>
Hi Junil,
[auto build test ERROR on staging/staging-testing]
[also build test ERROR on v4.11-rc2 next-20170310]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Junil-Lee/staging-android-ion-reduce-lock-contention-latency/20170317-021346
config: x86_64-randconfig-x004-201711 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64
All errors (new ones prefixed by >>):
drivers/staging/android/ion/ion_page_pool.c: In function 'ion_page_pool_remove':
>> drivers/staging/android/ion/ion_page_pool.c:68:9: error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
node = llist_entry((struct list_head *)node, struct page, lru);
^
drivers/staging/android/ion/ion_page_pool.c:74:9: error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
node = llist_entry((struct list_head *)node, struct page, lru);
^
cc1: some warnings being treated as errors
vim +68 drivers/staging/android/ion/ion_page_pool.c
62 struct llist_node *node;
63
64 if (high) {
65 BUG_ON(!atomic_read(&pool->high_count));
66 node = llist_del_first(&pool->high_items);
67 if (node)
> 68 node = llist_entry((struct list_head *)node, struct page, lru);
69 atomic_dec(&pool->high_count);
70 } else {
71 BUG_ON(!atomic_read(&pool->low_count));
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Hi Junil,
[auto build test WARNING on staging/staging-testing]
[also build test WARNING on v4.11-rc2 next-20170310]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Junil-Lee/staging-android-ion-reduce-lock-contention-latency/20170317-021346
config: i386-randconfig-i0-201711 (attached as .config)
compiler: gcc-4.8 (Debian 4.8.4-1) 4.8.4
reproduce:
# save the attached .config to linux build tree
make ARCH=i386
All warnings (new ones prefixed by >>):
drivers/staging/android/ion/ion_page_pool.c: In function 'ion_page_pool_remove':
>> drivers/staging/android/ion/ion_page_pool.c:68:9: warning: assignment from incompatible pointer type [enabled by default]
node = llist_entry((struct list_head *)node, struct page, lru);
^
drivers/staging/android/ion/ion_page_pool.c:74:9: warning: assignment from incompatible pointer type [enabled by default]
node = llist_entry((struct list_head *)node, struct page, lru);
^
vim +68 drivers/staging/android/ion/ion_page_pool.c
52 llist_add((struct llist_node *)&page->lru, &pool->low_items);
53 atomic_inc(&pool->low_count);
54 }
55
56 return 0;
57 }
58
59 static struct page *ion_page_pool_remove(struct ion_page_pool *pool, bool high)
60 {
61 struct page *page = NULL;
62 struct llist_node *node;
63
64 if (high) {
65 BUG_ON(!atomic_read(&pool->high_count));
66 node = llist_del_first(&pool->high_items);
67 if (node)
> 68 node = llist_entry((struct list_head *)node, struct page, lru);
69 atomic_dec(&pool->high_count);
70 } else {
71 BUG_ON(!atomic_read(&pool->low_count));
72 node = llist_del_first(&pool->low_items);
73 if (node)
74 node = llist_entry((struct list_head *)node, struct page, lru);
75 atomic_dec(&pool->low_count);
76 }
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation