mm/slub: Detach node lock from counting free objects
Subsystems (2)
PATCH 1/1 mm/slub: Detach node lock from counting free objects
The lock, protecting the node partial list, is taken when couting the free objects resident in that list. It introduces locking contention when the page(s) is moved between CPU and node partial lists in allocation path on another CPU. So reading "/proc/slabinfo" can possibily block the slab allocation on another CPU for a while, 200ms in extreme cases. If the slab object is to carry network packet, targeting the far-end disk array, it causes block IO jitter issue.
This fixes the block IO jitter issue by caching the total inuse objects in the node in advance. The value is retrieved without taking the node partial list lock on reading "/proc/slabinfo".
--- mm/slab.h | 1 + mm/slub.c | 42 +++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 17 deletions(-)
mm/slab.h +1 -0
@@ -619,6 +619,7 @@ struct kmem_cache_node {#ifdef CONFIG_SLUB_DEBUG atomic_long_t nr_slabs;atomic_long_t total_objects; +atomic_long_t total_inuse;struct list_head full;#endif #endif
mm/slub.c +25 -17
130 lines not on this page · raw message
Signature · 1 line
2.23.0
Hi,
I would greatly appreciate it if you kindly give me some feedback on this patch.
--
Best wishes,
Wen
Quoted 183 lines · Wen Yang
The lock, protecting the node partial list, is taken when couting the free objects resident in that list. It introduces locking contention when the page(s) is moved between CPU and node partial lists in allocation path on another CPU. So reading "
/proc/slabinfo" can possibily block the slab allocation on another CPU for a while, 200ms in extreme cases. If the slab object is to carry network packet, targeting the far-end disk array, it causes block IO jitter issue.This fixes the block IO jitter issue by caching the total inuse objects in the node in advance. The value is retrieved without taking the node partial list lock on reading "
/proc/slabinfo".Signed-off-by Wen Yang <[email protected]>Cc Christoph Lameter <[email protected]>Cc Pekka Enberg <[email protected]>Cc David Rientjes <[email protected]>Cc Joonsoo Kim <[email protected]>Cc Andrew Morton <[email protected]>Cc Xunlei Pang <[email protected]>--- mm/slab.h | 1 + mm/slub.c | 42 +++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 17 deletions(-)mm/slab.h +1
-0@@ -619,6 +619,7 @@ struct kmem_cache_node {#ifdef CONFIG_SLUB_DEBUG atomic_long_t nr_slabs;atomic_long_t total_objects; +atomic_long_t total_inuse;struct list_head full;#endif #endif mm/slub.c +25
-17@@ -1060,7 +1060,8 @@ static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)return atomic_long_read (&n->nr_slabs); } -static inline void inc_slabs_node (struct kmem_cache *s,int node,int objects) +static inline void inc_slabs_node (struct kmem_cache *s,int node,int objects, +int inuse) {struct kmem_cache_node *n =get_node (s, node);@@ -1073,14 +1074,17 @@ static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)if (likely (n)) {atomic_long_inc (&n->nr_slabs);atomic_long_add (objects, &n->total_objects); +atomic_long_add (inuse, &n->total_inuse); } } -static inline void dec_slabs_node (struct kmem_cache *s,int node,int objects) +static inline void dec_slabs_node (struct kmem_cache *s,int node,int objects, +int inuse) {struct kmem_cache_node *n =get_node (s, node);atomic_long_dec (&n->nr_slabs);atomic_long_sub (objects, &n->total_objects); +atomic_long_sub (inuse, &n->total_inuse); }/* Object debug checks for alloc/free paths */ @@ -1395,9 +1399,11 @@ static inline unsigned long slabs_node(struct kmem_cache *s, int node)static inline unsigned long node_nr_slabs (struct kmem_cache_node *n) {return 0 ; }static inline void inc_slabs_node (struct kmem_cache *s,int node, -int objects) {} +int objects, +int inuse) {}static inline void dec_slabs_node (struct kmem_cache *s,int node, -int objects) {} +int objects, +int inuse) {}#endif /* CONFIG_SLUB_DEBUG */ @@ -1708,7 +1714,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)if (!page)return NULL ; -inc_slabs_node (s,page_to_nid (page), page->objects); +inc_slabs_node (s,page_to_nid (page), page->objects, page->inuse);return page; }@@ -1768,7 +1774,9 @@ static void free_slab(struct kmem_cache *s, struct page *page)static void discard_slab (struct kmem_cache *s,struct page *page) { -dec_slabs_node (s,page_to_nid (page), page->objects); +int inuse = page->objects; + +dec_slabs_node (s,page_to_nid (page), page->objects, inuse);free_slab (s, page); }@@ -2396,9 +2404,9 @@ static inline int node_match(struct page *page, int node) }#ifdef CONFIG_SLUB_DEBUG -static int count_free (struct page *page) +static inline unsigned long node_nr_inuse (struct kmem_cache_node *n) { -return page->objects - page->inuse; +return atomic_long_read (&n->total_inuse); }static inline unsigned long node_nr_objs (struct kmem_cache_node *n)@@ -2448,14 +2456,14 @@ slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)for_each_kmem_cache_node (s, node, n) {unsigned long nr_slabs;unsigned long nr_objs; -unsigned long nr_free; +unsigned long nr_inuse; - nr_free =count_partial (n, count_free); nr_slabs =node_nr_slabs (n); nr_objs =node_nr_objs (n); + nr_inuse =node_nr_inuse (n);pr_warn (" node %d: slabs: %ld, objs: %ld, free: %ld\n" , - node, nr_slabs, nr_objs, nr_free); + node, nr_slabs, nr_objs, nr_objs - nr_inuse); }#endif }@@ -3348,6 +3356,7 @@ init_kmem_cache_node(struct kmem_cache_node *n)#ifdef CONFIG_SLUB_DEBUG atomic_long_set (&n->nr_slabs,0 );atomic_long_set (&n->total_objects,0 ); +atomic_long_set (&n->total_inuse,0 );INIT_LIST_HEAD (&n->full);#endif }@@ -3411,7 +3420,7 @@ static void early_kmem_cache_node_alloc(int node) page->frozen =0 ; kmem_cache_node->node[node] = n;init_kmem_cache_node (n); -inc_slabs_node (kmem_cache_node, node, page->objects); +inc_slabs_node (kmem_cache_node, node, page->objects, page->inuse);/* * No locks need to be taken here as it has just been @@ -4857,8 +4866,7 @@ static ssize_t show_slab_objects(struct kmem_cache *s,if (flags &SO_TOTAL ) x =atomic_long_read (&n->total_objects);else if (flags &SO_OBJECTS ) - x =atomic_long_read (&n->total_objects) - -count_partial (n, count_free); + x =atomic_long_read (&n->total_inuse);else x =atomic_long_read (&n->nr_slabs); total += x;@@ -5900,17 +5908,17 @@ void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo) {unsigned long nr_slabs =0 ;unsigned long nr_objs =0 ; -unsigned long nr_free =0 ; +unsigned long nr_inuse =0 ;int node;struct kmem_cache_node *n;for_each_kmem_cache_node (s, node, n) { nr_slabs +=node_nr_slabs (n); nr_objs +=node_nr_objs (n); - nr_free +=count_partial (n, count_free); + nr_inuse +=node_nr_inuse (n); } - sinfo->active_objs = nr_objs - nr_free; + sinfo->active_objs = nr_inuse; sinfo->num_objs = nr_objs; sinfo->active_slabs = nr_slabs; sinfo->num_slabs = nr_slabs;
Quoted 2 lines · Wen Yang
I would greatly appreciate it if you kindly give me some feedback on this patch.
I think the measure is too severe given its use and the general impact on code.
Maybe avoid taking the lock or reducing the time a lock is taken when reading /proc/slabinfo is the best approach?
Also you could cache the value in the userspace application? Why is this value read continually?
Andrew Morton
maintainerQuoted 6 lines · On Sat, 8 Feb 2020 21:41:49 +0000 (UTC) Christopher Lameter <[email protected]>…
Quoted 2 lines · Wen Yang
I would greatly appreciate it if you kindly give me some feedback on this patch.
I think the measure is too severe given its use and the general impact on code.
Severe in what way? It's really a quite simple change, although quite a few edits were needed.
Quoted 5 lines
Maybe avoid taking the lock or reducing the time a lock is taken when reading
/proc/slabinfois the best approach?Also you could cache the value in the userspace application? Why is this value read continually?
: reading "/proc/slabinfo" can possibly block the slab allocation on : another CPU for a while, 200ms in extreme cases
That was bad of us. It would be good to stop doing this.
Quoted 4 lines · Andrew Morton
: reading "
/proc/slabinfo" can possibly block the slab allocation on : another CPU for a while, 200ms in extreme casesThat was bad of us. It would be good to stop doing this.
The count is not needed for any operations. Just for the slabinfo output. The value has no operational value for the allocator itself. So why use extra logic to track it in potentially performance critical paths?
One could estimate the number of objects from the number of allocated slabs instead?
Andrew Morton
maintainerQuoted 15 lines · On Sat, 1 Feb 2020 11:15:02 +0800 Wen Yang <[email protected]> wrote:
The lock, protecting the node partial list, is taken when couting the free objects resident in that list. It introduces locking contention when the page(s) is moved between CPU and node partial lists in allocation path on another CPU. So reading "
/proc/slabinfo" can possibily block the slab allocation on another CPU for a while, 200ms in extreme cases. If the slab object is to carry network packet, targeting the far-end disk array, it causes block IO jitter issue.This fixes the block IO jitter issue by caching the total inuse objects in the node in advance. The value is retrieved without taking the node partial list lock on reading "
/proc/slabinfo"....
mm/slub.c static void free_slab(struct kmem_cache *s, struct page *page) · line 1779
Quoted 7 lines
mm/slub.c +3
-1@@ -1768,7 +1774,9 @@ static void free_slab(struct kmem_cache *s, struct page *page)static void discard_slab (struct kmem_cache *s,struct page *page) { -dec_slabs_node (s,page_to_nid (page), page->objects); +int inuse = page->objects; + +dec_slabs_node (s,page_to_nid (page), page->objects, inuse);
Is this right? dec_slabs_node(..., page->objects, page->objects)?
If no, we could simply pass the page* to inc_slabs_node/dec_slabs_node and save a function argument.
If yes then why?
Quoted 3 lines
mm/slub.c +0
-0free_slab (s, page); }
Quoted 27 lines · Andrew Morton
Quoted 15 lines · On Sat, 1 Feb 2020 11:15:02 +0800 Wen Yang <[email protected]> wrote:
The lock, protecting the node partial list, is taken when couting the free objects resident in that list. It introduces locking contention when the page(s) is moved between CPU and node partial lists in allocation path on another CPU. So reading "
/proc/slabinfo" can possibily block the slab allocation on another CPU for a while, 200ms in extreme cases. If the slab object is to carry network packet, targeting the far-end disk array, it causes block IO jitter issue.This fixes the block IO jitter issue by caching the total inuse objects in the node in advance. The value is retrieved without taking the node partial list lock on reading "
/proc/slabinfo"....
Review static void free_slab(struct kmem_cache *s, struct page *page) · line 1779
Quoted 7 lines
(quoted diff) +3
-1@@ -1768,7 +1774,9 @@ static void free_slab(struct kmem_cache *s, struct page *page) static void discard_slab(struct kmem_cache *s, struct page *page) { - dec_slabs_node(s, page_to_nid(page), page->objects); + int inuse = page->objects; + + dec_slabs_node(s, page_to_nid(page), page->objects, inuse);Is this right?
dec_slabs_node(..., page->objects, page->objects)?
Quoted 5 lines
If no, we could simply pass the page* to inc_slabs_node/dec_slabs_node and save a function argument.
If yes then why?
Thanks for your comments.
We are happy to improve this patch based on your suggestions.
When the user reads /proc/slabinfo, in order to obtain the active_objs information, the kernel traverses all slabs and executes the following code snippet:
static unsigned long count_partial(struct kmem_cache_node *n,
int (*get_count)(struct page *))
{
unsigned long flags;
unsigned long x = 0;
struct page *page;
spin_lock_irqsave(&n->list_lock, flags);
list_for_each_entry(page, &n->partial, slab_list)
x += get_count(page);
spin_unlock_irqrestore(&n->list_lock, flags);
return x;
}It may cause performance issues.
Christoph suggested "you could cache the value in the userspace application? Why is this value read continually?", But reading the /proc/slabinfo is initiated by the user program. As a cloud provider, we cannot control user behavior. If a user program inadvertently executes cat /proc/slabinfo, it may affect other user programs.
As Christoph said: "The count is not needed for any operations. Just for the slabinfo output. The value has no operational value for the allocator itself. So why use extra logic to track it in potentially performance critical paths?"
In this way, could we show the approximate value of active_objs in the /proc/slabinfo?
Based on the following information:
In the discard_slab() function, page->inuse is equal to page->total_objects;
In the allocate_slab() function, page->inuse is also equal to page->total_objects (with one exception: for kmem_cache_node, page-> inuse equals 1); page->inuse will only change continuously when the obj is constantly allocated or released. (This should be the performance critical path emphasized by Christoph)
When users query the global slabinfo information, we may use total_objects to approximate active_objs.
In this way, the modified patch is as follows:
mm/slub.c +1 -3
@@ -5900,17 +5900,15 @@ void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo) {unsigned long nr_slabs =0 ;unsigned long nr_objs =0 ; -unsigned long nr_free =0 ;int node;struct kmem_cache_node *n;for_each_kmem_cache_node (s, node, n) { nr_slabs +=node_nr_slabs (n); nr_objs +=node_nr_objs (n); - nr_free +=count_partial (n, count_free); } - sinfo->active_objs = nr_objs - nr_free; + sinfo->active_objs = nr_objs; sinfo->num_objs = nr_objs; sinfo->active_slabs = nr_slabs; sinfo->num_slabs = nr_slabs;
In addition, when the user really needs to view the precise active_obj value of a slab, he can query this single slab info through an interface similar to the following, which avoids traversing all the slabs.
# cat /sys/kernel/slab/kmalloc-512/total_objects 1472 N0=1472 # cat /sys/kernel/slab/kmalloc-512/objects 1311 N0=1311
or
# cat /sys/kernel/slab/kmalloc-8k/total_objects 60 N0=60 # cat /sys/kernel/slab/kmalloc-8k/objects 60 N0=60
Best wishes,
Wen
Quoted 3 lines
Quoted 3 lines
(quoted diff) +0
-0free_slab(s, page); }
Quoted 81 lines · Wen Yang
Quoted 26 lines · Andrew Morton
Quoted 15 lines · On Sat, 1 Feb 2020 11:15:02 +0800 Wen Yang <[email protected]> wrote:
The lock, protecting the node partial list, is taken when couting the free objects resident in that list. It introduces locking contention when the page(s) is moved between CPU and node partial lists in allocation path on another CPU. So reading "
/proc/slabinfo" can possibily block the slab allocation on another CPU for a while, 200ms in extreme cases. If the slab object is to carry network packet, targeting the far-end disk array, it causes block IO jitter issue.This fixes the block IO jitter issue by caching the total inuse objects in the node in advance. The value is retrieved without taking the node partial list lock on reading "
/proc/slabinfo"....
Review static void free_slab(struct kmem_cache *s, struct page *page) · line 1778
Quoted 6 lines
(quoted diff) +3
-1@@ -1768,7 +1774,9 @@ static void free_slab(struct kmem_cache *s, struct page *page) static void discard_slab(struct kmem_cache *s, struct page *page) { - dec_slabs_node(s, page_to_nid(page), page->objects); + int inuse = page->objects; + + dec_slabs_node(s, page_to_nid(page), page->objects, inuse);Is this right?
dec_slabs_node(..., page->objects, page->objects)?Quoted 5 lines
If no, we could simply pass the page* to inc_slabs_node/dec_slabs_node and save a function argument.
If yes then why?
Thanks for your comments.
We are happy to improve this patch based on your suggestions.When the user reads
/proc/slabinfo, in order to obtain theactive_objsinformation, the kernel traverses all slabs and executes the following code snippet:
static unsigned long count_partial(structkmem_cache_node*n,
int (*get_count)(struct page *)){ unsigned long flags; unsigned long x = 0; struct page *page; spin_lock_irqsave(&n->list_lock, flags); list_for_each_entry(page, &n->partial, slab_list) x += get_count(page); spin_unlock_irqrestore(&n->list_lock, flags); return x; }It may cause performance issues.
Christoph suggested "you could cache the value in the userspace application? Why is this value read continually?", But reading the
/proc/slabinfois initiated by the user program. As a cloud provider, we cannot control user behavior. If a user program inadvertently executes cat/proc/slabinfo, it may affect other user programs.As Christoph said: "The count is not needed for any operations. Just for the slabinfo output. The value has no operational value for the allocator itself. So why use extra logic to track it in potentially performance critical paths?"
In this way, could we show the approximate value of
active_objsin the/proc/slabinfo?Based on the following information:
In thediscard_slab()function,page->inuseis equal topage->total_objects; In theallocate_slab()function,page->inuseis also equal topage->total_objects(with one exception: forkmem_cache_node, page-> inuse equals 1);page->inusewill only change continuously when the obj is constantly allocated or released. (This should be the performance critical path emphasized by Christoph)
Quoted 2 lines
When users query the global slabinfo information, we may use
total_objectsto approximateactive_objs.
Well, from one point of view, it makes no sense, because the ratio between these two numbers is very meaningful: it's the slab utilization rate.
On the other side, with enabled per-cpu partial lists active_objs has nothing to do with the reality anyway, so I agree with you, calling count_partial() is almost useless.
That said, I wonder if the right thing to do is something like the patch below?
Thanks!
Roman
--
mm/slub.c +3 -1
@@ -2411,14 +2411,16 @@ static inline unsigned long node_nr_objs(struct kmem_cache_node *n)static unsigned long count_partial (struct kmem_cache_node *n,int (*get_count)(struct page *)) { -unsigned long flags;unsigned long x =0 ; +#ifdef CONFIG_SLUB_CPU_PARTIAL +unsigned long flags;struct page *page;spin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (page, &n->partial, slab_list) x +=get_count (page);spin_unlock_irqrestore (&n->list_lock, flags); +#endif return x; }#endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
Quoted 113 lines · Roman Gushchin
Quoted 81 lines · Wen Yang
Quoted 26 lines · Andrew Morton
Quoted 15 lines · On Sat, 1 Feb 2020 11:15:02 +0800 Wen Yang <[email protected]> wrote:
The lock, protecting the node partial list, is taken when couting the free objects resident in that list. It introduces locking contention when the page(s) is moved between CPU and node partial lists in allocation path on another CPU. So reading "
/proc/slabinfo" can possibily block the slab allocation on another CPU for a while, 200ms in extreme cases. If the slab object is to carry network packet, targeting the far-end disk array, it causes block IO jitter issue.This fixes the block IO jitter issue by caching the total inuse objects in the node in advance. The value is retrieved without taking the node partial list lock on reading "
/proc/slabinfo"....
Review static void free_slab(struct kmem_cache *s, struct page *page) · line 1778
Quoted 6 lines
(quoted diff) +3
-1@@ -1768,7 +1774,9 @@ static void free_slab(struct kmem_cache *s, struct page *page) static void discard_slab(struct kmem_cache *s, struct page *page) { - dec_slabs_node(s, page_to_nid(page), page->objects); + int inuse = page->objects; + + dec_slabs_node(s, page_to_nid(page), page->objects, inuse);Is this right?
dec_slabs_node(..., page->objects, page->objects)?Quoted 5 lines
If no, we could simply pass the page* to inc_slabs_node/dec_slabs_node and save a function argument.
If yes then why?
Thanks for your comments.
We are happy to improve this patch based on your suggestions.When the user reads
/proc/slabinfo, in order to obtain theactive_objsinformation, the kernel traverses all slabs and executes the following code snippet:
static unsigned long count_partial(structkmem_cache_node*n,
int (*get_count)(struct page *)){ unsigned long flags; unsigned long x = 0; struct page *page; spin_lock_irqsave(&n->list_lock, flags); list_for_each_entry(page, &n->partial, slab_list) x += get_count(page); spin_unlock_irqrestore(&n->list_lock, flags); return x; }It may cause performance issues.
Christoph suggested "you could cache the value in the userspace application? Why is this value read continually?", But reading the
/proc/slabinfois initiated by the user program. As a cloud provider, we cannot control user behavior. If a user program inadvertently executes cat/proc/slabinfo, it may affect other user programs.As Christoph said: "The count is not needed for any operations. Just for the slabinfo output. The value has no operational value for the allocator itself. So why use extra logic to track it in potentially performance critical paths?"
In this way, could we show the approximate value of
active_objsin the/proc/slabinfo?Based on the following information:
In thediscard_slab()function,page->inuseis equal topage->total_objects; In theallocate_slab()function,page->inuseis also equal topage->total_objects(with one exception: forkmem_cache_node, page-> inuse equals 1);page->inusewill only change continuously when the obj is constantly allocated or released. (This should be the performance critical path emphasized by Christoph)Quoted 2 lines
When users query the global slabinfo information, we may use
total_objectsto approximateactive_objs.Well, from one point of view, it makes no sense, because the ratio between these two numbers is very meaningful: it's the slab utilization rate.
On the other side, with enabled per-cpu partial lists
active_objshas nothing to do with the reality anyway, so I agree with you, callingcount_partial()is almost useless.That said, I wonder if the right thing to do is something like the patch below?
Thanks!
Roman
--
mm/slub.c +2
-1@@ -2411,14 +2411,16 @@ static inline unsigned long node_nr_objs(struct kmem_cache_node *n)static unsigned long count_partial (struct kmem_cache_node *n,int (*get_count)(struct page *)) { -unsigned long flags;unsigned long x =0 ; +#ifdef CONFIG_SLUB_CPU_PARTIAL +unsigned long flags;
mm/slub.c static inline unsigned long node_nr_objs(struct kmem_cache_node *n) · line 2426
Quoted 10 lines
mm/slub.c +1
-0struct page *page;spin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (page, &n->partial, slab_list) x +=get_count (page);spin_unlock_irqrestore (&n->list_lock, flags); +#endif return x; }#endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
Hi Roman,
Thanks for your comments.
In the server scenario, SLUB_CPU_PARTIAL is turned on by default, and can improve the performance of the cloud server, as follows:
default y
depends on SLUB && SMP
bool "SLUB per cpu partial cache"
help
Per cpu partial caches accelerate objects allocation and freeing that is local to a processor at the price of more indeterminism in the latency of the free. On overflow these caches will be cleared which requires the taking of locks that may cause latency spikes. Typically one would choose no for a realtime system.
Best wishes,
Wen
Quoted 128 lines · Wen Yang
Quoted 112 lines · Roman Gushchin
Quoted 81 lines · Wen Yang
Quoted 26 lines · Andrew Morton
Quoted 15 lines · On Sat, 1 Feb 2020 11:15:02 +0800 Wen Yang <[email protected]> wrote:
The lock, protecting the node partial list, is taken when couting the free objects resident in that list. It introduces locking contention when the page(s) is moved between CPU and node partial lists in allocation path on another CPU. So reading "
/proc/slabinfo" can possibily block the slab allocation on another CPU for a while, 200ms in extreme cases. If the slab object is to carry network packet, targeting the far-end disk array, it causes block IO jitter issue.This fixes the block IO jitter issue by caching the total inuse objects in the node in advance. The value is retrieved without taking the node partial list lock on reading "
/proc/slabinfo"....
Review static void free_slab(struct kmem_cache *s, struct page *page) · line 1778
Quoted 6 lines
(quoted diff) +3
-1@@ -1768,7 +1774,9 @@ static void free_slab(struct kmem_cache *s, struct page *page) static void discard_slab(struct kmem_cache *s, struct page *page) { - dec_slabs_node(s, page_to_nid(page), page->objects); + int inuse = page->objects; + + dec_slabs_node(s, page_to_nid(page), page->objects, inuse);Is this right?
dec_slabs_node(..., page->objects, page->objects)?Quoted 5 lines
If no, we could simply pass the page* to inc_slabs_node/dec_slabs_node and save a function argument.
If yes then why?
Thanks for your comments.
We are happy to improve this patch based on your suggestions.When the user reads
/proc/slabinfo, in order to obtain theactive_objsinformation, the kernel traverses all slabs and executes the following code snippet:
static unsigned long count_partial(structkmem_cache_node*n,
int (*get_count)(struct page *)){ unsigned long flags; unsigned long x = 0; struct page *page; spin_lock_irqsave(&n->list_lock, flags); list_for_each_entry(page, &n->partial, slab_list) x += get_count(page); spin_unlock_irqrestore(&n->list_lock, flags); return x; }It may cause performance issues.
Christoph suggested "you could cache the value in the userspace application? Why is this value read continually?", But reading the
/proc/slabinfois initiated by the user program. As a cloud provider, we cannot control user behavior. If a user program inadvertently executes cat/proc/slabinfo, it may affect other user programs.As Christoph said: "The count is not needed for any operations. Just for the slabinfo output. The value has no operational value for the allocator itself. So why use extra logic to track it in potentially performance critical paths?"
In this way, could we show the approximate value of
active_objsin the/proc/slabinfo?Based on the following information:
In thediscard_slab()function,page->inuseis equal topage->total_objects; In theallocate_slab()function,page->inuseis also equal topage->total_objects(with one exception: forkmem_cache_node, page-> inuse equals 1);page->inusewill only change continuously when the obj is constantly allocated or released. (This should be the performance critical path emphasized by Christoph)Quoted 2 lines
When users query the global slabinfo information, we may use
total_objectsto approximateactive_objs.Well, from one point of view, it makes no sense, because the ratio between these two numbers is very meaningful: it's the slab utilization rate.
On the other side, with enabled per-cpu partial lists
active_objshas nothing to do with the reality anyway, so I agree with you, callingcount_partial()is almost useless.That said, I wonder if the right thing to do is something like the patch below?
Thanks!
Roman
--
mm/slub.c +2
-1@@ -2411,14 +2411,16 @@ static inline unsigned long node_nr_objs(struct kmem_cache_node *n)static unsigned long count_partial (struct kmem_cache_node *n,int (*get_count)(struct page *)) { -unsigned long flags;unsigned long x =0 ; +#ifdef CONFIG_SLUB_CPU_PARTIAL +unsigned long flags;mm/slub.c static inline unsigned long node_nr_objs(struct kmem_cache_node *n) · line 2426
Quoted 10 lines
mm/slub.c +1
-0struct page *page;spin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (page, &n->partial, slab_list) x +=get_count (page);spin_unlock_irqrestore (&n->list_lock, flags); +#endif return x; }#endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */ Hi Roman,
Quoted 4 lines
Thanks for your comments.
In the server scenario,
SLUB_CPU_PARTIALis turned on by default, and can improve the performance of the cloud server, as follows:
Hello, Wen!
That's exactly my point: if CONFIG_SLUB_CPU_PARTIAL is on, count_partial() is useless anyway because the returned number is far from the reality. So if we define active_objects == total_objects, as you basically suggest, we do not introduce any regression. Actually I think it's even preferable to show the unrealistic uniform 100% slab utilization rather than some very high but incorrect value.
And on real-time systems uncontrolled readings of /proc/slabinfo is less of a concern, I hope.
Thank you!
Quoted 142 lines · Roman Gushchin
Quoted 128 lines · Wen Yang
Quoted 112 lines · Roman Gushchin
Quoted 81 lines · Wen Yang
Quoted 26 lines · Andrew Morton
Quoted 15 lines · On Sat, 1 Feb 2020 11:15:02 +0800 Wen Yang <[email protected]> wrote:
The lock, protecting the node partial list, is taken when couting the free objects resident in that list. It introduces locking contention when the page(s) is moved between CPU and node partial lists in allocation path on another CPU. So reading "
/proc/slabinfo" can possibily block the slab allocation on another CPU for a while, 200ms in extreme cases. If the slab object is to carry network packet, targeting the far-end disk array, it causes block IO jitter issue.This fixes the block IO jitter issue by caching the total inuse objects in the node in advance. The value is retrieved without taking the node partial list lock on reading "
/proc/slabinfo"....
Review static void free_slab(struct kmem_cache *s, struct page *page) · line 1778
Quoted 6 lines
(quoted diff) +3
-1@@ -1768,7 +1774,9 @@ static void free_slab(struct kmem_cache *s, struct page *page) static void discard_slab(struct kmem_cache *s, struct page *page) { - dec_slabs_node(s, page_to_nid(page), page->objects); + int inuse = page->objects; + + dec_slabs_node(s, page_to_nid(page), page->objects, inuse);Is this right?
dec_slabs_node(..., page->objects, page->objects)?Quoted 5 lines
If no, we could simply pass the page* to inc_slabs_node/dec_slabs_node and save a function argument.
If yes then why?
Thanks for your comments.
We are happy to improve this patch based on your suggestions.When the user reads
/proc/slabinfo, in order to obtain theactive_objsinformation, the kernel traverses all slabs and executes the following code snippet:
static unsigned long count_partial(structkmem_cache_node*n,
int (*get_count)(struct page *)){ unsigned long flags; unsigned long x = 0; struct page *page; spin_lock_irqsave(&n->list_lock, flags); list_for_each_entry(page, &n->partial, slab_list) x += get_count(page); spin_unlock_irqrestore(&n->list_lock, flags); return x; }It may cause performance issues.
Christoph suggested "you could cache the value in the userspace application? Why is this value read continually?", But reading the
/proc/slabinfois initiated by the user program. As a cloud provider, we cannot control user behavior. If a user program inadvertently executes cat/proc/slabinfo, it may affect other user programs.As Christoph said: "The count is not needed for any operations. Just for the slabinfo output. The value has no operational value for the allocator itself. So why use extra logic to track it in potentially performance critical paths?"
In this way, could we show the approximate value of
active_objsin the/proc/slabinfo?Based on the following information:
In thediscard_slab()function,page->inuseis equal topage->total_objects; In theallocate_slab()function,page->inuseis also equal topage->total_objects(with one exception: forkmem_cache_node, page-> inuse equals 1);page->inusewill only change continuously when the obj is constantly allocated or released. (This should be the performance critical path emphasized by Christoph)Quoted 2 lines
When users query the global slabinfo information, we may use
total_objectsto approximateactive_objs.Well, from one point of view, it makes no sense, because the ratio between these two numbers is very meaningful: it's the slab utilization rate.
On the other side, with enabled per-cpu partial lists
active_objshas nothing to do with the reality anyway, so I agree with you, callingcount_partial()is almost useless.That said, I wonder if the right thing to do is something like the patch below?
Thanks!
Roman
--
mm/slub.c +2
-1@@ -2411,14 +2411,16 @@ static inline unsigned long node_nr_objs(struct kmem_cache_node *n)static unsigned long count_partial (struct kmem_cache_node *n,int (*get_count)(struct page *)) { -unsigned long flags;unsigned long x =0 ; +#ifdef CONFIG_SLUB_CPU_PARTIAL +unsigned long flags;mm/slub.c static inline unsigned long node_nr_objs(struct kmem_cache_node *n) · line 2426
Quoted 10 lines
mm/slub.c +1
-0struct page *page;spin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (page, &n->partial, slab_list) x +=get_count (page);spin_unlock_irqrestore (&n->list_lock, flags); +#endif return x; }#endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */ Hi Roman,
Quoted 4 lines
Thanks for your comments.
In the server scenario,
SLUB_CPU_PARTIALis turned on by default, and can improve the performance of the cloud server, as follows:Hello, Wen!
That's exactly my point: if
CONFIG_SLUB_CPU_PARTIALis on,count_partial()is useless anyway because the returned number is far from the reality. So if we defineactive_objects==total_objects, as you basically suggest, we do not introduce any regression. Actually I think it's even preferable to show the unrealistic uniform 100% slab utilization rather than some very high but incorrect value.
Quoted 5 lines
And on real-time systems uncontrolled readings of
/proc/slabinfois less of a concern, I hope.Thank you!
Great!
We only need to correct a typo to achieve this goal, as follows:
Change #ifdef CONFIG_SLUB_CPU_PARTIAL to #ifndef CONFIG_SLUB_CPU_PARTIAL
We will continue testing and send the modified patch soon.
Thank you very much.
Quoted 148 lines · Wen Yang
Quoted 145 lines · Roman Gushchin
Quoted 128 lines · Wen Yang
Quoted 112 lines · Roman Gushchin
Quoted 81 lines · Wen Yang
Quoted 26 lines · Andrew Morton
Quoted 15 lines · On Sat, 1 Feb 2020 11:15:02 +0800 Wen Yang <[email protected]> wrote:
The lock, protecting the node partial list, is taken when couting the free objects resident in that list. It introduces locking contention when the page(s) is moved between CPU and node partial lists in allocation path on another CPU. So reading "
/proc/slabinfo" can possibily block the slab allocation on another CPU for a while, 200ms in extreme cases. If the slab object is to carry network packet, targeting the far-end disk array, it causes block IO jitter issue.This fixes the block IO jitter issue by caching the total inuse objects in the node in advance. The value is retrieved without taking the node partial list lock on reading "
/proc/slabinfo"....
Review static void free_slab(struct kmem_cache *s, struct page *page) · line 1778
Quoted 6 lines
(quoted diff) +3
-1@@ -1768,7 +1774,9 @@ static void free_slab(struct kmem_cache *s, struct page *page) static void discard_slab(struct kmem_cache *s, struct page *page) { - dec_slabs_node(s, page_to_nid(page), page->objects); + int inuse = page->objects; + + dec_slabs_node(s, page_to_nid(page), page->objects, inuse);Is this right?
dec_slabs_node(..., page->objects, page->objects)?Quoted 5 lines
If no, we could simply pass the page* to inc_slabs_node/dec_slabs_node and save a function argument.
If yes then why?
Thanks for your comments.
We are happy to improve this patch based on your suggestions.When the user reads
/proc/slabinfo, in order to obtain theactive_objsinformation, the kernel traverses all slabs and executes the following code snippet:
static unsigned long count_partial(structkmem_cache_node*n,
int (*get_count)(struct page *)){ unsigned long flags; unsigned long x = 0; struct page *page; spin_lock_irqsave(&n->list_lock, flags); list_for_each_entry(page, &n->partial, slab_list) x += get_count(page); spin_unlock_irqrestore(&n->list_lock, flags); return x; }It may cause performance issues.
Christoph suggested "you could cache the value in the userspace application? Why is this value read continually?", But reading the
/proc/slabinfois initiated by the user program. As a cloud provider, we cannot control user behavior. If a user program inadvertently executes cat/proc/slabinfo, it may affect other user programs.As Christoph said: "The count is not needed for any operations. Just for the slabinfo output. The value has no operational value for the allocator itself. So why use extra logic to track it in potentially performance critical paths?"
In this way, could we show the approximate value of
active_objsin the/proc/slabinfo?Based on the following information:
In thediscard_slab()function,page->inuseis equal topage->total_objects; In theallocate_slab()function,page->inuseis also equal topage->total_objects(with one exception: forkmem_cache_node, page-> inuse equals 1);page->inusewill only change continuously when the obj is constantly allocated or released. (This should be the performance critical path emphasized by Christoph)Quoted 2 lines
When users query the global slabinfo information, we may use
total_objectsto approximateactive_objs.Well, from one point of view, it makes no sense, because the ratio between these two numbers is very meaningful: it's the slab utilization rate.
On the other side, with enabled per-cpu partial lists
active_objshas nothing to do with the reality anyway, so I agree with you, callingcount_partial()is almost useless.That said, I wonder if the right thing to do is something like the patch below?
Thanks!
Roman
--
mm/slub.c +2
-1@@ -2411,14 +2411,16 @@ static inline unsigned long node_nr_objs(struct kmem_cache_node *n)static unsigned long count_partial (struct kmem_cache_node *n,int (*get_count)(struct page *)) { -unsigned long flags;unsigned long x =0 ; +#ifdef CONFIG_SLUB_CPU_PARTIAL +unsigned long flags;mm/slub.c static inline unsigned long node_nr_objs(struct kmem_cache_node *n) · line 2426
Quoted 10 lines
mm/slub.c +1
-0struct page *page;spin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (page, &n->partial, slab_list) x +=get_count (page);spin_unlock_irqrestore (&n->list_lock, flags); +#endif return x; }#endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */ Hi Roman,
Quoted 4 lines
Thanks for your comments.
In the server scenario,
SLUB_CPU_PARTIALis turned on by default, and can improve the performance of the cloud server, as follows:Hello, Wen!
That's exactly my point: if
CONFIG_SLUB_CPU_PARTIALis on,count_partial()is useless anyway because the returned number is far from the reality. So if we defineactive_objects==total_objects, as you basically suggest, we do not introduce any regression. Actually I think it's even preferable to show the unrealistic uniform 100% slab utilization rather than some very high but incorrect value.And on real-time systems uncontrolled readings of
/proc/slabinfois less of a concern, I hope.
Quoted 6 lines
Quoted 2 lines
Thank you!
Great!
We only need to correct a typo to achieve this goal, as follows:
Change #ifdefCONFIG_SLUB_CPU_PARTIALto #ifndefCONFIG_SLUB_CPU_PARTIAL
Yes, you're obviously right.
Thanks!