OSDN Git Service

bpf: move memory size checks to bpf_map_charge_init()
[tomoyo/tomoyo-test1.git] / net / core / bpf_sk_storage.c
index cc9597a..f40e3d3 100644 (file)
@@ -627,6 +627,7 @@ static struct bpf_map *bpf_sk_storage_map_alloc(union bpf_attr *attr)
        unsigned int i;
        u32 nbuckets;
        u64 cost;
+       int ret;
 
        smap = kzalloc(sizeof(*smap), GFP_USER | __GFP_NOWARN);
        if (!smap)
@@ -635,13 +636,21 @@ static struct bpf_map *bpf_sk_storage_map_alloc(union bpf_attr *attr)
 
        smap->bucket_log = ilog2(roundup_pow_of_two(num_possible_cpus()));
        nbuckets = 1U << smap->bucket_log;
+       cost = sizeof(*smap->buckets) * nbuckets + sizeof(*smap);
+
+       ret = bpf_map_charge_init(&smap->map.memory, cost);
+       if (ret < 0) {
+               kfree(smap);
+               return ERR_PTR(ret);
+       }
+
        smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets,
                                 GFP_USER | __GFP_NOWARN);
        if (!smap->buckets) {
+               bpf_map_charge_finish(&smap->map.memory);
                kfree(smap);
                return ERR_PTR(-ENOMEM);
        }
-       cost = sizeof(*smap->buckets) * nbuckets + sizeof(*smap);
 
        for (i = 0; i < nbuckets; i++) {
                INIT_HLIST_HEAD(&smap->buckets[i].list);
@@ -651,7 +660,6 @@ static struct bpf_map *bpf_sk_storage_map_alloc(union bpf_attr *attr)
        smap->elem_size = sizeof(struct bpf_sk_storage_elem) + attr->value_size;
        smap->cache_idx = (unsigned int)atomic_inc_return(&cache_idx) %
                BPF_SK_STORAGE_CACHE_SIZE;
-       smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
 
        return &smap->map;
 }