Skip to content

Commit

Permalink
Merge tag 'memblock-v5.18-rc1' of git://git.kernel.org/pub/scm/linux/…
Browse files Browse the repository at this point in the history
…kernel/git/rppt/memblock

Pull memblock updates from Mike Rapoport:
 "Test suite and a small cleanup:

   - A small cleanup of unused variable in __next_mem_pfn_range_in_zone

   - Initial test suite to simulate memblock behaviour in userspace"

* tag 'memblock-v5.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rppt/memblock: (27 commits)
  memblock tests: Add TODO and README files
  memblock tests: Add memblock_alloc_try_nid tests for bottom up
  memblock tests: Add memblock_alloc_try_nid tests for top down
  memblock tests: Add memblock_alloc_from tests for bottom up
  memblock tests: Add memblock_alloc_from tests for top down
  memblock tests: Add memblock_alloc tests for bottom up
  memblock tests: Add memblock_alloc tests for top down
  memblock tests: Add simulation of physical memory
  memblock tests: Split up reset_memblock function
  memblock tests: Fix testing with 32-bit physical addresses
  memblock: __next_mem_pfn_range_in_zone: remove unneeded local variable nid
  memblock tests: Add memblock_free tests
  memblock tests: Add memblock_add_node test
  memblock tests: Add memblock_remove tests
  memblock tests: Add memblock_reserve tests
  memblock tests: Add memblock_add tests
  memblock tests: Add memblock reset function
  memblock tests: Add skeleton of the memblock simulator
  tools/include: Add debugfs.h stub
  tools/include: Add pfn.h stub
  ...
  • Loading branch information
torvalds committed Mar 27, 2022
2 parents 88b3be5 + 58ffc34 commit 02f9a04
Show file tree
Hide file tree
Showing 42 changed files with 3,934 additions and 71 deletions.
1 change: 1 addition & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -12550,6 +12550,7 @@ S: Maintained
F: Documentation/core-api/boot-time-mm.rst
F: include/linux/memblock.h
F: mm/memblock.c
F: tools/testing/memblock/

MEMORY CONTROLLER DRIVERS
M: Krzysztof Kozlowski <[email protected]>
Expand Down
5 changes: 2 additions & 3 deletions mm/memblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,11 +1284,10 @@ __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
{
int zone_nid = zone_to_nid(zone);
phys_addr_t spa, epa;
int nid;

__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
&memblock.memory, &memblock.reserved,
&spa, &epa, &nid);
&spa, &epa, NULL);

while (*idx != U64_MAX) {
unsigned long epfn = PFN_DOWN(epa);
Expand All @@ -1315,7 +1314,7 @@ __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,

__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
&memblock.memory, &memblock.reserved,
&spa, &epa, &nid);
&spa, &epa, NULL);
}

/* signal end of iteration */
Expand Down
2 changes: 2 additions & 0 deletions tools/include/linux/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <asm/atomic.h>

void atomic_long_set(atomic_long_t *v, long i);

/* atomic_cmpxchg_relaxed */
#ifndef atomic_cmpxchg_relaxed
#define atomic_cmpxchg_relaxed atomic_cmpxchg
Expand Down
10 changes: 10 additions & 0 deletions tools/include/linux/cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _TOOLS_LINUX_CACHE_H
#define _TOOLS_LINUX_CACHE_H

#define L1_CACHE_SHIFT 5
#define L1_CACHE_BYTES (1 << L1_CACHE_SHIFT)

#define SMP_CACHE_BYTES L1_CACHE_BYTES

#endif
5 changes: 5 additions & 0 deletions tools/include/linux/debugfs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _TOOLS_DEBUGFS_H
#define _TOOLS_DEBUGFS_H

#endif
28 changes: 28 additions & 0 deletions tools/include/linux/gfp.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _TOOLS_INCLUDE_LINUX_GFP_H
#define _TOOLS_INCLUDE_LINUX_GFP_H

#include <linux/types.h>

#define __GFP_BITS_SHIFT 26
#define __GFP_BITS_MASK ((gfp_t)((1 << __GFP_BITS_SHIFT) - 1))

