Skip to contentSkip to conversation

Access rules for current->memcg

DISCUSSION Nikolay Borisov 3 participants 15 messages Jul 16, 2015 – Jul 21, 2015 13 messages not in the archive cgroup

lore
Share & tools
lore
#0 lore raw

Hello,

I'd like to ask what are the locking rules when using mem_cgroup_from_task(current)? Currently I'm doing this under rcu_read_lock which I believe is sufficient. However, I've seen patches where reference is obtained via mem_cgroup_from_task and then css_tryget_online is used on the resulting cgroup?

Looking at the context of css_tryget_online it seems it will only succeed if the cgroup isn't being terminated (cgroup_destroy_locked isn't being invoked). Judging from this then if a css_tryget_online succeeds this means the caller can be sure they are working with a live cgroup, however, what happens if the process of acuiqring a reference on css is skipped AND the caller is under RCU read lock? They are guaranteed to succeed, but after the rcu read lock is released the cgroup might go away ?

Essentially my use case is to obtain a reference to a memcg for the current process and query some counter values IOW - just reading from the memcg. Do I need to acquire a CSS reference in this case?

Regards,
Nikolay

A message that is not in the archive (sent off-list or not yet received): 55A7B2D0.1030506-/[email protected]

#2 lore raw
Quoted 7 lines · Nikolay Borisov

Hello,

I'd like to ask what are the locking rules when using mem_cgroup_from_task(current)? Currently I'm doing this under rcu_read_lock which I believe is sufficient. However, I've seen patches where reference is obtained via mem_cgroup_from_task and then css_tryget_online is used on the resulting cgroup?

RCU will guarantee that the memcg will not go away. The rest depends on what you want to do with it. If you want to use it outside of RCU you have to take a reference. And then it depends what the memcg is used for - some operations can be done also on the offline memcg.

Btw. mem_cgroup_from_task is not the proper interface for you. You really want to do
memcg = get_mem_cgroup_from_mm(current->mm)
[...]
css_put(&memcg)

Quoted 6 lines

Looking at the context of css_tryget_online it seems it will only succeed if the cgroup isn't being terminated (cgroup_destroy_locked isn't being invoked). Judging from this then if a css_tryget_online succeeds this means the caller can be sure they are working with a live cgroup, however, what happens if the process of acuiqring a reference on css is skipped AND the caller is under RCU read lock?

The memcg will not get freed. It still might be offline.

Quoted 3 lines

They are
guaranteed to succeed, but after the rcu read lock is released the cgroup might go away ?

Yes it might go away.

Quoted 3 lines

Essentially my use case is to obtain a reference to a memcg for the current process and query some counter values IOW - just reading from the memcg. Do I need to acquire a CSS reference in this case?

I would strongly recommend using get_mem_cgroup_from_mm as shown above.

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 14 lines · Michal Hocko
Quoted 7 lines · Nikolay Borisov

Hello,

I'd like to ask what are the locking rules when using mem_cgroup_from_task(current)? Currently I'm doing this under rcu_read_lock which I believe is sufficient. However, I've seen patches where reference is obtained via mem_cgroup_from_task and then css_tryget_online is used on the resulting cgroup?

RCU will guarantee that the memcg will not go away. The rest depends on what you want to do with it. If you want to use it outside of RCU you have to take a reference. And then it depends what the memcg is used for - some operations can be done also on the offline memcg.

Quoted 5 lines

Btw. mem_cgroup_from_task is not the proper interface for you. You really want to do
memcg = get_mem_cgroup_from_mm(current->mm)
[...]
css_put(&memcg)

Unfortunately this function is static, do you think there might be any value of a patch that exposes it upstream?

Quoted 14 lines
Quoted 7 lines

Looking at the context of css_tryget_online it seems it will only succeed if the cgroup isn't being terminated (cgroup_destroy_locked isn't being invoked). Judging from this then if a css_tryget_online succeeds this means the caller can be sure they are working with a live cgroup, however, what happens if the process of acuiqring a reference on css is skipped AND the caller is under RCU read lock?

The memcg will not get freed. It still might be offline.

