diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index a2dc64de5213f3600e79839ce3f8d26b52708344..4059f94e9bb5bf7316227984151052b47e321b47 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -991,11 +991,15 @@ static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
 		err = -EINVAL;
 		goto free_htab;
 	}
+	err = bpf_map_charge_init(&htab->map.memory, cost);
+	if (err)
+		goto free_htab;
 
 	htab->buckets = bpf_map_area_alloc(htab->buckets_num *
 					   sizeof(struct bpf_htab_bucket),
 					   htab->map.numa_node);
 	if (!htab->buckets) {
+		bpf_map_charge_finish(&htab->map.memory);
 		err = -ENOMEM;
 		goto free_htab;
 	}