Skip to content

Commit 1e4df18

Browse files
ftang1tehcaster
authored andcommitted
mm/slub: Move krealloc() and related code to slub.c
This is a preparation for the following refactoring of krealloc(), for more efficient function calling as it will call some internal functions defined in slub.c. Signed-off-by: Feng Tang <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]>
1 parent fb5eda0 commit 1e4df18

File tree

2 files changed

+84
-84
lines changed

2 files changed

+84
-84
lines changed

mm/slab_common.c

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,90 +1190,6 @@ module_init(slab_proc_init);
11901190

11911191
#endif /* CONFIG_SLUB_DEBUG */
11921192

1193-
static __always_inline __realloc_size(2) void *
1194-
__do_krealloc(const void *p, size_t new_size, gfp_t flags)
1195-
{
1196-
void *ret;
1197-
size_t ks;
1198-
1199-
/* Check for double-free before calling ksize. */
1200-
if (likely(!ZERO_OR_NULL_PTR(p))) {
1201-
if (!kasan_check_byte(p))
1202-
return NULL;
1203-
ks = ksize(p);
1204-
} else
1205-
ks = 0;
1206-
1207-
/* If the object still fits, repoison it precisely. */
1208-
if (ks >= new_size) {
1209-
/* Zero out spare memory. */
1210-
if (want_init_on_alloc(flags)) {
1211-
kasan_disable_current();
1212-
memset(kasan_reset_tag(p) + new_size, 0, ks - new_size);
1213-
kasan_enable_current();
1214-
}
1215-
1216-
p = kasan_krealloc((void *)p, new_size, flags);
1217-
return (void *)p;
1218-
}
1219-
1220-
ret = kmalloc_node_track_caller_noprof(new_size, flags, NUMA_NO_NODE, _RET_IP_);
1221-
if (ret && p) {
1222-
/* Disable KASAN checks as the object's redzone is accessed. */
1223-
kasan_disable_current();
1224-
memcpy(ret, kasan_reset_tag(p), ks);
1225-
kasan_enable_current();
1226-
}
1227-
1228-
return ret;
1229-
}
1230-
1231-
/**
1232-
* krealloc - reallocate memory. The contents will remain unchanged.
1233-
* @p: object to reallocate memory for.
1234-
* @new_size: how many bytes of memory are required.
1235-
* @flags: the type of memory to allocate.
1236-
*
1237-
* If @p is %NULL, krealloc() behaves exactly like kmalloc(). If @new_size
1238-
* is 0 and @p is not a %NULL pointer, the object pointed to is freed.
1239-
*
1240-
* If __GFP_ZERO logic is requested, callers must ensure that, starting with the
1241-
* initial memory allocation, every subsequent call to this API for the same
1242-
* memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
1243-
* __GFP_ZERO is not fully honored by this API.
1244-
*
1245-
* This is the case, since krealloc() only knows about the bucket size of an
1246-
* allocation (but not the exact size it was allocated with) and hence
1247-
* implements the following semantics for shrinking and growing buffers with
1248-
* __GFP_ZERO.
1249-
*
1250-
* new bucket
1251-
* 0 size size
1252-
* |--------|----------------|
1253-
* | keep | zero |
1254-
*
1255-
* In any case, the contents of the object pointed to are preserved up to the
1256-
* lesser of the new and old sizes.
1257-
*
1258-
* Return: pointer to the allocated memory or %NULL in case of error
1259-
*/
1260-
void *krealloc_noprof(const void *p, size_t new_size, gfp_t flags)
1261-
{
1262-
void *ret;
1263-
1264-
if (unlikely(!new_size)) {
1265-
kfree(p);
1266-
return ZERO_SIZE_PTR;
1267-
}
1268-
1269-
ret = __do_krealloc(p, new_size, flags);
1270-
if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
1271-
kfree(p);
1272-
1273-
return ret;
1274-
}
1275-
EXPORT_SYMBOL(krealloc_noprof);
1276-
12771193
/**
12781194
* kfree_sensitive - Clear sensitive information in memory before freeing
12791195
* @p: object to free memory of

mm/slub.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4711,6 +4711,90 @@ void kfree(const void *object)
47114711
}
47124712
EXPORT_SYMBOL(kfree);
47134713

4714+
static __always_inline __realloc_size(2) void *
4715+
__do_krealloc(const void *p, size_t new_size, gfp_t flags)
4716+
{
4717+
void *ret;
4718+
size_t ks;
4719+
4720+
/* Check for double-free before calling ksize. */
4721+
if (likely(!ZERO_OR_NULL_PTR(p))) {
4722+
if (!kasan_check_byte(p))
4723+
return NULL;
4724+
ks = ksize(p);
4725+
} else
4726+
ks = 0;
4727+
4728+
/* If the object still fits, repoison it precisely. */
4729+
if (ks >= new_size) {
4730+
/* Zero out spare memory. */
4731+
if (want_init_on_alloc(flags)) {
4732+
kasan_disable_current();
4733+
memset(kasan_reset_tag(p) + new_size, 0, ks - new_size);
4734+
kasan_enable_current();
4735+
}
4736+
4737+
p = kasan_krealloc((void *)p, new_size, flags);
4738+
return (void *)p;
4739+
}
4740+
4741+
ret = kmalloc_node_track_caller_noprof(new_size, flags, NUMA_NO_NODE, _RET_IP_);
4742+
if (ret && p) {
4743+
/* Disable KASAN checks as the object's redzone is accessed. */
4744+
kasan_disable_current();
4745+
memcpy(ret, kasan_reset_tag(p), ks);
4746+
kasan_enable_current();
4747+
}
4748+
4749+
return ret;
4750+
}
4751+
4752+
/**
4753+
* krealloc - reallocate memory. The contents will remain unchanged.
4754+
* @p: object to reallocate memory for.
4755+
* @new_size: how many bytes of memory are required.
4756+
* @flags: the type of memory to allocate.
4757+
*
4758+
* If @p is %NULL, krealloc() behaves exactly like kmalloc(). If @new_size
4759+
* is 0 and @p is not a %NULL pointer, the object pointed to is freed.
4760+
*
4761+
* If __GFP_ZERO logic is requested, callers must ensure that, starting with the
4762+
* initial memory allocation, every subsequent call to this API for the same
4763+
* memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
4764+
* __GFP_ZERO is not fully honored by this API.
4765+
*
4766+
* This is the case, since krealloc() only knows about the bucket size of an
4767+
* allocation (but not the exact size it was allocated with) and hence
4768+
* implements the following semantics for shrinking and growing buffers with
4769+
* __GFP_ZERO.
4770+
*
4771+
* new bucket
4772+
* 0 size size
4773+
* |--------|----------------|
4774+
* | keep | zero |
4775+
*
4776+
* In any case, the contents of the object pointed to are preserved up to the
4777+
* lesser of the new and old sizes.
4778+
*
4779+
* Return: pointer to the allocated memory or %NULL in case of error
4780+
*/
4781+
void *krealloc_noprof(const void *p, size_t new_size, gfp_t flags)
4782+
{
4783+
void *ret;
4784+
4785+
if (unlikely(!new_size)) {
4786+
kfree(p);
4787+
return ZERO_SIZE_PTR;
4788+
}
4789+
4790+
ret = __do_krealloc(p, new_size, flags);
4791+
if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
4792+
kfree(p);
4793+
4794+
return ret;
4795+
}
4796+
EXPORT_SYMBOL(krealloc_noprof);
4797+
47144798
struct detached_freelist {
47154799
struct slab *slab;
47164800
void *tail;

0 commit comments

Comments
 (0)