Quoted 3 lines

They are
guaranteed to succeed, but after the rcu read lock is released the cgroup might go away ?

Quoted 8 lines

Yes it might go away.

Quoted 3 lines

Essentially my use case is to obtain a reference to a memcg for the current process and query some counter values IOW - just reading from the memcg. Do I need to acquire a CSS reference in this case?

I would strongly recommend using get_mem_cgroup_from_mm as shown above.

Thanks for the info !

A message that is not in the archive (sent off-list or not yet received): 55A7C9B4.3010907-/[email protected]

#6 lore raw
Quoted 17 lines · Nikolay Borisov
Quoted 14 lines · Michal Hocko
Quoted 7 lines · Nikolay Borisov

Hello,

I'd like to ask what are the locking rules when using mem_cgroup_from_task(current)? Currently I'm doing this under rcu_read_lock which I believe is sufficient. However, I've seen patches where reference is obtained via mem_cgroup_from_task and then css_tryget_online is used on the resulting cgroup?

RCU will guarantee that the memcg will not go away. The rest depends on what you want to do with it. If you want to use it outside of RCU you have to take a reference. And then it depends what the memcg is used for - some operations can be done also on the offline memcg.

Quoted 8 lines
Quoted 5 lines

Btw. mem_cgroup_from_task is not the proper interface for you. You really want to do
memcg = get_mem_cgroup_from_mm(current->mm)
[...]
css_put(&memcg)

Unfortunately this function is static, do you think there might be any value of a patch that exposes it upstream?

Ohh, you are right! I thought I made it visible with my recent changes but nope. There are no external users currently.

Could you tell us more why it would be useful for you?

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw
Quoted 24 lines · Michal Hocko
Quoted 23 lines · Nikolay Borisov
Quoted 19 lines · Michal Hocko
Quoted 7 lines · Nikolay Borisov

Hello,

I'd like to ask what are the locking rules when using mem_cgroup_from_task(current)? Currently I'm doing this under rcu_read_lock which I believe is sufficient. However, I've seen patches where reference is obtained via mem_cgroup_from_task and then css_tryget_online is used on the resulting cgroup?

RCU will guarantee that the memcg will not go away. The rest depends on what you want to do with it. If you want to use it outside of RCU you have to take a reference. And then it depends what the memcg is used for - some operations can be done also on the offline memcg.

Btw. mem_cgroup_from_task is not the proper interface for you. You really want to do
memcg = get_mem_cgroup_from_mm(current->mm)
[...]
css_put(&memcg)

Quoted 7 lines
Quoted 2 lines

Unfortunately this function is static, do you think there might be any value of a patch that exposes it upstream?

Ohh, you are right! I thought I made it visible with my recent changes but nope. There are no external users currently.

Could you tell us more why it would be useful for you?

In my particular use case I have to query the memcg's various counters to expose them to the user in a different way than via the cgroup files (memory.limit_in_bytes etc).

Quoted 3 lines

--
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): CAJFSNy6sLX82+3ZW_COr__pDTd9aSGgL1bjryMKKcVPhEN0F9g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#10 lore raw
Quoted 28 lines · Nikolay Borisov
Quoted 27 lines · Michal Hocko
Quoted 25 lines · Nikolay Borisov
Quoted 14 lines · Michal Hocko
Quoted 7 lines · Nikolay Borisov

Hello,

I'd like to ask what are the locking rules when using mem_cgroup_from_task(current)? Currently I'm doing this under rcu_read_lock which I believe is sufficient. However, I've seen patches where reference is obtained via mem_cgroup_from_task and then css_tryget_online is used on the resulting cgroup?

RCU will guarantee that the memcg will not go away. The rest depends on what you want to do with it. If you want to use it outside of RCU you have to take a reference. And then it depends what the memcg is used for - some operations can be done also on the offline memcg.

Quoted 5 lines

Btw. mem_cgroup_from_task is not the proper interface for you. You really want to do
memcg = get_mem_cgroup_from_mm(current->mm)
[...]
css_put(&memcg)

Unfortunately this function is static, do you think there might be any value of a patch that exposes it upstream?

