From: Johannes Weiner Date: Mon, 2 Mar 2026 19:50:15 +0000 (-0500) Subject: mm: memcg: simplify objcg charge size and stock remainder math X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9d181e47098d6911f4a5b3d9feff60c2bf6786a2;p=thirdparty%2Fkernel%2Flinux.git mm: memcg: simplify objcg charge size and stock remainder math 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 Acked-by: Shakeel Butt Acked-by: Roman Gushchin Reviewed-by: Vlastimil Babka (SUSE) Reviewed-by: Hao Li Cc: Michal Hocko Cc: Muchun Song Signed-off-by: Andrew Morton --- diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 5262533d08287..5dd61e35f50d1 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -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; }