Skip to content

Commit 54f7802

Browse files
committed
Allowing to enabled CPU_PIN on other oses than Linux (FreeBSD at the moment tough).
New internal api `_iso_getcpu` abstraction, for now working on amd64 arch for non Linux.
1 parent 06458cc commit 54f7802

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ HUGE_PAGES = -DHUGE_PAGES=1
9393
## and has negative performance implications
9494
CPU_PIN = -DCPU_PIN=0
9595

96+
SCHED_GETCPU =
97+
9698
## Enable the allocation sanity feature. This works a lot
9799
## like GWP-ASAN does. It samples calls to iso_alloc and
98100
## randomly swaps them out for raw page allocations that
@@ -169,12 +171,16 @@ endif
169171

170172
ifeq ($(UNAME), Linux)
171173
STRIP = strip -s $(BUILD_DIR)/$(LIBNAME)
174+
SCHED_GETCPU = -DSCHED_GETCPU
172175
endif
173176

174177
ifeq ($(UNAME), FreeBSD)
175178
STRIP = strip -s $(BUILD_DIR)/$(LIBNAME)
176179
## Using spinlocks to avoid recursive locks contentions with calloc
177180
USE_SPINLOCK = -DUSE_SPINLOCK=1
181+
## Once FreeBSD 13.1 becomes the minimal non EOL version
182+
## it can be enabled
183+
## SCHED_GETCPU = -DSCHED_GETCPU
178184
endif
179185

180186
HOOKS = $(MALLOC_HOOK)
@@ -187,7 +193,7 @@ else
187193
BUILD_ERROR_FLAGS := $(BUILD_ERROR_FLAGS) -Wno-attributes -Wno-unused-variable
188194
endif
189195
CFLAGS = $(COMMON_CFLAGS) $(SECURITY_FLAGS) $(BUILD_ERROR_FLAGS) $(HOOKS) $(HEAP_PROFILER) -fvisibility=hidden \
190-
-std=c11 $(SANITIZER_SUPPORT) $(ALLOC_SANITY) $(UNINIT_READ_SANITY) $(CPU_PIN) $(EXPERIMENTAL) $(UAF_PTR_PAGE) \
196+
-std=c11 $(SANITIZER_SUPPORT) $(ALLOC_SANITY) $(UNINIT_READ_SANITY) $(CPU_PIN) $(SCHED_GETCPU) $(EXPERIMENTAL) $(UAF_PTR_PAGE) \
191197
$(VERIFY_BIT_SLOT_CACHE) $(NAMED_MAPPINGS) $(ABORT_ON_NULL) $(NO_ZERO_ALLOCATIONS) $(ABORT_NO_ENTROPY) \
192198
$(ISO_DTOR_CLEANUP) $(SHUFFLE_BIT_SLOT_CACHE) $(USE_SPINLOCK) -pthread $(HUGE_PAGES) $(USE_MLOCK) $(MEMORY_TAGGING)
193199
CXXFLAGS = $(COMMON_CFLAGS) -DCPP_SUPPORT=1 -std=c++17 $(SANITIZER_SUPPORT) $(HOOKS)

include/iso_alloc_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,7 @@ INTERNAL_HIDDEN void _iso_alloc_search_stack(uint8_t *stack_start);
605605
#if UNIT_TESTING
606606
EXTERNAL_API iso_alloc_root *_get_root(void);
607607
#endif
608+
609+
#if CPU_PIN
610+
INTERNAL_HIDDEN INLINE int _iso_getcpu(void);
611+
#endif

src/iso_alloc.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ INTERNAL_HIDDEN iso_alloc_zone_t *_iso_new_zone(size_t size, bool internal, int3
812812
get_next_free_bit_slot(new_zone);
813813

814814
#if CPU_PIN
815-
new_zone->cpu_core = sched_getcpu();
815+
new_zone->cpu_core = _iso_getcpu();
816816
#endif
817817

818818
POISON_ZONE(new_zone);
@@ -903,7 +903,7 @@ INTERNAL_HIDDEN bit_slot_t iso_scan_zone_free_slot_slow(iso_alloc_zone_t *zone)
903903

904904
INTERNAL_HIDDEN iso_alloc_zone_t *is_zone_usable(iso_alloc_zone_t *zone, size_t size) {
905905
#if CPU_PIN
906-
if(zone->cpu_core != sched_getcpu()) {
906+
if(zone->cpu_core != _iso_getcpu()) {
907907
return false;
908908
}
909909
#endif
@@ -2117,3 +2117,26 @@ EXTERNAL_API iso_alloc_root *_get_root(void) {
21172117
return _root;
21182118
}
21192119
#endif
2120+
2121+
#if CPU_PIN
2122+
/* sched_getcpu's performance depends on the
2123+
* architecture/kernel version, so we lower
2124+
* the cost of feature's abstraction here.
2125+
*/
2126+
INTERNAL_HIDDEN INLINE int _iso_getcpu(void) {
2127+
#if defined(SCHED_GETCPU)
2128+
return sched_getcpu();
2129+
#elif defined(__x86_64__)
2130+
/* rdtscp is not always available and is pretty slow
2131+
* we instead load from the global descriptor table
2132+
* then "mov" it to a.
2133+
*/
2134+
unsigned int a;
2135+
const unsigned int cpunodesegment = 15 * 8 + 3;
2136+
__asm__ volatile("lsl %1, %0" : "=r"(a) : "r"(cpunodesegment));
2137+
return (int)(a & 0xfff);
2138+
#else
2139+
return -1;
2140+
#endif
2141+
}
2142+
#endif

0 commit comments

Comments
 (0)