linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: fenghua.yu@intel.com, reinette.chatre@intel.com,
	tglx@linutronix.de, mingo@redhat.com, bp@alien8.de,
	x86@kernel.org, hpa@zytor.com,
	Willem de Bruijn <willemb@google.com>
Subject: [PATCH] x86/resctrl: Fix memory leak on kernfs dir removal
Date: Mon, 26 Oct 2020 11:09:13 -0400	[thread overview]
Message-ID: <20201026150913.2485171-1-willemdebruijn.kernel@gmail.com> (raw)

From: Willem de Bruijn <willemb@google.com>

Resctrl takes an extra kernfs ref on directory entries, to access
the entry on cleanup in rdtgroup_kn_unlock after removing the entire
subtree with kfree_remove.

But the path takes an extra ref both on mkdir and on rmdir.

The kernfs_get on mkdir causes a memleak in the unlikely exit with
error in the same function, as no extra kernfs_put exists and no extra
rdtgroup_kn_unlock occurs.

More importantly, essentially the same happens in the normal path, as
this simple program demonstrates:

    for i in {1..200000}; do
      mkdir /sys/fs/resctrl/task1
      rmdir /sys/fs/resctrl/task1
    done
    slabtop

When taking an extra ref for the duration of kernfs_remove, it is
easiest to reason about when holding this extra ref as short as
possible. For that, the refcnt on error reason and free on umount
(rmdir_all_sub), remove the first kernfs_get on mkdir, leaving the
other on rmdir.

As the caller of rdtgroup_rmdir, kernfs_iop_rmdir, itself takes a
reference on the kernfs object, the extra reference is possibly not
needed at all.

Fixes: 60cf5e101fd4 ("x86/intel_rdt: Add mkdir to resctrl file system")
Signed-off-by: Willem de Bruijn <willemb@google.com>
---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
index 4d02ec8f371e..115a86bf6bd8 100644
--- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
+++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
@@ -2839,14 +2839,6 @@ static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
 	}
 	rdtgrp->kn = kn;
 
-	/*
-	 * kernfs_remove() will drop the reference count on "kn" which
-	 * will free it. But we still need it to stick around for the
-	 * rdtgroup_kn_unlock(kn} call below. Take one extra reference
-	 * here, which will be dropped inside rdtgroup_kn_unlock().
-	 */
-	kernfs_get(kn);
-
 	ret = rdtgroup_kn_set_ugid(kn);
 	if (ret) {
 		rdt_last_cmd_puts("kernfs perm error\n");
-- 
2.29.0.rc1.297.gfa9743e501-goog


             reply	other threads:[~2020-10-26 15:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-26 15:09 Willem de Bruijn [this message]
2020-10-26 16:24 ` [PATCH] x86/resctrl: Fix memory leak on kernfs dir removal Reinette Chatre
2020-10-26 16:48   ` Willem de Bruijn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201026150913.2485171-1-willemdebruijn.kernel@gmail.com \
    --to=willemdebruijn.kernel@gmail.com \
    --cc=bp@alien8.de \
    --cc=fenghua.yu@intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=reinette.chatre@intel.com \
    --cc=tglx@linutronix.de \
    --cc=willemb@google.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).