Skip to content

Commit 10c1285

Browse files
committed
libceph: avoid a __vmalloc() deadlock in ceph_kvmalloc()
The vmalloc allocator doesn't fully respect the specified gfp mask: while the actual pages are allocated as requested, the page table pages are always allocated with GFP_KERNEL. ceph_kvmalloc() may be called with GFP_NOFS and GFP_NOIO (for ceph and rbd respectively), so this may result in a deadlock. There is no real reason for the current PAGE_ALLOC_COSTLY_ORDER logic, it's just something that seemed sensible at the time (ceph_kvmalloc() predates kvmalloc()). kvmalloc() is smarter: in an attempt to reduce long term fragmentation, it first tries to kmalloc non-disruptively. Switch to kvmalloc() and set the respective PF_MEMALLOC_* flag using the scope API to avoid the deadlock. Note that kvmalloc() needs to be passed GFP_KERNEL to enable the fallback. Signed-off-by: Ilya Dryomov <idryomov@gmail.com> Reviewed-by: Jeff Layton <jlayton@kernel.org>
1 parent 6fd4e63 commit 10c1285

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

‎net/ceph/ceph_common.c‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/nsproxy.h>
1414
#include <linux/parser.h>
1515
#include <linux/sched.h>
16+
#include <linux/sched/mm.h>
1617
#include <linux/seq_file.h>
1718
#include <linux/slab.h>
1819
#include <linux/statfs.h>
@@ -185,18 +186,34 @@ int ceph_compare_options(struct ceph_options *new_opt,
185186
}
186187
EXPORT_SYMBOL(ceph_compare_options);
187188

189+
/*
190+
* kvmalloc() doesn't fall back to the vmalloc allocator unless flags are
191+
* compatible with (a superset of) GFP_KERNEL. This is because while the
192+
* actual pages are allocated with the specified flags, the page table pages
193+
* are always allocated with GFP_KERNEL. map_vm_area() doesn't even take
194+
* flags because GFP_KERNEL is hard-coded in {p4d,pud,pmd,pte}_alloc().
195+
*
196+
* ceph_kvmalloc() may be called with GFP_KERNEL, GFP_NOFS or GFP_NOIO.
197+
*/
188198
void *ceph_kvmalloc(size_t size, gfp_t flags)
189199
{
190-
if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
191-
void *ptr = kmalloc(size, flags | __GFP_NOWARN);
192-
if (ptr)
193-
return ptr;
200+
void *p;
201+
202+
if ((flags & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) {
203+
p = kvmalloc(size, flags);
204+
} else if ((flags & (__GFP_IO | __GFP_FS)) == __GFP_IO) {
205+
unsigned int nofs_flag = memalloc_nofs_save();
206+
p = kvmalloc(size, GFP_KERNEL);
207+
memalloc_nofs_restore(nofs_flag);
208+
} else {
209+
unsigned int noio_flag = memalloc_noio_save();
210+
p = kvmalloc(size, GFP_KERNEL);
211+
memalloc_noio_restore(noio_flag);
194212
}
195213

196-
return __vmalloc(size, flags, PAGE_KERNEL);
214+
return p;
197215
}
198216

199-
200217
static int parse_fsid(const char *str, struct ceph_fsid *fsid)
201218
{
202219
int i = 0;

0 commit comments

Comments
 (0)