Quoted 8 lines
Quoted 4 lines

Ohh, you are right! I thought I made it visible with my recent changes but nope. There are no external users currently.

Could you tell us more why it would be useful for you?

In my particular use case I have to query the memcg's various counters to expose them to the user in a different way than via the cgroup files (memory.limit_in_bytes etc).

Why is the regular interface not sufficient?

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw
Quoted 32 lines · Michal Hocko
Quoted 31 lines · Nikolay Borisov
Quoted 30 lines · Michal Hocko
Quoted 17 lines · Nikolay Borisov
Quoted 14 lines · Michal Hocko
Quoted 7 lines · Nikolay Borisov

Hello,

I'd like to ask what are the locking rules when using mem_cgroup_from_task(current)? Currently I'm doing this under rcu_read_lock which I believe is sufficient. However, I've seen patches where reference is obtained via mem_cgroup_from_task and then css_tryget_online is used on the resulting cgroup?

RCU will guarantee that the memcg will not go away. The rest depends on what you want to do with it. If you want to use it outside of RCU you have to take a reference. And then it depends what the memcg is used for - some operations can be done also on the offline memcg.

Quoted 8 lines
Quoted 5 lines

Btw. mem_cgroup_from_task is not the proper interface for you. You really want to do
memcg = get_mem_cgroup_from_mm(current->mm)
[...]
css_put(&memcg)

Unfortunately this function is static, do you think there might be any value of a patch that exposes it upstream?

Ohh, you are right! I thought I made it visible with my recent changes but nope. There are no external users currently.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Could you tell us more why it would be useful for you?

In my particular use case I have to query the memcg's various counters to expose them to the user in a different way than via the cgroup files (memory.limit_in_bytes etc).

Why is the regular interface not sufficient?

In my particular case I'm interested in playing with the contents of /proc/meminfo, so that processes running inside a cgroup only see the the system as defined by the memcg restrictions

A message that is not in the archive (sent off-list or not yet received): 55A8ABC9.7090701-/[email protected]

#14 lore raw
Quoted 4 lines · Nikolay Borisov
Quoted 1 line · Michal Hocko

On Fri 17-07-15 00:21:51, Nikolay Borisov wrote:

[...]

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

In my particular use case I have to query the memcg's various counters to expose them to the user in a different way than via the cgroup files (memory.limit_in_bytes etc).

Why is the regular interface not sufficient?

In my particular case I'm interested in playing with the contents of /proc/meminfo, so that processes running inside a cgroup only see the the system as defined by the memcg restrictions

I assume that this is an attempt to containerize /proc/meminfo. I am not sure this is a great idea. There are counters which do not have memcg specific counterpart or such a counterpart would be missleading (e.g. slab, swap statistics).

Is this an out-of-tree project or you are trying to push your changes to the Linus tree somewhere? I haven't noticed such patches.

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#16 lore raw
Quoted 12 lines · Michal Hocko
Quoted 4 lines · Nikolay Borisov
Quoted 1 line · Michal Hocko

On Fri 17-07-15 00:21:51, Nikolay Borisov wrote:

[...]

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

In my particular use case I have to query the memcg's various counters to expose them to the user in a different way than via the cgroup files (memory.limit_in_bytes etc).

Why is the regular interface not sufficient?

Quoted 8 lines
Quoted 3 lines

In my particular case I'm interested in playing with the contents of /proc/meminfo, so that processes running inside a cgroup only see the the system as defined by the memcg restrictions

I assume that this is an attempt to containerize /proc/meminfo. I am not sure this is a great idea. There are counters which do not have memcg specific counterpart or such a counterpart would be missleading (e.g. slab, swap statistics).

Why would swap be misleading? What about memsw.limit_in_bytes - memory.limit_in_bytes for the total swap and calculating the swap usage based on memory.memsw.max_usage_in_bytes - memory.usage_in_bytes ?

Quoted 3 lines

Is this an out-of-tree project or you are trying to push your changes to the Linus tree somewhere? I haven't noticed such patches.