#define __GFP_HIGH 0x20u
#define __GFP_IO 0x40u
#define __GFP_FS 0x80u
#define __GFP_NOWARN 0x200u
#define __GFP_ZERO 0x8000u
#define __GFP_ATOMIC 0x80000u
#define __GFP_ACCOUNT 0x100000u
#define __GFP_DIRECT_RECLAIM 0x400000u
#define __GFP_KSWAPD_RECLAIM 0x2000000u

#define __GFP_RECLAIM (__GFP_DIRECT_RECLAIM | __GFP_KSWAPD_RECLAIM)

#define GFP_ZONEMASK 0x0fu
#define GFP_ATOMIC (__GFP_HIGH | __GFP_ATOMIC | __GFP_KSWAPD_RECLAIM)
#define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS)
#define GFP_NOWAIT (__GFP_KSWAPD_RECLAIM)

static inline bool gfpflags_allow_blocking(const gfp_t gfp_flags)
{
return !!(gfp_flags & __GFP_DIRECT_RECLAIM);
}

#endif /* _TOOLS_INCLUDE_LINUX_GFP_H */
5 changes: 5 additions & 0 deletions tools/include/linux/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _TOOLS_IO_H
#define _TOOLS_IO_H

#endif
6 changes: 6 additions & 0 deletions tools/include/linux/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#define UINT_MAX (~0U)
#endif

#define _RET_IP_ ((unsigned long)__builtin_return_address(0))

#define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
#define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))

Expand Down Expand Up @@ -51,6 +53,10 @@
_min1 < _min2 ? _min1 : _min2; })
#endif

#define max_t(type, x, y) max((type)x, (type)y)
#define min_t(type, x, y) min((type)x, (type)y)
#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)

#ifndef BUG_ON
#ifdef NDEBUG
#define BUG_ON(cond) do { if (cond) {} } while (0)
Expand Down
42 changes: 42 additions & 0 deletions tools/include/linux/mm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _TOOLS_LINUX_MM_H
#define _TOOLS_LINUX_MM_H

#include <linux/mmzone.h>
#include <uapi/linux/const.h>

#define PAGE_SHIFT 12
#define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT)
#define PAGE_MASK (~(PAGE_SIZE - 1))

#define PHYS_ADDR_MAX (~(phys_addr_t)0)

#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
#define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
#define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a))

#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)

#define __va(x) ((void *)((unsigned long)(x)))
#define __pa(x) ((unsigned long)(x))

#define pfn_to_page(pfn) ((void *)((pfn) * PAGE_SIZE))

#define phys_to_virt phys_to_virt
static inline void *phys_to_virt(unsigned long address)
{
return __va(address);
}

void reserve_bootmem_region(phys_addr_t start, phys_addr_t end);

static inline void totalram_pages_inc(void)
{
}

static inline void totalram_pages_add(long count)
{
}

#endif
10 changes: 10 additions & 0 deletions tools/include/linux/pfn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _TOOLS_LINUX_PFN_H_
#define _TOOLS_LINUX_PFN_H_

#include <linux/mm.h>

#define PFN_UP(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
#define PFN_DOWN(x) ((x) >> PAGE_SHIFT)
#define PFN_PHYS(x) ((phys_addr_t)(x) << PAGE_SHIFT)
#endif
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef SLAB_H
#define SLAB_H
#ifndef _TOOLS_SLAB_H
#define _TOOLS_SLAB_H

#include <linux/types.h>
#include <linux/gfp.h>

#define SLAB_HWCACHE_ALIGN 1
#define SLAB_PANIC 2
#define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */

void *kmalloc(size_t size, gfp_t);
void kfree(void *);
#define kzalloc_node(size, flags, node) kmalloc(size, flags)

void *kmalloc(size_t size, gfp_t gfp);
void kfree(void *p);

bool slab_is_available(void);

enum slab_state {
DOWN,
PARTIAL,
PARTIAL_NODE,
UP,
FULL
};

static inline void *kzalloc(size_t size, gfp_t gfp)
{
return kmalloc(size, gfp | __GFP_ZERO);
return kmalloc(size, gfp | __GFP_ZERO);
}

void *kmem_cache_alloc(struct kmem_cache *cachep, int flags);
Expand All @@ -24,4 +35,4 @@ struct kmem_cache *kmem_cache_create(const char *name, unsigned int size,
unsigned int align, unsigned int flags,
void (*ctor)(void *));

