Skip to contentSkip to conversation

mm: memcontrol: do not miss MEMCG_MAX events for enforced allocations

PATCH Applied Roman Gushchin 5 participants 22 messages Jul 2, 2022 – Jul 8, 2022

Subsystems (4)
lore
Share & tools
lore

Roman Gushchin

author maintainer
#0 lore raw

PATCH 1/1 mm: memcontrol: do not miss MEMCG_MAX events for enforced allocations

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen. So the cgroup can significantly exceed the memory.max limit without even triggering MEMCG_MAX events.

Fix this by making sure that we never enforce allocations without raising a MEMCG_MAX event.

---
 mm/memcontrol.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/memcontrol.c +9 -0
index 655c09393ad5..eb383695659a 100644
@@ -2577,6 +2577,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	bool passed_oom = false;
 	bool may_swap = true;
 	bool drained = false;
+	bool raised_max_event = false;
 	unsigned long pflags;
 
 retry:
@@ -2616,6 +2617,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		goto nomem;
 
 	memcg_memory_event(mem_over_limit, MEMCG_MAX);
+	raised_max_event = true;
 
 	psi_memstall_enter(&pflags);
 	nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
@@ -2682,6 +2684,13 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
 		return -ENOMEM;
 force:
+	/*
+	 * If the allocation has to be enforced, don't forget to raise
+	 * a MEMCG_MAX event.
+	 */
+	if (!raised_max_event)
+		memcg_memory_event(mem_over_limit, MEMCG_MAX);
+
 	/*
 	 * The allocation either can't fail or will lead to more memory
 	 * being freed very soon.  Allow memory usage go over the limit
Signature · 1 line
2.36.1
#1 lore raw
Quoted 10 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Quoted 57 lines

So the cgroup can
significantly exceed the memory.max limit without even triggering MEMCG_MAX events.

Fix this by making sure that we never enforce allocations without raising a MEMCG_MAX event.

---
 mm/memcontrol.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/memcontrol.c +9 -1
index 655c09393ad5..eb383695659a 100644
@@ -2577,6 +2577,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
        bool passed_oom = false;
        bool may_swap = true;
        bool drained = false;
+       bool raised_max_event = false;
        unsigned long pflags;
 
 retry:
@@ -2616,6 +2617,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
                goto nomem;
 
        memcg_memory_event(mem_over_limit, MEMCG_MAX);
+       raised_max_event = true;
 
        psi_memstall_enter(&pflags);
        nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
@@ -2682,6 +2684,13 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
        if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
                return -ENOMEM;
 force:
+       /*
+        * If the allocation has to be enforced, don't forget to raise
+        * a MEMCG_MAX event.
+        */
+       if (!raised_max_event)
+               memcg_memory_event(mem_over_limit, MEMCG_MAX);
+
        /*
         * The allocation either can't fail or will lead to more memory
         * being freed very soon.  Allow memory usage go over the limit
--

2.36.1

author maintainer
#2 lore raw
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Thanks!

Acked-by
#3 lore raw
Quoted 17 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Quoted 9 lines

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

SGTM and you can add:

author maintainer
#4 lore raw
Quoted 28 lines · Shakeel Butt
Quoted 17 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Quoted 9 lines

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 3 lines

SGTM and you can add:

Thank you!

#5 lore raw
In reply to Roman Gushchin
Quoted 16 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Quoted 8 lines

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely? How can it ever go away then? Also is this something that we actually want to encourage?

In other words shouldn't those remote charges be redirected when the target memcg is offline?

Signature · 2 lines
Michal Hocko
SUSE Labs
#6 lore raw
Quoted 26 lines · Michal Hocko
Quoted 16 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Quoted 8 lines

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 3 lines

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely? How can it ever go away then? Also is this something that we actually want to encourage?

One more question. Mostly out of curiosity. How is userspace actually acting on those events? Are watchers still active on those dead memcgs?

Signature · 2 lines
Michal Hocko
SUSE Labs
author maintainer
#7 lore raw
Quoted 27 lines · Michal Hocko
Quoted 26 lines · Michal Hocko
Quoted 24 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 6 lines
Quoted 3 lines

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely? How can it ever go away then? Also is this something that we actually want to encourage?

One more question. Mostly out of curiosity. How is userspace actually acting on those events? Are watchers still active on those dead memcgs?

Idk, the whole problem was reported by Yafang, so he probably has a better answer. But in general events are recursive and the cgroup doesn't have to be dying, it can be simple abandoned.

Thanks!

#8 lore raw
Quoted 33 lines · Roman Gushchin
Quoted 31 lines · Michal Hocko
Quoted 29 lines · Michal Hocko
Quoted 16 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Quoted 8 lines

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely? How can it ever go away then? Also is this something that we actually want to encourage?

Quoted 7 lines
Quoted 2 lines

One more question. Mostly out of curiosity. How is userspace actually acting on those events? Are watchers still active on those dead memcgs?

Idk, the whole problem was reported by Yafang, so he probably has a better answer. But in general events are recursive and the cgroup doesn't have to be dying, it can be simple abandoned.

Regarding the pinned bpf programs, it can run without a user agent. That means the cgroup may not be dead, but just not populated. (But in our case, the cgroup will be deleted after the user agent exits.)

Signature · 2 lines
Regards
Yafang
#9 lore raw
Quoted 37 lines · Yafang Shao
Quoted 36 lines · Roman Gushchin
Quoted 33 lines · Michal Hocko
Quoted 26 lines · Michal Hocko
Quoted 16 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Quoted 8 lines

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 3 lines

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely? How can it ever go away then? Also is this something that we actually want to encourage?

One more question. Mostly out of curiosity. How is userspace actually acting on those events? Are watchers still active on those dead memcgs?

Quoted 8 lines
Quoted 4 lines

Idk, the whole problem was reported by Yafang, so he probably has a better answer. But in general events are recursive and the cgroup doesn't have to be dying, it can be simple abandoned.

Regarding the pinned bpf programs, it can run without a user agent. That means the cgroup may not be dead, but just not populated. (But in our case, the cgroup will be deleted after the user agent exits.)

OK, that makes sense.

Signature · 2 lines
Michal Hocko
SUSE Labs
author maintainer
#10 lore raw
In reply to Michal Hocko
Quoted 20 lines · Michal Hocko
Quoted 19 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

Quoted 8 lines
Quoted 5 lines

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely?

Yes.

Quoted 1 line

How can it ever go away then?

Bpf map should be deleted by a user first.

Quoted 1 line

Also is this something that we actually want to encourage?

Not really. We can implement reparenting (probably objcg-based), I think it's a good idea in general. I can take a look, but can't promise it will be fast.

In thory we can't forbid deleting cgroups with associated bpf maps, but I don't thinks it's a good idea.

Quoted 2 lines

In other words shouldn't those remote charges be redirected when the target memcg is offline?

Reparenting is the best answer I have.

Thanks!

#11 lore raw
Quoted 28 lines · Roman Gushchin
Quoted 26 lines · Michal Hocko
Quoted 24 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 9 lines
Quoted 2 lines

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely?

Yes.

Quoted 1 line

How can it ever go away then?

Bpf map should be deleted by a user first.

It can't apply to pinned bpf maps, because the user expects the bpf maps to continue working after the user agent exits.

Quoted 8 lines
Quoted 1 line

Also is this something that we actually want to encourage?

Not really. We can implement reparenting (probably objcg-based), I think it's a good idea in general. I can take a look, but can't promise it will be fast.

In thory we can't forbid deleting cgroups with associated bpf maps, but I don't thinks it's a good idea.

Agreed. It is not a good idea.

Quoted 5 lines
Quoted 2 lines

In other words shouldn't those remote charges be redirected when the target memcg is offline?

Reparenting is the best answer I have.

At the cost of increasing the complexity of deployment, that may not be a good idea neither.

Signature · 2 lines
Regards
Yafang
author maintainer
#12 lore raw
Quoted 53 lines · Yafang Shao
Quoted 28 lines · Roman Gushchin
Quoted 26 lines · Michal Hocko
Quoted 24 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 9 lines
Quoted 2 lines

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely?

Yes.

Quoted 1 line

How can it ever go away then?

Bpf map should be deleted by a user first.

It can't apply to pinned bpf maps, because the user expects the bpf maps to continue working after the user agent exits.

Quoted 8 lines
Quoted 1 line

Also is this something that we actually want to encourage?

Not really. We can implement reparenting (probably objcg-based), I think it's a good idea in general. I can take a look, but can't promise it will be fast.

In thory we can't forbid deleting cgroups with associated bpf maps, but I don't thinks it's a good idea.

Agreed. It is not a good idea.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

In other words shouldn't those remote charges be redirected when the target memcg is offline?

Reparenting is the best answer I have.

At the cost of increasing the complexity of deployment, that may not be a good idea neither.

What do you mean? Can you please elaborate on it?

Thanks!

#13 lore raw
Quoted 58 lines · Roman Gushchin
Quoted 56 lines · Yafang Shao
Quoted 28 lines · Roman Gushchin
Quoted 26 lines · Michal Hocko
Quoted 24 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 9 lines
Quoted 2 lines

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely?

Yes.

Quoted 1 line

How can it ever go away then?

Bpf map should be deleted by a user first.

It can't apply to pinned bpf maps, because the user expects the bpf maps to continue working after the user agent exits.

Quoted 8 lines
Quoted 1 line

Also is this something that we actually want to encourage?

Not really. We can implement reparenting (probably objcg-based), I think it's a good idea in general. I can take a look, but can't promise it will be fast.

In thory we can't forbid deleting cgroups with associated bpf maps, but I don't thinks it's a good idea.

Agreed. It is not a good idea.

Quoted 3 lines
Quoted 2 lines

In other words shouldn't those remote charges be redirected when the target memcg is offline?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Reparenting is the best answer I have.

At the cost of increasing the complexity of deployment, that may not be a good idea neither.

What do you mean? Can you please elaborate on it?

parent memcg
|
bpf memcg <- limit the memory size of bpf
programs
/ \
bpf user agent pinned bpf program

After bpf user agents exit, the bpf memcg will be dead, and then all its memory will be reparented.
That is okay for preallocated bpf maps, but not okay for non-preallocated bpf maps.
Because the bpf maps will continue to charge, but as all its memory and objcg are reparented, so we have to limit the bpf memory size in the parent as follows,

parent memcg <- limit the memory size of bpf
programs

                         |
                    bpf memcg
                        /           \
         bpf user agent     pinned bpf program

That means parent memcg can't be deleted and can only contain one bpf memcg. It may work if we use systemd to manage the memcgs, but it will be a problem if we use k8s to manage the memcgs.

Signature · 2 lines
Regards
Yafang
author maintainer
#14 lore raw
Quoted 75 lines · Yafang Shao
Quoted 58 lines · Roman Gushchin
Quoted 56 lines · Yafang Shao
Quoted 28 lines · Roman Gushchin
Quoted 26 lines · Michal Hocko
Quoted 24 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 9 lines
Quoted 2 lines

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely?

Yes.

Quoted 1 line

How can it ever go away then?

Bpf map should be deleted by a user first.

It can't apply to pinned bpf maps, because the user expects the bpf maps to continue working after the user agent exits.

Quoted 8 lines
Quoted 1 line

Also is this something that we actually want to encourage?

Not really. We can implement reparenting (probably objcg-based), I think it's a good idea in general. I can take a look, but can't promise it will be fast.

In thory we can't forbid deleting cgroups with associated bpf maps, but I don't thinks it's a good idea.

Agreed. It is not a good idea.

Quoted 3 lines
Quoted 2 lines

In other words shouldn't those remote charges be redirected when the target memcg is offline?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Reparenting is the best answer I have.

At the cost of increasing the complexity of deployment, that may not be a good idea neither.

What do you mean? Can you please elaborate on it?

parent memcg
|
bpf memcg <- limit the memory size of bpf
programs
/ \
bpf user agent pinned bpf program

Quoted 7 lines

After bpf user agents exit, the bpf memcg will be dead, and then all its memory will be reparented.
That is okay for preallocated bpf maps, but not okay for non-preallocated bpf maps.
Because the bpf maps will continue to charge, but as all its memory and objcg are reparented, so we have to limit the bpf memory size in the parent as follows,

So you're relying on the memory limit of a dying cgroup?
Sorry, but I don't think we can seriously discuss such a design. A dying cgroup is invisible for a user, a user can't change any tunables, they have zero visibility into any stats or charges. Why would you do this?

If you want the cgroup to be an active part of the memory management process, don't delete it. There are exactly zero guarantees about what happens with a memory cgroup after being deleted by a user, it's all implementation details.

Anyway, here is the patch for reparenting bpf maps:
https://github.com/rgushchin/linux/commit/f57df8bb35770507a4624fe52216b6c14f39c50c

I gonna post it to bpf@ after some testing.

Thanks!

#15 lore raw
Quoted 85 lines · Roman Gushchin
Quoted 75 lines · Yafang Shao
Quoted 58 lines · Roman Gushchin
Quoted 56 lines · Yafang Shao
Quoted 28 lines · Roman Gushchin
Quoted 26 lines · Michal Hocko
Quoted 24 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 9 lines
Quoted 2 lines

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely?

Yes.

Quoted 1 line

How can it ever go away then?

Bpf map should be deleted by a user first.

It can't apply to pinned bpf maps, because the user expects the bpf maps to continue working after the user agent exits.

Quoted 8 lines
Quoted 1 line

Also is this something that we actually want to encourage?

Not really. We can implement reparenting (probably objcg-based), I think it's a good idea in general. I can take a look, but can't promise it will be fast.

In thory we can't forbid deleting cgroups with associated bpf maps, but I don't thinks it's a good idea.

Agreed. It is not a good idea.

Quoted 3 lines
Quoted 2 lines

In other words shouldn't those remote charges be redirected when the target memcg is offline?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Reparenting is the best answer I have.

At the cost of increasing the complexity of deployment, that may not be a good idea neither.

What do you mean? Can you please elaborate on it?

parent memcg
|
bpf memcg <- limit the memory size of bpf
programs
/ \
bpf user agent pinned bpf program

Quoted 7 lines

After bpf user agents exit, the bpf memcg will be dead, and then all its memory will be reparented.
That is okay for preallocated bpf maps, but not okay for non-preallocated bpf maps.
Because the bpf maps will continue to charge, but as all its memory and objcg are reparented, so we have to limit the bpf memory size in the parent as follows,

Quoted 1 line

So you're relying on the memory limit of a dying cgroup?

No. I didn't say it. What I said is you can't use a dying cgroup to limit it, that's why I said that we have to use parant memcg to limit it.

Quoted 9 lines

Sorry, but I don't think we can seriously discuss such a design. A dying cgroup is invisible for a user, a user can't change any tunables, they have zero visibility into any stats or charges. Why would you do this?

If you want the cgroup to be an active part of the memory management process, don't delete it. There are exactly zero guarantees about what happens with a memory cgroup after being deleted by a user, it's all implementation details.

Quoted 5 lines

Anyway, here is the patch for reparenting bpf maps:
https://github.com/rgushchin/linux/commit/f57df8bb35770507a4624fe52216b6c14f39c50c

I gonna post it to bpf@ after some testing.

I will take a look at it.
But AFAIK the reparenting can't resolve the problem of non-preallocated maps.

Signature · 2 lines
Regards
Yafang
author maintainer
#16 lore raw
Quoted 101 lines · Yafang Shao
Quoted 85 lines · Roman Gushchin
Quoted 75 lines · Yafang Shao
Quoted 58 lines · Roman Gushchin
Quoted 56 lines · Yafang Shao
Quoted 28 lines · Roman Gushchin
Quoted 26 lines · Michal Hocko
Quoted 24 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 9 lines
Quoted 2 lines

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely?

Yes.

Quoted 1 line

How can it ever go away then?

Bpf map should be deleted by a user first.

It can't apply to pinned bpf maps, because the user expects the bpf maps to continue working after the user agent exits.

Quoted 8 lines
Quoted 1 line

Also is this something that we actually want to encourage?

Not really. We can implement reparenting (probably objcg-based), I think it's a good idea in general. I can take a look, but can't promise it will be fast.

In thory we can't forbid deleting cgroups with associated bpf maps, but I don't thinks it's a good idea.

Agreed. It is not a good idea.

Quoted 3 lines
Quoted 2 lines

In other words shouldn't those remote charges be redirected when the target memcg is offline?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Reparenting is the best answer I have.

At the cost of increasing the complexity of deployment, that may not be a good idea neither.

What do you mean? Can you please elaborate on it?

parent memcg
|
bpf memcg <- limit the memory size of bpf
programs
/ \
bpf user agent pinned bpf program

Quoted 7 lines

After bpf user agents exit, the bpf memcg will be dead, and then all its memory will be reparented.
That is okay for preallocated bpf maps, but not okay for non-preallocated bpf maps.
Because the bpf maps will continue to charge, but as all its memory and objcg are reparented, so we have to limit the bpf memory size in the parent as follows,

Quoted 1 line

So you're relying on the memory limit of a dying cgroup?

No. I didn't say it. What I said is you can't use a dying cgroup to limit it, that's why I said that we have to use parant memcg to limit it.

Quoted 9 lines

Sorry, but I don't think we can seriously discuss such a design. A dying cgroup is invisible for a user, a user can't change any tunables, they have zero visibility into any stats or charges. Why would you do this?

If you want the cgroup to be an active part of the memory management process, don't delete it. There are exactly zero guarantees about what happens with a memory cgroup after being deleted by a user, it's all implementation details.

Quoted 8 lines
Quoted 5 lines

Anyway, here is the patch for reparenting bpf maps:
https://github.com/rgushchin/linux/commit/f57df8bb35770507a4624fe52216b6c14f39c50c

I gonna post it to bpf@ after some testing.

I will take a look at it.
But AFAIK the reparenting can't resolve the problem of non-preallocated maps.

Sorry, what's the problem then?

Michal asked how we can prevent an indefinite pinning of a dying memcg by an associated bpf map being used by other processes, and I guess the objcg-based reparenting is the best answer here. You said it will complicate the deployment? What does it mean?

From a user's POV there is no visible difference. What am I missing here? Yes, if we reparent the bpf map, memory.max of the original memory cgroup will not apply, but as I said, if you want it to be effective, don't delete the cgroup.

Thanks!

#17 lore raw
Quoted 106 lines · Roman Gushchin
Quoted 104 lines · Yafang Shao
Quoted 85 lines · Roman Gushchin
Quoted 75 lines · Yafang Shao
Quoted 58 lines · Roman Gushchin
Quoted 56 lines · Yafang Shao
Quoted 28 lines · Roman Gushchin
Quoted 26 lines · Michal Hocko
Quoted 24 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

Quoted 7 lines
Quoted 4 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen.

The patch looks good but the above sentence is confusing. What might never happen? Reclaim or MAX event on dying memcg?

Direct reclaim and MAX events. I agree it might be not clear without looking into the code. How about something like this?

"It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the direct reclaim and MEMCG_MAX events will be raised. However a bpf map can belong to a dying/abandoned memory cgroup, so there will be no allocations from a process context and no MEMCG_MAX events will be triggered."

Quoted 9 lines
Quoted 2 lines

Could you expand little bit more on the situation? Can those charges to offline memcg happen indefinetely?

Yes.

Quoted 1 line

How can it ever go away then?

Bpf map should be deleted by a user first.

It can't apply to pinned bpf maps, because the user expects the bpf maps to continue working after the user agent exits.

Quoted 8 lines
Quoted 1 line

Also is this something that we actually want to encourage?

Not really. We can implement reparenting (probably objcg-based), I think it's a good idea in general. I can take a look, but can't promise it will be fast.

In thory we can't forbid deleting cgroups with associated bpf maps, but I don't thinks it's a good idea.

Agreed. It is not a good idea.

Quoted 3 lines
Quoted 2 lines

In other words shouldn't those remote charges be redirected when the target memcg is offline?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Reparenting is the best answer I have.

At the cost of increasing the complexity of deployment, that may not be a good idea neither.

What do you mean? Can you please elaborate on it?

parent memcg
|
bpf memcg <- limit the memory size of bpf
programs
/ \
bpf user agent pinned bpf program

Quoted 7 lines

After bpf user agents exit, the bpf memcg will be dead, and then all its memory will be reparented.
That is okay for preallocated bpf maps, but not okay for non-preallocated bpf maps.
Because the bpf maps will continue to charge, but as all its memory and objcg are reparented, so we have to limit the bpf memory size in the parent as follows,

Quoted 1 line

So you're relying on the memory limit of a dying cgroup?

No. I didn't say it. What I said is you can't use a dying cgroup to limit it, that's why I said that we have to use parant memcg to limit it.

Quoted 12 lines

Sorry, but I don't think we can seriously discuss such a design. A dying cgroup is invisible for a user, a user can't change any tunables, they have zero visibility into any stats or charges. Why would you do this?

If you want the cgroup to be an active part of the memory management process, don't delete it. There are exactly zero guarantees about what happens with a memory cgroup after being deleted by a user, it's all implementation details.

Anyway, here is the patch for reparenting bpf maps:
https://github.com/rgushchin/linux/commit/f57df8bb35770507a4624fe52216b6c14f39c50c

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I gonna post it to bpf@ after some testing.

I will take a look at it.
But AFAIK the reparenting can't resolve the problem of non-preallocated maps.

Sorry, what's the problem then?

The problem is, the bpf memcg or its parent memcg can't be destroyed currently. IOW, you have to forbid the user to rmdir.

Reparenting is an improvement for the preallocated bpf map, because all its memory is charged, so the memg is useless any more. So it can be destroyed and thus the reparenting is an improvement.

But for the non-preallocated bpf map, the memcg still has to do the limit work, that means, it can't be destroyed currently. If you reparent it, then the parent can't be destroyed. So why not forbid destroying the bpf memcg in the first place?
The reparenting just increases the complexity for this case.

Quoted 4 lines

Michal asked how we can prevent an indefinite pinning of a dying memcg by an associated bpf map being used by other processes, and I guess the objcg-based reparenting is the best answer here. You said it will complicate the deployment? What does it mean?

See my reply above.

Quoted 4 lines

From a user's POV there is no visible difference. What am I missing here? Yes, if we reparent the bpf map, memory.max of the original memory cgroup will not apply, but as I said, if you want it to be effective, don't delete the cgroup.

Signature · 2 lines
Regards
Yafang
#18 lore raw
In reply to Roman Gushchin
Quoted 5 lines · Roman Gushchin

Anyway, here is the patch for reparenting bpf maps:
https://github.com/rgushchin/linux/commit/f57df8bb35770507a4624fe52216b6c14f39c50c

I gonna post it to bpf@ after some testing.

Please do. It looks good.
It needs #ifdef CONFIG_MEMCG_KMEM
because get_obj_cgroup_from_current() is undefined otherwise. Ideally just adding a static inline to a .h ?

and
if (map->objcg)
memcg = get_mem_cgroup_from_objcg(map->objcg);

or !NULL check inside get_mem_cgroup_from_objcg()
which would be better.

author maintainer
#19 lore raw
Quoted 11 lines · Alexei Starovoitov
Quoted 5 lines · Roman Gushchin

Anyway, here is the patch for reparenting bpf maps:
https://github.com/rgushchin/linux/commit/f57df8bb35770507a4624fe52216b6c14f39c50c

I gonna post it to bpf@ after some testing.

Please do. It looks good.
It needs #ifdef CONFIG_MEMCG_KMEM
because get_obj_cgroup_from_current() is undefined otherwise. Ideally just adding a static inline to a .h ?

Actually all call sites are already under CONFIG_MEMCG_KMEM.

Quoted 7 lines

and
if (map->objcg)
memcg = get_mem_cgroup_from_objcg(map->objcg);

or !NULL check inside get_mem_cgroup_from_objcg()
which would be better.

Yes, you're right, as now we need to handle it specially.

In the near future it won't be necessary. There are patches in mm-unstable which make objcg API useful outside of CONFIG_MEMCG_KMEM. In particular it means that objcg will be created for the root_mem_cgroup. So map->objcg can always point at a valid objcg and we will be able to drop this check.

Will post an updated version shortly.

Thanks!

Acked-by on v1 1/1
#20 lore raw
In reply to Roman Gushchin
Quoted 4 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

So I guess this will be a GFP_ATOMIC request failing due to the hard limit, right? I think it would be easier to understand if the specific allocation request type was mentioned.

Quoted 6 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen. So the cgroup can significantly exceed the memory.max limit without even triggering MEMCG_MAX events.

More on that in other reply.

Quoted 12 lines

Fix this by making sure that we never enforce allocations without raising a MEMCG_MAX event.

The patch makes sense to me though even without the weird charge to a dead memcg aspect. It is true that a very calm memcg can trigger the even much later after a GFP_ATOMIC charge (or __GFP_HIGH in general) fails.

Quoted 40 lines
---
 mm/memcontrol.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/memcontrol.c +9 -0
index 655c09393ad5..eb383695659a 100644
@@ -2577,6 +2577,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	bool passed_oom = false;
 	bool may_swap = true;
 	bool drained = false;
+	bool raised_max_event = false;
 	unsigned long pflags;
 
 retry:
@@ -2616,6 +2617,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		goto nomem;
 
 	memcg_memory_event(mem_over_limit, MEMCG_MAX);
+	raised_max_event = true;
 
 	psi_memstall_enter(&pflags);
 	nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
@@ -2682,6 +2684,13 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
 		return -ENOMEM;
 force:
+	/*
+	 * If the allocation has to be enforced, don't forget to raise
+	 * a MEMCG_MAX event.
+	 */
+	if (!raised_max_event)
+		memcg_memory_event(mem_over_limit, MEMCG_MAX);
+
 	/*
 	 * The allocation either can't fail or will lead to more memory
 	 * being freed very soon.  Allow memory usage go over the limit
Signature · 1 line
2.36.1
Signature · 2 lines
Michal Hocko
SUSE Labs
author maintainer
#21 lore raw
Quoted 9 lines · Michal Hocko
Quoted 4 lines · Roman Gushchin

Yafang Shao reported an issue related to the accounting of bpf
memory: if a bpf map is charged indirectly for memory consumed from an interrupt context and allocations are enforced, MEMCG_MAX events are not raised.

So I guess this will be a GFP_ATOMIC request failing due to the hard limit, right? I think it would be easier to understand if the specific allocation request type was mentioned.

It all started from the discussion here:
https://www.spinics.net/lists/linux-mm/msg302319.html

Please, take a look.

Quoted 23 lines
Quoted 7 lines

It's not/less of an issue in a generic case because consequent allocations from a process context will trigger the reclaim and MEMCG_MAX events. However a bpf map can belong to a dying/abandoned memory cgroup, so it might never happen. So the cgroup can significantly exceed the memory.max limit without even triggering MEMCG_MAX events.

More on that in other reply.

Quoted 12 lines

Fix this by making sure that we never enforce allocations without raising a MEMCG_MAX event.

Quoted 4 lines

The patch makes sense to me though even without the weird charge to a dead memcg aspect. It is true that a very calm memcg can trigger the even much later after a GFP_ATOMIC charge (or __GFP_HIGH in general) fails.

Good point!

Quoted 2 lines

Thanks!