It's an out-of-tree project and I'm well aware there are certain numbers which do not correspond 1:1 but that is fine for my use case.

A message that is not in the archive (sent off-list or not yet received): 55ACD9F8.2040802-/[email protected]

#18 lore raw
Quoted 19 lines · Nikolay Borisov
Quoted 16 lines · Michal Hocko
Quoted 4 lines · Nikolay Borisov
Quoted 1 line · Michal Hocko

On Fri 17-07-15 00:21:51, Nikolay Borisov wrote:

[...]

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

In my particular use case I have to query the memcg's various counters to expose them to the user in a different way than via the cgroup files (memory.limit_in_bytes etc).

Why is the regular interface not sufficient?

In my particular case I'm interested in playing with the contents of /proc/meminfo, so that processes running inside a cgroup only see the the system as defined by the memcg restrictions

Quoted 6 lines
Quoted 4 lines

I assume that this is an attempt to containerize /proc/meminfo. I am not sure this is a great idea. There are counters which do not have memcg specific counterpart or such a counterpart would be missleading (e.g. slab, swap statistics).

Why would swap be misleading?

Because the swap space is inherently a shared resource.

Quoted 1 line

What about memsw.limit_in_bytes - memory.limit_in_bytes for the total swap

No this is not how the swap extension works. memsw counter covers usage+swap. You can have up to memsw.limit_in_bytes swapped out. So you would have to do min(memsw.limit_in_bytes, TotalSwap) but even then it wouldn't tell you much because that would be the case for other memory cgroups as well. I would be quite hard to distribute the TotalSwap for all the cgroups.

Quoted 2 lines

and calculating the swap usage
based on memory.memsw.max_usage_in_bytes - memory.usage_in_bytes ?

This doesn't make much sense to me. Swap usage per memcg is exported by memory.stat file. But this is not what you want to export. meminfo exports SwapFree which would tell you how much memory could be swapped out before the anonymous memory is not reclaimable anymore. This is impossible to find out in general - especially when the system is allowed to be overcommit.

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#20 lore raw
Quoted 25 lines · Michal Hocko
Quoted 24 lines · Nikolay Borisov
Quoted 20 lines · Michal Hocko
Quoted 4 lines · Nikolay Borisov
Quoted 1 line · Michal Hocko

On Fri 17-07-15 00:21:51, Nikolay Borisov wrote:

[...]

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

In my particular use case I have to query the memcg's various counters to expose them to the user in a different way than via the cgroup files (memory.limit_in_bytes etc).

Why is the regular interface not sufficient?

In my particular case I'm interested in playing with the contents of /proc/meminfo, so that processes running inside a cgroup only see the the system as defined by the memcg restrictions

I assume that this is an attempt to containerize /proc/meminfo. I am not sure this is a great idea. There are counters which do not have memcg specific counterpart or such a counterpart would be missleading (e.g. slab, swap statistics).

Quoted 8 lines
Quoted 1 line

Why would swap be misleading?

Because the swap space is inherently a shared resource.

Quoted 1 line

What about memsw.limit_in_bytes - memory.limit_in_bytes for the total swap

No this is not how the swap extension works. memsw counter covers usage+swap. You can have up to memsw.limit_in_bytes swapped out. So

I think you are wrong with that assumption. According to the documentation here:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/sec-memory.html
(the box titled "Setting the memory.memsw.limit_in_bytes and memory.limit_in_bytes parameters") it seems that the space that could be swapped is actually memsw.limit_in_bytes - memory.limit_in_bytes.

The way I understand is you will only start swapping after the limit in memory.limit_in_bytes has been exhausted and you will be able to swap only until memsw.limit_in_bytes is exhausted (but since memory.usage_in_bytes is already calculated in, which would equal to memory.limit_in_bytes in a memory pressure situation) you effectively have memsw.limit_in_bytes - memory.limit_in_bytes, no?

Quoted 8 lines

you would have to do min(memsw.limit_in_bytes, TotalSwap) but even then it wouldn't tell you much because that would be the case for other memory cgroups as well. I would be quite hard to distribute the TotalSwap for all the cgroups.

