History

alloc_tag: handle module codetag load errors as module load failures

Failures inside codetag_load_module() are currently ignored. As a result an error there would not cause a module load failure and freeing of the associated resources. Correct this behavior by propagating the error code to the caller and handling possible errors. With this change, error to allocate percpu counters, which happens at this stage, will not be ignored and will cause a module load failure and freeing of resources. With this change we also do not need to disable memory allocation profiling when this error happens, instead we fail to load the module.

Suren Baghdasaryan committed 1 year ago

Commit 044d2ae1 parent 9c49e5d4 files · +39 −20Browse files

Unified · Split

4 files
  1. include/linux/codetag.h +4 −4
  2. kernel/module/main.c +3 −2
  3. lib/alloc_tag.c +7 −5
  4. lib/codetag.c +25 −9
include/linux/codetag.h+4−4
@@ -36,8 +36,8 @@ union codetag_ref { · 35 unmodified lines
36struct codetag_type_desc {36struct codetag_type_desc {
37 const char *section;37 const char *section;
38 size_t tag_size;38 size_t tag_size;
39 void (*module_load)(struct module *mod,39 int (*module_load)(struct module *mod,
40 struct codetag *start, struct codetag *end);40 struct codetag *start, struct codetag *end);
41 void (*module_unload)(struct module *mod,41 void (*module_unload)(struct module *mod,
42 struct codetag *start, struct codetag *end);42 struct codetag *start, struct codetag *end);
43#ifdef CONFIG_MODULES43#ifdef CONFIG_MODULES
@@ -89,7 +89,7 @@ void *codetag_alloc_module_section(struct module *mod, const char *name, · 45 unmodified lines
89 unsigned long align);89 unsigned long align);
90void codetag_free_module_sections(struct module *mod);90void codetag_free_module_sections(struct module *mod);
91void codetag_module_replaced(struct module *mod, struct module *new_mod);91void codetag_module_replaced(struct module *mod, struct module *new_mod);
92void codetag_load_module(struct module *mod);92int codetag_load_module(struct module *mod);
93void codetag_unload_module(struct module *mod);93void codetag_unload_module(struct module *mod);
94 94
95#else /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */95#else /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */
@@ -103,7 +103,7 @@ codetag_alloc_module_section(struct module *mod, const char *name, · 7 unmodified lines
103 unsigned long align) { return NULL; }103 unsigned long align) { return NULL; }
104static inline void codetag_free_module_sections(struct module *mod) {}104static inline void codetag_free_module_sections(struct module *mod) {}
105static inline void codetag_module_replaced(struct module *mod, struct module *new_mod) {}105static inline void codetag_module_replaced(struct module *mod, struct module *new_mod) {}
106static inline void codetag_load_module(struct module *mod) {}106static inline int codetag_load_module(struct module *mod) { return 0; }
107static inline void codetag_unload_module(struct module *mod) {}107static inline void codetag_unload_module(struct module *mod) {}
108 108
109#endif /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */109#endif /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */
kernel/module/main.c+3−2
@@ -3386,11 +3386,12 @@ static int load_module(struct load_info *info, const char __user *uargs, · 3385 unmodified lines
3386 goto sysfs_cleanup;3386 goto sysfs_cleanup;
3387 }3387 }
3388 3388
3389 if (codetag_load_module(mod))
3390 goto sysfs_cleanup;
3391
3389 /* Get rid of temporary copy. */3392 /* Get rid of temporary copy. */
3390 free_copy(info, flags);3393 free_copy(info, flags);
3391 3394
3392 codetag_load_module(mod);
3393
3394 /* Done! */3395 /* Done! */
3395 trace_module_load(mod);3396 trace_module_load(mod);
3396 3397
lib/alloc_tag.c+7−5
@@ -607,15 +607,16 @@ out: · 606 unmodified lines
607 mas_unlock(&mas);607 mas_unlock(&mas);
608}608}
609 609
610static void load_module(struct module *mod, struct codetag *start, struct codetag *stop)610static int load_module(struct module *mod, struct codetag *start, struct codetag *stop)
611{611{
612 /* Allocate module alloc_tag percpu counters */612 /* Allocate module alloc_tag percpu counters */
613 struct alloc_tag *start_tag;613 struct alloc_tag *start_tag;
614 struct alloc_tag *stop_tag;614 struct alloc_tag *stop_tag;
615 struct alloc_tag *tag;615 struct alloc_tag *tag;
616 616
617 /* percpu counters for core allocations are already statically allocated */
617 if (!mod)618 if (!mod)
618 return;619 return 0;
619 620
620 start_tag = ct_to_alloc_tag(start);621 start_tag = ct_to_alloc_tag(start);
621 stop_tag = ct_to_alloc_tag(stop);622 stop_tag = ct_to_alloc_tag(stop);
@@ -627,12 +628,13 @@ static void load_module(struct module *mod, struct codetag *start, struct codeta · 5 unmodified lines
627 free_percpu(tag->counters);628 free_percpu(tag->counters);
628 tag->counters = NULL;629 tag->counters = NULL;
629 }630 }
630 shutdown_mem_profiling(true);631 pr_err("Failed to allocate memory for allocation tag percpu counters in the module %s\n",
631 pr_err("Failed to allocate memory for allocation tag percpu counters in the module %s. Memory allocation profiling is disabled!\n",
632 mod->name);632 mod->name);
633 break;633 return -ENOMEM;
634 }634 }
635 }635 }
636
637 return 0;
636}638}
637 639
638static void replace_module(struct module *mod, struct module *new_mod)640static void replace_module(struct module *mod, struct module *new_mod)
lib/codetag.c+25−9
@@ -167,6 +167,7 @@ static int codetag_module_init(struct codetag_type *cttype, struct module *mod) · 166 unmodified lines
167{167{
168 struct codetag_range range;168 struct codetag_range range;
169 struct codetag_module *cmod;169 struct codetag_module *cmod;
170 int mod_id;
170 int err;171 int err;
171 172
172 range = get_section_range(mod, cttype->desc.section);173 range = get_section_range(mod, cttype->desc.section);
@@ -190,11 +191,20 @@ static int codetag_module_init(struct codetag_type *cttype, struct module *mod) · 17 unmodified lines
190 cmod->range = range;191 cmod->range = range;
191 192
192 down_write(&cttype->mod_lock);193 down_write(&cttype->mod_lock);
193 err = idr_alloc(&cttype->mod_idr, cmod, 0, 0, GFP_KERNEL);194 mod_id = idr_alloc(&cttype->mod_idr, cmod, 0, 0, GFP_KERNEL);
194 if (err >= 0) {195 if (mod_id >= 0) {
195 cttype->count += range_size(cttype, &range);196 if (cttype->desc.module_load) {
196 if (cttype->desc.module_load)197 err = cttype->desc.module_load(mod, range.start, range.stop);
197 cttype->desc.module_load(mod, range.start, range.stop);198 if (!err)
199 cttype->count += range_size(cttype, &range);
200 else
201 idr_remove(&cttype->mod_idr, mod_id);
202 } else {
203 cttype->count += range_size(cttype, &range);
204 err = 0;
205 }
206 } else {
207 err = mod_id;
198 }208 }
199 up_write(&cttype->mod_lock);209 up_write(&cttype->mod_lock);
200 210
@@ -295,17 +305,23 @@ void codetag_module_replaced(struct module *mod, struct module *new_mod) · 94 unmodified lines
295 mutex_unlock(&codetag_lock);305 mutex_unlock(&codetag_lock);
296}306}
297 307
298void codetag_load_module(struct module *mod)308int codetag_load_module(struct module *mod)
299{309{
300 struct codetag_type *cttype;310 struct codetag_type *cttype;
311 int ret = 0;
301 312
302 if (!mod)313 if (!mod)
303 return;314 return 0;
304 315
305 mutex_lock(&codetag_lock);316 mutex_lock(&codetag_lock);
306 list_for_each_entry(cttype, &codetag_types, link)317 list_for_each_entry(cttype, &codetag_types, link) {
307 codetag_module_init(cttype, mod);318 ret = codetag_module_init(cttype, mod);
319 if (ret)
320 break;
321 }
308 mutex_unlock(&codetag_lock);322 mutex_unlock(&codetag_lock);
323
324 return ret;
309}325}
310 326
311void codetag_unload_module(struct module *mod)327void codetag_unload_module(struct module *mod)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t