From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 314FA3F9F21 for ; Wed, 10 Jun 2026 10:46:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781088402; cv=none; b=ItcJHI2ilEO6ZuDqqXidDwk+xQq8jqHEhdR2wkO96vSuY+vzO+QpxAcGcYhhDuL0B9GCK5je5f+W/7Xh7wVMLOur5ajTP5eYf0RqLFai/JGnSzipHH7Ply9IjBTiAmKjrLGj2HzK/JCO+fo0UjqB5KlBW1kbcy0GmAjbb/A7Wh4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781088402; c=relaxed/simple; bh=gQzKM1qI775IjCUEbBcVlW/lmGqZPvj9D+gU93Y14i0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=jDKjN+12NRj/zBIIUyv21SByAuO03gfvyj3pjVP03S+YSuRFyd6emfjWnMIUQTZ/k57UVAdF2EkE3ccWUQg+G8CpFOT8Tv2SRteG4ak/bNCqcdcOnCcnV3gY0NOlwfLrJQ98pf28ddThOeLDC/Oyoq9aJtNqph/5CkgF7Ukqt6Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=J7sMymAY; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="J7sMymAY" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-490acbb0f89so44986055e9.0 for ; Wed, 10 Jun 2026 03:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781088395; x=1781693195; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Xrt0LCXtCGxG4r1DSnamedxnwcAsXfmO6rDbEUTJaIk=; b=J7sMymAYTKyAPUsxil/ggYE4a0AHiKno4J1+soUxfyPft9MsPa1tcKm85h34S26fvP phmIVJOgFskGqL7/IAi5ojv7AoXVjJdsDZpk/P9kCAzovSj48UhYqV/iYVk9jLjgezIA 4OPbsk1ESvEFR51PZbUpT80Ltrm77ZjPMS4jZ4j0XM0S1msL0cSatq/ZXW+IvF7r+ySR CHMMo11FQ+oob5Hg0nhYUFNT2rKdNcVcikDYBxpSu2xu2tG2yKicRlyTq28i3lu4CEug gLS13WaEq99oO0kq/6ltavSuaEcrv8FtlTYUVUgetypdVDHCXoBGOkGX+akScueFNz/+ Fxog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781088395; x=1781693195; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xrt0LCXtCGxG4r1DSnamedxnwcAsXfmO6rDbEUTJaIk=; b=bB94O8k1mLn+trNS4+959FhqHVUkPEZPkMhihx1r2o8O3uY80XLbJmDgPBZcVXyyBH fzJlDzNxldBBSiL/Rfdvs934Nju6efeKcFcOJJwWozRvuQvmKzu5h3HCENTpDeIJkO2V bqEIMZCryvK72K3SCK4X+oR3gua/H/XzY0CpkNTvFC8d7bDQTc0blsjP8NZwD4zp4H9z V9bPw9IyjjDd4iN5pIk8JeB3QUrJXSFpIAQaiSKwi9aTCyXLZJ7UIjxeIY3duo/r5yRH 3cv3aYz67W1CVLkXPjAnElESUXtiAf4WQ1xQU18bx4M2iSDSNS5LNxDbNlt36tRbAhiy 464Q== X-Forwarded-Encrypted: i=1; AFNElJ9FabQuuFOu4FXtnfshdgL/Ne2tD8Y0WPONQd7QcJH1++pLLg7z0zuBxv5tbI4D1OM6Cq9pj1yoP+RlkEk=@vger.kernel.org X-Gm-Message-State: AOJu0Ywn9Cxr5KoDl2C4VgwJzurzTw9dekt9tIqy4CncFnBGbwHv7Ffj Oyf0lCrMiM2e/7QADdDUdgd0+lcWWsDI84sHp/eMkejApT67eVREJT2P X-Gm-Gg: Acq92OF2r2w2ZunWJIo88dBoqfxVTmkzNAE1Gybg+jUzGmxPIPNcdBWUMhHplpNVwsV wv5kFHKp+4jnMqEqF0aKBCgFgwRMzxv2D4mZ1PMdeMyfj8pEaacc+BLEWYKOUzdIYHa4Asjl7l/ uld++hubp+TGDQo7Xtpxm3NpwGtBuyoqCnIyXjus7jobm3csU2ve8HZdzytfck3F3ubnzqJC9ML 2jltdVdCpeVuS4u1G/oH1I4C/krunfnu1ORGWdCvvbvlEKclZue7+U4ggveZhB3GhSX5aJsGezo osjCvIPoPg51cXEk9exSTYfZCRSDCeOlXX6laRpg2ddYDvqt98mELST8W/FHRVRlmZDhsx6cnB9 awAqDGORF1NNrglwIyCGrlwjGx6VX+9rsU1aGGVoz4XTEf5CRwCmSWZ0gbHeDn6Udz9l++LHO+/ 9FboAfOxBzCXqOqZtEvh9gJHAr0pCFklfroo87ad1BDbz61qwqVavFc4dq4YRehqrrhRCd0nfxc KTFKXOQqaunD334oYXDoSprhQMu1KmDulrYYyHZ3ctN X-Received: by 2002:a05:600c:c493:b0:490:bb3e:30c2 with SMTP id 5b1f17b1804b1-490c25f1682mr418534705e9.18.1781088395188; Wed, 10 Jun 2026 03:46:35 -0700 (PDT) Received: from fedora (cpc92878-cmbg18-2-0-cust539.5-4.cable.virginm.net. [86.16.54.28]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490bc391aaasm641985295e9.1.2026.06.10.03.46.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jun 2026 03:46:34 -0700 (PDT) Date: Wed, 10 Jun 2026 11:46:32 +0100 From: Vishal Moola To: chenyichong Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] tools/mm/page_owner_sort: free per-record allocations Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Wed, Jun 10, 2026 at 10:11:08AM +0800, chenyichong wrote: > add_list() allocates a command name and text buffer for every page owner record. The cleanup path only frees the outer list array, leaving both allocations for every retained record behind until process exit. > > Records discarded while culling also lose their allocation references when the list is compacted. Free those records as they are merged, track the compacted list size, and release the remaining per-record allocations on exit. Also handle allocation failures in get_comm() and unwind the command name if allocating the text buffer fails. Please take a look at the process[1]. Aka wrap commit messages at ~75 characters. Running scripts/checkpatch.pl should catch this for you. > Signed-off-by: chenyichong Also I hope chenyichong is your name :). I'm not that familiar with chinese names, and the lowercase one-word makes it easy to mistake as an alias (we don't allow aliases). I'm used to seeing "[First Name] [Surname]." > --- > tools/mm/page_owner_sort.c | 22 +++++++++++++++++++++- > 1 file changed, 21 insertions(+), 1 deletion(-) > > diff --git a/tools/mm/page_owner_sort.c b/tools/mm/page_owner_sort.c > index e6954909401c..67a7fc6d9de2 100644 > --- a/tools/mm/page_owner_sort.c > +++ b/tools/mm/page_owner_sort.c > @@ -372,6 +372,9 @@ static char *get_comm(char *buf) > { > char *comm_str = malloc(TASK_COMM_LEN); > > + if (!comm_str) > + return NULL; > + > memset(comm_str, 0, TASK_COMM_LEN); > > search_pattern(&comm_pattern, comm_str, buf); > @@ -386,6 +389,12 @@ static char *get_comm(char *buf) > return comm_str; > } > > +static void free_block_list(struct block_list *block) > +{ > + free(block->comm); > + free(block->txt); > +} > + > static int get_arg_type(const char *arg) > { > if (!strcmp(arg, "pid") || !strcmp(arg, "p")) > @@ -480,9 +489,15 @@ static bool add_list(char *buf, int len, char *ext_buf) > list[list_size].pid = get_pid(buf); > list[list_size].tgid = get_tgid(buf); > list[list_size].comm = get_comm(buf); > + if (!list[list_size].comm) { > + fprintf(stderr, "Out of memory\n"); > + return false; > + } > list[list_size].txt = malloc(len+1); > if (!list[list_size].txt) { > fprintf(stderr, "Out of memory\n"); > + free(list[list_size].comm); > + list[list_size].comm = NULL; > return false; Returning false here sends us back to the error handling path in main() where you end up calling your free_block_list() anyway. So we don't need this here, right? > } > memcpy(list[list_size].txt, buf, len); > @@ -841,8 +856,10 @@ int main(int argc, char **argv) > } else { > list[count-1].num += list[i].num; > list[count-1].page_num += list[i].page_num; > + free_block_list(&list[i]); > } > } > + list_size = count; > > qsort(list, count, sizeof(list[0]), compare_sort_condition); > > @@ -876,8 +893,11 @@ int main(int argc, char **argv) > free(ext_buf); > if (buf) > free(buf); > - if (list) > + if (list) { > + for (i = 0; i < list_size; i++) > + free_block_list(&list[i]); > free(list); > + } > out_ts: > regfree(&ts_nsec_pattern); > out_comm: [1] https://docs.kernel.org/process/submitting-patches.html