Quoted 2 lines

and calculating the swap usage
based on memory.memsw.max_usage_in_bytes - memory.usage_in_bytes ?

Quoted 6 lines

This doesn't make much sense to me. Swap usage per memcg is exported by memory.stat file. But this is not what you want to export. meminfo exports SwapFree which would tell you how much memory could be swapped out before the anonymous memory is not reclaimable anymore. This is impossible to find out in general - especially when the system is allowed to be overcommit.

I looked more carefully into the code and saw that the page_counters (which back memory/memsw.limit/usage_in_bytes) are charged during the try_charge whereas the per-cpu statistics (which back the info in memory.stats) are updated after committing the charge. I assume in the case where charges are not canceled the data in memory.stats and memory.memsw.max_usage_in_bytes - memory.usage_in_bytes should be identical?

A message that is not in the archive (sent off-list or not yet received): 55AE0C18.5000304-/[email protected]

#22 lore raw
Quoted 14 lines · Nikolay Borisov
Quoted 5 lines · Michal Hocko
Quoted 1 line · Nikolay Borisov

What about memsw.limit_in_bytes - memory.limit_in_bytes for the total swap

No this is not how the swap extension works. memsw counter covers usage+swap. You can have up to memsw.limit_in_bytes swapped out. So

I think you are wrong with that assumption. According to the documentation here:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/sec-memory.html
(the box titled "Setting the memory.memsw.limit_in_bytes and memory.limit_in_bytes parameters") it seems that the space that could be swapped is actually memsw.limit_in_bytes - memory.limit_in_bytes.

Quoted 6 lines

The way I understand is you will only start swapping after the limit in memory.limit_in_bytes has been exhausted and you will be able to swap only until memsw.limit_in_bytes is exhausted (but since memory.usage_in_bytes is already calculated in, which would equal to memory.limit_in_bytes in a memory pressure situation) you effectively have memsw.limit_in_bytes - memory.limit_in_bytes, no?

No, the cgroup can also swap due to global/parental memory pressure, not just due to memory.limit_in_bytes. In that case, it can get its pages swapped out until it reaches memsw.limit_in_bytes, with memory consumption well below memory.limit or even 0.

#23 lore raw
In reply to a message not in the archive
Quoted 44 lines · Nikolay Borisov
Quoted 25 lines · Michal Hocko
Quoted 24 lines · Nikolay Borisov
Quoted 20 lines · Michal Hocko
Quoted 4 lines · Nikolay Borisov
Quoted 1 line · Michal Hocko

On Fri 17-07-15 00:21:51, Nikolay Borisov wrote:

[...]

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

In my particular use case I have to query the memcg's various counters to expose them to the user in a different way than via the cgroup files (memory.limit_in_bytes etc).

Why is the regular interface not sufficient?

In my particular case I'm interested in playing with the contents of /proc/meminfo, so that processes running inside a cgroup only see the the system as defined by the memcg restrictions

I assume that this is an attempt to containerize /proc/meminfo. I am not sure this is a great idea. There are counters which do not have memcg specific counterpart or such a counterpart would be missleading (e.g. slab, swap statistics).

Quoted 8 lines
Quoted 1 line

Why would swap be misleading?

Because the swap space is inherently a shared resource.

Quoted 1 line

What about memsw.limit_in_bytes - memory.limit_in_bytes for the total swap

No this is not how the swap extension works. memsw counter covers usage+swap. You can have up to memsw.limit_in_bytes swapped out. So

I think you are wrong with that assumption. According to the documentation here:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/sec-memory.html
(the box titled "Setting the memory.memsw.limit_in_bytes and memory.limit_in_bytes parameters") it seems that the space that could be swapped is actually memsw.limit_in_bytes - memory.limit_in_bytes.

Quoted 6 lines

The way I understand is you will only start swapping after the limit in memory.limit_in_bytes has been exhausted and you will be able to swap only until memsw.limit_in_bytes is exhausted (but since memory.usage_in_bytes is already calculated in, which would equal to memory.limit_in_bytes in a memory pressure situation) you effectively have memsw.limit_in_bytes - memory.limit_in_bytes, no?

