xref: /linux-6.15/tools/perf/util/map.c (revision 00a62703)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "symbol.h"
3 #include <errno.h>
4 #include <inttypes.h>
5 #include <limits.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
11 #include "map.h"
12 #include "thread.h"
13 #include "vdso.h"
14 #include "build-id.h"
15 #include "util.h"
16 #include "debug.h"
17 #include "machine.h"
18 #include <linux/string.h>
19 #include "srcline.h"
20 #include "namespaces.h"
21 #include "unwind.h"
22 
23 static void __maps__insert(struct maps *maps, struct map *map);
24 
25 static inline int is_anon_memory(const char *filename, u32 flags)
26 {
27 	return flags & MAP_HUGETLB ||
28 	       !strcmp(filename, "//anon") ||
29 	       !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
30 	       !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
31 }
32 
33 static inline int is_no_dso_memory(const char *filename)
34 {
35 	return !strncmp(filename, "[stack", 6) ||
36 	       !strncmp(filename, "/SYSV",5)   ||
37 	       !strcmp(filename, "[heap]");
38 }
39 
40 static inline int is_android_lib(const char *filename)
41 {
42 	return !strncmp(filename, "/data/app-lib", 13) ||
43 	       !strncmp(filename, "/system/lib", 11);
44 }
45 
46 static inline bool replace_android_lib(const char *filename, char *newfilename)
47 {
48 	const char *libname;
49 	char *app_abi;
50 	size_t app_abi_length, new_length;
51 	size_t lib_length = 0;
52 
53 	libname  = strrchr(filename, '/');
54 	if (libname)
55 		lib_length = strlen(libname);
56 
57 	app_abi = getenv("APP_ABI");
58 	if (!app_abi)
59 		return false;
60 
61 	app_abi_length = strlen(app_abi);
62 
63 	if (!strncmp(filename, "/data/app-lib", 13)) {
64 		char *apk_path;
65 
66 		if (!app_abi_length)
67 			return false;
68 
69 		new_length = 7 + app_abi_length + lib_length;
70 
71 		apk_path = getenv("APK_PATH");
72 		if (apk_path) {
73 			new_length += strlen(apk_path) + 1;
74 			if (new_length > PATH_MAX)
75 				return false;
76 			snprintf(newfilename, new_length,
77 				 "%s/libs/%s/%s", apk_path, app_abi, libname);
78 		} else {
79 			if (new_length > PATH_MAX)
80 				return false;
81 			snprintf(newfilename, new_length,
82 				 "libs/%s/%s", app_abi, libname);
83 		}
84 		return true;
85 	}
86 
87 	if (!strncmp(filename, "/system/lib/", 11)) {
88 		char *ndk, *app;
89 		const char *arch;
90 		size_t ndk_length;
91 		size_t app_length;
92 
93 		ndk = getenv("NDK_ROOT");
94 		app = getenv("APP_PLATFORM");
95 
96 		if (!(ndk && app))
97 			return false;
98 
99 		ndk_length = strlen(ndk);
100 		app_length = strlen(app);
101 
102 		if (!(ndk_length && app_length && app_abi_length))
103 			return false;
104 
105 		arch = !strncmp(app_abi, "arm", 3) ? "arm" :
106 		       !strncmp(app_abi, "mips", 4) ? "mips" :
107 		       !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
108 
109 		if (!arch)
110 			return false;
111 
112 		new_length = 27 + ndk_length +
113 			     app_length + lib_length
114 			   + strlen(arch);
115 
116 		if (new_length > PATH_MAX)
117 			return false;
118 		snprintf(newfilename, new_length,
119 			"%s/platforms/%s/arch-%s/usr/lib/%s",
120 			ndk, app, arch, libname);
121 
122 		return true;
123 	}
124 	return false;
125 }
126 
127 void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
128 {
129 	map->start    = start;
130 	map->end      = end;
131 	map->pgoff    = pgoff;
132 	map->reloc    = 0;
133 	map->dso      = dso__get(dso);
134 	map->map_ip   = map__map_ip;
135 	map->unmap_ip = map__unmap_ip;
136 	RB_CLEAR_NODE(&map->rb_node);
137 	map->groups   = NULL;
138 	map->erange_warned = false;
139 	refcount_set(&map->refcnt, 1);
140 }
141 
142 struct map *map__new(struct machine *machine, u64 start, u64 len,
143 		     u64 pgoff, u32 d_maj, u32 d_min, u64 ino,
144 		     u64 ino_gen, u32 prot, u32 flags, char *filename,
145 		     struct thread *thread)
146 {
147 	struct map *map = malloc(sizeof(*map));
148 	struct nsinfo *nsi = NULL;
149 	struct nsinfo *nnsi;
150 
151 	if (map != NULL) {
152 		char newfilename[PATH_MAX];
153 		struct dso *dso;
154 		int anon, no_dso, vdso, android;
155 
156 		android = is_android_lib(filename);
157 		anon = is_anon_memory(filename, flags);
158 		vdso = is_vdso_map(filename);
159 		no_dso = is_no_dso_memory(filename);
160 
161 		map->maj = d_maj;
162 		map->min = d_min;
163 		map->ino = ino;
164 		map->ino_generation = ino_gen;
165 		map->prot = prot;
166 		map->flags = flags;
167 		nsi = nsinfo__get(thread->nsinfo);
168 
169 		if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
170 			snprintf(newfilename, sizeof(newfilename),
171 				 "/tmp/perf-%d.map", nsi->pid);
172 			filename = newfilename;
173 		}
174 
175 		if (android) {
176 			if (replace_android_lib(filename, newfilename))
177 				filename = newfilename;
178 		}
179 
180 		if (vdso) {
181 			/* The vdso maps are always on the host and not the
182 			 * container.  Ensure that we don't use setns to look
183 			 * them up.
184 			 */
185 			nnsi = nsinfo__copy(nsi);
186 			if (nnsi) {
187 				nsinfo__put(nsi);
188 				nnsi->need_setns = false;
189 				nsi = nnsi;
190 			}
191 			pgoff = 0;
192 			dso = machine__findnew_vdso(machine, thread);
193 		} else
194 			dso = machine__findnew_dso(machine, filename);
195 
196 		if (dso == NULL)
197 			goto out_delete;
198 
199 		map__init(map, start, start + len, pgoff, dso);
200 
201 		if (anon || no_dso) {
202 			map->map_ip = map->unmap_ip = identity__map_ip;
203 
204 			/*
205 			 * Set memory without DSO as loaded. All map__find_*
206 			 * functions still return NULL, and we avoid the
207 			 * unnecessary map__load warning.
208 			 */
209 			if (!(prot & PROT_EXEC))
210 				dso__set_loaded(dso);
211 		}
212 		dso->nsinfo = nsi;
213 		dso__put(dso);
214 	}
215 	return map;
216 out_delete:
217 	nsinfo__put(nsi);
218 	free(map);
219 	return NULL;
220 }
221 
222 /*
223  * Constructor variant for modules (where we know from /proc/modules where
224  * they are loaded) and for vmlinux, where only after we load all the
225  * symbols we'll know where it starts and ends.
226  */
227 struct map *map__new2(u64 start, struct dso *dso)
228 {
229 	struct map *map = calloc(1, (sizeof(*map) +
230 				     (dso->kernel ? sizeof(struct kmap) : 0)));
231 	if (map != NULL) {
232 		/*
233 		 * ->end will be filled after we load all the symbols
234 		 */
235 		map__init(map, start, 0, 0, dso);
236 	}
237 
238 	return map;
239 }
240 
241 /*
242  * Use this and __map__is_kmodule() for map instances that are in
243  * machine->kmaps, and thus have map->groups->machine all properly set, to
244  * disambiguate between the kernel and modules.
245  *
246  * When the need arises, introduce map__is_{kernel,kmodule)() that
247  * checks (map->groups != NULL && map->groups->machine != NULL &&
248  * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
249  */
250 bool __map__is_kernel(const struct map *map)
251 {
252 	return machine__kernel_map(map->groups->machine) == map;
253 }
254 
255 bool map__has_symbols(const struct map *map)
256 {
257 	return dso__has_symbols(map->dso);
258 }
259 
260 static void map__exit(struct map *map)
261 {
262 	BUG_ON(!RB_EMPTY_NODE(&map->rb_node));
263 	dso__zput(map->dso);
264 }
265 
266 void map__delete(struct map *map)
267 {
268 	map__exit(map);
269 	free(map);
270 }
271 
272 void map__put(struct map *map)
273 {
274 	if (map && refcount_dec_and_test(&map->refcnt))
275 		map__delete(map);
276 }
277 
278 void map__fixup_start(struct map *map)
279 {
280 	struct rb_root *symbols = &map->dso->symbols;
281 	struct rb_node *nd = rb_first(symbols);
282 	if (nd != NULL) {
283 		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
284 		map->start = sym->start;
285 	}
286 }
287 
288 void map__fixup_end(struct map *map)
289 {
290 	struct rb_root *symbols = &map->dso->symbols;
291 	struct rb_node *nd = rb_last(symbols);
292 	if (nd != NULL) {
293 		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
294 		map->end = sym->end;
295 	}
296 }
297 
298 #define DSO__DELETED "(deleted)"
299 
300 int map__load(struct map *map)
301 {
302 	const char *name = map->dso->long_name;
303 	int nr;
304 
305 	if (dso__loaded(map->dso))
306 		return 0;
307 
308 	nr = dso__load(map->dso, map);
309 	if (nr < 0) {
310 		if (map->dso->has_build_id) {
311 			char sbuild_id[SBUILD_ID_SIZE];
312 
313 			build_id__sprintf(map->dso->build_id,
314 					  sizeof(map->dso->build_id),
315 					  sbuild_id);
316 			pr_warning("%s with build id %s not found",
317 				   name, sbuild_id);
318 		} else
319 			pr_warning("Failed to open %s", name);
320 
321 		pr_warning(", continuing without symbols\n");
322 		return -1;
323 	} else if (nr == 0) {
324 #ifdef HAVE_LIBELF_SUPPORT
325 		const size_t len = strlen(name);
326 		const size_t real_len = len - sizeof(DSO__DELETED);
327 
328 		if (len > sizeof(DSO__DELETED) &&
329 		    strcmp(name + real_len + 1, DSO__DELETED) == 0) {
330 			pr_warning("%.*s was updated (is prelink enabled?). "
331 				"Restart the long running apps that use it!\n",
332 				   (int)real_len, name);
333 		} else {
334 			pr_warning("no symbols found in %s, maybe install "
335 				   "a debug package?\n", name);
336 		}
337 #endif
338 		return -1;
339 	}
340 
341 	return 0;
342 }
343 
344 struct symbol *map__find_symbol(struct map *map, u64 addr)
345 {
346 	if (map__load(map) < 0)
347 		return NULL;
348 
349 	return dso__find_symbol(map->dso, addr);
350 }
351 
352 struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
353 {
354 	if (map__load(map) < 0)
355 		return NULL;
356 
357 	if (!dso__sorted_by_name(map->dso))
358 		dso__sort_by_name(map->dso);
359 
360 	return dso__find_symbol_by_name(map->dso, name);
361 }
362 
363 struct map *map__clone(struct map *from)
364 {
365 	struct map *map = memdup(from, sizeof(*map));
366 
367 	if (map != NULL) {
368 		refcount_set(&map->refcnt, 1);
369 		RB_CLEAR_NODE(&map->rb_node);
370 		dso__get(map->dso);
371 		map->groups = NULL;
372 	}
373 
374 	return map;
375 }
376 
377 int map__overlap(struct map *l, struct map *r)
378 {
379 	if (l->start > r->start) {
380 		struct map *t = l;
381 		l = r;
382 		r = t;
383 	}
384 
385 	if (l->end > r->start)
386 		return 1;
387 
388 	return 0;
389 }
390 
391 size_t map__fprintf(struct map *map, FILE *fp)
392 {
393 	return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
394 		       map->start, map->end, map->pgoff, map->dso->name);
395 }
396 
397 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
398 {
399 	const char *dsoname = "[unknown]";
400 
401 	if (map && map->dso) {
402 		if (symbol_conf.show_kernel_path && map->dso->long_name)
403 			dsoname = map->dso->long_name;
404 		else
405 			dsoname = map->dso->name;
406 	}
407 
408 	return fprintf(fp, "%s", dsoname);
409 }
410 
411 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
412 			 FILE *fp)
413 {
414 	char *srcline;
415 	int ret = 0;
416 
417 	if (map && map->dso) {
418 		srcline = get_srcline(map->dso,
419 				      map__rip_2objdump(map, addr), NULL,
420 				      true, true, addr);
421 		if (srcline != SRCLINE_UNKNOWN)
422 			ret = fprintf(fp, "%s%s", prefix, srcline);
423 		free_srcline(srcline);
424 	}
425 	return ret;
426 }
427 
428 /**
429  * map__rip_2objdump - convert symbol start address to objdump address.
430  * @map: memory map
431  * @rip: symbol start address
432  *
433  * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
434  * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
435  * relative to section start.
436  *
437  * Return: Address suitable for passing to "objdump --start-address="
438  */
439 u64 map__rip_2objdump(struct map *map, u64 rip)
440 {
441 	if (!map->dso->adjust_symbols)
442 		return rip;
443 
444 	if (map->dso->rel)
445 		return rip - map->pgoff;
446 
447 	/*
448 	 * kernel modules also have DSO_TYPE_USER in dso->kernel,
449 	 * but all kernel modules are ET_REL, so won't get here.
450 	 */
451 	if (map->dso->kernel == DSO_TYPE_USER)
452 		return rip + map->dso->text_offset;
453 
454 	return map->unmap_ip(map, rip) - map->reloc;
455 }
456 
457 /**
458  * map__objdump_2mem - convert objdump address to a memory address.
459  * @map: memory map
460  * @ip: objdump address
461  *
462  * Closely related to map__rip_2objdump(), this function takes an address from
463  * objdump and converts it to a memory address.  Note this assumes that @map
464  * contains the address.  To be sure the result is valid, check it forwards
465  * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
466  *
467  * Return: Memory address.
468  */
469 u64 map__objdump_2mem(struct map *map, u64 ip)
470 {
471 	if (!map->dso->adjust_symbols)
472 		return map->unmap_ip(map, ip);
473 
474 	if (map->dso->rel)
475 		return map->unmap_ip(map, ip + map->pgoff);
476 
477 	/*
478 	 * kernel modules also have DSO_TYPE_USER in dso->kernel,
479 	 * but all kernel modules are ET_REL, so won't get here.
480 	 */
481 	if (map->dso->kernel == DSO_TYPE_USER)
482 		return map->unmap_ip(map, ip - map->dso->text_offset);
483 
484 	return ip + map->reloc;
485 }
486 
487 static void maps__init(struct maps *maps)
488 {
489 	maps->entries = RB_ROOT;
490 	init_rwsem(&maps->lock);
491 }
492 
493 void map_groups__init(struct map_groups *mg, struct machine *machine)
494 {
495 	maps__init(&mg->maps);
496 	mg->machine = machine;
497 	refcount_set(&mg->refcnt, 1);
498 }
499 
500 static void __maps__purge(struct maps *maps)
501 {
502 	struct rb_root *root = &maps->entries;
503 	struct rb_node *next = rb_first(root);
504 
505 	while (next) {
506 		struct map *pos = rb_entry(next, struct map, rb_node);
507 
508 		next = rb_next(&pos->rb_node);
509 		rb_erase_init(&pos->rb_node, root);
510 		map__put(pos);
511 	}
512 }
513 
514 static void maps__exit(struct maps *maps)
515 {
516 	down_write(&maps->lock);
517 	__maps__purge(maps);
518 	up_write(&maps->lock);
519 }
520 
521 void map_groups__exit(struct map_groups *mg)
522 {
523 	maps__exit(&mg->maps);
524 }
525 
526 bool map_groups__empty(struct map_groups *mg)
527 {
528 	return !maps__first(&mg->maps);
529 }
530 
531 struct map_groups *map_groups__new(struct machine *machine)
532 {
533 	struct map_groups *mg = malloc(sizeof(*mg));
534 
535 	if (mg != NULL)
536 		map_groups__init(mg, machine);
537 
538 	return mg;
539 }
540 
541 void map_groups__delete(struct map_groups *mg)
542 {
543 	map_groups__exit(mg);
544 	free(mg);
545 }
546 
547 void map_groups__put(struct map_groups *mg)
548 {
549 	if (mg && refcount_dec_and_test(&mg->refcnt))
550 		map_groups__delete(mg);
551 }
552 
553 struct symbol *map_groups__find_symbol(struct map_groups *mg,
554 				       u64 addr, struct map **mapp)
555 {
556 	struct map *map = map_groups__find(mg, addr);
557 
558 	/* Ensure map is loaded before using map->map_ip */
559 	if (map != NULL && map__load(map) >= 0) {
560 		if (mapp != NULL)
561 			*mapp = map;
562 		return map__find_symbol(map, map->map_ip(map, addr));
563 	}
564 
565 	return NULL;
566 }
567 
568 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
569 					 struct map **mapp)
570 {
571 	struct symbol *sym;
572 	struct rb_node *nd;
573 
574 	down_read(&maps->lock);
575 
576 	for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
577 		struct map *pos = rb_entry(nd, struct map, rb_node);
578 
579 		sym = map__find_symbol_by_name(pos, name);
580 
581 		if (sym == NULL)
582 			continue;
583 		if (mapp != NULL)
584 			*mapp = pos;
585 		goto out;
586 	}
587 
588 	sym = NULL;
589 out:
590 	up_read(&maps->lock);
591 	return sym;
592 }
593 
594 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
595 					       const char *name,
596 					       struct map **mapp)
597 {
598 	return maps__find_symbol_by_name(&mg->maps, name, mapp);
599 }
600 
601 int map_groups__find_ams(struct addr_map_symbol *ams)
602 {
603 	if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
604 		if (ams->map->groups == NULL)
605 			return -1;
606 		ams->map = map_groups__find(ams->map->groups, ams->addr);
607 		if (ams->map == NULL)
608 			return -1;
609 	}
610 
611 	ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
612 	ams->sym = map__find_symbol(ams->map, ams->al_addr);
613 
614 	return ams->sym ? 0 : -1;
615 }
616 
617 static size_t maps__fprintf(struct maps *maps, FILE *fp)
618 {
619 	size_t printed = 0;
620 	struct rb_node *nd;
621 
622 	down_read(&maps->lock);
623 
624 	for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
625 		struct map *pos = rb_entry(nd, struct map, rb_node);
626 		printed += fprintf(fp, "Map:");
627 		printed += map__fprintf(pos, fp);
628 		if (verbose > 2) {
629 			printed += dso__fprintf(pos->dso, fp);
630 			printed += fprintf(fp, "--\n");
631 		}
632 	}
633 
634 	up_read(&maps->lock);
635 
636 	return printed;
637 }
638 
639 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
640 {
641 	return maps__fprintf(&mg->maps, fp);
642 }
643 
644 static void __map_groups__insert(struct map_groups *mg, struct map *map)
645 {
646 	__maps__insert(&mg->maps, map);
647 	map->groups = mg;
648 }
649 
650 static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
651 {
652 	struct rb_root *root;
653 	struct rb_node *next;
654 	int err = 0;
655 
656 	down_write(&maps->lock);
657 
658 	root = &maps->entries;
659 	next = rb_first(root);
660 
661 	while (next) {
662 		struct map *pos = rb_entry(next, struct map, rb_node);
663 		next = rb_next(&pos->rb_node);
664 
665 		if (!map__overlap(pos, map))
666 			continue;
667 
668 		if (verbose >= 2) {
669 
670 			if (use_browser) {
671 				pr_warning("overlapping maps in %s "
672 					   "(disable tui for more info)\n",
673 					   map->dso->name);
674 			} else {
675 				fputs("overlapping maps:\n", fp);
676 				map__fprintf(map, fp);
677 				map__fprintf(pos, fp);
678 			}
679 		}
680 
681 		rb_erase_init(&pos->rb_node, root);
682 		/*
683 		 * Now check if we need to create new maps for areas not
684 		 * overlapped by the new map:
685 		 */
686 		if (map->start > pos->start) {
687 			struct map *before = map__clone(pos);
688 
689 			if (before == NULL) {
690 				err = -ENOMEM;
691 				goto put_map;
692 			}
693 
694 			before->end = map->start;
695 			__map_groups__insert(pos->groups, before);
696 			if (verbose >= 2 && !use_browser)
697 				map__fprintf(before, fp);
698 			map__put(before);
699 		}
700 
701 		if (map->end < pos->end) {
702 			struct map *after = map__clone(pos);
703 
704 			if (after == NULL) {
705 				err = -ENOMEM;
706 				goto put_map;
707 			}
708 
709 			after->start = map->end;
710 			__map_groups__insert(pos->groups, after);
711 			if (verbose >= 2 && !use_browser)
712 				map__fprintf(after, fp);
713 			map__put(after);
714 		}
715 put_map:
716 		map__put(pos);
717 
718 		if (err)
719 			goto out;
720 	}
721 
722 	err = 0;
723 out:
724 	up_write(&maps->lock);
725 	return err;
726 }
727 
728 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
729 				   FILE *fp)
730 {
731 	return maps__fixup_overlappings(&mg->maps, map, fp);
732 }
733 
734 /*
735  * XXX This should not really _copy_ te maps, but refcount them.
736  */
737 int map_groups__clone(struct thread *thread, struct map_groups *parent)
738 {
739 	struct map_groups *mg = thread->mg;
740 	int err = -ENOMEM;
741 	struct map *map;
742 	struct maps *maps = &parent->maps;
743 
744 	down_read(&maps->lock);
745 
746 	for (map = maps__first(maps); map; map = map__next(map)) {
747 		struct map *new = map__clone(map);
748 		if (new == NULL)
749 			goto out_unlock;
750 
751 		err = unwind__prepare_access(thread, new, NULL);
752 		if (err)
753 			goto out_unlock;
754 
755 		map_groups__insert(mg, new);
756 		map__put(new);
757 	}
758 
759 	err = 0;
760 out_unlock:
761 	up_read(&maps->lock);
762 	return err;
763 }
764 
765 static void __maps__insert(struct maps *maps, struct map *map)
766 {
767 	struct rb_node **p = &maps->entries.rb_node;
768 	struct rb_node *parent = NULL;
769 	const u64 ip = map->start;
770 	struct map *m;
771 
772 	while (*p != NULL) {
773 		parent = *p;
774 		m = rb_entry(parent, struct map, rb_node);
775 		if (ip < m->start)
776 			p = &(*p)->rb_left;
777 		else
778 			p = &(*p)->rb_right;
779 	}
780 
781 	rb_link_node(&map->rb_node, parent, p);
782 	rb_insert_color(&map->rb_node, &maps->entries);
783 	map__get(map);
784 }
785 
786 void maps__insert(struct maps *maps, struct map *map)
787 {
788 	down_write(&maps->lock);
789 	__maps__insert(maps, map);
790 	up_write(&maps->lock);
791 }
792 
793 static void __maps__remove(struct maps *maps, struct map *map)
794 {
795 	rb_erase_init(&map->rb_node, &maps->entries);
796 	map__put(map);
797 }
798 
799 void maps__remove(struct maps *maps, struct map *map)
800 {
801 	down_write(&maps->lock);
802 	__maps__remove(maps, map);
803 	up_write(&maps->lock);
804 }
805 
806 struct map *maps__find(struct maps *maps, u64 ip)
807 {
808 	struct rb_node **p, *parent = NULL;
809 	struct map *m;
810 
811 	down_read(&maps->lock);
812 
813 	p = &maps->entries.rb_node;
814 	while (*p != NULL) {
815 		parent = *p;
816 		m = rb_entry(parent, struct map, rb_node);
817 		if (ip < m->start)
818 			p = &(*p)->rb_left;
819 		else if (ip >= m->end)
820 			p = &(*p)->rb_right;
821 		else
822 			goto out;
823 	}
824 
825 	m = NULL;
826 out:
827 	up_read(&maps->lock);
828 	return m;
829 }
830 
831 struct map *maps__first(struct maps *maps)
832 {
833 	struct rb_node *first = rb_first(&maps->entries);
834 
835 	if (first)
836 		return rb_entry(first, struct map, rb_node);
837 	return NULL;
838 }
839 
840 struct map *map__next(struct map *map)
841 {
842 	struct rb_node *next = rb_next(&map->rb_node);
843 
844 	if (next)
845 		return rb_entry(next, struct map, rb_node);
846 	return NULL;
847 }
848 
849 struct kmap *map__kmap(struct map *map)
850 {
851 	if (!map->dso || !map->dso->kernel) {
852 		pr_err("Internal error: map__kmap with a non-kernel map\n");
853 		return NULL;
854 	}
855 	return (struct kmap *)(map + 1);
856 }
857 
858 struct map_groups *map__kmaps(struct map *map)
859 {
860 	struct kmap *kmap = map__kmap(map);
861 
862 	if (!kmap || !kmap->kmaps) {
863 		pr_err("Internal error: map__kmaps with a non-kernel map\n");
864 		return NULL;
865 	}
866 	return kmap->kmaps;
867 }
868