]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm: memcg: simplify objcg charge size and stock remainder math
authorJohannes Weiner <jweiner@meta.com>
Mon, 2 Mar 2026 19:50:15 +0000 (14:50 -0500)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 5 Apr 2026 20:53:16 +0000 (13:53 -0700)
Use PAGE_ALIGN() and a more natural cache remainder calculation.

Link: https://lkml.kernel.org/r/20260302195305.620713-3-hannes@cmpxchg.org
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Roman Gushchin <roman.gushchin@linux.dev>
Reviewed-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
Reviewed-by: Hao Li <hao.li@linux.dev>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/memcontrol.c

index 5262533d0828718ca992121e18376a684e7f9403..5dd61e35f50d1ef2d35f011b35a12348ca18712a 100644 (file)
@@ -3159,7 +3159,7 @@ out:
 static int obj_cgroup_charge_account(struct obj_cgroup *objcg, gfp_t gfp, size_t size,
                                     struct pglist_data *pgdat, enum node_stat_item idx)
 {
-       unsigned int nr_pages, nr_bytes;
+       size_t charge_size, remainder;
        int ret;
 
        if (likely(consume_obj_stock(objcg, size, pgdat, idx)))
@@ -3188,16 +3188,12 @@ static int obj_cgroup_charge_account(struct obj_cgroup *objcg, gfp_t gfp, size_t
         * bytes is (sizeof(object) + PAGE_SIZE - 2) if there is no data
         * race.
         */
-       nr_pages = size >> PAGE_SHIFT;
-       nr_bytes = size & (PAGE_SIZE - 1);
+       charge_size = PAGE_ALIGN(size);
+       remainder = charge_size - size;
 
-       if (nr_bytes)
-               nr_pages += 1;
-
-       ret = obj_cgroup_charge_pages(objcg, gfp, nr_pages);
-       if (!ret && (nr_bytes || pgdat))
-               refill_obj_stock(objcg, nr_bytes ? PAGE_SIZE - nr_bytes : 0,
-                                        false, size, pgdat, idx);
+       ret = obj_cgroup_charge_pages(objcg, gfp, charge_size >> PAGE_SHIFT);
+       if (!ret && (remainder || pgdat))
+               refill_obj_stock(objcg, remainder, false, size, pgdat, idx);
 
        return ret;
 }