No. Your memory might get swapped out even before you hit the hard limit if there is an external memory pressure. This would be either a global memory pressure or a hard limit triggered up in the hierarchy.

Take the most trivial situation when memsw and hard limits are equal. You would have a 0 swap space which is not the case obviously.

Please have a look at Documentation/cgroups/memory.txt and '2.4 Swap Extension (CONFIG_MEMCG_SWAP)' for more information.

Quoted 15 lines
Quoted 8 lines

you would have to do min(memsw.limit_in_bytes, TotalSwap) but even then it wouldn't tell you much because that would be the case for other memory cgroups as well. I would be quite hard to distribute the TotalSwap for all the cgroups.

Quoted 2 lines

and calculating the swap usage
based on memory.memsw.max_usage_in_bytes - memory.usage_in_bytes ?

Quoted 6 lines

This doesn't make much sense to me. Swap usage per memcg is exported by memory.stat file. But this is not what you want to export. meminfo exports SwapFree which would tell you how much memory could be swapped out before the anonymous memory is not reclaimable anymore. This is impossible to find out in general - especially when the system is allowed to be overcommit.

Quoted 6 lines

I looked more carefully into the code and saw that the page_counters (which back memory/memsw.limit/usage_in_bytes) are charged during the try_charge whereas the per-cpu statistics (which back the info in memory.stats) are updated after committing the charge. I assume in the case where charges are not canceled the data in memory.stats and memory.memsw.max_usage_in_bytes - memory.usage_in_bytes should be identical?

I am not sure what you mean here. max_usage_in_bytes is a historical value which was the maximum charge used at some point in time. usage_in_bytes is always the _current_ value of the charge counter. max_usage_in_bytes-usage_in_bytes doesn't tell you much really.

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#25 lore raw
Quoted 84 lines · Michal Hocko
Quoted 44 lines · Nikolay Borisov
Quoted 25 lines · Michal Hocko
Quoted 24 lines · Nikolay Borisov
Quoted 20 lines · Michal Hocko
Quoted 4 lines · Nikolay Borisov
Quoted 1 line · Michal Hocko

On Fri 17-07-15 00:21:51, Nikolay Borisov wrote:

[...]

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

In my particular use case I have to query the memcg's various counters to expose them to the user in a different way than via the cgroup files (memory.limit_in_bytes etc).

Why is the regular interface not sufficient?

In my particular case I'm interested in playing with the contents of /proc/meminfo, so that processes running inside a cgroup only see the the system as defined by the memcg restrictions

I assume that this is an attempt to containerize /proc/meminfo. I am not sure this is a great idea. There are counters which do not have memcg specific counterpart or such a counterpart would be missleading (e.g. slab, swap statistics).

Quoted 8 lines
Quoted 1 line

Why would swap be misleading?

Because the swap space is inherently a shared resource.

Quoted 1 line

What about memsw.limit_in_bytes - memory.limit_in_bytes for the total swap

No this is not how the swap extension works. memsw counter covers usage+swap. You can have up to memsw.limit_in_bytes swapped out. So

I think you are wrong with that assumption. According to the documentation here:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/sec-memory.html
(the box titled "Setting the memory.memsw.limit_in_bytes and memory.limit_in_bytes parameters") it seems that the space that could be swapped is actually memsw.limit_in_bytes - memory.limit_in_bytes.

Quoted 6 lines

The way I understand is you will only start swapping after the limit in memory.limit_in_bytes has been exhausted and you will be able to swap only until memsw.limit_in_bytes is exhausted (but since memory.usage_in_bytes is already calculated in, which would equal to memory.limit_in_bytes in a memory pressure situation) you effectively have memsw.limit_in_bytes - memory.limit_in_bytes, no?

No. Your memory might get swapped out even before you hit the hard limit if there is an external memory pressure. This would be either a global memory pressure or a hard limit triggered up in the hierarchy.

Take the most trivial situation when memsw and hard limits are equal. You would have a 0 swap space which is not the case obviously.

