All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
@ 2018-03-15 15:01 Kirill Tkhai
  2018-03-15 15:53 ` Darrick J. Wong
  2018-03-15 17:49 ` Michal Hocko
  0 siblings, 2 replies; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-15 15:01 UTC (permalink / raw)
  To: darrick.wong, linux-xfs, linux-kernel, akpm, ktkhai

xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
So, it's called for memcg reclaim too, e.g. this list is shrinked
disproportionality to another lists.

This looks confusing, so I'm reporting about this.
Consider this patch as RFC.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 fs/xfs/xfs_super.c |    2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
index 951271f57d00..124568aefa94 100644
--- a/fs/xfs/xfs_super.c
+++ b/fs/xfs/xfs_super.c
@@ -1788,6 +1788,8 @@ xfs_fs_nr_cached_objects(
 	struct super_block	*sb,
 	struct shrink_control	*sc)
 {
+	if (sc->memcg)
+		return 0;
 	return xfs_reclaim_inodes_count(XFS_M(sb));
 }
 

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-15 15:01 [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim Kirill Tkhai
@ 2018-03-15 15:53 ` Darrick J. Wong
  2018-03-15 16:06   ` Kirill Tkhai
  2018-03-15 17:49 ` Michal Hocko
  1 sibling, 1 reply; 22+ messages in thread
From: Darrick J. Wong @ 2018-03-15 15:53 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: linux-xfs, linux-kernel, akpm

On Thu, Mar 15, 2018 at 06:01:34PM +0300, Kirill Tkhai wrote:
> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
> So, it's called for memcg reclaim too, e.g. this list is shrinked
> disproportionality to another lists.
> 
> This looks confusing, so I'm reporting about this.
> Consider this patch as RFC.

So... I think the reasoning here is that xfs doesn't allocate inodes on
behalf of any particular memcg (or put another way the cost of the
inodes is shared by everything in the system) so if the shrinkers get
called because a particular memcg is bumping up against its limits it
makes no sense to try to purge xfs inodes?

Followup questions: does the same reasoning apply to the xfs buffer and
quota shrinkers?  Do any filesystems associate their metadata memory
allocations with a specific memcg?  If not, why not put this in
super_cache_{scan,count}?

--D

> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
> ---
>  fs/xfs/xfs_super.c |    2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
> index 951271f57d00..124568aefa94 100644
> --- a/fs/xfs/xfs_super.c
> +++ b/fs/xfs/xfs_super.c
> @@ -1788,6 +1788,8 @@ xfs_fs_nr_cached_objects(
>  	struct super_block	*sb,
>  	struct shrink_control	*sc)
>  {
> +	if (sc->memcg)
> +		return 0;
>  	return xfs_reclaim_inodes_count(XFS_M(sb));
>  }
>  
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-15 15:53 ` Darrick J. Wong
@ 2018-03-15 16:06   ` Kirill Tkhai
  0 siblings, 0 replies; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-15 16:06 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: linux-xfs, linux-kernel, akpm

On 15.03.2018 18:53, Darrick J. Wong wrote:
> On Thu, Mar 15, 2018 at 06:01:34PM +0300, Kirill Tkhai wrote:
>> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
>> So, it's called for memcg reclaim too, e.g. this list is shrinked
>> disproportionality to another lists.
>>
>> This looks confusing, so I'm reporting about this.
>> Consider this patch as RFC.
> 
> So... I think the reasoning here is that xfs doesn't allocate inodes on
> behalf of any particular memcg (or put another way the cost of the
> inodes is shared by everything in the system) so if the shrinkers get
> called because a particular memcg is bumping up against its limits it
> makes no sense to try to purge xfs inodes?

Yes, since shrinking xfs cached objects doesn't free memcg kmem.
 
> Followup questions: does the same reasoning apply to the xfs buffer and
> quota shrinkers?

It's not need, as these shrinker don't have SHRINKER_MEMCG_AWARE flag.
So, they are called only in case of global reclaim (memcg == NULL).

But they may require another type of change. They use list_lru,
so they may need to have SHRINKER_MEMCG_AWARE flag. This is depends
on how objects linked to xfs_buftarg::bt_lru and xfs_quotainfo::qi_lru
are allocated. If they are accounted to memcg (i.e., use GFP_ACCOUNT
flag in their kmalloc/slab alloc/etc), they have to have this flag.
Ideally, all user initiated allocations should be made with this flag.

If they already use GFP_ACCOUNT while shrinker is not marked as SHRINKER_MEMCG_AWARE,
it never shrinks objects related to memcgs.

> Do any filesystems associate their metadata memory
> allocations with a specific memcg?  If not, why not put this in
> super_cache_{scan,count}?

They do, since generic super_cache_{scan,count} have a deal with sb's s_dentry_lru
and s_inode_lru, and use list lru, which supports memcg accounting.
 
> --D
> 
>> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
>> ---
>>  fs/xfs/xfs_super.c |    2 ++
>>  1 file changed, 2 insertions(+)
>>
>> diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
>> index 951271f57d00..124568aefa94 100644
>> --- a/fs/xfs/xfs_super.c
>> +++ b/fs/xfs/xfs_super.c
>> @@ -1788,6 +1788,8 @@ xfs_fs_nr_cached_objects(
>>  	struct super_block	*sb,
>>  	struct shrink_control	*sc)
>>  {
>> +	if (sc->memcg)
>> +		return 0;
>>  	return xfs_reclaim_inodes_count(XFS_M(sb));
>>  }
>>  
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-15 15:01 [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim Kirill Tkhai
  2018-03-15 15:53 ` Darrick J. Wong
@ 2018-03-15 17:49 ` Michal Hocko
  2018-03-15 19:28   ` Kirill Tkhai
  1 sibling, 1 reply; 22+ messages in thread
From: Michal Hocko @ 2018-03-15 17:49 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: darrick.wong, linux-xfs, linux-kernel, akpm

On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
> So, it's called for memcg reclaim too, e.g. this list is shrinked
> disproportionality to another lists.
> 
> This looks confusing, so I'm reporting about this.
> Consider this patch as RFC.

Could you be more specific about the problem you are trying to solve?
Because we do skip shrinkers which are not memcg aware by
shrink_slab:
		/*
		 * If kernel memory accounting is disabled, we ignore
		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
		 * passing NULL for memcg.
		 */
		if (memcg_kmem_enabled() &&
		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
			continue;

Or am I missing something?
 
> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
> ---
>  fs/xfs/xfs_super.c |    2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
> index 951271f57d00..124568aefa94 100644
> --- a/fs/xfs/xfs_super.c
> +++ b/fs/xfs/xfs_super.c
> @@ -1788,6 +1788,8 @@ xfs_fs_nr_cached_objects(
>  	struct super_block	*sb,
>  	struct shrink_control	*sc)
>  {
> +	if (sc->memcg)
> +		return 0;
>  	return xfs_reclaim_inodes_count(XFS_M(sb));
>  }
>  

-- 
Michal Hocko
SUSE Labs

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-15 17:49 ` Michal Hocko
@ 2018-03-15 19:28   ` Kirill Tkhai
  2018-03-15 19:32     ` Michal Hocko
  2018-03-15 23:03     ` Dave Chinner
  0 siblings, 2 replies; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-15 19:28 UTC (permalink / raw)
  To: Michal Hocko; +Cc: darrick.wong, linux-xfs, linux-kernel, akpm

On 15.03.2018 20:49, Michal Hocko wrote:
> On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
>> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
>> So, it's called for memcg reclaim too, e.g. this list is shrinked
>> disproportionality to another lists.
>>
>> This looks confusing, so I'm reporting about this.
>> Consider this patch as RFC.
> 
> Could you be more specific about the problem you are trying to solve?
> Because we do skip shrinkers which are not memcg aware by
> shrink_slab:
> 		/*
> 		 * If kernel memory accounting is disabled, we ignore
> 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
> 		 * passing NULL for memcg.
> 		 */
> 		if (memcg_kmem_enabled() &&
> 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
> 			continue;
> 
> Or am I missing something?

sb->s_op->nr_cached_objects is a sub-method of generic super_cache_count().
super_cache_count() is owned and only called by superblock's shrinker,
which does have SHRINKER_MEMCG_AWARE flag.

>  
>> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
>> ---
>>  fs/xfs/xfs_super.c |    2 ++
>>  1 file changed, 2 insertions(+)
>>
>> diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
>> index 951271f57d00..124568aefa94 100644
>> --- a/fs/xfs/xfs_super.c
>> +++ b/fs/xfs/xfs_super.c
>> @@ -1788,6 +1788,8 @@ xfs_fs_nr_cached_objects(
>>  	struct super_block	*sb,
>>  	struct shrink_control	*sc)
>>  {
>> +	if (sc->memcg)
>> +		return 0;
>>  	return xfs_reclaim_inodes_count(XFS_M(sb));
>>  }
>>  
> 

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-15 19:28   ` Kirill Tkhai
@ 2018-03-15 19:32     ` Michal Hocko
  2018-03-15 19:42       ` Kirill Tkhai
  2018-03-15 23:03     ` Dave Chinner
  1 sibling, 1 reply; 22+ messages in thread
From: Michal Hocko @ 2018-03-15 19:32 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: darrick.wong, linux-xfs, linux-kernel, akpm

On Thu 15-03-18 22:28:43, Kirill Tkhai wrote:
> On 15.03.2018 20:49, Michal Hocko wrote:
> > On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
> >> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
> >> So, it's called for memcg reclaim too, e.g. this list is shrinked
> >> disproportionality to another lists.
> >>
> >> This looks confusing, so I'm reporting about this.
> >> Consider this patch as RFC.
> > 
> > Could you be more specific about the problem you are trying to solve?
> > Because we do skip shrinkers which are not memcg aware by
> > shrink_slab:
> > 		/*
> > 		 * If kernel memory accounting is disabled, we ignore
> > 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
> > 		 * passing NULL for memcg.
> > 		 */
> > 		if (memcg_kmem_enabled() &&
> > 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
> > 			continue;
> > 
> > Or am I missing something?
> 
> sb->s_op->nr_cached_objects is a sub-method of generic super_cache_count().
> super_cache_count() is owned and only called by superblock's shrinker,
> which does have SHRINKER_MEMCG_AWARE flag.

Ohh, I see. I thought it was a standard "top-level" shrinker.
-- 
Michal Hocko
SUSE Labs

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-15 19:32     ` Michal Hocko
@ 2018-03-15 19:42       ` Kirill Tkhai
  0 siblings, 0 replies; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-15 19:42 UTC (permalink / raw)
  To: Michal Hocko; +Cc: darrick.wong, linux-xfs, linux-kernel, akpm

On 15.03.2018 22:32, Michal Hocko wrote:
> On Thu 15-03-18 22:28:43, Kirill Tkhai wrote:
>> On 15.03.2018 20:49, Michal Hocko wrote:
>>> On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
>>>> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
>>>> So, it's called for memcg reclaim too, e.g. this list is shrinked
>>>> disproportionality to another lists.
>>>>
>>>> This looks confusing, so I'm reporting about this.
>>>> Consider this patch as RFC.
>>>
>>> Could you be more specific about the problem you are trying to solve?
>>> Because we do skip shrinkers which are not memcg aware by
>>> shrink_slab:
>>> 		/*
>>> 		 * If kernel memory accounting is disabled, we ignore
>>> 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
>>> 		 * passing NULL for memcg.
>>> 		 */
>>> 		if (memcg_kmem_enabled() &&
>>> 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
>>> 			continue;
>>>
>>> Or am I missing something?
>>
>> sb->s_op->nr_cached_objects is a sub-method of generic super_cache_count().
>> super_cache_count() is owned and only called by superblock's shrinker,
>> which does have SHRINKER_MEMCG_AWARE flag.
> 
> Ohh, I see. I thought it was a standard "top-level" shrinker.

There are one more in addition (it has to go in this patch):

.free_cached_objects    = xfs_fs_free_cached_objects

which is not memcg aware. It also needs to have the same fix,
if xfs_reclaim_inodes_count() is need.

Kirill

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-15 19:28   ` Kirill Tkhai
  2018-03-15 19:32     ` Michal Hocko
@ 2018-03-15 23:03     ` Dave Chinner
  2018-03-16  8:55       ` Kirill Tkhai
  1 sibling, 1 reply; 22+ messages in thread
From: Dave Chinner @ 2018-03-15 23:03 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On Thu, Mar 15, 2018 at 10:28:43PM +0300, Kirill Tkhai wrote:
> On 15.03.2018 20:49, Michal Hocko wrote:
> > On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
> >> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
> >> So, it's called for memcg reclaim too, e.g. this list is shrinked
> >> disproportionality to another lists.
> >>
> >> This looks confusing, so I'm reporting about this.
> >> Consider this patch as RFC.
> > 
> > Could you be more specific about the problem you are trying to solve?
> > Because we do skip shrinkers which are not memcg aware by
> > shrink_slab:
> > 		/*
> > 		 * If kernel memory accounting is disabled, we ignore
> > 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
> > 		 * passing NULL for memcg.
> > 		 */
> > 		if (memcg_kmem_enabled() &&
> > 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
> > 			continue;
> > 
> > Or am I missing something?
> 
> sb->s_op->nr_cached_objects is a sub-method of generic super_cache_count().
> super_cache_count() is owned and only called by superblock's shrinker,
> which does have SHRINKER_MEMCG_AWARE flag.

Xfs inodes are accounted to memcgs when they are allocated.  All the
memcg reclaim stuff is done at the VFS inode cache level - all the
XFS inode cache shrinker does is clean up inodes that are not
referenced by the VFS inode cache because the memcg aware reclaim
has already freed them.

i.e. what the XFS inode cache is doing is perfectly reasonable -
memcg aware inode reclaim is occuring at the VFS level, but on XFS
that does not free the inodes as they are still referenced
internally by XFS. However, once the inode is removed from the VFS
LRU, all memcg information about the inode is destroyed, so there's
nothing in the XFS layers that cares about memcgs.

Hence when the XFS inode shrinker then called to run a
garbage collection pass on unreferenced inodes - the inodes that
are now unreferenced in the memcg due to the VFS inode shrinker pass
- it frees inodes regardless of the memcg context it was called from
because that information is no longer present in the inode cache.
Hence we just ignore memcgs at this level.

So, is there a problem you are actually trying to fix, or is this
simply a "I don't understand how the superblock shrinkers work,
please explain" patch?

Cheers,

Dave.
-- 
Dave Chinner
david@fromorbit.com

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-15 23:03     ` Dave Chinner
@ 2018-03-16  8:55       ` Kirill Tkhai
  2018-03-16 21:39         ` Dave Chinner
  0 siblings, 1 reply; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-16  8:55 UTC (permalink / raw)
  To: Dave Chinner; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On 16.03.2018 02:03, Dave Chinner wrote:
> On Thu, Mar 15, 2018 at 10:28:43PM +0300, Kirill Tkhai wrote:
>> On 15.03.2018 20:49, Michal Hocko wrote:
>>> On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
>>>> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
>>>> So, it's called for memcg reclaim too, e.g. this list is shrinked
>>>> disproportionality to another lists.
>>>>
>>>> This looks confusing, so I'm reporting about this.
>>>> Consider this patch as RFC.
>>>
>>> Could you be more specific about the problem you are trying to solve?
>>> Because we do skip shrinkers which are not memcg aware by
>>> shrink_slab:
>>> 		/*
>>> 		 * If kernel memory accounting is disabled, we ignore
>>> 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
>>> 		 * passing NULL for memcg.
>>> 		 */
>>> 		if (memcg_kmem_enabled() &&
>>> 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
>>> 			continue;
>>>
>>> Or am I missing something?
>>
>> sb->s_op->nr_cached_objects is a sub-method of generic super_cache_count().
>> super_cache_count() is owned and only called by superblock's shrinker,
>> which does have SHRINKER_MEMCG_AWARE flag.
> 
> Xfs inodes are accounted to memcgs when they are allocated.  All the
> memcg reclaim stuff is done at the VFS inode cache level - all the
> XFS inode cache shrinker does is clean up inodes that are not
> referenced by the VFS inode cache because the memcg aware reclaim
> has already freed them.
> 
> i.e. what the XFS inode cache is doing is perfectly reasonable -
> memcg aware inode reclaim is occuring at the VFS level, but on XFS
> that does not free the inodes as they are still referenced
> internally by XFS. However, once the inode is removed from the VFS
> LRU, all memcg information about the inode is destroyed, so there's
> nothing in the XFS layers that cares about memcgs.

So, after inode is removed from LRU, memory still remains accounted
to memcg till the time they are actually freed. I personally don't
care, just to mention.
 
> Hence when the XFS inode shrinker then called to run a
> garbage collection pass on unreferenced inodes - the inodes that
> are now unreferenced in the memcg due to the VFS inode shrinker pass
> - it frees inodes regardless of the memcg context it was called from
> because that information is no longer present in the inode cache.
> Hence we just ignore memcgs at this level.

But xfs_fs_free_cached_objects() returns number of these freed object
as result to super_cache_scan(), so shrinker interprets them as related
to a memcg, while they may be related to another memcg. This introduces
a disproportion relatively to another shrinkers called to memcg.
Is there a problem? This is what my patch about.

> So, is there a problem you are actually trying to fix, or is this
> simply a "I don't understand how the superblock shrinkers work,
> please explain" patch?

I work on some shrinker changes, and just want to understand they don't
touch anything. 

Kirill

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-16  8:55       ` Kirill Tkhai
@ 2018-03-16 21:39         ` Dave Chinner
  2018-03-19 11:06           ` Kirill Tkhai
  0 siblings, 1 reply; 22+ messages in thread
From: Dave Chinner @ 2018-03-16 21:39 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On Fri, Mar 16, 2018 at 11:55:30AM +0300, Kirill Tkhai wrote:
> On 16.03.2018 02:03, Dave Chinner wrote:
> > On Thu, Mar 15, 2018 at 10:28:43PM +0300, Kirill Tkhai wrote:
> >> On 15.03.2018 20:49, Michal Hocko wrote:
> >>> On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
> >>>> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
> >>>> So, it's called for memcg reclaim too, e.g. this list is shrinked
> >>>> disproportionality to another lists.
> >>>>
> >>>> This looks confusing, so I'm reporting about this.
> >>>> Consider this patch as RFC.
> >>>
> >>> Could you be more specific about the problem you are trying to solve?
> >>> Because we do skip shrinkers which are not memcg aware by
> >>> shrink_slab:
> >>> 		/*
> >>> 		 * If kernel memory accounting is disabled, we ignore
> >>> 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
> >>> 		 * passing NULL for memcg.
> >>> 		 */
> >>> 		if (memcg_kmem_enabled() &&
> >>> 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
> >>> 			continue;
> >>>
> >>> Or am I missing something?
> >>
> >> sb->s_op->nr_cached_objects is a sub-method of generic super_cache_count().
> >> super_cache_count() is owned and only called by superblock's shrinker,
> >> which does have SHRINKER_MEMCG_AWARE flag.
> > 
> > Xfs inodes are accounted to memcgs when they are allocated.  All the
> > memcg reclaim stuff is done at the VFS inode cache level - all the
> > XFS inode cache shrinker does is clean up inodes that are not
> > referenced by the VFS inode cache because the memcg aware reclaim
> > has already freed them.
> > 
> > i.e. what the XFS inode cache is doing is perfectly reasonable -
> > memcg aware inode reclaim is occuring at the VFS level, but on XFS
> > that does not free the inodes as they are still referenced
> > internally by XFS. However, once the inode is removed from the VFS
> > LRU, all memcg information about the inode is destroyed, so there's
> > nothing in the XFS layers that cares about memcgs.
> 
> So, after inode is removed from LRU, memory still remains accounted
> to memcg till the time they are actually freed. I personally don't
> care, just to mention.
>  
> > Hence when the XFS inode shrinker then called to run a
> > garbage collection pass on unreferenced inodes - the inodes that
> > are now unreferenced in the memcg due to the VFS inode shrinker pass
> > - it frees inodes regardless of the memcg context it was called from
> > because that information is no longer present in the inode cache.
> > Hence we just ignore memcgs at this level.
> 
> But xfs_fs_free_cached_objects() returns number of these freed object
> as result to super_cache_scan(), so shrinker interprets them as related
> to a memcg, while they may be related to another memcg. This introduces
> a disproportion relatively to another shrinkers called to memcg.

In what way? All memcgs see tha same values from the backing cache
and so try to do the same amount of scanning work. The shrinker
accounting simply doesn't care where the objects are scanned from,
as long as it comes from the same place as the calculation of the
number of objects in the cache it's about to scan.

The memcg accounting, OTOH, is completely divorced from the
shrinker, so if it's still got too much slab memory accounted to it,
it will simply run the shrinker again and do some more memory
reclaim.

XFS does this for IO efficiency purposes, not because it's ideal
behaviour for memcgs. If we start doing exact memcg-only inode
reclaim, we're going back to the bad old days where inode reclaim
causes really nasty small random write storms that essentially
starve the storage subsystem of all other IO for seconds at a time.
That is a *far worse problem* from a system performance POV than
having the memcg memory reclaim run a couple more shrinker passes
and do a little more work....

> Is there a problem? This is what my patch about.

You've described some theoretical issue, but not described any user
visible or performance impacting behaviour that users actually care
about. What reproducable, observable behaviour does it fix/change?

> > So, is there a problem you are actually trying to fix, or is this
> > simply a "I don't understand how the superblock shrinkers work,
> > please explain" patch?
> 
> I work on some shrinker changes, and just want to understand they don't
> touch anything. 

Oh, goody, another person about to learn the hard way that shrinkers
are far more varied and complex than page reclaim :P

Cheers,

Dave.
-- 
Dave Chinner
david@fromorbit.com

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-16 21:39         ` Dave Chinner
@ 2018-03-19 11:06           ` Kirill Tkhai
  2018-03-19 11:25             ` Kirill Tkhai
  2018-03-20  0:18             ` Dave Chinner
  0 siblings, 2 replies; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-19 11:06 UTC (permalink / raw)
  To: Dave Chinner; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On 17.03.2018 00:39, Dave Chinner wrote:
> On Fri, Mar 16, 2018 at 11:55:30AM +0300, Kirill Tkhai wrote:
>> On 16.03.2018 02:03, Dave Chinner wrote:
>>> On Thu, Mar 15, 2018 at 10:28:43PM +0300, Kirill Tkhai wrote:
>>>> On 15.03.2018 20:49, Michal Hocko wrote:
>>>>> On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
>>>>>> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
>>>>>> So, it's called for memcg reclaim too, e.g. this list is shrinked
>>>>>> disproportionality to another lists.
>>>>>>
>>>>>> This looks confusing, so I'm reporting about this.
>>>>>> Consider this patch as RFC.
>>>>>
>>>>> Could you be more specific about the problem you are trying to solve?
>>>>> Because we do skip shrinkers which are not memcg aware by
>>>>> shrink_slab:
>>>>> 		/*
>>>>> 		 * If kernel memory accounting is disabled, we ignore
>>>>> 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
>>>>> 		 * passing NULL for memcg.
>>>>> 		 */
>>>>> 		if (memcg_kmem_enabled() &&
>>>>> 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
>>>>> 			continue;
>>>>>
>>>>> Or am I missing something?
>>>>
>>>> sb->s_op->nr_cached_objects is a sub-method of generic super_cache_count().
>>>> super_cache_count() is owned and only called by superblock's shrinker,
>>>> which does have SHRINKER_MEMCG_AWARE flag.
>>>
>>> Xfs inodes are accounted to memcgs when they are allocated.  All the
>>> memcg reclaim stuff is done at the VFS inode cache level - all the
>>> XFS inode cache shrinker does is clean up inodes that are not
>>> referenced by the VFS inode cache because the memcg aware reclaim
>>> has already freed them.
>>>
>>> i.e. what the XFS inode cache is doing is perfectly reasonable -
>>> memcg aware inode reclaim is occuring at the VFS level, but on XFS
>>> that does not free the inodes as they are still referenced
>>> internally by XFS. However, once the inode is removed from the VFS
>>> LRU, all memcg information about the inode is destroyed, so there's
>>> nothing in the XFS layers that cares about memcgs.
>>
>> So, after inode is removed from LRU, memory still remains accounted
>> to memcg till the time they are actually freed. I personally don't
>> care, just to mention.
>>  
>>> Hence when the XFS inode shrinker then called to run a
>>> garbage collection pass on unreferenced inodes - the inodes that
>>> are now unreferenced in the memcg due to the VFS inode shrinker pass
>>> - it frees inodes regardless of the memcg context it was called from
>>> because that information is no longer present in the inode cache.
>>> Hence we just ignore memcgs at this level.
>>
>> But xfs_fs_free_cached_objects() returns number of these freed object
>> as result to super_cache_scan(), so shrinker interprets them as related
>> to a memcg, while they may be related to another memcg. This introduces
>> a disproportion relatively to another shrinkers called to memcg.
> 
> In what way? All memcgs see tha same values from the backing cache
> and so try to do the same amount of scanning work. The shrinker
> accounting simply doesn't care where the objects are scanned from,
> as long as it comes from the same place as the calculation of the
> number of objects in the cache it's about to scan.

shrinker->count_objects() result is used to count number of objects,
do_shrink_slab() should shrink during the call:

        freeable = shrinker->count_objects(shrinker, shrinkctl);

Then shrinker takes part of this value:

        delta = freeable >> priority;
        delta *= 4;
        do_div(delta, shrinker->seeks);

This is a number, the shrinker tries to shrink during the call.
Let the priority is DEF_PRIORITY. Then, shrinker try to shrink
freeable*4/(shrinker->seeks * 2^DEF_PRIORITY) enteties from every
shrinker. Equal percent of every memcg shrinker.

When XFS shows global number of cached objects in count_objects(),
shrinker also tryes to shrink the same percent of global objects,
as for other memcg shrinkers. So, when you have small memcg
with 128Mb memory allowed and small number of tasks related to it,
you may meet 1Gb of cached objects, which were queued by another
big cgroup. So, the small cgroup may shrink number of objects of
size more, then its own. It's not fair. That's all I'm worry in
this message.
 
> The memcg accounting, OTOH, is completely divorced from the
> shrinker, so if it's still got too much slab memory accounted to it,
> it will simply run the shrinker again and do some more memory
> reclaim.

This message is not about OOM, it's about imbalance. See above.

> XFS does this for IO efficiency purposes, not because it's ideal
> behaviour for memcgs. If we start doing exact memcg-only inode
> reclaim, we're going back to the bad old days where inode reclaim
> causes really nasty small random write storms that essentially
> starve the storage subsystem of all other IO for seconds at a time.
> That is a *far worse problem* from a system performance POV than
> having the memcg memory reclaim run a couple more shrinker passes
> and do a little more work....

Ok, this is a point.

>> Is there a problem? This is what my patch about.
> 
> You've described some theoretical issue, but not described any user
> visible or performance impacting behaviour that users actually care
> about. What reproducable, observable behaviour does it fix/change?

Strange question. Do you fix problems only when you meet a reproducable
BUG()? This is the kernel, and many problem may be racy. But this
does not mean, it's prohibited to discuss about them.

>>> So, is there a problem you are actually trying to fix, or is this
>>> simply a "I don't understand how the superblock shrinkers work,
>>> please explain" patch?
>>
>> I work on some shrinker changes, and just want to understand they don't
>> touch anything. 
> 
> Oh, goody, another person about to learn the hard way that shrinkers
> are far more varied and complex than page reclaim :P

It may be a surprise, but I have to say, that all memcg-aware shrinkers
are based on list_lrus. XFS is exception. So, your words are not about
shrinkers in general, just about XFS.

Regards,
Kirill

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-19 11:06           ` Kirill Tkhai
@ 2018-03-19 11:25             ` Kirill Tkhai
  2018-03-20  0:18             ` Dave Chinner
  1 sibling, 0 replies; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-19 11:25 UTC (permalink / raw)
  To: Dave Chinner; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On 19.03.2018 14:06, Kirill Tkhai wrote:
> On 17.03.2018 00:39, Dave Chinner wrote:
>> On Fri, Mar 16, 2018 at 11:55:30AM +0300, Kirill Tkhai wrote:
>>> On 16.03.2018 02:03, Dave Chinner wrote:
>>>> On Thu, Mar 15, 2018 at 10:28:43PM +0300, Kirill Tkhai wrote:
>>>>> On 15.03.2018 20:49, Michal Hocko wrote:
>>>>>> On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
>>>>>>> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
>>>>>>> So, it's called for memcg reclaim too, e.g. this list is shrinked
>>>>>>> disproportionality to another lists.
>>>>>>>
>>>>>>> This looks confusing, so I'm reporting about this.
>>>>>>> Consider this patch as RFC.
>>>>>>
>>>>>> Could you be more specific about the problem you are trying to solve?
>>>>>> Because we do skip shrinkers which are not memcg aware by
>>>>>> shrink_slab:
>>>>>> 		/*
>>>>>> 		 * If kernel memory accounting is disabled, we ignore
>>>>>> 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
>>>>>> 		 * passing NULL for memcg.
>>>>>> 		 */
>>>>>> 		if (memcg_kmem_enabled() &&
>>>>>> 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
>>>>>> 			continue;
>>>>>>
>>>>>> Or am I missing something?
>>>>>
>>>>> sb->s_op->nr_cached_objects is a sub-method of generic super_cache_count().
>>>>> super_cache_count() is owned and only called by superblock's shrinker,
>>>>> which does have SHRINKER_MEMCG_AWARE flag.
>>>>
>>>> Xfs inodes are accounted to memcgs when they are allocated.  All the
>>>> memcg reclaim stuff is done at the VFS inode cache level - all the
>>>> XFS inode cache shrinker does is clean up inodes that are not
>>>> referenced by the VFS inode cache because the memcg aware reclaim
>>>> has already freed them.
>>>>
>>>> i.e. what the XFS inode cache is doing is perfectly reasonable -
>>>> memcg aware inode reclaim is occuring at the VFS level, but on XFS
>>>> that does not free the inodes as they are still referenced
>>>> internally by XFS. However, once the inode is removed from the VFS
>>>> LRU, all memcg information about the inode is destroyed, so there's
>>>> nothing in the XFS layers that cares about memcgs.
>>>
>>> So, after inode is removed from LRU, memory still remains accounted
>>> to memcg till the time they are actually freed. I personally don't
>>> care, just to mention.
>>>  
>>>> Hence when the XFS inode shrinker then called to run a
>>>> garbage collection pass on unreferenced inodes - the inodes that
>>>> are now unreferenced in the memcg due to the VFS inode shrinker pass
>>>> - it frees inodes regardless of the memcg context it was called from
>>>> because that information is no longer present in the inode cache.
>>>> Hence we just ignore memcgs at this level.
>>>
>>> But xfs_fs_free_cached_objects() returns number of these freed object
>>> as result to super_cache_scan(), so shrinker interprets them as related
>>> to a memcg, while they may be related to another memcg. This introduces
>>> a disproportion relatively to another shrinkers called to memcg.
>>
>> In what way? All memcgs see tha same values from the backing cache
>> and so try to do the same amount of scanning work. The shrinker
>> accounting simply doesn't care where the objects are scanned from,
>> as long as it comes from the same place as the calculation of the
>> number of objects in the cache it's about to scan.
> 
> shrinker->count_objects() result is used to count number of objects,
> do_shrink_slab() should shrink during the call:
> 
>         freeable = shrinker->count_objects(shrinker, shrinkctl);
> 
> Then shrinker takes part of this value:
> 
>         delta = freeable >> priority;
>         delta *= 4;
>         do_div(delta, shrinker->seeks);
> 
> This is a number, the shrinker tries to shrink during the call.
> Let the priority is DEF_PRIORITY. Then, shrinker try to shrink
> freeable*4/(shrinker->seeks * 2^DEF_PRIORITY) enteties from every
> shrinker. Equal percent of every memcg shrinker.
> 
> When XFS shows global number of cached objects in count_objects(),
> shrinker also tryes to shrink the same percent of global objects,
> as for other memcg shrinkers. So, when you have small memcg
> with 128Mb memory allowed and small number of tasks related to it,
> you may meet 1Gb of cached objects, which were queued by another
> big cgroup. So, the small cgroup may shrink number of objects of
> size more, then its own. It's not fair. That's all I'm worry in
> this message.
>  
>> The memcg accounting, OTOH, is completely divorced from the
>> shrinker, so if it's still got too much slab memory accounted to it,
>> it will simply run the shrinker again and do some more memory
>> reclaim.
> 
> This message is not about OOM, it's about imbalance. See above.
> 
>> XFS does this for IO efficiency purposes, not because it's ideal
>> behaviour for memcgs. If we start doing exact memcg-only inode
>> reclaim, we're going back to the bad old days where inode reclaim
>> causes really nasty small random write storms that essentially
>> starve the storage subsystem of all other IO for seconds at a time.
>> That is a *far worse problem* from a system performance POV than
>> having the memcg memory reclaim run a couple more shrinker passes
>> and do a little more work....
> 
> Ok, this is a point.
> 
>>> Is there a problem? This is what my patch about.
>>
>> You've described some theoretical issue, but not described any user
>> visible or performance impacting behaviour that users actually care
>> about. What reproducable, observable behaviour does it fix/change?
> 
> Strange question. Do you fix problems only when you meet a reproducable
> BUG()? This is the kernel, and many problem may be racy. But this
> does not mean, it's prohibited to discuss about them.
> 
>>>> So, is there a problem you are actually trying to fix, or is this
>>>> simply a "I don't understand how the superblock shrinkers work,
>>>> please explain" patch?
>>>
>>> I work on some shrinker changes, and just want to understand they don't
>>> touch anything. 
>>
>> Oh, goody, another person about to learn the hard way that shrinkers
>> are far more varied and complex than page reclaim :P
> 
> It may be a surprise, but I have to say, that all memcg-aware shrinkers
> are based on list_lrus. XFS is exception. So, your words are not about
> shrinkers in general, just about XFS.

Just to clarify. I personally do not care about this problem. Consider
this as RFC/possible error report. If you are not interested in this,
let's stop the discussion.

Kirill

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-19 11:06           ` Kirill Tkhai
  2018-03-19 11:25             ` Kirill Tkhai
@ 2018-03-20  0:18             ` Dave Chinner
  2018-03-20 13:15               ` Kirill Tkhai
  1 sibling, 1 reply; 22+ messages in thread
From: Dave Chinner @ 2018-03-20  0:18 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On Mon, Mar 19, 2018 at 02:06:01PM +0300, Kirill Tkhai wrote:
> On 17.03.2018 00:39, Dave Chinner wrote:
> > On Fri, Mar 16, 2018 at 11:55:30AM +0300, Kirill Tkhai wrote:
> >> On 16.03.2018 02:03, Dave Chinner wrote:
> >>> On Thu, Mar 15, 2018 at 10:28:43PM +0300, Kirill Tkhai wrote:
> >>>> On 15.03.2018 20:49, Michal Hocko wrote:
> >>>>> On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
> >>>>>> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
> >>>>>> So, it's called for memcg reclaim too, e.g. this list is shrinked
> >>>>>> disproportionality to another lists.
> >>>>>>
> >>>>>> This looks confusing, so I'm reporting about this.
> >>>>>> Consider this patch as RFC.
> >>>>>
> >>>>> Could you be more specific about the problem you are trying to solve?
> >>>>> Because we do skip shrinkers which are not memcg aware by
> >>>>> shrink_slab:
> >>>>> 		/*
> >>>>> 		 * If kernel memory accounting is disabled, we ignore
> >>>>> 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
> >>>>> 		 * passing NULL for memcg.
> >>>>> 		 */
> >>>>> 		if (memcg_kmem_enabled() &&
> >>>>> 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
> >>>>> 			continue;
> >>>>>
> >>>>> Or am I missing something?
> >>>>
> >>>> sb->s_op->nr_cached_objects is a sub-method of generic super_cache_count().
> >>>> super_cache_count() is owned and only called by superblock's shrinker,
> >>>> which does have SHRINKER_MEMCG_AWARE flag.
> >>>
> >>> Xfs inodes are accounted to memcgs when they are allocated.  All the
> >>> memcg reclaim stuff is done at the VFS inode cache level - all the
> >>> XFS inode cache shrinker does is clean up inodes that are not
> >>> referenced by the VFS inode cache because the memcg aware reclaim
> >>> has already freed them.
> >>>
> >>> i.e. what the XFS inode cache is doing is perfectly reasonable -
> >>> memcg aware inode reclaim is occuring at the VFS level, but on XFS
> >>> that does not free the inodes as they are still referenced
> >>> internally by XFS. However, once the inode is removed from the VFS
> >>> LRU, all memcg information about the inode is destroyed, so there's
> >>> nothing in the XFS layers that cares about memcgs.
> >>
> >> So, after inode is removed from LRU, memory still remains accounted
> >> to memcg till the time they are actually freed. I personally don't
> >> care, just to mention.
> >>  
> >>> Hence when the XFS inode shrinker then called to run a
> >>> garbage collection pass on unreferenced inodes - the inodes that
> >>> are now unreferenced in the memcg due to the VFS inode shrinker pass
> >>> - it frees inodes regardless of the memcg context it was called from
> >>> because that information is no longer present in the inode cache.
> >>> Hence we just ignore memcgs at this level.
> >>
> >> But xfs_fs_free_cached_objects() returns number of these freed object
> >> as result to super_cache_scan(), so shrinker interprets them as related
> >> to a memcg, while they may be related to another memcg. This introduces
> >> a disproportion relatively to another shrinkers called to memcg.
> > 
> > In what way? All memcgs see tha same values from the backing cache
> > and so try to do the same amount of scanning work. The shrinker
> > accounting simply doesn't care where the objects are scanned from,
> > as long as it comes from the same place as the calculation of the
> > number of objects in the cache it's about to scan.
> 
> shrinker->count_objects() result is used to count number of objects,
> do_shrink_slab() should shrink during the call:
> 
>         freeable = shrinker->count_objects(shrinker, shrinkctl);
> 
> Then shrinker takes part of this value:
> 
>         delta = freeable >> priority;
>         delta *= 4;
>         do_div(delta, shrinker->seeks);
> 
> This is a number, the shrinker tries to shrink during the call.
> Let the priority is DEF_PRIORITY. Then, shrinker try to shrink
> freeable*4/(shrinker->seeks * 2^DEF_PRIORITY) enteties from every
> shrinker. Equal percent of every memcg shrinker.
> 
> When XFS shows global number of cached objects in count_objects(),
> shrinker also tryes to shrink the same percent of global objects,
> as for other memcg shrinkers. So, when you have small memcg
> with 128Mb memory allowed and small number of tasks related to it,
> you may meet 1Gb of cached objects, which were queued by another
> big cgroup. So, the small cgroup may shrink number of objects of
> size more, then its own. It's not fair. That's all I'm worry in
> this message.

Actually, it is fair, because:

        /* proportion the scan between the caches */
        dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
        inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
        fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);

This means that if the number of objects in the memcg aware VFS
caches are tiny compared to the global XFS inode cache, then they
only get a tiny amount of the total scanning done by the shrinker.

And because all of the memcg shrinkers walk the global inode cache,
the overall scanning is shared between all memcgs under memory
pressure evenly and so none are unfairly penalised by having to
scan the global inode cache to trigger final garbage collection.

If you just look at a single memcg and shrinker in isolation, then
you are missing the forest for the trees...

> > You've described some theoretical issue, but not described any user
> > visible or performance impacting behaviour that users actually care
> > about. What reproducable, observable behaviour does it fix/change?
> 
> Strange question. Do you fix problems only when you meet a reproducable
> BUG()? This is the kernel, and many problem may be racy. But this
> does not mean, it's prohibited to discuss about them.

That's a bit extreme.

I expect people who are trying to fix bugs in complex subsystems
to be able to explain what the bug is they are fixing, what impact
it has and how they observed/measured the impact. Especially for
somethign as critical as shrinker infrastructure - if we screw up the
calculations we'll break systems everywhere and when we have to fix
those bugs we need to know why those changes were made. That means
the reasons for making the change (i.e. what problem was being
fixed) needs to be documented in the commit message.

> >>> So, is there a problem you are actually trying to fix, or is this
> >>> simply a "I don't understand how the superblock shrinkers work,
> >>> please explain" patch?
> >>
> >> I work on some shrinker changes, and just want to understand they don't
> >> touch anything. 
> > 
> > Oh, goody, another person about to learn the hard way that shrinkers
> > are far more varied and complex than page reclaim :P
> 
> It may be a surprise, but I have to say, that all memcg-aware shrinkers
> are based on list_lrus. XFS is exception. So, your words are not about
> shrinkers in general, just about XFS.

Speaking as the original author of the list_lru infrastructure for
shrinkers, you've got that all back to front. The list_lru was
developed specifically for XFS scalibilty and performance reasons
i.e. SHRINKER_NUMA_AWARE.

The memcg stuff was tacked onto the side of the list-lrus later on
as it was a convenient abstraction to hide the bogosities of memcg
accounting and reclaim from the rest of the mm subsystem.  IOWs, the
memcg infrastructure is the exception case here, not XFS....

Cheers,

Dave.
-- 
Dave Chinner
david@fromorbit.com

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-20  0:18             ` Dave Chinner
@ 2018-03-20 13:15               ` Kirill Tkhai
  2018-03-20 14:34                 ` Dave Chinner
  0 siblings, 1 reply; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-20 13:15 UTC (permalink / raw)
  To: Dave Chinner; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On 20.03.2018 03:18, Dave Chinner wrote:
> On Mon, Mar 19, 2018 at 02:06:01PM +0300, Kirill Tkhai wrote:
>> On 17.03.2018 00:39, Dave Chinner wrote:
>>> On Fri, Mar 16, 2018 at 11:55:30AM +0300, Kirill Tkhai wrote:
>>>> On 16.03.2018 02:03, Dave Chinner wrote:
>>>>> On Thu, Mar 15, 2018 at 10:28:43PM +0300, Kirill Tkhai wrote:
>>>>>> On 15.03.2018 20:49, Michal Hocko wrote:
>>>>>>> On Thu 15-03-18 18:01:34, Kirill Tkhai wrote:
>>>>>>>> xfs_reclaim_inodes_count(XFS_M(sb)) does not care about memcg.
>>>>>>>> So, it's called for memcg reclaim too, e.g. this list is shrinked
>>>>>>>> disproportionality to another lists.
>>>>>>>>
>>>>>>>> This looks confusing, so I'm reporting about this.
>>>>>>>> Consider this patch as RFC.
>>>>>>>
>>>>>>> Could you be more specific about the problem you are trying to solve?
>>>>>>> Because we do skip shrinkers which are not memcg aware by
>>>>>>> shrink_slab:
>>>>>>> 		/*
>>>>>>> 		 * If kernel memory accounting is disabled, we ignore
>>>>>>> 		 * SHRINKER_MEMCG_AWARE flag and call all shrinkers
>>>>>>> 		 * passing NULL for memcg.
>>>>>>> 		 */
>>>>>>> 		if (memcg_kmem_enabled() &&
>>>>>>> 		    !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE))
>>>>>>> 			continue;
>>>>>>>
>>>>>>> Or am I missing something?
>>>>>>
>>>>>> sb->s_op->nr_cached_objects is a sub-method of generic super_cache_count().
>>>>>> super_cache_count() is owned and only called by superblock's shrinker,
>>>>>> which does have SHRINKER_MEMCG_AWARE flag.
>>>>>
>>>>> Xfs inodes are accounted to memcgs when they are allocated.  All the
>>>>> memcg reclaim stuff is done at the VFS inode cache level - all the
>>>>> XFS inode cache shrinker does is clean up inodes that are not
>>>>> referenced by the VFS inode cache because the memcg aware reclaim
>>>>> has already freed them.
>>>>>
>>>>> i.e. what the XFS inode cache is doing is perfectly reasonable -
>>>>> memcg aware inode reclaim is occuring at the VFS level, but on XFS
>>>>> that does not free the inodes as they are still referenced
>>>>> internally by XFS. However, once the inode is removed from the VFS
>>>>> LRU, all memcg information about the inode is destroyed, so there's
>>>>> nothing in the XFS layers that cares about memcgs.
>>>>
>>>> So, after inode is removed from LRU, memory still remains accounted
>>>> to memcg till the time they are actually freed. I personally don't
>>>> care, just to mention.
>>>>  
>>>>> Hence when the XFS inode shrinker then called to run a
>>>>> garbage collection pass on unreferenced inodes - the inodes that
>>>>> are now unreferenced in the memcg due to the VFS inode shrinker pass
>>>>> - it frees inodes regardless of the memcg context it was called from
>>>>> because that information is no longer present in the inode cache.
>>>>> Hence we just ignore memcgs at this level.
>>>>
>>>> But xfs_fs_free_cached_objects() returns number of these freed object
>>>> as result to super_cache_scan(), so shrinker interprets them as related
>>>> to a memcg, while they may be related to another memcg. This introduces
>>>> a disproportion relatively to another shrinkers called to memcg.
>>>
>>> In what way? All memcgs see tha same values from the backing cache
>>> and so try to do the same amount of scanning work. The shrinker
>>> accounting simply doesn't care where the objects are scanned from,
>>> as long as it comes from the same place as the calculation of the
>>> number of objects in the cache it's about to scan.
>>
>> shrinker->count_objects() result is used to count number of objects,
>> do_shrink_slab() should shrink during the call:
>>
>>         freeable = shrinker->count_objects(shrinker, shrinkctl);
>>
>> Then shrinker takes part of this value:
>>
>>         delta = freeable >> priority;
>>         delta *= 4;
>>         do_div(delta, shrinker->seeks);
>>
>> This is a number, the shrinker tries to shrink during the call.
>> Let the priority is DEF_PRIORITY. Then, shrinker try to shrink
>> freeable*4/(shrinker->seeks * 2^DEF_PRIORITY) enteties from every
>> shrinker. Equal percent of every memcg shrinker.
>>
>> When XFS shows global number of cached objects in count_objects(),
>> shrinker also tryes to shrink the same percent of global objects,
>> as for other memcg shrinkers. So, when you have small memcg
>> with 128Mb memory allowed and small number of tasks related to it,
>> you may meet 1Gb of cached objects, which were queued by another
>> big cgroup. So, the small cgroup may shrink number of objects of
>> size more, then its own. It's not fair. That's all I'm worry in
>> this message.
> 
> Actually, it is fair, because:
> 
>         /* proportion the scan between the caches */
>         dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
>         inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
>         fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
> 
> This means that if the number of objects in the memcg aware VFS
> caches are tiny compared to the global XFS inode cache, then they
> only get a tiny amount of the total scanning done by the shrinker.

This is just wrong. If you have two memcgs, the first is charged
by xfs dentries and inodes, while the second is not charged by xfs
dentries and inodes, the second will response for xfs shrink as well
as the first.

When we call shrink xfs partition of the second memcg, total_objects
in super_cache_count() will consist only of cached objects.

Then, we will call super_cache_scan() in the loop, and 

total_objects = dentries + inodes + fs_objects + 1 = fs_objects
s_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects) ~ almost equal to sc->nr_to_scan.

So, if the first memcg is huge and it's charged 10GB to cached objects,
the second memcg will shrink signify part of this objects (depending
on do_shrink_slab() priority) in case of reclaim coming.

> And because all of the memcg shrinkers walk the global inode cache,
> the overall scanning is shared between all memcgs under memory
> pressure evenly and so none are unfairly penalised by having to
> scan the global inode cache to trigger final garbage collection.

So, if there is single memcg using xfs, the rest of system is
responsible for its shrinking. Completely unfair.
Isn't this easy to understand?

> If you just look at a single memcg and shrinker in isolation, then
> you are missing the forest for the trees...
> 
>>> You've described some theoretical issue, but not described any user
>>> visible or performance impacting behaviour that users actually care
>>> about. What reproducable, observable behaviour does it fix/change?
>>
>> Strange question. Do you fix problems only when you meet a reproducable
>> BUG()? This is the kernel, and many problem may be racy. But this
>> does not mean, it's prohibited to discuss about them.
> 
> That's a bit extreme.
> 
> I expect people who are trying to fix bugs in complex subsystems
> to be able to explain what the bug is they are fixing, what impact
> it has and how they observed/measured the impact. Especially for
> somethign as critical as shrinker infrastructure - if we screw up the
> calculations we'll break systems everywhere and when we have to fix
> those bugs we need to know why those changes were made. That means
> the reasons for making the change (i.e. what problem was being
> fixed) needs to be documented in the commit message.
> 
>>>>> So, is there a problem you are actually trying to fix, or is this
>>>>> simply a "I don't understand how the superblock shrinkers work,
>>>>> please explain" patch?
>>>>
>>>> I work on some shrinker changes, and just want to understand they don't
>>>> touch anything. 
>>>
>>> Oh, goody, another person about to learn the hard way that shrinkers
>>> are far more varied and complex than page reclaim :P
>>
>> It may be a surprise, but I have to say, that all memcg-aware shrinkers
>> are based on list_lrus. XFS is exception. So, your words are not about
>> shrinkers in general, just about XFS.
> 
> Speaking as the original author of the list_lru infrastructure for
> shrinkers, you've got that all back to front. The list_lru was
> developed specifically for XFS scalibilty and performance reasons
> i.e. SHRINKER_NUMA_AWARE.
> 
> The memcg stuff was tacked onto the side of the list-lrus later on
> as it was a convenient abstraction to hide the bogosities of memcg
> accounting and reclaim from the rest of the mm subsystem.  IOWs, the
> memcg infrastructure is the exception case here, not XFS....

I don't reduce someone's merits. And this patch is RFC about the
unfairness, I described. If xfs people are not interested in this
theme, then I am not interested even more.

Kirill

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-20 13:15               ` Kirill Tkhai
@ 2018-03-20 14:34                 ` Dave Chinner
  2018-03-21 16:15                   ` Kirill Tkhai
  0 siblings, 1 reply; 22+ messages in thread
From: Dave Chinner @ 2018-03-20 14:34 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On Tue, Mar 20, 2018 at 04:15:16PM +0300, Kirill Tkhai wrote:
> On 20.03.2018 03:18, Dave Chinner wrote:
> > On Mon, Mar 19, 2018 at 02:06:01PM +0300, Kirill Tkhai wrote:
> >> On 17.03.2018 00:39, Dave Chinner wrote:
> > Actually, it is fair, because:
> > 
> >         /* proportion the scan between the caches */
> >         dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
> >         inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
> >         fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
> > 
> > This means that if the number of objects in the memcg aware VFS
> > caches are tiny compared to the global XFS inode cache, then they
> > only get a tiny amount of the total scanning done by the shrinker.
> 
> This is just wrong. If you have two memcgs, the first is charged
> by xfs dentries and inodes, while the second is not charged by xfs
> dentries and inodes, the second will response for xfs shrink as well
> as the first.

That makes no sense to me. Shrinkers are per-sb, so they only
shrink per-filesystem dentries and inodes. If your memcgs are on
different filesystems, then they'll behave according to the size of
that filesystem's caches, not the cache of some other filesystem.

> When we call shrink xfs partition of the second memcg, total_objects
> in super_cache_count() will consist only of cached objects.
> 
> Then, we will call super_cache_scan() in the loop, and 
> 
> total_objects = dentries + inodes + fs_objects + 1 = fs_objects
> s_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects) ~ almost equal to sc->nr_to_scan.
> 
> So, if the first memcg is huge and it's charged 10GB to cached objects,
> the second memcg will shrink signify part of this objects (depending
> on do_shrink_slab() priority) in case of reclaim coming.

Do we really care which memcg does the global reclaim work as long
as it all gets done in a timely manner? The fact is that when there
is global memory pressure, shrinkers from every memcg will be doing
work all the time and so it really does not matter which shrinker
context does what work. In such cases, memory reclaim is all about
bulk throughput, and we trade of instantenous fairness for overall
fairness and higher bulk throughput.

And, well, the bulk of XFS inode reclaim is not done by the shrinker
contexts - memcg or otherwise - it's done by an asycnhronous,
non-blocking per-filesystem background work which is kicked by a
shrinker scan being run on that filesystem . The only reason we keep
the shrinkers doing direct work is to throttle direct reclaim to the
overall background inode reclaim rate. It's hardly fair for the
filesystem to automatically do the work the memcg should be doing
and doing it faster than the memcg reclaim can do it, is it?

FWIW, inodes are RCU objects, so they aren't freed until RCU grace
periods expire and that means memcg reclaim does not actually free
inodes immediately. They are always delayed to some time after the
memcg shrinker has run and reported it freed those objects.  IOWs,
it really doesn't matter which memcg context does the "freeing"
work, because the freeing of inode memory in any given memcg
actually occurs some time later from callbacks run in a global,
non-memcg aware context....

> > And because all of the memcg shrinkers walk the global inode cache,
> > the overall scanning is shared between all memcgs under memory
> > pressure evenly and so none are unfairly penalised by having to
> > scan the global inode cache to trigger final garbage collection.
> 
> So, if there is single memcg using xfs, the rest of system is
> responsible for its shrinking. Completely unfair.

If there is only one memcg that is generating internal memory
pressure, then only that memcg will be running the shrinker and
reclaiming dentries, inodes and XFS inodes from that memcg. Nothing
else will be running reclaim and hence only the memcg shrinker will
be doing reclaim work. I don't see where you get the "rest of the
system is responsible for it's shrinking" from - that just isn't the
way the shrinker infrastructure works, regardless of whether XFS is
involved or not.

> Isn't this easy to understand?

First rule of shrinkers: Shrinkers *aren't easy to understand.*
Second rule of shrinkers: See the first rule.

Cheers,

Dave.
-- 
Dave Chinner
david@fromorbit.com

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-20 14:34                 ` Dave Chinner
@ 2018-03-21 16:15                   ` Kirill Tkhai
  2018-03-22  5:01                     ` Dave Chinner
  0 siblings, 1 reply; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-21 16:15 UTC (permalink / raw)
  To: Dave Chinner; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On 20.03.2018 17:34, Dave Chinner wrote:
> On Tue, Mar 20, 2018 at 04:15:16PM +0300, Kirill Tkhai wrote:
>> On 20.03.2018 03:18, Dave Chinner wrote:
>>> On Mon, Mar 19, 2018 at 02:06:01PM +0300, Kirill Tkhai wrote:
>>>> On 17.03.2018 00:39, Dave Chinner wrote:
>>> Actually, it is fair, because:
>>>
>>>         /* proportion the scan between the caches */
>>>         dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
>>>         inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
>>>         fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
>>>
>>> This means that if the number of objects in the memcg aware VFS
>>> caches are tiny compared to the global XFS inode cache, then they
>>> only get a tiny amount of the total scanning done by the shrinker.
>>
>> This is just wrong. If you have two memcgs, the first is charged
>> by xfs dentries and inodes, while the second is not charged by xfs
>> dentries and inodes, the second will response for xfs shrink as well
>> as the first.
> 
> That makes no sense to me. Shrinkers are per-sb, so they only
> shrink per-filesystem dentries and inodes. If your memcgs are on
> different filesystems, then they'll behave according to the size of
> that filesystem's caches, not the cache of some other filesystem.

But this break the isolation purpose of memcg. When someone places
different services in different pairs of memcg and cpu cgroup, he
wants do not do foreign work.

>> When we call shrink xfs partition of the second memcg, total_objects
>> in super_cache_count() will consist only of cached objects.
>>
>> Then, we will call super_cache_scan() in the loop, and 
>>
>> total_objects = dentries + inodes + fs_objects + 1 = fs_objects
>> s_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects) ~ almost equal to sc->nr_to_scan.
>>
>> So, if the first memcg is huge and it's charged 10GB to cached objects,
>> the second memcg will shrink signify part of this objects (depending
>> on do_shrink_slab() priority) in case of reclaim coming.
> 
> Do we really care which memcg does the global reclaim work as long
> as it all gets done in a timely manner? The fact is that when there
> is global memory pressure, shrinkers from every memcg will be doing
> work all the time and so it really does not matter which shrinker
> context does what work. In such cases, memory reclaim is all about
> bulk throughput, and we trade of instantenous fairness for overall
> fairness and higher bulk throughput.

There is container workload, when services are executed in different
memcg to controll their resources. If there are database and mail server
in two containers, you want to make them use their own resources,
and don't want to make database shrink mail server inodes and vise versa.
You just want to allocate resources enough for both of them, and make
them not affect each other.
 
> And, well, the bulk of XFS inode reclaim is not done by the shrinker
> contexts - memcg or otherwise - it's done by an asycnhronous,
> non-blocking per-filesystem background work which is kicked by a
> shrinker scan being run on that filesystem . The only reason we keep
> the shrinkers doing direct work is to throttle direct reclaim to the
> overall background inode reclaim rate. It's hardly fair for the
> filesystem to automatically do the work the memcg should be doing
> and doing it faster than the memcg reclaim can do it, is it?

But is there another solution? Something like work queue or kthread.

> FWIW, inodes are RCU objects, so they aren't freed until RCU grace
> periods expire and that means memcg reclaim does not actually free
> inodes immediately. They are always delayed to some time after the
> memcg shrinker has run and reported it freed those objects.  IOWs,
> it really doesn't matter which memcg context does the "freeing"
> work, because the freeing of inode memory in any given memcg
> actually occurs some time later from callbacks run in a global,
> non-memcg aware context....
> 
>>> And because all of the memcg shrinkers walk the global inode cache,
>>> the overall scanning is shared between all memcgs under memory
>>> pressure evenly and so none are unfairly penalised by having to
>>> scan the global inode cache to trigger final garbage collection.
>>
>> So, if there is single memcg using xfs, the rest of system is
>> responsible for its shrinking. Completely unfair.
> 
> If there is only one memcg that is generating internal memory
> pressure, then only that memcg will be running the shrinker and
> reclaiming dentries, inodes and XFS inodes from that memcg. Nothing
> else will be running reclaim and hence only the memcg shrinker will
> be doing reclaim work. I don't see where you get the "rest of the
> system is responsible for it's shrinking" from - that just isn't the
> way the shrinker infrastructure works, regardless of whether XFS is
> involved or not.

Every shrinker is called for every memcg. So, if any other memcg meets
reclaim, it will also shrink these cached objects.
 
>> Isn't this easy to understand?
> 
> First rule of shrinkers: Shrinkers *aren't easy to understand.*
> Second rule of shrinkers: See the first rule.

Kirill

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-21 16:15                   ` Kirill Tkhai
@ 2018-03-22  5:01                     ` Dave Chinner
  2018-03-22 16:52                       ` Kirill Tkhai
  0 siblings, 1 reply; 22+ messages in thread
From: Dave Chinner @ 2018-03-22  5:01 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On Wed, Mar 21, 2018 at 07:15:14PM +0300, Kirill Tkhai wrote:
> On 20.03.2018 17:34, Dave Chinner wrote:
> > On Tue, Mar 20, 2018 at 04:15:16PM +0300, Kirill Tkhai wrote:
> >> On 20.03.2018 03:18, Dave Chinner wrote:
> >>> On Mon, Mar 19, 2018 at 02:06:01PM +0300, Kirill Tkhai wrote:
> >>>> On 17.03.2018 00:39, Dave Chinner wrote:
> >>> Actually, it is fair, because:
> >>>
> >>>         /* proportion the scan between the caches */
> >>>         dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
> >>>         inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
> >>>         fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
> >>>
> >>> This means that if the number of objects in the memcg aware VFS
> >>> caches are tiny compared to the global XFS inode cache, then they
> >>> only get a tiny amount of the total scanning done by the shrinker.
> >>
> >> This is just wrong. If you have two memcgs, the first is charged
> >> by xfs dentries and inodes, while the second is not charged by xfs
> >> dentries and inodes, the second will response for xfs shrink as well
> >> as the first.
> > 
> > That makes no sense to me. Shrinkers are per-sb, so they only
> > shrink per-filesystem dentries and inodes. If your memcgs are on
> > different filesystems, then they'll behave according to the size of
> > that filesystem's caches, not the cache of some other filesystem.
> 
> But this break the isolation purpose of memcg. When someone places
> different services in different pairs of memcg and cpu cgroup, he
> wants do not do foreign work.

Too bad. Filesystems *break memcg isolation all the time*.

Think about it. The filesystem journal is owned by the filesystem,
not the memcg that is writing a transaction to it.  If one memcg
does lots of metadata modifications, it can use all the journal
space and starve iitself and all other memcgs of journal space while
the filesystem does writeback to clear it out.

Another example: if one memcg is manipulating free space in a
particular allocation group, other memcgs that need to manipulate
space in those allocation groups will block and be prevented from
operating until the other memcg finishes it's work.

Another example: inode IO in XFS are done in clusters of 32. read or
write any inode in the cluster, and we read/write all the other
inodes in that cluster, too. Hence if we need to read an inode and
the cluster is busy because the filesystem journal needed flushing
or another inode in the cluster is being unlinked (e.g. by a process
in a different memcg), then the read in the first memcg will block
until whatever is being done on the buffer is complete. IOWs, even
at the physical on-disk inode level we violate memcg resource 
isolation principles.

I can go on, but I think you get the picture: Filesystems are full
of global structures whose access arbitration mechanisms
fundamentally break the concept of operational memcg isolation.

With that in mind, we really don't care that the shrinker does
global work and violate memcg isolation principles because we
violately them everywhere. IOWs, if we try to enforce memcg
isolation in the shrinker, then we can't guarantee forwards progress
in memory reclaim because we end up with multi-dimensional memcg
dependencies at the physical layers of the filesystem structure....

I don't expect people who know nothing about XFS or filesystems to
understand the complexity of the interactions we are dealing with
here. Everything is a compromise when it comes to the memory reclaim
code as tehre are so many corner cases we have to handle. In this
situation, perfect is the enemy of good....

Cheers,

Dave.
-- 
Dave Chinner
david@fromorbit.com

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-22  5:01                     ` Dave Chinner
@ 2018-03-22 16:52                       ` Kirill Tkhai
  2018-03-22 23:46                         ` Dave Chinner
  0 siblings, 1 reply; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-22 16:52 UTC (permalink / raw)
  To: Dave Chinner; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On 22.03.2018 08:01, Dave Chinner wrote:
> On Wed, Mar 21, 2018 at 07:15:14PM +0300, Kirill Tkhai wrote:
>> On 20.03.2018 17:34, Dave Chinner wrote:
>>> On Tue, Mar 20, 2018 at 04:15:16PM +0300, Kirill Tkhai wrote:
>>>> On 20.03.2018 03:18, Dave Chinner wrote:
>>>>> On Mon, Mar 19, 2018 at 02:06:01PM +0300, Kirill Tkhai wrote:
>>>>>> On 17.03.2018 00:39, Dave Chinner wrote:
>>>>> Actually, it is fair, because:
>>>>>
>>>>>         /* proportion the scan between the caches */
>>>>>         dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
>>>>>         inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
>>>>>         fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
>>>>>
>>>>> This means that if the number of objects in the memcg aware VFS
>>>>> caches are tiny compared to the global XFS inode cache, then they
>>>>> only get a tiny amount of the total scanning done by the shrinker.
>>>>
>>>> This is just wrong. If you have two memcgs, the first is charged
>>>> by xfs dentries and inodes, while the second is not charged by xfs
>>>> dentries and inodes, the second will response for xfs shrink as well
>>>> as the first.
>>>
>>> That makes no sense to me. Shrinkers are per-sb, so they only
>>> shrink per-filesystem dentries and inodes. If your memcgs are on
>>> different filesystems, then they'll behave according to the size of
>>> that filesystem's caches, not the cache of some other filesystem.
>>
>> But this break the isolation purpose of memcg. When someone places
>> different services in different pairs of memcg and cpu cgroup, he
>> wants do not do foreign work.
> 
> Too bad. Filesystems *break memcg isolation all the time*.
> 
> Think about it. The filesystem journal is owned by the filesystem,
> not the memcg that is writing a transaction to it.  If one memcg
> does lots of metadata modifications, it can use all the journal
> space and starve iitself and all other memcgs of journal space while
> the filesystem does writeback to clear it out.
> 
> Another example: if one memcg is manipulating free space in a
> particular allocation group, other memcgs that need to manipulate
> space in those allocation groups will block and be prevented from
> operating until the other memcg finishes it's work.
> 
> Another example: inode IO in XFS are done in clusters of 32. read or
> write any inode in the cluster, and we read/write all the other
> inodes in that cluster, too. Hence if we need to read an inode and
> the cluster is busy because the filesystem journal needed flushing
> or another inode in the cluster is being unlinked (e.g. by a process
> in a different memcg), then the read in the first memcg will block
> until whatever is being done on the buffer is complete. IOWs, even
> at the physical on-disk inode level we violate memcg resource 
> isolation principles.
> 
> I can go on, but I think you get the picture: Filesystems are full
> of global structures whose access arbitration mechanisms
> fundamentally break the concept of operational memcg isolation.
> 
> With that in mind, we really don't care that the shrinker does
> global work and violate memcg isolation principles because we
> violately them everywhere. IOWs, if we try to enforce memcg
> isolation in the shrinker, then we can't guarantee forwards progress
> in memory reclaim because we end up with multi-dimensional memcg
> dependencies at the physical layers of the filesystem structure....

Here is the problem I'm solving: https://lkml.org/lkml/2018/3/21/365.
Current shrinker is not scalable. Then there are many memcg and mounts,
the time of iteration shrink_slab() in case of global reclaim can
take much time. There is times of shrink_slab() by the link. A node
with 200 containers may waste 4 seconds on global reclaim just to
iterate over all shrinkers for all cgroups, call shrinker::count_objects()
and receive 0 zero objects.

Can't we call shrink of shared objects only for top memcg? Something like
this:

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 8fcd9f8d7390..13429366c276 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -2569,6 +2569,10 @@ static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
 			}
 		} while ((memcg = mem_cgroup_iter(root, memcg, &reclaim)));
 
+		/* Called only for top memcg */
+		shrink_shared_objects();
+
+
 		if (global_reclaim(sc))
 			shrink_slab(sc->gfp_mask, pgdat->node_id, NULL,
 				    sc->priority);

"Top memcg" means a memcg, which meets reclaim. It is root_mem_cgroup in case of
global reclaim; and it's task's current memcg if there is memcg reclaim.

> I don't expect people who know nothing about XFS or filesystems to
> understand the complexity of the interactions we are dealing with
> here. Everything is a compromise when it comes to the memory reclaim
> code as tehre are so many corner cases we have to handle. In this
> situation, perfect is the enemy of good...

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-22 16:52                       ` Kirill Tkhai
@ 2018-03-22 23:46                         ` Dave Chinner
  2018-03-23 12:39                           ` Kirill Tkhai
  0 siblings, 1 reply; 22+ messages in thread
From: Dave Chinner @ 2018-03-22 23:46 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On Thu, Mar 22, 2018 at 07:52:37PM +0300, Kirill Tkhai wrote:
> Here is the problem I'm solving: https://lkml.org/lkml/2018/3/21/365.

Oh, finally you tell me what the problem is that you're trying to
solve. I *asked this several times* and got no response. Thank you
for wasting so much of my time.

> Current shrinker is not scalable. Then there are many memcg and mounts,
> the time of iteration shrink_slab() in case of global reclaim can
> take much time. There is times of shrink_slab() by the link. A node
> with 200 containers may waste 4 seconds on global reclaim just to
> iterate over all shrinkers for all cgroups, call shrinker::count_objects()
> and receive 0 zero objects.

So, your problem is the way the memcgs were tacked onto the side
of the list_lru infrastructure and are iterated, which has basically
nothing to do with the way the low level XFS inode shrinker behaves.

/me looks at the patches

/me shudders at the thought of external "cache has freeable items"
control for the shrinking of vfs caches.

Biggest problem I see with this is the scope for coherency bugs ini
the "memcg shrinker has freeable items" tracking. If that happens,
there's no way of getting that memcg to run it's shrinker ever
again. That seems very, very fragile and likely to be an endless
source of OOM bugs. The whole point of the shrinker polling
infrastructure is that it is not susceptible to this sort of bug.

Really, the problem is that there's no separate list of memcg aware
shrinkers, so every registered shrinker has to be iterated just to
find the one shrinker that is memcg aware.

So why not just do the simple thing which is create a separate
"memcg-aware" shrinker list (i.e. create shrinker_list_memcg
alongside shrinker_list) and only iterate the shrinker_list_memcg
when a memcg is passed to shrink_slab()?

That means we'll only run 2 shrinkers per memcg at most (sueprblock
and working set) per memcg reclaim call. That's a simple 10-20 line
change, not a whole mess of new code and issues...

> Can't we call shrink of shared objects only for top memcg? Something like
> this:

What's a "shared object", and how is it any different to a normal
slab cache object?

CHeers,

Dave.
-- 
Dave Chinner
david@fromorbit.com

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-22 23:46                         ` Dave Chinner
@ 2018-03-23 12:39                           ` Kirill Tkhai
  2018-03-26  2:37                             ` Dave Chinner
  0 siblings, 1 reply; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-23 12:39 UTC (permalink / raw)
  To: Dave Chinner; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On 23.03.2018 02:46, Dave Chinner wrote:
> On Thu, Mar 22, 2018 at 07:52:37PM +0300, Kirill Tkhai wrote:
>> Here is the problem I'm solving: https://lkml.org/lkml/2018/3/21/365.
> 
> Oh, finally you tell me what the problem is that you're trying to
> solve. I *asked this several times* and got no response. Thank you
> for wasting so much of my time.
> 
>> Current shrinker is not scalable. Then there are many memcg and mounts,
>> the time of iteration shrink_slab() in case of global reclaim can
>> take much time. There is times of shrink_slab() by the link. A node
>> with 200 containers may waste 4 seconds on global reclaim just to
>> iterate over all shrinkers for all cgroups, call shrinker::count_objects()
>> and receive 0 zero objects.
> 
> So, your problem is the way the memcgs were tacked onto the side
> of the list_lru infrastructure and are iterated, which has basically
> nothing to do with the way the low level XFS inode shrinker behaves.
> 
> /me looks at the patches
> 
> /me shudders at the thought of external "cache has freeable items"
> control for the shrinking of vfs caches.
> 
> Biggest problem I see with this is the scope for coherency bugs ini
> the "memcg shrinker has freeable items" tracking. If that happens,
> there's no way of getting that memcg to run it's shrinker ever
> again. That seems very, very fragile and likely to be an endless
> source of OOM bugs. The whole point of the shrinker polling
> infrastructure is that it is not susceptible to this sort of bug.
> 
> Really, the problem is that there's no separate list of memcg aware
> shrinkers, so every registered shrinker has to be iterated just to
> find the one shrinker that is memcg aware.

I don't think the logic is difficult. There are generic rules,
and the only task is to teach them memcg-aware shrinkers. Currently,
they are only super block and workingsets shrinkers, and both of
them are based on generic list_lru infrastructure. Shrinker-related
bit is also cleared in generic code (shrink_slab()) only, and
the algorhythm doesn't allow to clear it without double check.
The only principle modification I'm thinking about is we should
clear the bit only when the shrinker is called with maximum
parameters: priority and GFP.

There are a lot of performance improving synchronizations in kernel,
and they had been refused, the kernel would have remained in the age
of big kernel lock.

> So why not just do the simple thing which is create a separate
> "memcg-aware" shrinker list (i.e. create shrinker_list_memcg
> alongside shrinker_list) and only iterate the shrinker_list_memcg
> when a memcg is passed to shrink_slab()?
> 
> That means we'll only run 2 shrinkers per memcg at most (sueprblock
> and working set) per memcg reclaim call. That's a simple 10-20 line
> change, not a whole mess of new code and issues...

It was the first optimization, which came to my head, by there is no
almost a performance profit, since memcg-aware shrinkers still be called
per every memcg, and they are the biggest part of shrinkers in the system.
 
>> Can't we call shrink of shared objects only for top memcg? Something like
>> this:
> 
> What's a "shared object", and how is it any different to a normal
> slab cache object?

Sorry, it's erratum. I'm speaking about cached objects. I mean something like
the below. The patch makes cached objects be cleared outside the memcg iteration
cycle (it has not sense to call them for every memcg since cached object logic
just ignores memcg).

---
diff --git a/fs/super.c b/fs/super.c
index 0660083427fa..c7321c42ab1f 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -61,8 +61,8 @@ static unsigned long super_cache_scan(struct shrinker *shrink,
 	long	fs_objects = 0;
 	long	total_objects;
 	long	freed = 0;
-	long	dentries;
-	long	inodes;
+	long	dentries = 0;
+	long	inodes = 0;
 
 	sb = container_of(shrink, struct super_block, s_shrink);
 
@@ -76,19 +76,27 @@ static unsigned long super_cache_scan(struct shrinker *shrink,
 	if (!trylock_super(sb))
 		return SHRINK_STOP;
 
-	if (sb->s_op->nr_cached_objects)
-		fs_objects = sb->s_op->nr_cached_objects(sb, sc);
+	if (sc->cached) {
+		if (sb->s_op->nr_cached_objects) {
+			fs_objects = sb->s_op->nr_cached_objects(sb, sc);
+			if (!fs_objects)
+				fs_objects = 1;
+
+			sc->nr_to_scan = fs_objects + 1;
+			freed = sb->s_op->free_cached_objects(sb, sc);
+		}
+		goto unlock;
+	}
 
 	inodes = list_lru_shrink_count(&sb->s_inode_lru, sc);
 	dentries = list_lru_shrink_count(&sb->s_dentry_lru, sc);
-	total_objects = dentries + inodes + fs_objects + 1;
+	total_objects = dentries + inodes + 1;
 	if (!total_objects)
 		total_objects = 1;
 
 	/* proportion the scan between the caches */
 	dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
 	inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
-	fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
 
 	/*
 	 * prune the dcache first as the icache is pinned by it, then
@@ -101,12 +109,7 @@ static unsigned long super_cache_scan(struct shrinker *shrink,
 	freed = prune_dcache_sb(sb, sc);
 	sc->nr_to_scan = inodes + 1;
 	freed += prune_icache_sb(sb, sc);
-
-	if (fs_objects) {
-		sc->nr_to_scan = fs_objects + 1;
-		freed += sb->s_op->free_cached_objects(sb, sc);
-	}
-
+unlock:
 	up_read(&sb->s_umount);
 	return freed;
 }
@@ -127,11 +130,13 @@ static unsigned long super_cache_count(struct shrinker *shrink,
 	 * ensures the safety of call to list_lru_shrink_count() and
 	 * s_op->nr_cached_objects().
 	 */
-	if (sb->s_op && sb->s_op->nr_cached_objects)
-		total_objects = sb->s_op->nr_cached_objects(sb, sc);
-
-	total_objects += list_lru_shrink_count(&sb->s_dentry_lru, sc);
-	total_objects += list_lru_shrink_count(&sb->s_inode_lru, sc);
+	if (sc->cached) {
+		if (sb->s_op && sb->s_op->nr_cached_objects)
+			total_objects = sb->s_op->nr_cached_objects(sb, sc);
+	} else {
+		total_objects += list_lru_shrink_count(&sb->s_dentry_lru, sc);
+		total_objects += list_lru_shrink_count(&sb->s_inode_lru, sc);
+	}
 
 	total_objects = vfs_pressure_ratio(total_objects);
 	return total_objects;
diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h
index a3894918a436..c817173e19be 100644
--- a/include/linux/shrinker.h
+++ b/include/linux/shrinker.h
@@ -28,6 +28,7 @@ struct shrink_control {
 
 	/* current node being shrunk (for NUMA aware shrinkers) */
 	int nid;
+	u8 cached:1;
 
 	/* current memcg being shrunk (for memcg aware shrinkers) */
 	struct mem_cgroup *memcg;
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 8fcd9f8d7390..e117830f07fd 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -482,6 +482,33 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
 	return freed;
 }
 
+static void shrink_cached_slab(gfp_t gfp_mask, int nid, int priority)
+{
+	struct shrinker *shrinker;
+
+	if (!down_read_trylock(&shrinker_rwsem))
+		return;
+
+	list_for_each_entry(shrinker, &shrinker_list, list) {
+		struct shrink_control sc = {
+			.gfp_mask = gfp_mask,
+			.nid = nid,
+			.cached = 1,
+		};
+
+		if (!(shrinker->flags & SHRINKER_MEMCG_AWARE))
+			continue;
+		if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
+			sc.nid = 0;
+		if (rwsem_is_contended(&shrinker_rwsem))
+			break;
+
+		do_shrink_slab(&sc, shrinker, priority);
+	}
+
+	up_read(&shrinker_rwsem);
+}
+
 void drop_slab_node(int nid)
 {
 	unsigned long freed;
@@ -493,6 +520,8 @@ void drop_slab_node(int nid)
 		do {
 			freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
 		} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
+
+		shrink_cached_slab(GFP_KERNEL, nid, 0);
 	} while (freed > 10);
 }
 
@@ -2573,6 +2602,8 @@ static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
 			shrink_slab(sc->gfp_mask, pgdat->node_id, NULL,
 				    sc->priority);
 
+		shrink_cached_slab(sc->gfp_mask, pgdat->node_id, sc->priority);
+
 		if (reclaim_state) {
 			sc->nr_reclaimed += reclaim_state->reclaimed_slab;
 			reclaim_state->reclaimed_slab = 0;

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-23 12:39                           ` Kirill Tkhai
@ 2018-03-26  2:37                             ` Dave Chinner
  2018-03-26 11:16                               ` Kirill Tkhai
  0 siblings, 1 reply; 22+ messages in thread
From: Dave Chinner @ 2018-03-26  2:37 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On Fri, Mar 23, 2018 at 03:39:53PM +0300, Kirill Tkhai wrote:
> On 23.03.2018 02:46, Dave Chinner wrote:
> > On Thu, Mar 22, 2018 at 07:52:37PM +0300, Kirill Tkhai wrote:
> >> Here is the problem I'm solving: https://lkml.org/lkml/2018/3/21/365.
> > 
> > Oh, finally you tell me what the problem is that you're trying to
> > solve. I *asked this several times* and got no response. Thank you
> > for wasting so much of my time.
> > 
> >> Current shrinker is not scalable. Then there are many memcg and mounts,
> >> the time of iteration shrink_slab() in case of global reclaim can
> >> take much time. There is times of shrink_slab() by the link. A node
> >> with 200 containers may waste 4 seconds on global reclaim just to
> >> iterate over all shrinkers for all cgroups, call shrinker::count_objects()
> >> and receive 0 zero objects.
> > 
> > So, your problem is the way the memcgs were tacked onto the side
> > of the list_lru infrastructure and are iterated, which has basically
> > nothing to do with the way the low level XFS inode shrinker behaves.
> > 
> > /me looks at the patches
> > 
> > /me shudders at the thought of external "cache has freeable items"
> > control for the shrinking of vfs caches.
> > 
> > Biggest problem I see with this is the scope for coherency bugs ini
> > the "memcg shrinker has freeable items" tracking. If that happens,
> > there's no way of getting that memcg to run it's shrinker ever
> > again. That seems very, very fragile and likely to be an endless
> > source of OOM bugs. The whole point of the shrinker polling
> > infrastructure is that it is not susceptible to this sort of bug.
> > 
> > Really, the problem is that there's no separate list of memcg aware
> > shrinkers, so every registered shrinker has to be iterated just to
> > find the one shrinker that is memcg aware.
> 
> I don't think the logic is difficult. There are generic rules,
> and the only task is to teach them memcg-aware shrinkers. Currently,
> they are only super block and workingsets shrinkers, and both of
> them are based on generic list_lru infrastructure. Shrinker-related
> bit is also cleared in generic code (shrink_slab()) only, and
> the algorhythm doesn't allow to clear it without double check.
> The only principle modification I'm thinking about is we should
> clear the bit only when the shrinker is called with maximum
> parameters: priority and GFP.

Lots of "simple logic" combined together makes for a complex mass of
difficult to understand and debug code.

And, really, you're not suffering from a memcg problem - you're
suffering from a "there are thousands of shrinkers" scalability
issue because superblocks have per-superblock shrinker contexts and
you have thousands of mounted filesystems.

> There are a lot of performance improving synchronizations in kernel,
> and they had been refused, the kernel would have remained in the age
> of big kernel lock.

That's false equivalence and hyperbole. The shrinkers are not
limiting what every Linux user can do with their hardware. It's not
a fundamental architectural limitation.  These sorts of arguments
are not convincing - this is the second time I've told you this, so
please stick to technical arguments and drop the dramatic
"anti-progress" conspiracy theory bullshit.

> > So why not just do the simple thing which is create a separate
> > "memcg-aware" shrinker list (i.e. create shrinker_list_memcg
> > alongside shrinker_list) and only iterate the shrinker_list_memcg
> > when a memcg is passed to shrink_slab()?
> > 
> > That means we'll only run 2 shrinkers per memcg at most (sueprblock
> > and working set) per memcg reclaim call. That's a simple 10-20 line
> > change, not a whole mess of new code and issues...
> 
> It was the first optimization, which came to my head, by there is no
> almost a performance profit, since memcg-aware shrinkers still be called
> per every memcg, and they are the biggest part of shrinkers in the system.

Sure, but a polling algorithm is not a fundamental performance
limitation.

The problem here is that the memcg infrastructure has caused an
exponential explosion in shrinker scanning.

> >> Can't we call shrink of shared objects only for top memcg? Something like
> >> this:
> > 
> > What's a "shared object", and how is it any different to a normal
> > slab cache object?
> 
> Sorry, it's erratum. I'm speaking about cached objects. I mean something like
> the below. The patch makes cached objects be cleared outside the memcg iteration
> cycle (it has not sense to call them for every memcg since cached object logic
> just ignores memcg).

The cached flag seems like a hack to me. It does nothing to address
the number of shrinker callout calls (it actually increases them!),
just tries to hack around something you want a specific shrinker to
avoid doing.

I've asked *repeatedly* for a description of the actual workload
problems the XFS shrinker behaviour is causing you. In the absence
of any workload description, I'm simply going to NACK any changes
that try to work around this behaviour. The rest of this reply is
about shrinker infrastructure and (the lack of) memcg integration.

> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -482,6 +482,33 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
>  	return freed;
>  }
>  
> +static void shrink_cached_slab(gfp_t gfp_mask, int nid, int priority)
> +{
> +	struct shrinker *shrinker;
> +
> +	if (!down_read_trylock(&shrinker_rwsem))
> +		return;
> +
> +	list_for_each_entry(shrinker, &shrinker_list, list) {
> +		struct shrink_control sc = {
> +			.gfp_mask = gfp_mask,
> +			.nid = nid,
> +			.cached = 1,
> +		};
> +
> +		if (!(shrinker->flags & SHRINKER_MEMCG_AWARE))
> +			continue;
> +		if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
> +			sc.nid = 0;
> +		if (rwsem_is_contended(&shrinker_rwsem))
> +			break;
> +
> +		do_shrink_slab(&sc, shrinker, priority);
> +	}
> +
> +	up_read(&shrinker_rwsem);

Ok, so this shrinks /only/ non-memcg slabs. It's identical to
calling shrink_slab() with a null memcg. We already do this in all
the relevant memory reclaim paths when necessary....

> +}
> +
>  void drop_slab_node(int nid)
>  {
>  	unsigned long freed;
> @@ -493,6 +520,8 @@ void drop_slab_node(int nid)
>  		do {
>  			freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
>  		} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
> +
> +		shrink_cached_slab(GFP_KERNEL, nid, 0);
>  	} while (freed > 10);

... like here, where the inner loop always calls shrink_slab() with
a NULL memcg as it's first iteration. So your addition here is
basically a no-op - we've already run non-memcg cache reclaim by the
time your addition to run non-memcg cache reclaim is called. And
here:

> @@ -2573,6 +2602,8 @@ static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
>  			shrink_slab(sc->gfp_mask, pgdat->node_id, NULL,
>  				    sc->priority);
>  
> +		shrink_cached_slab(sc->gfp_mask, pgdat->node_id, sc->priority);
> +

You're adding unconditional global slab cache reclaim to a path that
already does this but only in the calling contexts where it global
slab reclaim is actually necessary. Doing unconditional global slab
reclaim here will cause regressions.

So, let's address the number of shrinker callouts from
drop_caches(). You're original example from here:

https://lkml.org/lkml/2018/3/21/365

| In case of global reclaim, a task has to iterate all over the memcgs
| and to call all the memcg-aware shrinkers for all of them. This means,
| the task has to visit 200 * 10 = 2000 shrinkers for every memcg,
| and since there are 2000 memcgs, the total calls of do_shrink_slab()
| are 2000 * 2000 = 4000000.

The loop is:

drop_slab()
  for_each_online_node(nid)
    drop_slab_node(nid);
      for each memcg including NULL
        shrink_slab(nid, memcg)

With 200 containers w/ 10 mounts X 10 memcgs each. That means we
have 2000 mounts and hence 2000 memcg aware superblock shrinkers.
we have 2000 memcgs, so each superblock has 2000 memcgs attached to
it. And then we have N nodes in the machine.

So drop_slab() will call drop_slab_node() N times, and drop_slab
node will iterate all the memcgs until there's no more to free.
Each memcg with then iterate all shrinkers to find the memcg aware
shrinkers, then do work.  IOWs, we actually have a minimum scan of N
* 2000 * 2000 calls to shrink_slab(), not just 2000 * 2000.

The thing is, memcg shrinker contexts are not NUMA aware. They use
the list array that was implemented for memory nodes and instead use
it for a memcg list array, indexed by memcg ID rather than node ID.
IOWs, memcgs are not numa aware, so they do not need to be called
for every single node in the system. Only shrinkers in a global
context (i.e. no memcg) that are NUMA aware need to be called for
every node.

The patchset you posted implemented that mapping by adding a bitmap
of memcg-internal shrinker dirty state and a global array that
points directly to the struct shrinker that the bitmap index is
associated with. This externalises the dirty tracking into the memcg
code, which as I've already said I don't like.

The real problem here is that the shrinker itself is supposed to
keep context specific call-to-call information. In the case of
NUMA-aware shrinkers, that's kept in the shrinker->nr_deferred[nid]
array.  That's dynamically allocated for numa aware shrinkers, but
all memcg's that are called for the given shrinker all smash their
state into shrinker->nr_deferred[nid] - they don't have their own
nr_deferred state.

This actually screws up the nr_deferred state for global reclaim,
and causes unexpected unfairness to memcg reclaim. i.e. Global,
non-memcg reclaim can defer pressure to all memcgs that shrinker is
called on for that node because the nr_deferred count is shared
based on nid. That means work we defer from one call to a memcg on
nid=0 will be applied to the next memcg recalim call on nid=0,
regardless of what it is. And if that first shrinker is then called
next on nid=1, it won't get it's nr_deferred from it's last call on
nid=0, it will get whatever is currently stored on nid=1.

IOWs, the memcg shrinker infrastructure itself violates the
principles of memcg isolation and fairness by completely screwing up
the call-to-call deferred work counts for individual memcgs and
global shrinker reclaim. If you're interested in fairness and memcg
isolation in reclaim, then this is the first problem you need to
solve.

IMO, What we should be doing here is something like this:

drop_slab()
{
  do {
    freed = shrink_slab_memcg()
    freed += shrink_slab_numa()
    freed += shrink_slab_simple()
  } while (freed < 10)
}

i.e. we set up a distinct separation of shrinker roles according to
the contexts in which they are called. e.g.

shrink_slab_memcg()
{
  for_each_memcg(memcg)
    for_each_shrinker(&memcg_shrinker_list) {
      nr_deferred = shrinker_get_memcg_deferred(shrinker, memcg)
      do_shrink_slab(shrinker, 0, memcg, &nr_deferred))
      shrinker_store_memcg_deferred(shrinker, memcg, nr_deferred)
    }
}

shrink_slab_numa()
{
  for_each_online_node(nid)
    for_each_shrinker(&numa_shrinker_list) {
      nr_deferred = shrinker_get_node_deferred(shrinker, nid)
      do_shrink_slab(shrinker, nid, NULL, &nr_deferred)
      shrinker_store_node_deferred(shrinker, nid, nr_deferred)
    }
}

shrink_slab_simple()
{
    for_each_shrinker(&simple_shrinker_list) {
      nr_deferred = shrinker_get_node_deferred(shrinker, 0)
      do_shrink_slab(shrinker, 0, NULL, &nr_deferred)
      shrinker_store_node_deferred(shrinker, 0, nr_deferred)
    }
}

[ Note: This implies a shrinker can be on multiple lists. That's
fine, we can already do concurrent calls into a shrinker from
different reclaim contexts. But in doing this, the "iterate shrinker
list to find memcg aware shrinkers" goes aware. Similarly with the
basic shrinkers - we stop hitting them for every node we scan in
global reclaim and hence putting lots more pressure on them because
they are being treated as "one cache per node" instead of a single
cache. ]

The simple and numa cases can share nr_deferred[0] as they currently
do because a shrinker will only ever be one or the other, but we
still need shrink_slab_memcg() to have per-memcg nr_deferred values
because simple shrinkers can still be MEMCG_AWARE.

This makes it simpler to see the different calling contexts of
shrinkers, and easier for the calling code to call the correct
shrinker context with the correct state.  It also makes it easier to
pull memcg-awareness into the struct shrinker.

Further, it allows us to solve the dirty/clean shrinker skip
optimisations in a generic manner, because that can now be stored on
a per-node or per-memcg basis inside the struct shrinker. It's
available to all shrinkers, rather than being just a memcg-specific
optimisation, and it's easy to implement as part of a shrinker list
walk (i.e. for_each_dirty_shrinker()).

We can also add simple wrappers like super_mark_shrinker_dirty(sb)
when we add an item to the LRU in the appropriate places to do the
work of activating the shrinker.

This seems much more appropriate to me, because it fixes the problem
of scanning/polling too many shrinkers at the shrinker state/context
level. That makes it generic and not memcg specific so helps
non-memcg system/workloads, too. It also fixes existing call-to-call
shrinker state problems that memcg reclaim currently causes and so
there's multiple levels of win here. Once this is all sorted out,
then we can deal with the subsystem shrinker behaviour knowing it
doesn't have to work around imbalance and fairness problems in the
infrastructure....

Cheers,

Dave.
-- 
Dave Chinner
david@fromorbit.com

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

* Re: [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim
  2018-03-26  2:37                             ` Dave Chinner
@ 2018-03-26 11:16                               ` Kirill Tkhai
  0 siblings, 0 replies; 22+ messages in thread
From: Kirill Tkhai @ 2018-03-26 11:16 UTC (permalink / raw)
  To: Dave Chinner; +Cc: Michal Hocko, darrick.wong, linux-xfs, linux-kernel, akpm

On 26.03.2018 05:37, Dave Chinner wrote:
> On Fri, Mar 23, 2018 at 03:39:53PM +0300, Kirill Tkhai wrote:
>> On 23.03.2018 02:46, Dave Chinner wrote:
>>> On Thu, Mar 22, 2018 at 07:52:37PM +0300, Kirill Tkhai wrote:
>>>> Here is the problem I'm solving: https://lkml.org/lkml/2018/3/21/365.
>>>
>>> Oh, finally you tell me what the problem is that you're trying to
>>> solve. I *asked this several times* and got no response. Thank you
>>> for wasting so much of my time.
>>>
>>>> Current shrinker is not scalable. Then there are many memcg and mounts,
>>>> the time of iteration shrink_slab() in case of global reclaim can
>>>> take much time. There is times of shrink_slab() by the link. A node
>>>> with 200 containers may waste 4 seconds on global reclaim just to
>>>> iterate over all shrinkers for all cgroups, call shrinker::count_objects()
>>>> and receive 0 zero objects.
>>>
>>> So, your problem is the way the memcgs were tacked onto the side
>>> of the list_lru infrastructure and are iterated, which has basically
>>> nothing to do with the way the low level XFS inode shrinker behaves.
>>>
>>> /me looks at the patches
>>>
>>> /me shudders at the thought of external "cache has freeable items"
>>> control for the shrinking of vfs caches.
>>>
>>> Biggest problem I see with this is the scope for coherency bugs ini
>>> the "memcg shrinker has freeable items" tracking. If that happens,
>>> there's no way of getting that memcg to run it's shrinker ever
>>> again. That seems very, very fragile and likely to be an endless
>>> source of OOM bugs. The whole point of the shrinker polling
>>> infrastructure is that it is not susceptible to this sort of bug.
>>>
>>> Really, the problem is that there's no separate list of memcg aware
>>> shrinkers, so every registered shrinker has to be iterated just to
>>> find the one shrinker that is memcg aware.
>>
>> I don't think the logic is difficult. There are generic rules,
>> and the only task is to teach them memcg-aware shrinkers. Currently,
>> they are only super block and workingsets shrinkers, and both of
>> them are based on generic list_lru infrastructure. Shrinker-related
>> bit is also cleared in generic code (shrink_slab()) only, and
>> the algorhythm doesn't allow to clear it without double check.
>> The only principle modification I'm thinking about is we should
>> clear the bit only when the shrinker is called with maximum
>> parameters: priority and GFP.
> 
> Lots of "simple logic" combined together makes for a complex mass of
> difficult to understand and debug code.
> 
> And, really, you're not suffering from a memcg problem - you're
> suffering from a "there are thousands of shrinkers" scalability
> issue because superblocks have per-superblock shrinker contexts and
> you have thousands of mounted filesystems.

Yes, but it's the way of how the containers are arranged. There are several
FS_USERNS_MOUNT tagged filesystems in kernel, and these filesystems are tagged
this flag, because they are safe and widely used in containers. It's impossible
to unite all of them as the only partition.

Also, separate ext4/xfs/etc partitions may be useful for some systems. You just
can't know all the workloads, people use it.

>> There are a lot of performance improving synchronizations in kernel,
>> and they had been refused, the kernel would have remained in the age
>> of big kernel lock.
> 
> That's false equivalence and hyperbole. The shrinkers are not
> limiting what every Linux user can do with their hardware. It's not
> a fundamental architectural limitation.  These sorts of arguments
> are not convincing - this is the second time I've told you this, so
> please stick to technical arguments and drop the dramatic
> "anti-progress" conspiracy theory bullshit.

Your view is limited by XFS, but the fact you don't use this doesn't
mean nobody is need such functionality. 
 
>>> So why not just do the simple thing which is create a separate
>>> "memcg-aware" shrinker list (i.e. create shrinker_list_memcg
>>> alongside shrinker_list) and only iterate the shrinker_list_memcg
>>> when a memcg is passed to shrink_slab()?
>>>
>>> That means we'll only run 2 shrinkers per memcg at most (sueprblock
>>> and working set) per memcg reclaim call. That's a simple 10-20 line
>>> change, not a whole mess of new code and issues...
>>
>> It was the first optimization, which came to my head, by there is no
>> almost a performance profit, since memcg-aware shrinkers still be called
>> per every memcg, and they are the biggest part of shrinkers in the system.
> 
> Sure, but a polling algorithm is not a fundamental performance
> limitation.
> 
> The problem here is that the memcg infrastructure has caused an
> exponential explosion in shrinker scanning.

Yeah, so this is the reason I seeking for the solution.

>>>> Can't we call shrink of shared objects only for top memcg? Something like
>>>> this:
>>>
>>> What's a "shared object", and how is it any different to a normal
>>> slab cache object?
>>
>> Sorry, it's erratum. I'm speaking about cached objects. I mean something like
>> the below. The patch makes cached objects be cleared outside the memcg iteration
>> cycle (it has not sense to call them for every memcg since cached object logic
>> just ignores memcg).
> 
> The cached flag seems like a hack to me. It does nothing to address
> the number of shrinker callout calls (it actually increases them!),
> just tries to hack around something you want a specific shrinker to
> avoid doing.

This is a prototype, and this is not difficult to teach the code to differ
shrinkers which need such the call, and the rest of them, and to implement
some generic way for that.

Since shrinkers will call nr_cached_objects() and free_cached_objects()
only for top memcg of reclaim, it will decrease the number of calls
for every system, which has more that 1 memcg. I.e., almost for everyone.

> I've asked *repeatedly* for a description of the actual workload
> problems the XFS shrinker behaviour is causing you. In the absence
> of any workload description, I'm simply going to NACK any changes
> that try to work around this behaviour. The rest of this reply is
> about shrinker infrastructure and (the lack of) memcg integration.

I've already pointed the problem. And the patchset I referred needs some
preparations around nr_cached_objects() and free_cached_objects()
to differ really populated memcg shrinkers and not.

Fortunately, this problem ends on the level of generic fs shrinker,
and it doesn't touch specific-fs level. So, I have no patches for
XFS and its shrinkers.

I just reported xfs guys there is a problem, they may be interested in.
But since nobody deploys many containers on XFS, and nobody is interested,
this discussion really has no sense.

>> --- a/mm/vmscan.c
>> +++ b/mm/vmscan.c
>> @@ -482,6 +482,33 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
>>  	return freed;
>>  }
>>  
>> +static void shrink_cached_slab(gfp_t gfp_mask, int nid, int priority)
>> +{
>> +	struct shrinker *shrinker;
>> +
>> +	if (!down_read_trylock(&shrinker_rwsem))
>> +		return;
>> +
>> +	list_for_each_entry(shrinker, &shrinker_list, list) {
>> +		struct shrink_control sc = {
>> +			.gfp_mask = gfp_mask,
>> +			.nid = nid,
>> +			.cached = 1,
>> +		};
>> +
>> +		if (!(shrinker->flags & SHRINKER_MEMCG_AWARE))
>> +			continue;
>> +		if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
>> +			sc.nid = 0;
>> +		if (rwsem_is_contended(&shrinker_rwsem))
>> +			break;
>> +
>> +		do_shrink_slab(&sc, shrinker, priority);
>> +	}
>> +
>> +	up_read(&shrinker_rwsem);
> 
> Ok, so this shrinks /only/ non-memcg slabs. It's identical to
> calling shrink_slab() with a null memcg. We already do this in all
> the relevant memory reclaim paths when necessary....

No, it's not so. We call shrink_slab() with null memcg to iterate
!memcg-aware shrinkers only. While the new function is used to
shrink cached objects of memcg-aware shrinkers. There is a difference.

>> +}
>> +
>>  void drop_slab_node(int nid)
>>  {
>>  	unsigned long freed;
>> @@ -493,6 +520,8 @@ void drop_slab_node(int nid)
>>  		do {
>>  			freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
>>  		} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
>> +
>> +		shrink_cached_slab(GFP_KERNEL, nid, 0);
>>  	} while (freed > 10);
> 
> ... like here, where the inner loop always calls shrink_slab() with
> a NULL memcg as it's first iteration. So your addition here is
> basically a no-op - we've already run non-memcg cache reclaim by the
> time your addition to run non-memcg cache reclaim is called. And
> here:

As I pointed below, shrink_cached_slab() works with memcg-aware shrinkers.

>> @@ -2573,6 +2602,8 @@ static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
>>  			shrink_slab(sc->gfp_mask, pgdat->node_id, NULL,
>>  				    sc->priority);
>>  
>> +		shrink_cached_slab(sc->gfp_mask, pgdat->node_id, sc->priority);
>> +
> 
> You're adding unconditional global slab cache reclaim to a path that
> already does this but only in the calling contexts where it global
> slab reclaim is actually necessary. Doing unconditional global slab
> reclaim here will cause regressions.

It's not a global reclaim. We only shrink cached objects there.
The flag ".cached = 1" in the functions indicates the shrinkers,
what they have to shrink.

> So, let's address the number of shrinker callouts from
> drop_caches(). You're original example from here:
> 
> https://lkml.org/lkml/2018/3/21/365
> 
> | In case of global reclaim, a task has to iterate all over the memcgs
> | and to call all the memcg-aware shrinkers for all of them. This means,
> | the task has to visit 200 * 10 = 2000 shrinkers for every memcg,
> | and since there are 2000 memcgs, the total calls of do_shrink_slab()
> | are 2000 * 2000 = 4000000.
> 
> The loop is:
> 
> drop_slab()
>   for_each_online_node(nid)
>     drop_slab_node(nid);
>       for each memcg including NULL
>         shrink_slab(nid, memcg)
> 
> With 200 containers w/ 10 mounts X 10 memcgs each. That means we
> have 2000 mounts and hence 2000 memcg aware superblock shrinkers.
> we have 2000 memcgs, so each superblock has 2000 memcgs attached to
> it. And then we have N nodes in the machine.
> 
> So drop_slab() will call drop_slab_node() N times, and drop_slab
> node will iterate all the memcgs until there's no more to free.
> Each memcg with then iterate all shrinkers to find the memcg aware
> shrinkers, then do work.  IOWs, we actually have a minimum scan of N
> * 2000 * 2000 calls to shrink_slab(), not just 2000 * 2000.
> 
> The thing is, memcg shrinker contexts are not NUMA aware. They use
> the list array that was implemented for memory nodes and instead use
> it for a memcg list array, indexed by memcg ID rather than node ID.
> IOWs, memcgs are not numa aware, so they do not need to be called
> for every single node in the system. Only shrinkers in a global
> context (i.e. no memcg) that are NUMA aware need to be called for
> every node.
> 
> The patchset you posted implemented that mapping by adding a bitmap
> of memcg-internal shrinker dirty state and a global array that
> points directly to the struct shrinker that the bitmap index is
> associated with. This externalises the dirty tracking into the memcg
> code, which as I've already said I don't like.
> 
> The real problem here is that the shrinker itself is supposed to
> keep context specific call-to-call information. In the case of
> NUMA-aware shrinkers, that's kept in the shrinker->nr_deferred[nid]
> array.  That's dynamically allocated for numa aware shrinkers, but
> all memcg's that are called for the given shrinker all smash their
> state into shrinker->nr_deferred[nid] - they don't have their own
> nr_deferred state.
>
> This actually screws up the nr_deferred state for global reclaim,
> and causes unexpected unfairness to memcg reclaim. i.e. Global,
> non-memcg reclaim can defer pressure to all memcgs that shrinker is
> called on for that node because the nr_deferred count is shared
> based on nid. That means work we defer from one call to a memcg on
> nid=0 will be applied to the next memcg recalim call on nid=0,
> regardless of what it is. And if that first shrinker is then called
> next on nid=1, it won't get it's nr_deferred from it's last call on
> nid=0, it will get whatever is currently stored on nid=1.
> 
> IOWs, the memcg shrinker infrastructure itself violates the
> principles of memcg isolation and fairness by completely screwing up
> the call-to-call deferred work counts for individual memcgs and
> global shrinker reclaim.

Yes, it may be a problem, and I've already seen that. But since
I've never meet the real bug reports about that, and nobody complained
on that, I prioritized the problem with scalability is the first.

> If you're interested in fairness and memcg
> isolation in reclaim, then this is the first problem you need to
> solve.
> 
> IMO, What we should be doing here is something like this:
> 
> drop_slab()
> {
>   do {
>     freed = shrink_slab_memcg()
>     freed += shrink_slab_numa()
>     freed += shrink_slab_simple()
>   } while (freed < 10)
> }
> 
> i.e. we set up a distinct separation of shrinker roles according to
> the contexts in which they are called. e.g.

Drop slab is just and interface for /proc. It may be useful to measure
the performance (what I actually do in the patchset), but this interface
is not frequently used in real life.

shrink_node() is the real-life reclaim hero.

> 
> shrink_slab_memcg()
> {
>   for_each_memcg(memcg)
>     for_each_shrinker(&memcg_shrinker_list) {
>       nr_deferred = shrinker_get_memcg_deferred(shrinker, memcg)
>       do_shrink_slab(shrinker, 0, memcg, &nr_deferred))
>       shrinker_store_memcg_deferred(shrinker, memcg, nr_deferred)
>     }
> }
> 
> shrink_slab_numa()
> {
>   for_each_online_node(nid)
>     for_each_shrinker(&numa_shrinker_list) {
>       nr_deferred = shrinker_get_node_deferred(shrinker, nid)
>       do_shrink_slab(shrinker, nid, NULL, &nr_deferred)
>       shrinker_store_node_deferred(shrinker, nid, nr_deferred)
>     }
> }
> 
> shrink_slab_simple()
> {
>     for_each_shrinker(&simple_shrinker_list) {
>       nr_deferred = shrinker_get_node_deferred(shrinker, 0)
>       do_shrink_slab(shrinker, 0, NULL, &nr_deferred)
>       shrinker_store_node_deferred(shrinker, 0, nr_deferred)
>     }
> }
> 
> [ Note: This implies a shrinker can be on multiple lists. That's
> fine, we can already do concurrent calls into a shrinker from
> different reclaim contexts. But in doing this, the "iterate shrinker
> list to find memcg aware shrinkers" goes aware. Similarly with the
> basic shrinkers - we stop hitting them for every node we scan in
> global reclaim and hence putting lots more pressure on them because
> they are being treated as "one cache per node" instead of a single
> cache. ]
> 
> The simple and numa cases can share nr_deferred[0] as they currently
> do because a shrinker will only ever be one or the other, but we
> still need shrink_slab_memcg() to have per-memcg nr_deferred values
> because simple shrinkers can still be MEMCG_AWARE.
> 
> This makes it simpler to see the different calling contexts of
> shrinkers, and easier for the calling code to call the correct
> shrinker context with the correct state.  It also makes it easier to
> pull memcg-awareness into the struct shrinker.
> 
> Further, it allows us to solve the dirty/clean shrinker skip
> optimisations in a generic manner, because that can now be stored on
> a per-node or per-memcg basis inside the struct shrinker. It's
> available to all shrinkers, rather than being just a memcg-specific
> optimisation, and it's easy to implement as part of a shrinker list
> walk (i.e. for_each_dirty_shrinker()).

nr_defered has a sense as a problem, but this is another problem, and
its solution doesn't help the problem I'm solving and see in real worloads
(these are just two different problems).

Implementing of shrink_slab_numa() will help /proc/sys/vm/drop_caches
only, which is mostly a debug tool. Not used in real life.

While separating memcg and !memcg -aware shrinkers I've already done
in the patchset.
 
> We can also add simple wrappers like super_mark_shrinker_dirty(sb)
> when we add an item to the LRU in the appropriate places to do the
> work of activating the shrinker.

Yeah, we may do that, if this improves readability for a user. The name
sounds good for me too.
 
> This seems much more appropriate to me, because it fixes the problem
> of scanning/polling too many shrinkers at the shrinker state/context
> level. That makes it generic and not memcg specific so helps
> non-memcg system/workloads, too. It also fixes existing call-to-call
> shrinker state problems that memcg reclaim currently causes and so
> there's multiple levels of win here. Once this is all sorted out,
> then we can deal with the subsystem shrinker behaviour knowing it
> doesn't have to work around imbalance and fairness problems in the
> infrastructure....

Currently, all memcg-aware shrinkers are numa-aware too. So, separating
them won't help. While, I've already separated memcg-aware and !memcg-aware
in the patchset.

Kirill

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

end of thread, other threads:[~2018-03-26 11:17 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-15 15:01 [PATCH RFC] xfs, memcg: Call xfs_fs_nr_cached_objects() only in case of global reclaim Kirill Tkhai
2018-03-15 15:53 ` Darrick J. Wong
2018-03-15 16:06   ` Kirill Tkhai
2018-03-15 17:49 ` Michal Hocko
2018-03-15 19:28   ` Kirill Tkhai
2018-03-15 19:32     ` Michal Hocko
2018-03-15 19:42       ` Kirill Tkhai
2018-03-15 23:03     ` Dave Chinner
2018-03-16  8:55       ` Kirill Tkhai
2018-03-16 21:39         ` Dave Chinner
2018-03-19 11:06           ` Kirill Tkhai
2018-03-19 11:25             ` Kirill Tkhai
2018-03-20  0:18             ` Dave Chinner
2018-03-20 13:15               ` Kirill Tkhai
2018-03-20 14:34                 ` Dave Chinner
2018-03-21 16:15                   ` Kirill Tkhai
2018-03-22  5:01                     ` Dave Chinner
2018-03-22 16:52                       ` Kirill Tkhai
2018-03-22 23:46                         ` Dave Chinner
2018-03-23 12:39                           ` Kirill Tkhai
2018-03-26  2:37                             ` Dave Chinner
2018-03-26 11:16                               ` Kirill Tkhai

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.