Commit e30ee581 authored by Zhi Zhang's avatar Zhi Zhang Committed by Ilya Dryomov

ceph: try to allocate enough memory for reserved caps

ceph_reserve_caps() may not reserve enough caps under high memory
pressure, but it saved the needed caps number that expected to
be reserved. When getting caps, crash would happen due to number
mismatch.

Now we will try to trim more caps when failing to allocate memory
for caps need to be reserved, then try again. If still failing to
allocate memory, return -ENOMEM.
Signed-off-by: default avatarZhi Zhang <zhang.david2011@gmail.com>
Reviewed-by: default avatar"Yan, Zheng" <zyan@redhat.com>
Signed-off-by: default avatarIlya Dryomov <idryomov@gmail.com>
parent 0f439c74
...@@ -154,13 +154,19 @@ void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta) ...@@ -154,13 +154,19 @@ void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
spin_unlock(&mdsc->caps_list_lock); spin_unlock(&mdsc->caps_list_lock);
} }
void ceph_reserve_caps(struct ceph_mds_client *mdsc, /*
* Called under mdsc->mutex.
*/
int ceph_reserve_caps(struct ceph_mds_client *mdsc,
struct ceph_cap_reservation *ctx, int need) struct ceph_cap_reservation *ctx, int need)
{ {
int i; int i, j;
struct ceph_cap *cap; struct ceph_cap *cap;
int have; int have;
int alloc = 0; int alloc = 0;
int max_caps;
bool trimmed = false;
struct ceph_mds_session *s;
LIST_HEAD(newcaps); LIST_HEAD(newcaps);
dout("reserve caps ctx=%p need=%d\n", ctx, need); dout("reserve caps ctx=%p need=%d\n", ctx, need);
...@@ -179,16 +185,37 @@ void ceph_reserve_caps(struct ceph_mds_client *mdsc, ...@@ -179,16 +185,37 @@ void ceph_reserve_caps(struct ceph_mds_client *mdsc,
spin_unlock(&mdsc->caps_list_lock); spin_unlock(&mdsc->caps_list_lock);
for (i = have; i < need; i++) { for (i = have; i < need; i++) {
retry:
cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
if (!cap) if (!cap) {
break; if (!trimmed) {
for (j = 0; j < mdsc->max_sessions; j++) {
s = __ceph_lookup_mds_session(mdsc, j);
if (!s)
continue;
mutex_unlock(&mdsc->mutex);
mutex_lock(&s->s_mutex);
max_caps = s->s_nr_caps - (need - i);
ceph_trim_caps(mdsc, s, max_caps);
mutex_unlock(&s->s_mutex);
ceph_put_mds_session(s);
mutex_lock(&mdsc->mutex);
}
trimmed = true;
goto retry;
} else {
pr_warn("reserve caps ctx=%p ENOMEM "
"need=%d got=%d\n",
ctx, need, have + alloc);
goto out_nomem;
}
}
list_add(&cap->caps_item, &newcaps); list_add(&cap->caps_item, &newcaps);
alloc++; alloc++;
} }
/* we didn't manage to reserve as much as we needed */ BUG_ON(have + alloc != need);
if (have + alloc != need)
pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
ctx, need, have + alloc);
spin_lock(&mdsc->caps_list_lock); spin_lock(&mdsc->caps_list_lock);
mdsc->caps_total_count += alloc; mdsc->caps_total_count += alloc;
...@@ -204,6 +231,24 @@ void ceph_reserve_caps(struct ceph_mds_client *mdsc, ...@@ -204,6 +231,24 @@ void ceph_reserve_caps(struct ceph_mds_client *mdsc,
dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n", dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
ctx, mdsc->caps_total_count, mdsc->caps_use_count, ctx, mdsc->caps_total_count, mdsc->caps_use_count,
mdsc->caps_reserve_count, mdsc->caps_avail_count); mdsc->caps_reserve_count, mdsc->caps_avail_count);
return 0;
out_nomem:
while (!list_empty(&newcaps)) {
cap = list_first_entry(&newcaps,
struct ceph_cap, caps_item);
list_del(&cap->caps_item);
kmem_cache_free(ceph_cap_cachep, cap);
}
spin_lock(&mdsc->caps_list_lock);
mdsc->caps_avail_count += have;
mdsc->caps_reserve_count -= have;
BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
mdsc->caps_reserve_count +
mdsc->caps_avail_count);
spin_unlock(&mdsc->caps_list_lock);
return -ENOMEM;
} }
int ceph_unreserve_caps(struct ceph_mds_client *mdsc, int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
......
...@@ -604,10 +604,20 @@ static void __register_request(struct ceph_mds_client *mdsc, ...@@ -604,10 +604,20 @@ static void __register_request(struct ceph_mds_client *mdsc,
struct ceph_mds_request *req, struct ceph_mds_request *req,
struct inode *dir) struct inode *dir)
{ {
int ret = 0;
req->r_tid = ++mdsc->last_tid; req->r_tid = ++mdsc->last_tid;
if (req->r_num_caps) if (req->r_num_caps) {
ceph_reserve_caps(mdsc, &req->r_caps_reservation, ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
req->r_num_caps); req->r_num_caps);
if (ret < 0) {
pr_err("__register_request %p "
"failed to reserve caps: %d\n", req, ret);
/* set req->r_err to fail early from __do_request */
req->r_err = ret;
return;
}
}
dout("__register_request %p tid %lld\n", req, req->r_tid); dout("__register_request %p tid %lld\n", req, req->r_tid);
ceph_mdsc_get_request(req); ceph_mdsc_get_request(req);
insert_request(&mdsc->request_tree, req); insert_request(&mdsc->request_tree, req);
...@@ -1545,9 +1555,9 @@ static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg) ...@@ -1545,9 +1555,9 @@ static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
/* /*
* Trim session cap count down to some max number. * Trim session cap count down to some max number.
*/ */
static int trim_caps(struct ceph_mds_client *mdsc, int ceph_trim_caps(struct ceph_mds_client *mdsc,
struct ceph_mds_session *session, struct ceph_mds_session *session,
int max_caps) int max_caps)
{ {
int trim_caps = session->s_nr_caps - max_caps; int trim_caps = session->s_nr_caps - max_caps;
...@@ -2776,7 +2786,7 @@ static void handle_session(struct ceph_mds_session *session, ...@@ -2776,7 +2786,7 @@ static void handle_session(struct ceph_mds_session *session,
break; break;
case CEPH_SESSION_RECALL_STATE: case CEPH_SESSION_RECALL_STATE:
trim_caps(mdsc, session, le32_to_cpu(h->max_caps)); ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
break; break;
case CEPH_SESSION_FLUSHMSG: case CEPH_SESSION_FLUSHMSG:
......
...@@ -444,4 +444,7 @@ ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target); ...@@ -444,4 +444,7 @@ ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target);
extern void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc, extern void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
struct ceph_mds_session *session); struct ceph_mds_session *session);
extern int ceph_trim_caps(struct ceph_mds_client *mdsc,
struct ceph_mds_session *session,
int max_caps);
#endif #endif
...@@ -649,7 +649,7 @@ extern int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check); ...@@ -649,7 +649,7 @@ extern int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check);
extern void ceph_caps_init(struct ceph_mds_client *mdsc); extern void ceph_caps_init(struct ceph_mds_client *mdsc);
extern void ceph_caps_finalize(struct ceph_mds_client *mdsc); extern void ceph_caps_finalize(struct ceph_mds_client *mdsc);
extern void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta); extern void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta);
extern void ceph_reserve_caps(struct ceph_mds_client *mdsc, extern int ceph_reserve_caps(struct ceph_mds_client *mdsc,
struct ceph_cap_reservation *ctx, int need); struct ceph_cap_reservation *ctx, int need);
extern int ceph_unreserve_caps(struct ceph_mds_client *mdsc, extern int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
struct ceph_cap_reservation *ctx); struct ceph_cap_reservation *ctx);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment