Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1923988yba; Tue, 2 Apr 2019 19:56:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqxzd75EIbE8kCnAfNrHt+TvfQiH6K7ok9TPf//5mg8qvkBA+4cp4cihWn2xvXysButSQKgH X-Received: by 2002:a17:902:7043:: with SMTP id h3mr20555126plt.228.1554260206229; Tue, 02 Apr 2019 19:56:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554260206; cv=none; d=google.com; s=arc-20160816; b=wCj+vafH6UmUJmMfWAfGFO1865C/OrPYPB6v69wUDzrxa2fVZKIGjRy09levAlWr3V f/fCNTPRFpcmcW83Ec91B9jbvwkqCvF7RRwkKj9a29PwpbPhX2+i8ny5Lf2Puglmyhkv TQ8qqdflbg9VbMs6DQpdwwizdnqf6riEsRNsOKZgu2CZj8+nMZUHxAFIk2tKoTO2G2Sd TbZHACAvs7u1giJML9RLT3E73ZxaQkdT80bccY9wewCi4oLgd8cdzupGf1g6aP0C+WBV elCJ0IJa1msH2U1cWJkmIfpNDIr+iBHLMJxhac2w6Qc8QUXfvpYuIKegZ9tfZSm+Q5jX OKkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=eKelixOSe2DiHbodegTi3JJbyphsCM3fp2oZSGXUKhw=; b=p/XAX7v2QzBZebsOpl6mu/ZGIu5DKRTd9hcxojsyxl75BnP2F/qfDCJax+NBT9dWLV U1DFq2klbhP5xJVNW/pagob3EuQ60DJtBmeLX1sZxerm9zcMzP7T/Q7ljBuohaPksyU1 jQDBnY4CZeL1sRTFCJTDG/G60lCVtABghoO07v5Qr645gZWibyjPRXRLunhVxPcQUlXP 26qkDHkW+k7D272A+vQUBQ4vkIabZIAkmZ7pEr0tdDWt9nePJH9Q5e8QyPnpWxRBIfLc 3orFZ+VZeZ5Q8kejksdAsgsEBk+BXa0iubLF+3FAfXmRe6zncE9fPoEawVXDyLMZQTSa FMnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=bGGzdKoy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f6si12997154plf.356.2019.04.02.19.56.30; Tue, 02 Apr 2019 19:56:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=bGGzdKoy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727659AbfDCCzF (ORCPT + 99 others); Tue, 2 Apr 2019 22:55:05 -0400 Received: from mail-qt1-f196.google.com ([209.85.160.196]:39029 "EHLO mail-qt1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726411AbfDCCzF (ORCPT ); Tue, 2 Apr 2019 22:55:05 -0400 Received: by mail-qt1-f196.google.com with SMTP id t28so17836552qte.6; Tue, 02 Apr 2019 19:55:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=eKelixOSe2DiHbodegTi3JJbyphsCM3fp2oZSGXUKhw=; b=bGGzdKoyz0x/RH1ldXcpVkZx8NGFxJD5FHlmGatEodEOsctamvqiFg+aSiGI3f96XT SqI3G3MdSNfIwtnQtPtP+IX/tYMC9VU4R4ZsaPGDtOCV4M6IrbRNLvH70QxXKD/UcRCM YFuXi7qllcPCcegfPe+EjYOjLko+hCXdlTBY/p7R9RpXx83k4YXUmQHLrtGHeu2E3BNV p+BYgtAF6N3A6GYZ/cZgKN3nyuZVeUBUQNXYf7kNPwPpFk7Hs6ztTLBuk2kqyO0xfdjP 1i7QjPdm+JR8xr57g/TFUzuJiWmaVcTODhGaLeqASJq8UKNbmxE9r3v93sl4VF/JrvK0 SxXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=eKelixOSe2DiHbodegTi3JJbyphsCM3fp2oZSGXUKhw=; b=j93J+7oc1ECZ86RBBvijre3SYdyzgmD71ky2i+Hi/strwEWLR+C3YURjnv2Q8DREom aKSqXVCknhWvn0CIoxp2U6Q2VyKXDyQi8nJvUARw17pfWOyyoIlGsLzUI865pKh7Sp3u /eX/BzYyfXNGXAY45iJhn/Cxkd8DhKh1PPT+N3zvJmllwHxeqzYckXbCyGAE8HVNpR9G Vq10V64MYCuiuRXUzQu2J3EKPUkyYSSk94mSjOq6yAhq2usFHIRLi3jsArbyiQhdmexM 6lxj7dn9ER4SVHLLs7RtQ3I5GmyFnCgtZCwxptuyKuQpi72NlfjENNZRSPZszU8KHby6 UfIw== X-Gm-Message-State: APjAAAVY+TMKk6TbU+Gko+VbSppET4tYJSYa6Y7Z6fd2RHqN8fokqICj ohp6535H+bkex8aaj9DGbm5SQrSiENJy/SYYDCc= X-Received: by 2002:ac8:262c:: with SMTP id u41mr5383757qtu.198.1554260103770; Tue, 02 Apr 2019 19:55:03 -0700 (PDT) MIME-Version: 1.0 References: <20190315111107.15154-1-lhenriques@suse.com> <87r2b4zd2q.fsf@suse.com> <87lg1c17b2.fsf@suse.com> <87a7hr19v7.fsf@suse.com> <87o963jkcj.fsf@suse.com> In-Reply-To: <87o963jkcj.fsf@suse.com> From: "Yan, Zheng" Date: Wed, 3 Apr 2019 10:54:51 +0800 Message-ID: Subject: Re: [PATCH] ceph: Fix a memory leak in ci->i_head_snapc To: Luis Henriques Cc: "Yan, Zheng" , Sage Weil , Ilya Dryomov , ceph-devel , Linux Kernel Mailing List , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 22, 2019 at 6:04 PM Luis Henriques wrote: > > Luis Henriques writes: > > > "Yan, Zheng" writes: > > > >> On Tue, Mar 19, 2019 at 12:22 AM Luis Henriques wrote: > >>> > >>> "Yan, Zheng" writes: > >>> > >>> > On Mon, Mar 18, 2019 at 6:33 PM Luis Henriques wrote: > >>> >> > >>> >> "Yan, Zheng" writes: > >>> >> > >>> >> > On Fri, Mar 15, 2019 at 7:13 PM Luis Henriques wrote: > >>> >> >> > >>> >> >> I'm occasionally seeing a kmemleak warning in xfstest generic/013: > >>> >> >> > >>> >> >> unreferenced object 0xffff8881fccca940 (size 32): > >>> >> >> comm "kworker/0:1", pid 12, jiffies 4295005883 (age 130.648s) > >>> >> >> hex dump (first 32 bytes): > >>> >> >> 01 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 ................ > >>> >> >> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ > >>> >> >> backtrace: > >>> >> >> [<00000000d741a1ea>] build_snap_context+0x5b/0x2a0 > >>> >> >> [<0000000021a00533>] rebuild_snap_realms+0x27/0x90 > >>> >> >> [<00000000ac538600>] rebuild_snap_realms+0x42/0x90 > >>> >> >> [<000000000e955fac>] ceph_update_snap_trace+0x2ee/0x610 > >>> >> >> [<00000000a9550416>] ceph_handle_snap+0x317/0x5f3 > >>> >> >> [<00000000fc287b83>] dispatch+0x362/0x176c > >>> >> >> [<00000000a312c741>] ceph_con_workfn+0x9ce/0x2cf0 > >>> >> >> [<000000004168e3a9>] process_one_work+0x1d4/0x400 > >>> >> >> [<000000002188e9e7>] worker_thread+0x2d/0x3c0 > >>> >> >> [<00000000b593e4b3>] kthread+0x112/0x130 > >>> >> >> [<00000000a8587dca>] ret_from_fork+0x35/0x40 > >>> >> >> [<00000000ba1c9c1d>] 0xffffffffffffffff > >>> >> >> > >>> >> >> It looks like it is possible that we miss a flush_ack from the MDS when, > >>> >> >> for example, umounting the filesystem. In that case, we can simply drop > >>> >> >> the reference to the ceph_snap_context obtained in ceph_queue_cap_snap(). > >>> >> >> > >>> >> >> Link: https://tracker.ceph.com/issues/38224 > >>> >> >> Cc: stable@vger.kernel.org > >>> >> >> Signed-off-by: Luis Henriques > >>> >> >> --- > >>> >> >> fs/ceph/caps.c | 7 +++++++ > >>> >> >> 1 file changed, 7 insertions(+) > >>> >> >> > >>> >> >> diff --git a/fs/ceph/caps.c b/fs/ceph/caps.c > >>> >> >> index 36a8dc699448..208f4dc6f574 100644 > >>> >> >> --- a/fs/ceph/caps.c > >>> >> >> +++ b/fs/ceph/caps.c > >>> >> >> @@ -1054,6 +1054,7 @@ int ceph_is_any_caps(struct inode *inode) > >>> >> >> static void drop_inode_snap_realm(struct ceph_inode_info *ci) > >>> >> >> { > >>> >> >> struct ceph_snap_realm *realm = ci->i_snap_realm; > >>> >> >> + > >>> >> >> spin_lock(&realm->inodes_with_caps_lock); > >>> >> >> list_del_init(&ci->i_snap_realm_item); > >>> >> >> ci->i_snap_realm_counter++; > >>> >> >> @@ -1063,6 +1064,12 @@ static void drop_inode_snap_realm(struct ceph_inode_info *ci) > >>> >> >> spin_unlock(&realm->inodes_with_caps_lock); > >>> >> >> ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc, > >>> >> >> realm); > >>> >> >> + /* > >>> >> >> + * ci->i_head_snapc should be NULL, but we may still be waiting for a > >>> >> >> + * flush_ack from the MDS. In that case, we still hold a ref for the > >>> >> >> + * ceph_snap_context and we need to drop it. > >>> >> >> + */ > >>> >> >> + ceph_put_snap_context(ci->i_head_snapc); > >>> >> >> } > >>> >> >> > >>> >> >> /* > >>> >> > > >>> >> > This does not seem right. i_head_snapc is cleared when > >>> >> > (ci->i_wrbuffer_ref_head == 0 && ci->i_dirty_caps == 0 && > >>> >> > ci->i_flushing_caps == 0) . Nothing do with dropping ci->i_snap_realm. > >>> >> > Did you see 'reconnect denied' during the test? If you did, the fix > >>> >> > should be in iterate_session_caps() > >>> >> > > >>> >> > >>> >> No, I didn't saw any 'reconnect denied' in the test. The test actually > >>> >> seems to execute fine, except from the memory leak. > >>> >> > >>> >> It's very difficult to reproduce this issue, but last time I managed to > >>> >> get this memory leak to trigger I actually had some debugging code in > >>> >> drop_inode_snap_realm, something like: > >>> >> > >>> >> if (ci->i_head_snapc) > >>> >> printk("i_head_snapc: 0x%px\n", ci->i_head_snapc); > >>> > > >>> > please add code that prints i_wrbuffer_ref_head, i_dirty_caps, > >>> > i_flushing_caps. and try reproducing it again. > >>> > > >>> > >>> Ok, it took me a few hours, but I managed to reproduce the bug, with > >>> those extra printks. All those values are set to 0 when the bug > >>> triggers (and i_head_snapc != NULL). > >>> > >> > >> Thanks, which test triggers this bug? > > > > That's generic/013. It usually triggers after a few hours of running it > > in a loop (I'm using a vstart cluster for that). > > > >> > >> I searched that code, found we may fail to cleanup i_head_snap in two > >> places. One is in ceph_queue_cap_snap, Another is in > >> remove_session_caps_cb(). > > > > Ah, great! I spent a lot of time looking but I couldn't really find it. > > My bet was that ceph_queue_cap_snap was doing the ceph_get_snap_context > > and that the corresponding ceph_put_snap_context would be done in > > handle_cap_flush_ack. That's why I mentioned the missing flush_ack from > > MDS. > > Something that I didn't said explicitly is that I *know* that the > unbalanced ceph_get_snap_context() is the one in function > ceph_queue_cap_snap(). I know this for sure because I've managed to > reproduce the issue several times with an instrumented kernel (with > tracepoints) that allowed me to keep track of all ceph_snap_context > operations (create, get, put, delete). > Sorry for the delay. please try diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c index 45f061e47721..5bc30e0d252a 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -1420,6 +1420,15 @@ static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap, list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove); ci->i_prealloc_cap_flush = NULL; } + + if (drop && + ci->i_wrbuffer_ref_head == 0 && + ci->i_wr_ref == 0 && + ci->i_dirty_caps == 0 && + ci->i_flushing_caps == 0) { + ceph_put_snap_context(ci->i_head_snapc); + ci->i_head_snapc = NULL; + } } spin_unlock(&ci->i_ceph_lock); while (!list_empty(&to_remove)) { diff --git a/fs/ceph/snap.c b/fs/ceph/snap.c index 89aa37fa0f84..0af4b3d32122 100644 --- a/fs/ceph/snap.c +++ b/fs/ceph/snap.c @@ -572,7 +572,12 @@ void ceph_queue_cap_snap(struct ceph_inode_info *ci) old_snapc = NULL; update_snapc: - if (ci->i_head_snapc) { + if (ci->i_wrbuffer_ref_head == 0 && + ci->i_wr_ref == 0 && + ci->i_dirty_caps == 0 && + ci->i_flushing_caps == 0) { + ci->i_head_snapc = NULL; + } else { ci->i_head_snapc = ceph_get_snap_context(new_snapc); dout(" new snapc is %p\n", new_snapc); } > Cheers, > -- > Luis