Please have a look at Documentation/cgroups/memory.txt and '2.4 Swap Extension (CONFIG_MEMCG_SWAP)' for more information.

Quoted 15 lines
Quoted 8 lines

you would have to do min(memsw.limit_in_bytes, TotalSwap) but even then it wouldn't tell you much because that would be the case for other memory cgroups as well. I would be quite hard to distribute the TotalSwap for all the cgroups.

Quoted 2 lines

and calculating the swap usage
based on memory.memsw.max_usage_in_bytes - memory.usage_in_bytes ?

Quoted 6 lines

This doesn't make much sense to me. Swap usage per memcg is exported by memory.stat file. But this is not what you want to export. meminfo exports SwapFree which would tell you how much memory could be swapped out before the anonymous memory is not reclaimable anymore. This is impossible to find out in general - especially when the system is allowed to be overcommit.

Quoted 6 lines

I looked more carefully into the code and saw that the page_counters (which back memory/memsw.limit/usage_in_bytes) are charged during the try_charge whereas the per-cpu statistics (which back the info in memory.stats) are updated after committing the charge. I assume in the case where charges are not canceled the data in memory.stats and memory.memsw.max_usage_in_bytes - memory.usage_in_bytes should be identical?

Quoted 5 lines

I am not sure what you mean here. max_usage_in_bytes is a historical value which was the maximum charge used at some point in time. usage_in_bytes is always the _current_ value of the charge counter. max_usage_in_bytes-usage_in_bytes doesn't tell you much really.

I have misunderstood me I have never, ever referred to max_usage. What I meant was that the information that memory.stat file provides is acquired by reading the memcg->stat->count[counter] and those values are updated when mem_cgroup_commit_charge() is invoked. And mem_cgroup_commit_charge is invoked AFTER mem_cgroup_try_charge, which updates the charge counters. So my point was that whether I read the swap value (for example) from the memory.stats file or whether I manually do the maths with subtraction as previously shown the 2 values should match.

Essentially whether information should be queried form the charge counter or from the mem_cgroup_stat_cpu struct. Does that make sense?

A message that is not in the archive (sent off-list or not yet received): 55AE1FB7.8050107-/[email protected]

#27 lore raw
Quoted 4 lines · Nikolay Borisov
Quoted 1 line · Michal Hocko

On Tue 21-07-15 12:08:40, Nikolay Borisov wrote:

[...]

Quoted 13 lines
Quoted 7 lines
Quoted 6 lines

I looked more carefully into the code and saw that the page_counters (which back memory/memsw.limit/usage_in_bytes) are charged during the try_charge whereas the per-cpu statistics (which back the info in memory.stats) are updated after committing the charge. I assume in the case where charges are not canceled the data in memory.stats and memory.memsw.max_usage_in_bytes - memory.usage_in_bytes should be identical?

Quoted 5 lines

I am not sure what you mean here. max_usage_in_bytes is a historical value which was the maximum charge used at some point in time. usage_in_bytes is always the _current_ value of the charge counter. max_usage_in_bytes-usage_in_bytes doesn't tell you much really.

Quoted 4 lines

I have misunderstood me I have never, ever referred to max_usage. What I meant was that the information that memory.stat file provides is acquired by reading the memcg->stat->count[counter] and those values are updated when mem_cgroup_commit_charge() is invoked.

True

Quoted 3 lines

And
mem_cgroup_commit_charge is invoked AFTER mem_cgroup_try_charge, which updates the charge counters.

Still true and you should realize that the commit is called very shortly after the charge. The race window is not really interesting for anything practical.

Quoted 4 lines

So my point was that whether I read the
swap value (for example) from the memory.stats file or whether I manually do the maths with subtraction as previously shown the 2 values should match.

Your subtraction simply doesn't work and doesn't tell you how much memory is swapped out from the memcg as explained in other email.

Quoted 2 lines

Essentially whether information should be queried form the charge counter or from the mem_cgroup_stat_cpu struct. Does that make sense?

memory.stat will tell you the information you are looking for. You simply cannot calculate those numbers from the counters.

Signature · 2 lines
Michal Hocko
SUSE Labs