From patchwork Tue Nov 14 03:42:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13454753 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 0196DC4167D for ; Tue, 14 Nov 2023 03:42:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06C576B0276; Mon, 13 Nov 2023 22:42:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 01DEA6B0280; Mon, 13 Nov 2023 22:42:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDAEE6B0281; Mon, 13 Nov 2023 22:42:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CAD586B0276 for ; Mon, 13 Nov 2023 22:42:22 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 83726409C6 for ; Tue, 14 Nov 2023 03:42:22 +0000 (UTC) X-FDA: 81455162124.19.EC6CFE4 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf22.hostedemail.com (Postfix) with ESMTP id BD096C0009 for ; Tue, 14 Nov 2023 03:42:20 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=b5qbzO+z; spf=pass (imf22.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699933340; 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=nBnWxULz4sB1k282R2qRVUVWhPl11XVq897JPJpGZLo=; b=ATHrqy4EymhsRB2aXfXNsMW/qcMNMKrTraB8Po+A0WyvhLRXLMOWN6jqYTK1F8uzny7Z0A dn9ECKMeJRzUW1oUQvxtVXeBsySzWCz3K3sCmlsax6sj0CnJ6bRtQniEUpv1XMTYKV/blJ oRUuRaxS8xMALYrB/5oJhDBr0FpY1do= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699933340; a=rsa-sha256; cv=none; b=CoI1RR/kfSlGLtp0aVpR9TDRTOsSIM0btorXakylf5XkMG8Vm5sLAUCIpXI/ON1DUoeqdB 9bVo7InC4ycr7nwK22n2aR74TRxjk39mZ/JblRyJgZL6Pn20t1ru4kSBRbdDhRM4/RNDIw GYrOeIU+8IBSCqq8NdBtdttAsM3uFMs= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=b5qbzO+z; spf=pass (imf22.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1cc5b7057d5so46883955ad.2 for ; Mon, 13 Nov 2023 19:42:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699933339; x=1700538139; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=nBnWxULz4sB1k282R2qRVUVWhPl11XVq897JPJpGZLo=; b=b5qbzO+zI6FOfEmpcQPrHqm3G7Eky+zqRS0HCbwHmGKXUM6xW2jq9EGTD5B/Z3epxj f4AQqh/RRsk3B5tX0gAopPDaRtJgH2SJtZTEU0elkBmQ2Feu45G12QES4SdAE7rWKOT3 htWFEgbZX8GTr3nM/YxGTp8F0u8npeHLdPaUNGNAP6SXoeDJztldcAzyxrQZG10JCSRa pMAhaav8xOWTCsJ0/3nq3A2Hhd56B3nxGbNjj5+K1rQ/fn9w5oNZC/v4hIgxjXby4BM0 o1o68eHNptOvY2Crnh7YMhYQgtSK4v2pFce0QHYJzeqSrF2/rkGLtL82hPzqpkiNOspJ ZXkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699933339; x=1700538139; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nBnWxULz4sB1k282R2qRVUVWhPl11XVq897JPJpGZLo=; b=JR5PsCAvyGR8CxYPWniLSTJgtBCsfGS5bSrxXqsi+dok2CRYtlFqnRktvY1D+Elwsx GqDxVs3fe05PV/W8Rv+V5Kq+mI6zdH71kEuoHG+PoWOAy9HP9Dl5hunJcfQRJZZ6EZL+ UsScY4shjeGZBrY4zaxZX1Z8f/AtZSlBf7rSKMLH2Uc01B0kvNGccvAjqqJpP4hVpWGp Mg8PtNe3/RTHrJVcL9Eps3ajbSPze+3if9+/PqhoNkoY85Snj1TWsPsn/pLpPDv2eEZ4 uWSwZAtmxPwmZMaDsSRIG25jlcjY3ShKadxt9ut/wTF/L2xKU8JjyHEL5WbsQLYd9cN9 Rjaw== X-Gm-Message-State: AOJu0Yzt79vz1L/LvyG4Wa3XO/lp7wEKU9zcKMPhaq3WV3VGhiBRqj9R hjO0gfjtSiymcmMVINBuD4E= X-Google-Smtp-Source: AGHT+IFZKyWTgOnAf2RyU4KYRqc/ajBaxuHyq8HIRTRHEhtfedd4oYxuHtV1own8Sm6lL4ko3S23Pw== X-Received: by 2002:a17:902:e54f:b0:1cc:520a:544 with SMTP id n15-20020a170902e54f00b001cc520a0544mr1322488plf.48.1699933339496; Mon, 13 Nov 2023 19:42:19 -0800 (PST) Received: from barry-desktop.hub ([2407:7000:8942:5500:4df7:6a7e:83c8:9693]) by smtp.gmail.com with ESMTPSA id jd12-20020a170903260c00b001ae0152d280sm4774143plb.193.2023.11.13.19.42.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Nov 2023 19:42:19 -0800 (PST) From: Barry Song <21cnbao@gmail.com> X-Google-Original-From: Barry Song To: akpm@linux-foundation.org, linux-mm@kvack.org, iamjoonsoo.kim@lge.com, audra@redhat.com Cc: linux-kernel@vger.kernel.org, shikemeng@huaweicloud.com, quic_yingangl@quicinc.com, 42.hyeyoo@gmail.com, Barry Song Subject: [PATCH v2] mm/page_owner: record and dump free_pid and free_tgid Date: Tue, 14 Nov 2023 16:42:02 +1300 Message-Id: <20231114034202.73098-1-v-songbaohua@oppo.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: BD096C0009 X-Rspam-User: X-Stat-Signature: psc5hp9agegizpgheed4p9c4cnhjcgc4 X-Rspamd-Server: rspam03 X-HE-Tag: 1699933340-557439 X-HE-Meta: U2FsdGVkX18O/fAPNHtpVnLRR/C8lxfVWVVf6bmKxqcLLBc5QU3/hZa2TXrSXZOIe2Slk4J+axJ79XdtxtQiMWj1gsI4RlcKaX0sLN9n2bcMN0gyGHNAotx0oexEwou8HRll2GevMOcx41JWFTinPDU4uFFDCFYTmuT/s2B4Mxu/4EjycTCw9jpfXNOEnNmqzMYlF8xBm588UaAeao3WrWakohZ4EiucCZ6jq9VyQHvKIYodHFvjC93z4FpiBhtl+RZc3qh1q6BgCf6xeBh7+9BCRkxxZH0hjowdLEmF6qdUak1N1WVZvNSrEsP/HgjkQUVwgE5KmIICo8NwSlU5eB2VwrwC/fdS4cfHafCF0PzKObczkfOBGsFSOtyc0kEDc4Jm0tyAtiGAkuMxJ2CEt6f8cyBFcHZDJh61CAqcztWYd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yFGZXvs1W6S2DN8t7rsr7cMvJAYYtFN8rJICgOZu X-Bogosity: Ham, tests=bogofilter, spamicity=0.000089, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: While investigating some complex memory allocation and free bugs especially in multi-processes and multi-threads cases, from time to time, I feel the free stack isn't sufficient as a page can be freed by processes or threads other than the one allocating it. And other processes and threads which free the page often have the exactly same free stack with the one allocating the page. We can't know who free the page only through the free stack though the current page_owner does tell us the pid and tgid of the one allocating the page. This makes the bug investigation often hard. Thus, This patch adds free pid and tgid in page_owner, so that we can easily figure out if the freeing is crossing processes or threads. Signed-off-by: Barry Song --- -v2: add missed free_pid and free_tgid copy in __folio_copy_owner mm/page_owner.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index 4f13ce7d2452..e7eba7688881 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -32,6 +32,8 @@ struct page_owner { char comm[TASK_COMM_LEN]; pid_t pid; pid_t tgid; + pid_t free_pid; + pid_t free_tgid; }; static bool page_owner_enabled __initdata; @@ -152,6 +154,8 @@ void __reset_page_owner(struct page *page, unsigned short order) page_owner = get_page_owner(page_ext); page_owner->free_handle = handle; page_owner->free_ts_nsec = free_ts_nsec; + page_owner->free_pid = current->pid; + page_owner->free_tgid = current->tgid; page_ext = page_ext_next(page_ext); } page_ext_put(page_ext); @@ -253,6 +257,8 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old) new_page_owner->handle = old_page_owner->handle; new_page_owner->pid = old_page_owner->pid; new_page_owner->tgid = old_page_owner->tgid; + new_page_owner->free_pid = old_page_owner->free_pid; + new_page_owner->free_tgid = old_page_owner->free_tgid; new_page_owner->ts_nsec = old_page_owner->ts_nsec; new_page_owner->free_ts_nsec = old_page_owner->ts_nsec; strcpy(new_page_owner->comm, old_page_owner->comm); @@ -495,7 +501,8 @@ void __dump_page_owner(const struct page *page) if (!handle) { pr_alert("page_owner free stack trace missing\n"); } else { - pr_alert("page last free stack trace:\n"); + pr_alert("page last free pid %d tgid %d stack trace:\n", + page_owner->free_pid, page_owner->free_tgid); stack_depot_print(handle); }