Skip to content

Commit eeb0bed

Browse files
committed
libceph: add ceph_kv{malloc,free}() and switch to them
Encapsulate kmalloc vs vmalloc memory allocation and freeing logic into two helpers, ceph_kvmalloc() and ceph_kvfree(), and switch to them. ceph_kvmalloc() kmalloc()'s a maximum of 8 pages, anything bigger is vmalloc()'ed with __GFP_HIGHMEM set. This changes the existing behaviour: - for buffers (ceph_buffer_new()), from trying to kmalloc() everything and using vmalloc() just as a fallback - for messages (ceph_msg_new()), from going to vmalloc() for anything bigger than a page - for messages (ceph_msg_new()), from disallowing vmalloc() to use high memory Signed-off-by: Ilya Dryomov <ilya.dryomov@inktank.com> Reviewed-by: Sage Weil <sage@inktank.com>
1 parent 80213a8 commit eeb0bed

6 files changed

Lines changed: 35 additions & 33 deletions

File tree

‎include/linux/ceph/buffer.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ struct ceph_buffer {
1717
struct kref kref;
1818
struct kvec vec;
1919
size_t alloc_len;
20-
bool is_vmalloc;
2120
};
2221

2322
extern struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp);

‎include/linux/ceph/libceph.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,18 @@ static inline int calc_pages_for(u64 off, u64 len)
173173
(off >> PAGE_CACHE_SHIFT);
174174
}
175175

176+
extern struct kmem_cache *ceph_inode_cachep;
177+
extern struct kmem_cache *ceph_cap_cachep;
178+
extern struct kmem_cache *ceph_dentry_cachep;
179+
extern struct kmem_cache *ceph_file_cachep;
180+
176181
/* ceph_common.c */
177182
extern bool libceph_compatible(void *data);
178183

179184
extern const char *ceph_msg_type_name(int type);
180185
extern int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid);
181-
extern struct kmem_cache *ceph_inode_cachep;
182-
extern struct kmem_cache *ceph_cap_cachep;
183-
extern struct kmem_cache *ceph_dentry_cachep;
184-
extern struct kmem_cache *ceph_file_cachep;
186+
extern void *ceph_kvmalloc(size_t size, gfp_t flags);
187+
extern void ceph_kvfree(const void *ptr);
185188

186189
extern struct ceph_options *ceph_parse_options(char *options,
187190
const char *dev_name, const char *dev_name_end,

‎include/linux/ceph/messenger.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ struct ceph_msg {
154154
struct list_head list_head; /* links for connection lists */
155155

156156
struct kref kref;
157-
bool front_is_vmalloc;
158157
bool more_to_follow;
159158
bool needs_out_seq;
160159
int front_alloc_len;

‎net/ceph/buffer.c‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <linux/ceph/buffer.h>
88
#include <linux/ceph/decode.h>
9+
#include <linux/ceph/libceph.h> /* for ceph_kv{malloc,free} */
910

1011
struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp)
1112
{
@@ -15,16 +16,10 @@ struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp)
1516
if (!b)
1617
return NULL;
1718

18-
b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN);
19-
if (b->vec.iov_base) {
20-
b->is_vmalloc = false;
21-
} else {
22-
b->vec.iov_base = __vmalloc(len, gfp | __GFP_HIGHMEM, PAGE_KERNEL);
23-
if (!b->vec.iov_base) {
24-
kfree(b);
25-
return NULL;
26-
}
27-
b->is_vmalloc = true;
19+
b->vec.iov_base = ceph_kvmalloc(len, gfp);
20+
if (!b->vec.iov_base) {
21+
kfree(b);
22+
return NULL;
2823
}
2924

3025
kref_init(&b->kref);
@@ -40,12 +35,7 @@ void ceph_buffer_release(struct kref *kref)
4035
struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref);
4136

4237
dout("buffer_release %p\n", b);
43-
if (b->vec.iov_base) {
44-
if (b->is_vmalloc)
45-
vfree(b->vec.iov_base);
46-
else
47-
kfree(b->vec.iov_base);
48-
}
38+
ceph_kvfree(b->vec.iov_base);
4939
kfree(b);
5040
}
5141
EXPORT_SYMBOL(ceph_buffer_release);

‎net/ceph/ceph_common.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/slab.h>
1616
#include <linux/statfs.h>
1717
#include <linux/string.h>
18+
#include <linux/vmalloc.h>
1819
#include <linux/nsproxy.h>
1920
#include <net/net_namespace.h>
2021

@@ -170,6 +171,25 @@ int ceph_compare_options(struct ceph_options *new_opt,
170171
}
171172
EXPORT_SYMBOL(ceph_compare_options);
172173

174+
void *ceph_kvmalloc(size_t size, gfp_t flags)
175+
{
176+
if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
177+
void *ptr = kmalloc(size, flags | __GFP_NOWARN);
178+
if (ptr)
179+
return ptr;
180+
}
181+
182+
return __vmalloc(size, flags | __GFP_HIGHMEM, PAGE_KERNEL);
183+
}
184+
185+
void ceph_kvfree(const void *ptr)
186+
{
187+
if (is_vmalloc_addr(ptr))
188+
vfree(ptr);
189+
else
190+
kfree(ptr);
191+
}
192+
173193

174194
static int parse_fsid(const char *str, struct ceph_fsid *fsid)
175195
{

‎net/ceph/messenger.c‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3131,13 +3131,7 @@ struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
31313131

31323132
/* front */
31333133
if (front_len) {
3134-
if (front_len > PAGE_CACHE_SIZE) {
3135-
m->front.iov_base = __vmalloc(front_len, flags,
3136-
PAGE_KERNEL);
3137-
m->front_is_vmalloc = true;
3138-
} else {
3139-
m->front.iov_base = kmalloc(front_len, flags);
3140-
}
3134+
m->front.iov_base = ceph_kvmalloc(front_len, flags);
31413135
if (m->front.iov_base == NULL) {
31423136
dout("ceph_msg_new can't allocate %d bytes\n",
31433137
front_len);
@@ -3259,10 +3253,7 @@ static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
32593253
void ceph_msg_kfree(struct ceph_msg *m)
32603254
{
32613255
dout("msg_kfree %p\n", m);
3262-
if (m->front_is_vmalloc)
3263-
vfree(m->front.iov_base);
3264-
else
3265-
kfree(m->front.iov_base);
3256+
ceph_kvfree(m->front.iov_base);
32663257
kmem_cache_free(ceph_msg_cache, m);
32673258
}
32683259

0 commit comments

Comments
 (0)