#endif /* SLAB_H */
#endif /* _TOOLS_SLAB_H */
10 changes: 10 additions & 0 deletions tools/include/linux/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,20 @@ typedef __u64 __bitwise __be64;
typedef __u16 __bitwise __sum16;
typedef __u32 __bitwise __wsum;

#ifdef CONFIG_PHYS_ADDR_T_64BIT
typedef u64 phys_addr_t;
#else
typedef u32 phys_addr_t;
#endif

typedef struct {
int counter;
} atomic_t;

typedef struct {
long counter;
} atomic_long_t;

#ifndef __aligned_u64
# define __aligned_u64 __u64 __attribute__((aligned(8)))
#endif
Expand Down
38 changes: 38 additions & 0 deletions tools/lib/slab.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: GPL-2.0

#include <stdio.h>
#include <string.h>

#include <urcu/uatomic.h>
#include <linux/slab.h>
#include <malloc.h>
#include <linux/gfp.h>

int kmalloc_nr_allocated;
int kmalloc_verbose;

void *kmalloc(size_t size, gfp_t gfp)
{
void *ret;

if (!(gfp & __GFP_DIRECT_RECLAIM))
return NULL;

ret = malloc(size);
uatomic_inc(&kmalloc_nr_allocated);
if (kmalloc_verbose)
printf("Allocating %p from malloc\n", ret);
if (gfp & __GFP_ZERO)
memset(ret, 0, size);
return ret;
}

void kfree(void *p)
{
if (!p)
return;
uatomic_dec(&kmalloc_nr_allocated);
if (kmalloc_verbose)
printf("Freeing %p to malloc\n", p);
free(p);
}
4 changes: 4 additions & 0 deletions tools/testing/memblock/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
main
memblock.c
linux/memblock.h
asm/cmpxchg.h
55 changes: 55 additions & 0 deletions tools/testing/memblock/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# SPDX-License-Identifier: GPL-2.0

# Memblock simulator requires AddressSanitizer (libasan) and liburcu development
# packages installed
CFLAGS += -I. -I../../include -Wall -O2 -fsanitize=address \
-fsanitize=undefined -D CONFIG_PHYS_ADDR_T_64BIT
LDFLAGS += -fsanitize=address -fsanitize=undefined
TARGETS = main
TEST_OFILES = tests/alloc_nid_api.o tests/alloc_helpers_api.o tests/alloc_api.o \
tests/basic_api.o tests/common.o
DEP_OFILES = memblock.o lib/slab.o mmzone.o slab.o
OFILES = main.o $(DEP_OFILES) $(TEST_OFILES)
EXTR_SRC = ../../../mm/memblock.c

ifeq ($(BUILD), 32)
CFLAGS += -m32
LDFLAGS += -m32
endif

# Process user parameters
include scripts/Makefile.include

main: $(OFILES)

$(OFILES): include

include: ../../../include/linux/memblock.h ../../include/linux/*.h \
../../include/asm/*.h

@mkdir -p linux
test -L linux/memblock.h || ln -s ../../../../include/linux/memblock.h linux/memblock.h
test -L asm/cmpxchg.h || ln -s ../../../arch/x86/include/asm/cmpxchg.h asm/cmpxchg.h

memblock.c: $(EXTR_SRC)
test -L memblock.c || ln -s $(EXTR_SRC) memblock.c

clean:
$(RM) $(TARGETS) $(OFILES) linux/memblock.h memblock.c asm/cmpxchg.h

help:
@echo 'Memblock simulator'
@echo ''
@echo 'Available targets:'
@echo ' main - Build the memblock simulator'
@echo ' clean - Remove generated files and symlinks in the directory'
@echo ''
@echo 'Configuration:'
@echo ' make NUMA=1 - simulate enabled NUMA'
@echo ' make MOVABLE_NODE=1 - override `movable_node_is_enabled`'
@echo ' definition to simulate movable NUMA nodes'
@echo ' make 32BIT_PHYS_ADDR_T=1 - Use 32 bit physical addresses'

vpath %.c ../../lib

.PHONY: clean include help
Loading

0 comments on commit 02f9a04

Please sign in to comment.