From patchwork Mon Sep 26 16:06:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 12989038 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 675E3C07E9D for ; Mon, 26 Sep 2022 16:06:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D44278E006E; Mon, 26 Sep 2022 12:06:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CCD1A8E0066; Mon, 26 Sep 2022 12:06:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6D158E006E; Mon, 26 Sep 2022 12:06:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A41CC8E0066 for ; Mon, 26 Sep 2022 12:06:25 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 7458D1C44FB for ; Mon, 26 Sep 2022 16:06:25 +0000 (UTC) X-FDA: 79954713930.18.075EC22 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf18.hostedemail.com (Postfix) with ESMTP id 24C4E1C0009 for ; Mon, 26 Sep 2022 16:06:24 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 28E3260F7C; Mon, 26 Sep 2022 16:06:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04571C433D6; Mon, 26 Sep 2022 16:06:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664208383; bh=Gd8nmWWpHS7qrcrAlAsgmMmvuXxIQOEs22IHlMZZio0=; h=From:To:Cc:Subject:Date:From; b=s/IlLIcBHSG0s6pt6JTYAGSMUnpI7AP5PfNCVMo0WEdxGZ4u35p00DaHZODGbg+bn IXZllruYUf8YTOOriAdfbGEMs7hWDUB+uV7XZskgpEHw9Q48qy/yaCEGt8+D3g4dxo vthD80bb/I74L2pCC+yIUFxw9eWYi9CSwiJs1hf+28mAg6vjX40GwPf936XBrLOY6D QDrkWqGKDokWzWZ0E/k94jM9rHWP0fpb951+UMjm4F7yRZH+mw7ZAJ55RMDWypSM3A m2vDCgy2YCtVat3QouKxo4rq4eEzAROnzJXyZqA+aetpgDZy5Md2g4YvsQOpFIH8EE 5OXs0bc48M5ug== From: SeongJae Park To: akpm@linux-foundation.org Cc: ppbuk5246@gmail.com, damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, SeongJae Park Subject: [PATCH v4] damon/sysfs: Fix possible memleak on damon_sysfs_add_target. Date: Mon, 26 Sep 2022 16:06:11 +0000 Message-Id: <20220926160611.48536-1-sj@kernel.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664208385; a=rsa-sha256; cv=none; b=dNhF0O8qLRFgpEvGX1iQTwBPZsB7Y4/ri6ardZ0OstlolRlfpBSvss7lWl8YM3o6trpsl7 ig3udbMfpBsuLVQs3nPgd/fcnZXHTxUYH4u18XP6YZ4r1+n7bnazHJm/LerjBT3zMaWwnM oZBUvc/hV8qfA65dI+ubntgfWDtY5YY= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="s/IlLIcB"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf18.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664208385; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=sMxovil3pwTpAtVBl8hoFUbB5bP5MdyUMFTu+GujlK0=; b=6hULbh4irAKmiXnVU2iBtPVU8qjjWnTQ+icAnhfE/fP7jEssQpnJRoQqkWzAJKACq/RPKD cYvYE5GrywistTNaWIUsGotUG08lTatUPnFH/FBgmzSZbX4eGsqLAaWsB/t7Iy+E1mCNlf vbfiH054gsUmrpbq3iC3QTC5u2luOgo= X-Rspam-User: X-Rspamd-Queue-Id: 24C4E1C0009 X-Rspamd-Server: rspam08 X-Stat-Signature: 85nmc1qhk5krhutpam5yyrsdt5x4ixfe Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="s/IlLIcB"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf18.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org X-HE-Tag: 1664208384-893006 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Levi Yun When damon_sysfs_add_target couldn't find proper task, New allocated damon_target structure isn't registered yet, So, it's impossible to free new allocated one by damon_sysfs_destroy_targets. By calling daemon_add_target as soon as allocating new target, Fix this possible memory leak. Fixes: a61ea561c871 ("mm/damon/sysfs: link DAMON for virtual address spaces monitoring") Cc: # 5.17.x Signed-off-by: Levi Yun Reviewed-by: SeongJae Park Signed-off-by: SeongJae Park --- Changes from v3 (https://lore.kernel.org/damon/20220925234327.26345-1-ppbuk5246@gmail.com/) - Fix Fixes: tag - Add patch changelog Changes from v2 (https://lore.kernel.org/damon/20220925234053.26090-1-ppbuk5246@gmail.com/) - Add Fixes: and Cc: stable Changes from v1 (https://lore.kernel.org/damon/20220925140257.23431-1-ppbuk5246@gmail.com/) - Do damon_add_target() earlier instead of explicitly freeing the object mm/damon/sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index 455215a5c059..9f1219a67e3f 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -2172,12 +2172,12 @@ static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target, if (!t) return -ENOMEM; + damon_add_target(ctx, t); if (damon_target_has_pid(ctx)) { t->pid = find_get_pid(sys_target->pid); if (!t->pid) goto destroy_targets_out; } - damon_add_target(ctx, t); err = damon_sysfs_set_regions(t, sys_target->regions); if (err) goto destroy_targets_out;