From nobody Wed Dec 31 03:34:18 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B920BC4332F for ; Thu, 9 Nov 2023 03:31:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232384AbjKIDbn (ORCPT ); Wed, 8 Nov 2023 22:31:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230451AbjKIDbe (ORCPT ); Wed, 8 Nov 2023 22:31:34 -0500 Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [IPv6:2001:41d0:203:375::b1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EACAA26AE for ; Wed, 8 Nov 2023 19:31:31 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1699500363; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=j2kk5g3tQ3UUIa7vViud2pPRUP19REPVC10h2UQ9aR4=; b=O+cLqZw78XMy/RUaAaJG1q1LYtKy68Wy9IpJueJN0f04+SHP2aLjD7VfxwdBnhr9sA4SZS 0j10Fv6LvgoIO525gB/h9KeEijjA0eytpeZ6o/jCo5nhzAI+RRiJBSRloSgdFyomMDg3S9 lSyyzdptnsa7UC71g413H54av3HNmfk= From: Jeff Xie To: akpm@linux-foundation.org, iamjoonsoo.kim@lge.com, vbabka@suse.cz, cl@linux.com, penberg@kernel.org, rientjes@google.com, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, willy@infradead.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, chensong_2000@189.cn, xiehuan09@gmail.com, Jeff Xie Subject: [RFC][PATCH 3/4] filemap: implement filemap allocate post callback for page_owner Date: Thu, 9 Nov 2023 11:25:20 +0800 Message-Id: <20231109032521.392217-4-jeff.xie@linux.dev> In-Reply-To: <20231109032521.392217-1-jeff.xie@linux.dev> References: <20231109032521.392217-1-jeff.xie@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Implement the callback function filemap_alloc_post_page_owner for the page_= owner to make the owner of the file page clearer For the pid 98: [root@JeffXie ]# cat /proc/98/maps=20 00400000-00401000 r--p 00000000 fd:00 1954 /test/mm/a.out 00401000-00499000 r-xp 00001000 fd:00 1954 /test/mm/a.out 00499000-004c2000 r--p 00099000 fd:00 1954 /test/mm/a.out 004c2000-004c6000 r--p 000c1000 fd:00 1954 /test/mm/a.out 004c6000-004c9000 rw-p 000c5000 fd:00 1954 /test/mm/a.out 004c9000-004ce000 rw-p 00000000 00:00 0=20 01d97000-01db9000 rw-p 00000000 00:00 0 [heap] 7f1588fc8000-7f1588fc9000 rw-p 00000000 fd:00 1945 /a.txt 7ffda207a000-7ffda209b000 rw-p 00000000 00:00 0 [stack] 7ffda2152000-7ffda2156000 r--p 00000000 00:00 0 [vvar] 7ffda2156000-7ffda2158000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall] added: "FILE_PAGE dev 253:0 ino:1954 index:0xc1 mapcount:1 refcount:2 0x4c2= 000 - 0x4c3000" Page allocated via order 0, mask 0x152c4a(GFP_NOFS|__GFP_HIGHMEM|__GFP_NOWA= RN|__GFP_NORETRY|__GFP_COMP|__GFP_HARDWALL|__GFP_MOVABLE),\ pid 98, tgid 98 (a.out), ts 28441476044 ns FILE_PAGE dev 253:0 ino:1954 index:0xc1 mapcount:1 refcount:2 0x4c2000 - 0x= 4c3000 PFN 0x5be8e type Movable Block 735 type Movable Flags 0x1fffc0000020028(upt= odate|lru|mappedtodisk|node=3D0|zone=3D1|lastcpupid=3D0x3fff) post_alloc_hook+0x77/0xf0 get_page_from_freelist+0x58d/0x14e0 __alloc_pages+0x1b2/0x380 alloc_pages_mpol+0x97/0x1f0 folio_alloc+0x18/0x50 page_cache_ra_unbounded+0x9b/0x1a0 filemap_fault+0x5f7/0xc20 __do_fault+0x31/0xc0 __handle_mm_fault+0x1333/0x1760 handle_mm_fault+0xbc/0x2f0 do_user_addr_fault+0x1f8/0x5e0 exc_page_fault+0x73/0x170 asm_exc_page_fault+0x26/0x30 Charged to memcg /=20 Signed-off-by: Jeff Xie --- include/linux/pagemap.h | 7 +++++++ mm/filemap.c | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index bcc1ea44b4e8..900aa136c71f 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -1514,4 +1514,11 @@ unsigned int i_blocks_per_page(struct inode *inode, = struct page *page) { return i_blocks_per_folio(inode, page_folio(page)); } +#ifndef CONFIG_PAGE_OWNER +static inline int filemap_alloc_post_page_owner(struct folio *folio, struc= t task_struct *tsk, + void *data, char *kbuf, size_t count) +{ + return 0; +} +#endif #endif /* _LINUX_PAGEMAP_H */ diff --git a/mm/filemap.c b/mm/filemap.c index 9710f43a89ac..0a346443309a 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include "internal.h" @@ -838,6 +839,48 @@ void replace_page_cache_folio(struct folio *old, struc= t folio *new) } EXPORT_SYMBOL_GPL(replace_page_cache_folio); =20 +#ifdef CONFIG_PAGE_OWNER +static int filemap_alloc_post_page_owner(struct folio *folio, struct task_= struct *tsk, + void *data, char *kbuf, size_t count) +{ + int ret; + int mapcount; + dev_t s_dev; + struct inode *inode; + struct vm_area_struct *vma; + struct mm_struct *mm; + unsigned long virtual_start =3D 0x0; + unsigned long virtual_end =3D 0x0; + struct address_space *mapping =3D data; + + mapcount =3D folio_mapcount(folio); + if (mapcount && tsk && tsk->mm) { + mm =3D tsk->mm; + VMA_ITERATOR(vmi, mm, 0); + mmap_read_lock(mm); + for_each_vma(vmi, vma) { + if (page_mapped_in_vma(&folio->page, vma)) { + virtual_start =3D vma_address(&folio->page, vma); + virtual_end =3D virtual_start + folio_nr_pages(folio) * PAGE_SIZE; + break; + } + } + mmap_read_unlock(mm); + } + + inode =3D mapping->host; + if (mapping->host->i_sb) + s_dev =3D mapping->host->i_sb->s_dev; + else + s_dev =3D mapping->host->i_rdev; + ret =3D scnprintf(kbuf, count, "FILE_PAGE dev %d:%d ino:%lu index:0x%lx m= apcount:%d refcount:%d 0x%lx - 0x%lx\n", + MAJOR(s_dev), MINOR(s_dev), inode->i_ino, folio->index, mapcount, folio_= ref_count(folio), + virtual_start, virtual_end); + + return ret; +} +#endif + noinline int __filemap_add_folio(struct address_space *mapping, struct folio *folio, pgoff_t index, gfp_t gfp, void **shadowp) { @@ -915,6 +958,7 @@ noinline int __filemap_add_folio(struct address_space *= mapping, if (xas_error(&xas)) goto error; =20 + set_folio_alloc_post_page_owner(folio, filemap_alloc_post_page_owner, map= ping); trace_mm_filemap_add_to_page_cache(folio); return 0; error: --=20 2.34.1