Skip to contentSkip to conversation

incoming

PATCH In review Andrew Morton 3 participants 37 messages Jun 11, 2020

Subsystems (41)
lore
Share & tools
lore

Andrew Morton

author maintainer
#0 lore raw

- various hotfixes and minor things

- hch's use_mm/unuse_mm clearnups

- new syscall process_madvise(): perform madvise() on a process other than self

25 patches, based on 6f630784cc0d92fb58ea326e2bc01aa056279ecb.

Subsystems affected by this patch series:

mm/hugetlb
scripts
kcov
lib
nilfs
checkpatch
lib
mm/debug
ocfs2
lib
misc
mm/madvise

Subsystem: mm/hugetlb

Dan Carpenter <[email protected]>:
khugepaged: selftests: fix timeout condition in wait_for_scan()

Subsystem: scripts

SeongJae Park <[email protected]>:
scripts/spelling: add a few more typos

Subsystem: kcov

Andrey Konovalov <[email protected]>:
kcov: check kcov_softirq in kcov_remote_stop()

Subsystem: lib

Joe Perches <[email protected]>:
lib/lz4/lz4_decompress.c: document deliberate use of `&'

Subsystem: nilfs

Ryusuke Konishi <[email protected]>:
nilfs2: fix null pointer dereference at nilfs_segctor_do_construct()

Subsystem: checkpatch

Tim Froidcoeur <[email protected]>:
checkpatch: correct check for kernel parameters doc

Subsystem: lib

Alexander Gordeev <[email protected]>:
lib: fix bitmap_parse() on 64-bit big endian archs

Subsystem: mm/debug

"Aneesh Kumar K.V" <[email protected]>:
mm/debug_vm_pgtable: fix kernel crash by checking for THP support

Subsystem: ocfs2

Keyur Patel <[email protected]>:
ocfs2: fix spelling mistake and grammar

Ben Widawsky <[email protected]>:
mm: add comments on pglist_data zones

Subsystem: lib

Wei Yang <[email protected]>:
lib: test get_count_order/long in test_bitops.c

Subsystem: misc

Walter Wu <[email protected]>:
stacktrace: cleanup inconsistent variable type

Christoph Hellwig <[email protected]>:
Patch series "improve use_mm / unuse_mm", v2:
kernel: move use_mm/unuse_mm to kthread.c
kernel: move use_mm/unuse_mm to kthread.c
kernel: better document the use_mm/unuse_mm API contract
kernel: set USER_DS in kthread_use_mm

Subsystem: mm/madvise

Minchan Kim <[email protected]>:
Patch series "introduce memory hinting API for external process", v7:
mm/madvise: pass task and mm to do_madvise
mm/madvise: introduce process_madvise() syscall: an external memory hinting API mm/madvise: check fatal signal pending of target process
pid: move pidfd_get_pid() to pid.c
mm/madvise: support both pid and pidfd for process_madvise

Oleksandr Natalenko <[email protected]>:
mm/madvise: allow KSM hints for remote API

Minchan Kim <[email protected]>:
mm: support vector address ranges for process_madvise
mm: use only pidfd for process_madvise syscall

YueHaibing <[email protected]>:
mm/madvise.c: remove duplicated include

 arch/alpha/kernel/syscalls/syscall.tbl              |    1 
 arch/arm/tools/syscall.tbl                          |    1 
 arch/arm64/include/asm/unistd.h                     |    2 
 arch/arm64/include/asm/unistd32.h                   |    4 
 arch/ia64/kernel/syscalls/syscall.tbl               |    1 
 arch/m68k/kernel/syscalls/syscall.tbl               |    1 
 arch/microblaze/kernel/syscalls/syscall.tbl         |    1 
 arch/mips/kernel/syscalls/syscall_n32.tbl           |    3 
 arch/mips/kernel/syscalls/syscall_n64.tbl           |    1 
 arch/mips/kernel/syscalls/syscall_o32.tbl           |    3 
 arch/parisc/kernel/syscalls/syscall.tbl             |    3 
 arch/powerpc/kernel/syscalls/syscall.tbl            |    3 
 arch/powerpc/platforms/powernv/vas-fault.c          |    4 
 arch/s390/kernel/syscalls/syscall.tbl               |    3 
 arch/sh/kernel/syscalls/syscall.tbl                 |    1 
 arch/sparc/kernel/syscalls/syscall.tbl              |    3 
 arch/x86/entry/syscalls/syscall_32.tbl              |    3 
 arch/x86/entry/syscalls/syscall_64.tbl              |    5 
 arch/xtensa/kernel/syscalls/syscall.tbl             |    1 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h          |    5 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_arcturus.c |    1 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c  |    1 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v7.c   |    2 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v8.c   |    2 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c   |    2 
 drivers/gpu/drm/i915/gvt/kvmgt.c                    |    2 
 drivers/usb/gadget/function/f_fs.c                  |   10 
 drivers/usb/gadget/legacy/inode.c                   |    6 
 drivers/vfio/vfio_iommu_type1.c                     |    6 
 drivers/vhost/vhost.c                               |    8 
 fs/aio.c                                            |    1 
 fs/io-wq.c                                          |   15 -
 fs/io_uring.c                                       |   11 
 fs/nilfs2/segment.c                                 |    2 
 fs/ocfs2/mmap.c                                     |    2 
 include/linux/compat.h                              |   10 
 include/linux/kthread.h                             |    9 
 include/linux/mm.h                                  |    3 
 include/linux/mmu_context.h                         |    5 
 include/linux/mmzone.h                              |   14 
 include/linux/pid.h                                 |    1 
 include/linux/stacktrace.h                          |    2 
 include/linux/syscalls.h                            |   16 -
 include/uapi/asm-generic/unistd.h                   |    7 
 kernel/exit.c                                       |   17 -
 kernel/kcov.c                                       |   26 +
 kernel/kthread.c                                    |   95 +++++-
 kernel/pid.c                                        |   17 +
 kernel/sys_ni.c                                     |    2 
 lib/Kconfig.debug                                   |   10 
 lib/bitmap.c                                        |    9 
 lib/lz4/lz4_decompress.c                            |    3 
 lib/test_bitops.c                                   |   53 +++
 mm/Makefile                                         |    2 
 mm/debug_vm_pgtable.c                               |    6 
 mm/madvise.c                                        |  295 ++++++++++++++------
 mm/mmu_context.c                                    |   64 ----
 mm/oom_kill.c                                       |    6 
 mm/vmacache.c                                       |    4 
 scripts/checkpatch.pl                               |    4 
 scripts/spelling.txt                                |    9 
 tools/testing/selftests/vm/khugepaged.c             |    2 
 62 files changed, 526 insertions(+), 285 deletions(-)
author maintainer
#1 lore raw

PATCH 1/25 khugepaged: selftests: fix timeout condition in wait_for_scan()

From: Dan Carpenter <[email protected]>
Subject: khugepaged: selftests: fix timeout condition in wait_for_scan()

The loop exits with "timeout" set to -1 and not to 0 so the test needs to be fixed.

Fixes e7b592f6caca ("khugepaged: add self test")
Cc Shuah Khan <[email protected]>
Cc Stephen Rothwell <[email protected]>
Cc Zi Yan <[email protected]>
Cc William Kucharski <[email protected]>
Cc Yang Shi <[email protected]>
Cc John Hubbard <[email protected]>

---

 tools/testing/selftests/vm/khugepaged.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/vm/khugepaged.c renamed from tools/testing/selftests/vm/khugepaged.c~khugepaged-selftests-fix-timeout-condition-in-wait_for_scan +1 -1
@@ -502,7 +502,7 @@ static bool wait_for_scan(const char *ms
 
 	madvise(p, hpage_pmd_size, MADV_NOHUGEPAGE);
 
-	return !timeout;
+	return timeout == -1;
 }
 
 static void alloc_at_fault(void)

_

author maintainer
#2 lore raw

PATCH 2/25 scripts/spelling: add a few more typos

In reply to Andrew Morton

From: SeongJae Park <[email protected]>
Subject: scripts/spelling: add a few more typos

This commit adds typos I found from another work.

---

 scripts/spelling.txt |    9 +++++++++
 1 file changed, 9 insertions(+)
scripts/spelling.txt renamed from scripts/spelling.txt~scripts-spelling-add-a-few-more-typos +9 -0
@@ -59,6 +59,7 @@ actualy||actually
 acumulating||accumulating
 acumulative||accumulative
 acumulator||accumulator
+acutally||actually
 adapater||adapter
 addional||additional
 additionaly||additionally
@@ -249,6 +250,7 @@ calescing||coalescing
 calle||called
 callibration||calibration
 callled||called
+callser||caller
 calucate||calculate
 calulate||calculate
 cancelation||cancellation
@@ -671,6 +673,7 @@ hanlde||handle
 hanled||handled
 happend||happened
 harware||hardware
+havind||having
 heirarchically||hierarchically
 helpfull||helpful
 hexdecimal||hexadecimal
@@ -845,6 +848,7 @@ logile||logfile
 loobpack||loopback
 loosing||losing
 losted||lost
+maangement||management
 machinary||machinery
 maibox||mailbox
 maintainance||maintenance
@@ -905,6 +909,7 @@ modfiy||modify
 modulues||modules
 momery||memory
 memomry||memory
+monitring||monitoring
 monochorome||monochrome
 monochromo||monochrome
 monocrome||monochrome
@@ -1010,6 +1015,7 @@ partiton||partition
 pased||passed
 passin||passing
 pathes||paths
+pattrns||patterns
 pecularities||peculiarities
 peformance||performance
 peforming||performing
@@ -1256,6 +1262,7 @@ shoule||should
 shrinked||shrunk
 siginificantly||significantly
 signabl||signal
+significanly||significantly
 similary||similarly
 similiar||similar
 simlar||similar
@@ -1371,6 +1378,7 @@ thead||thread
 therfore||therefore
 thier||their
 threds||threads
+threee||three
 threshhold||threshold
 thresold||threshold
 throught||through
@@ -1410,6 +1418,7 @@ tyep||type
 udpate||update
 uesd||used
 uknown||unknown
+usccess||success
 usupported||unsupported
 uncommited||uncommitted
 unconditionaly||unconditionally

_

author maintainer
#3 lore raw

PATCH 3/25 kcov: check kcov_softirq in kcov_remote_stop()

In reply to Andrew Morton

From: Andrey Konovalov <[email protected]>
Subject: kcov: check kcov_softirq in kcov_remote_stop()

kcov_remote_stop() should check that the corresponding kcov_remote_start() actually found the specified remote handle and started collecting coverage. This is done by checking the per thread kcov_softirq flag.

A particular failure scenario where this was observed involved a softirq with a remote coverage collection section coming between check_kcov_mode() and the access to t->kcov_area in __sanitizer_cov_trace_pc(). In that softirq kcov_remote_start() bailed out after kcov_remote_find() check, but the matching kcov_remote_stop() didn't check if kcov_remote_start() succeeded, and overwrote per thread kcov parameters with invalid (zero) values.

---

 kernel/kcov.c |   26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
kernel/kcov.c renamed from kernel/kcov.c~kcov-check-kcov_softirq-in-kcov_remote_stop +4 -7
@@ -427,7 +427,8 @@ void kcov_task_exit(struct task_struct *
 	 *        WARN_ON(!kcov->remote && kcov->t != t);
 	 *
 	 * For KCOV_REMOTE_ENABLE devices, the exiting task is either:
-	 * 2. A remote task between kcov_remote_start() and kcov_remote_stop().
+	 *
+	 * 1. A remote task between kcov_remote_start() and kcov_remote_stop().
 	 *    In this case we should print a warning right away, since a task
 	 *    shouldn't be exiting when it's in a kcov coverage collection
 	 *    section. Here t points to the task that is collecting remote
@@ -437,7 +438,7 @@ void kcov_task_exit(struct task_struct *
 	 *        WARN_ON(kcov->remote && kcov->t != t);
 	 *
 	 * 2. The task that created kcov exiting without calling KCOV_DISABLE,
-	 *    and then again we can make sure that t->kcov->t == t:
+	 *    and then again we make sure that t->kcov->t == t:
 	 *        WARN_ON(kcov->remote && kcov->t != t);
 	 *
 	 * By combining all three checks into one we get:
@@ -764,7 +765,7 @@ static const struct file_operations kcov
  * Internally, kcov_remote_start() looks up the kcov device associated with the
  * provided handle, allocates an area for coverage collection, and saves the
  * pointers to kcov and area into the current task_struct to allow coverage to
- * be collected via __sanitizer_cov_trace_pc()
+ * be collected via __sanitizer_cov_trace_pc().
  * In turns kcov_remote_stop() clears those pointers from task_struct to stop
  * collecting coverage and copies all collected coverage into the kcov area.
  */
@@ -972,16 +973,25 @@ void kcov_remote_stop(void)
 		local_irq_restore(flags);
 		return;
 	}
-	kcov = t->kcov;
-	area = t->kcov_area;
-	size = t->kcov_size;
-	sequence = t->kcov_sequence;
author maintainer
#4 lore raw

PATCH 4/25 lib/lz4/lz4_decompress.c: document deliberate use of `&'

In reply to Andrew Morton

From: Joe Perches <[email protected]>
Subject: lib/lz4/lz4_decompress.c: document deliberate use of `&'

This operation was intentional, but tools such as smatch will warn that it might not have been.

---

 lib/lz4/lz4_decompress.c |    3 +++
 1 file changed, 3 insertions(+)
lib/lz4/lz4_decompress.c renamed from lib/lz4/lz4_decompress.c~lib-lz4-lz4_decompressc-document-deliberate-use-of +3 -0
@@ -141,6 +141,9 @@ static FORCE_INLINE int LZ4_decompress_g
 		 * space in the output for those 18 bytes earlier, upon
 		 * entering the shortcut (in other words, there is a
 		 * combined check for both stages).
+		 *
+		 * The & in the likely() below is intentionally not && so that
+		 * some compilers can produce better parallelized runtime code
 		 */
 		if ((endOnInput ? length != RUN_MASK : length <= 8)
 		   /*

_

author maintainer
#5 lore raw

PATCH 5/25 nilfs2: fix null pointer dereference at nilfs_segctor_do_construct()

In reply to Andrew Morton

From: Ryusuke Konishi <[email protected]>
Subject: nilfs2: fix null pointer dereference at nilfs_segctor_do_construct()

After commit c3aab9a0bd91 ("mm/filemap.c: don't initiate writeback if mapping has no dirty pages"), the following null pointer dereference has been reported on nilfs2:

 BUG: kernel NULL pointer dereference, address: 00000000000000a8
 #PF: supervisor read access in kernel mode
 #PF: error_code(0x0000) - not-present page
 PGD 0 P4D 0
 Oops: 0000 [#1] SMP PTI
 ...
 RIP: 0010:percpu_counter_add_batch+0xa/0x60
 ...
 Call Trace:
  __test_set_page_writeback+0x2d3/0x330
  nilfs_segctor_do_construct+0x10d3/0x2110 [nilfs2]
  nilfs_segctor_construct+0x168/0x260 [nilfs2]
  nilfs_segctor_thread+0x127/0x3b0 [nilfs2]
  kthread+0xf8/0x130
  ...

This crash turned out to be caused by set_page_writeback() call for segment summary buffers at nilfs_segctor_prepare_write().

set_page_writeback() can call inc_wb_stat(inode_to_wb(inode), WB_WRITEBACK) where inode_to_wb(inode) is NULL if the inode of underlying block device does not have an associated wb.

This fixes the issue by calling inode_attach_wb() in advance to ensure to associate the bdev inode with its wb.

---

 fs/nilfs2/segment.c |    2 ++
 1 file changed, 2 insertions(+)
fs/nilfs2/segment.c renamed from fs/nilfs2/segment.c~nilfs2-fix-null-pointer-dereference-at-nilfs_segctor_do_construct +2 -0
@@ -2780,6 +2780,8 @@ int nilfs_attach_log_writer(struct super
 	if (!nilfs->ns_writer)
 		return -ENOMEM;
 
+	inode_attach_wb(nilfs->ns_bdev->bd_inode, NULL);
+
 	err = nilfs_segctor_start_thread(nilfs->ns_writer);
 	if (err) {
 		kfree(nilfs->ns_writer);

_

author maintainer
#6 lore raw

PATCH 5/25 nilfs2: fix null pointer dereference at nilfs_segctor_do_construct()

In reply to Andrew Morton

From: Ryusuke Konishi <[email protected]>
Subject: nilfs2: fix null pointer dereference at nilfs_segctor_do_construct()

After commit c3aab9a0bd91 ("mm/filemap.c: don't initiate writeback if mapping has no dirty pages"), the following null pointer dereference has been reported on nilfs2:

 BUG: kernel NULL pointer dereference, address: 00000000000000a8
 #PF: supervisor read access in kernel mode
 #PF: error_code(0x0000) - not-present page
 PGD 0 P4D 0
 Oops: 0000 [#1] SMP PTI
 ...
 RIP: 0010:percpu_counter_add_batch+0xa/0x60
 ...
 Call Trace:
  __test_set_page_writeback+0x2d3/0x330
  nilfs_segctor_do_construct+0x10d3/0x2110 [nilfs2]
  nilfs_segctor_construct+0x168/0x260 [nilfs2]
  nilfs_segctor_thread+0x127/0x3b0 [nilfs2]
  kthread+0xf8/0x130
  ...

This crash turned out to be caused by set_page_writeback() call for segment summary buffers at nilfs_segctor_prepare_write().

set_page_writeback() can call inc_wb_stat(inode_to_wb(inode), WB_WRITEBACK) where inode_to_wb(inode) is NULL if the inode of underlying block device does not have an associated wb.

This fixes the issue by calling inode_attach_wb() in advance to ensure to associate the bdev inode with its wb.

---

 fs/nilfs2/segment.c |    2 ++
 1 file changed, 2 insertions(+)
fs/nilfs2/segment.c renamed from fs/nilfs2/segment.c~nilfs2-fix-null-pointer-dereference-at-nilfs_segctor_do_construct +2 -0
@@ -2780,6 +2780,8 @@ int nilfs_attach_log_writer(struct super
 	if (!nilfs->ns_writer)
 		return -ENOMEM;
 
+	inode_attach_wb(nilfs->ns_bdev->bd_inode, NULL);
+
 	err = nilfs_segctor_start_thread(nilfs->ns_writer);
 	if (err) {
 		kfree(nilfs->ns_writer);

_

author maintainer
#7 lore raw

PATCH 6/25 checkpatch: correct check for kernel parameters doc

In reply to Andrew Morton

From: Tim Froidcoeur <[email protected]>
Subject: checkpatch: correct check for kernel parameters doc

Adding a new kernel parameter with documentation makes checkpatch complain "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst". The list of kernel parameters has moved to a separate txt file, but checkpatch has not been updated for this.

Make checkpatch.pl look for the documentation for new kernel parameters in kernel-parameters.txt instead of kernel-parameters.rst.

Fixes e52347bd66f6 ("Documentation/admin-guide: split the kernel parameter list to a separate file")
Cc Mauro Carvalho Chehab <[email protected]>

---

 scripts/checkpatch.pl |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-correct-check-for-kernel-parameters-doc +2 -2
@@ -2407,7 +2407,7 @@ sub process {
 
 		if ($rawline=~/^\+\+\+\s+(\S+)/) {
 			$setup_docs = 0;
-			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
+			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
 				$setup_docs = 1;
 			}
 			#next;
@@ -6388,7 +6388,7 @@ sub process {
 
 			if (!grep(/$name/, @setup_docs)) {
 				CHK("UNDOCUMENTED_SETUP",
-				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
+				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
 			}
 		}
 

_

author maintainer
#8 lore raw

PATCH 7/25 lib: fix bitmap_parse() on 64-bit big endian archs

In reply to Andrew Morton

From: Alexander Gordeev <[email protected]>
Subject: lib: fix bitmap_parse() on 64-bit big endian archs

Commit 2d6261583be0 ("lib: rework bitmap_parse()") does not take into account order of halfwords on 64-bit big endian architectures. As result (at least) Receive Packet Steering, IRQ affinity masks and runtime kernel test "test_bitmap" get broken on s390.

[[email protected]: convert infinite while loop to a for loop]
Fixes 2d6261583be0 ("lib: rework bitmap_parse()")
Cc Yury Norov <[email protected]>
Cc Amritha Nambiar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Kees Cook <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Miklos Szeredi <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Steffen Klassert <[email protected]>
Cc "Tobin C . Harding" <[email protected]>
Cc Vineet Gupta <[email protected]>
Cc Will Deacon <[email protected]>
Cc Willem de Bruijn <[email protected]>

---

 lib/bitmap.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
lib/bitmap.c renamed from lib/bitmap.c~lib-fix-bitmap_parse-on-64-bit-big-endian-archs +7 -2
@@ -741,8 +741,9 @@ int bitmap_parse(const char *start, unsi
 	int chunks = BITS_TO_U32(nmaskbits);
 	u32 *bitmap = (u32 *)maskp;
 	int unset_bit;
+	int chunk;
 
-	while (1) {
+	for (chunk = 0; ; chunk++) {
 		end = bitmap_find_region_reverse(start, end);
 		if (start > end)
 			break;
@@ -750,7 +751,11 @@ int bitmap_parse(const char *start, unsi
 		if (!chunks--)
 			return -EOVERFLOW;
 
-		end = bitmap_get_x32_reverse(start, end, bitmap++);
+#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
+		end = bitmap_get_x32_reverse(start, end, &bitmap[chunk ^ 1]);
+#else
+		end = bitmap_get_x32_reverse(start, end, &bitmap[chunk]);
+#endif
 		if (IS_ERR(end))
 			return PTR_ERR(end);
 	}

_

author maintainer
#9 lore raw

PATCH 8/25 mm/debug_vm_pgtable: fix kernel crash by checking for THP support

In reply to Andrew Morton

From: "Aneesh Kumar K.V" <[email protected]>
Subject: mm/debug_vm_pgtable: fix kernel crash by checking for THP support

Architectures can have CONFIG_TRANSPARENT_HUGEPAGE enabled but no THP support enabled based on platforms. For ex: with 4K PAGE_SIZE ppc64 supports THP only with radix translation.

This results in below crash when running with hash translation and 4K PAGE_SIZE.

kernel BUG at arch/powerpc/include/asm/book3s/64/hash-4k.h:140! cpu 0x61: Vector: 700 (Program Check) at [c000000ff948f860]

    pc: c0000000018810f8: debug_vm_pgtable+0x480/0x8b0
    lr: c0000000018810ec: debug_vm_pgtable+0x474/0x8b0

...
[c000000ff948faf0] c000000001880fec debug_vm_pgtable+0x374/0x8b0 (unreliable) [c000000ff948fbf0] c000000000011648 do_one_initcall+0x98/0x4f0 [c000000ff948fcd0] c000000001843928 kernel_init_freeable+0x330/0x3fc [c000000ff948fdb0] c0000000000122ac kernel_init+0x24/0x148 [c000000ff948fe20] c00000000000cc44 ret_from_kernel_thread+0x5c/0x78

Check for THP support correctly

Fixes 399145f9eb6c ("mm/debug: add tests validating architecture page table helpers")

---

 mm/debug_vm_pgtable.c |    6 ++++++
 1 file changed, 6 insertions(+)
mm/debug_vm_pgtable.c renamed from mm/debug_vm_pgtable.c~mm-debug_vm_pgtable-fix-kernel-crash-by-checking-for-thp-support +6 -0
@@ -60,6 +60,9 @@ static void __init pmd_basic_tests(unsig
 {
 	pmd_t pmd = pfn_pmd(pfn, prot);
 
+	if (!has_transparent_hugepage())
+		return;
+
 	WARN_ON(!pmd_same(pmd, pmd));
 	WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
 	WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
@@ -79,6 +82,9 @@ static void __init pud_basic_tests(unsig
 {
 	pud_t pud = pfn_pud(pfn, prot);
 
+	if (!has_transparent_hugepage())
+		return;
+
 	WARN_ON(!pud_same(pud, pud));
 	WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
 	WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));

_

author maintainer
#10 lore raw

PATCH 9/25 ocfs2: fix spelling mistake and grammar

In reply to Andrew Morton

From: Keyur Patel <[email protected]>
Subject: ocfs2: fix spelling mistake and grammar

./ocfs2/mmap.c:65: bebongs ==> belonging

---

 fs/ocfs2/mmap.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ocfs2/mmap.c renamed from fs/ocfs2/mmap.c~fs-ocfs2-fix-spelling-mistake-and-grammar +1 -1
@@ -62,7 +62,7 @@ static vm_fault_t __ocfs2_page_mkwrite(s
 	last_index = (size - 1) >> PAGE_SHIFT;
 
 	/*
-	 * There are cases that lead to the page no longer bebongs to the
+	 * There are cases that lead to the page no longer belonging to the
 	 * mapping.
 	 * 1) pagecache truncates locally due to memory pressure.
 	 * 2) pagecache truncates when another is taking EX lock against 

_

author maintainer
#11 lore raw

PATCH 10/25 mm: add comments on pglist_data zones

In reply to Andrew Morton

From: Ben Widawsky <[email protected]>
Subject: mm: add comments on pglist_data zones

While making other modifications it was easy to confuse the two struct members node_zones and node_zonelists. For those already familiar with the code, this might seem to be a silly patch, but it's quite helpful to disambiguate the similar-sounding fields

While here, add a small comment on why nr_zones isn't simply MAX_NR_ZONES

---

 include/linux/mmzone.h |   14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
include/linux/mmzone.h renamed from include/linux/mmzone.h~mm-add-comments-on-pglist_data-zones +13 -1
@@ -660,9 +660,21 @@ struct deferred_split {
  * per-zone basis.
  */
 typedef struct pglist_data {
+	/*
+	 * node_zones contains just the zones for THIS node. Not all of the
+	 * zones may be populated, but it is the full list. It is referenced by
+	 * this node's node_zonelists as well as other node's node_zonelists.
+	 */
 	struct zone node_zones[MAX_NR_ZONES];
+
+	/*
+	 * node_zonelists contains references to all zones in all nodes.
+	 * Generally the first zones will be references to this node's
+	 * node_zones.
+	 */
 	struct zonelist node_zonelists[MAX_ZONELISTS];
-	int nr_zones;
+
+	int nr_zones; /* number of populated zones in this node */
 #ifdef CONFIG_FLAT_NODE_MEM_MAP	/* means !SPARSEMEM */
 	struct page *node_mem_map;
 #ifdef CONFIG_PAGE_EXTENSION

_

author maintainer
#12 lore raw

PATCH 11/25 lib: test get_count_order/long in test_bitops.c

In reply to Andrew Morton

From: Wei Yang <[email protected]>
Subject: lib: test get_count_order/long in test_bitops.c

Add some tests for get_count_order/long in test_bitops.c.

[[email protected]: define local `i']
[[email protected]: enhancement, warning fix, cleanup per Geert]
[[email protected]: fix loop bound, per Wei Yang]
Cc Christian Brauner <[email protected]>
Cc Geert Uytterhoeven <[email protected]>

---

 lib/Kconfig.debug |   10 ++++----
 lib/test_bitops.c |   53 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 56 insertions(+), 7 deletions(-)
lib/Kconfig.debug renamed from lib/Kconfig.debug~lib-test-get_count_order-long-in-test_bitopsc +5 -5
@@ -2052,15 +2052,15 @@ config TEST_LKM
 	  If unsure, say N.
 
 config TEST_BITOPS
-	tristate "Test module for compilation of clear_bit/set_bit operations"
+	tristate "Test module for compilation of bitops operations"
 	depends on m
 	help
 	  This builds the "test_bitops" module that is much like the
 	  TEST_LKM module except that it does a basic exercise of the
-	  clear_bit and set_bit macros to make sure there are no compiler
-	  warnings from C=1 sparse checker or -Wextra compilations. It has
-	  no dependencies and doesn't run or load unless explicitly requested
-	  by name.  for example: modprobe test_bitops.
+	  set/clear_bit macros and get_count_order/long to make sure there are
+	  no compiler warnings from C=1 sparse checker or -Wextra
+	  compilations. It has no dependencies and doesn't run or load unless
+	  explicitly requested by name.  for example: modprobe test_bitops.
 
 	  If unsure, say N.
 
lib/test_bitops.c renamed from lib/test_bitops.c~lib-test-get_count_order-long-in-test_bitopsc +51 -2

78 lines not on this page · raw message

_

author maintainer
#13 lore raw

PATCH 12/25 stacktrace: cleanup inconsistent variable type

In reply to Andrew Morton

From: Walter Wu <[email protected]>
Subject: stacktrace: cleanup inconsistent variable type

Modify the variable type of 'skip' member of struct stack_trace. In theory, the 'skip' variable type should be unsigned int. There are two reasons:
- The 'skip' only has two situation, 1)Positive value, 2)Zero
- The 'skip' of struct stack_trace has inconsistent type with struct stack_trace_data, it makes a bit confusion in the relationship between struct stack_trace and stack_trace_data.

---

 include/linux/stacktrace.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/stacktrace.h renamed from include/linux/stacktrace.h~stacktrace-cleanup-inconsistent-variable-type +1 -1
@@ -64,7 +64,7 @@ void arch_stack_walk_user(stack_trace_co
 struct stack_trace {
 	unsigned int nr_entries, max_entries;
 	unsigned long *entries;
-	int skip;	/* input argument: How many entries to skip */
+	unsigned int skip;	/* input argument: How many entries to skip */
 };
 
 extern void save_stack_trace(struct stack_trace *trace);

_

author maintainer
#14 lore raw

PATCH 13/25 kernel: move use_mm/unuse_mm to kthread.c

In reply to Andrew Morton

From: Christoph Hellwig <[email protected]>
Subject: kernel: move use_mm/unuse_mm to kthread.c

Patch series "improve use_mm / unuse_mm", v2.

This series improves the use_mm / unuse_mm interface by better documenting the assumptions, and my taking the set_fs manipulations spread over the callers into the core API.

This patch (of 3):

Use the proper API instead.

These helpers are only for use with kernel threads, and I will tie them more into the kthread infrastructure going forward. Also move the prototypes to kthread.h - mmu_context.h was a little weird to start with as it otherwise contains very low-level MM bits.

---

 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h          |    1 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_arcturus.c |    1 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c  |    1 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v7.c   |    2 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v8.c   |    2 
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c   |    2 
 drivers/gpu/drm/i915/gvt/kvmgt.c                    |    2 
 drivers/usb/gadget/function/f_fs.c                  |    2 
 drivers/usb/gadget/legacy/inode.c                   |    2 
 drivers/vhost/vhost.c                               |    1 
 fs/aio.c                                            |    1 
 fs/io-wq.c                                          |    1 
 fs/io_uring.c                                       |    1 
 include/linux/kthread.h                             |    5 
 include/linux/mmu_context.h                         |    5 
 kernel/kthread.c                                    |   56 ++++++++
 mm/Makefile                                         |    2 
 mm/mmu_context.c                                    |   64 ----------
 18 files changed, 66 insertions(+), 85 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_arcturus.c renamed from drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_arcturus.c~kernel-move-use_mm-unuse_mm-to-kthreadc +0 -1
@@ -22,7 +22,6 @@
 #include <linux/module.h>
 #include <linux/fdtable.h>
 #include <linux/uaccess.h>
-#include <linux/mmu_context.h>
 #include <linux/firmware.h>
 #include "amdgpu.h"
 #include "amdgpu_amdkfd.h"
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c renamed from drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c~kernel-move-use_mm-unuse_mm-to-kthreadc +0 -1
@@ -19,7 +19,6 @@
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  * OTHER DEALINGS IN THE SOFTWARE.
  */
-#include <linux/mmu_context.h>
 #include "amdgpu.h"
 #include "amdgpu_amdkfd.h"
 #include "gc/gc_10_1_0_offset.h"
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v7.c renamed from drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v7.c~kernel-move-use_mm-unuse_mm-to-kthreadc +0 -2
@@ -20,8 +20,6 @@
  * OTHER DEALINGS IN THE SOFTWARE.
  */
 
-#include <linux/mmu_context.h>
-
 #include "amdgpu.h"
 #include "amdgpu_amdkfd.h"
 #include "cikd.h"
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v8.c renamed from drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v8.c~kernel-move-use_mm-unuse_mm-to-kthreadc +0 -2
@@ -20,8 +20,6 @@
  * OTHER DEALINGS IN THE SOFTWARE.
  */
 
-#include <linux/mmu_context.h>
-
 #include "amdgpu.h"
 #include "amdgpu_amdkfd.h"
 #include "gfx_v8_0.h"
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c renamed from drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c~kernel-move-use_mm-unuse_mm-to-kthreadc +0 -2
@@ -19,8 +19,6 @@
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  * OTHER DEALINGS IN THE SOFTWARE.
  */
-#include <linux/mmu_context.h>
-
 #include "amdgpu.h"
 #include "amdgpu_amdkfd.h"
 #include "gc/gc_9_0_offset.h"
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h renamed from drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h~kernel-move-use_mm-unuse_mm-to-kthreadc +1 -0
@@ -27,6 +27,7 @@
 
 #include <linux/types.h>
 #include <linux/mm.h>
+#include <linux/kthread.h>
 #include <linux/workqueue.h>
 #include <kgd_kfd_interface.h>
 #include <drm/ttm/ttm_execbuf_util.h>
drivers/gpu/drm/i915/gvt/kvmgt.c renamed from drivers/gpu/drm/i915/gvt/kvmgt.c~kernel-move-use_mm-unuse_mm-to-kthreadc +1 -1
@@ -31,7 +31,7 @@
 #include <linux/init.h>
 #include <linux/device.h>
 #include <linux/mm.h>
-#include <linux/mmu_context.h>
+#include <linux/kthread.h>
 #include <linux/sched/mm.h>
 #include <linux/types.h>
 #include <linux/list.h>
drivers/usb/gadget/function/f_fs.c renamed from drivers/usb/gadget/function/f_fs.c~kernel-move-use_mm-unuse_mm-to-kthreadc +1 -1
@@ -32,7 +32,7 @@
 #include <linux/usb/functionfs.h>
 
 #include <linux/aio.h>
-#include <linux/mmu_context.h>
+#include <linux/kthread.h>
 #include <linux/poll.h>
 #include <linux/eventfd.h>
 
drivers/usb/gadget/legacy/inode.c renamed from drivers/usb/gadget/legacy/inode.c~kernel-move-use_mm-unuse_mm-to-kthreadc +1 -1
@@ -21,7 +21,7 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/poll.h>
-#include <linux/mmu_context.h>
+#include <linux/kthread.h>
 #include <linux/aio.h>
 #include <linux/uio.h>
 #include <linux/refcount.h>
drivers/vhost/vhost.c renamed from drivers/vhost/vhost.c~kernel-move-use_mm-unuse_mm-to-kthreadc +0 -1
@@ -14,7 +14,6 @@
 #include <linux/vhost.h>
 #include <linux/uio.h>
 #include <linux/mm.h>
-#include <linux/mmu_context.h>
 #include <linux/miscdevice.h>
 #include <linux/mutex.h>
 #include <linux/poll.h>
fs/aio.c renamed from fs/aio.c~kernel-move-use_mm-unuse_mm-to-kthreadc +0 -1
@@ -27,7 +27,6 @@
 #include <linux/file.h>
 #include <linux/mm.h>
 #include <linux/mman.h>
-#include <linux/mmu_context.h>
 #include <linux/percpu.h>
 #include <linux/slab.h>
 #include <linux/timer.h>
fs/io_uring.c renamed from fs/io_uring.c~kernel-move-use_mm-unuse_mm-to-kthreadc +0 -1
@@ -55,7 +55,6 @@
 #include <linux/fdtable.h>
 #include <linux/mm.h>
 #include <linux/mman.h>
-#include <linux/mmu_context.h>
 #include <linux/percpu.h>
 #include <linux/slab.h>
 #include <linux/kthread.h>
fs/io-wq.c renamed from fs/io-wq.c~kernel-move-use_mm-unuse_mm-to-kthreadc +0 -1
@@ -10,7 +10,6 @@
 #include <linux/errno.h>
 #include <linux/sched/signal.h>
 #include <linux/mm.h>
-#include <linux/mmu_context.h>
 #include <linux/sched/mm.h>
 #include <linux/percpu.h>
 #include <linux/slab.h>
include/linux/kthread.h renamed from include/linux/kthread.h~kernel-move-use_mm-unuse_mm-to-kthreadc +5 -0
@@ -5,6 +5,8 @@
 #include <linux/err.h>
 #include <linux/sched.h>
 
+struct mm_struct;
+
 __printf(4, 5)
 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
 					   void *data,
@@ -198,6 +200,9 @@ bool kthread_cancel_delayed_work_sync(st
 
 void kthread_destroy_worker(struct kthread_worker *worker);
 
+void use_mm(struct mm_struct *mm);
+void unuse_mm(struct mm_struct *mm);
+
 struct cgroup_subsys_state;
 
 #ifdef CONFIG_BLK_CGROUP
include/linux/mmu_context.h renamed from include/linux/mmu_context.h~kernel-move-use_mm-unuse_mm-to-kthreadc +0 -5
@@ -4,11 +4,6 @@
 
 #include <asm/mmu_context.h>
 
-struct mm_struct;
-
-void use_mm(struct mm_struct *mm);
-void unuse_mm(struct mm_struct *mm);
-
 /* Architectures that care about IRQ state in switch_mm can override this. */
 #ifndef switch_mm_irqs_off
 # define switch_mm_irqs_off switch_mm
kernel/kthread.c renamed from kernel/kthread.c~kernel-move-use_mm-unuse_mm-to-kthreadc +56 -0

81 lines not on this page · raw message

mm/Makefile renamed from mm/Makefile~kernel-move-use_mm-unuse_mm-to-kthreadc +1 -1
@@ -41,7 +41,7 @@ obj-y			:= filemap.o mempool.o oom_kill.
 			   maccess.o page-writeback.o \
 			   readahead.o swap.o truncate.o vmscan.o shmem.o \
 			   util.o mmzone.o vmstat.o backing-dev.o \
-			   mm_init.o mmu_context.o percpu.o slab_common.o \
+			   mm_init.o percpu.o slab_common.o \
 			   compaction.o vmacache.o \
 			   interval_tree.o list_lru.o workingset.o \
 			   debug.o gup.o $(mmu-y)
mm/mmu_context.c deleted +0 -64

64 lines not on this page · raw message

_

author maintainer
#15 lore raw

PATCH 14/25 kernel: move use_mm/unuse_mm to kthread.c

In reply to Andrew Morton

From: Christoph Hellwig <[email protected]>
Subject: kernel: move use_mm/unuse_mm to kthread.c

cover the newly merged use_mm/unuse_mm caller in vfio

---

 drivers/vfio/vfio_iommu_type1.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/vfio_iommu_type1.c renamed from drivers/vfio/vfio_iommu_type1.c~kernel-move-use_mm-unuse_mm-to-kthreadc-v2 +1 -1
@@ -27,7 +27,7 @@
 #include <linux/iommu.h>
 #include <linux/module.h>
 #include <linux/mm.h>
-#include <linux/mmu_context.h>
+#include <linux/kthread.h>
 #include <linux/rbtree.h>
 #include <linux/sched/signal.h>
 #include <linux/sched/mm.h>

_

author maintainer
#16 lore raw

PATCH 15/25 kernel: better document the use_mm/unuse_mm API contract

In reply to Andrew Morton

From: Christoph Hellwig <[email protected]>
Subject: kernel: better document the use_mm/unuse_mm API contract

Switch the function documentation to kerneldoc comments, and add WARN_ON_ONCE asserts that the calling thread is a kernel thread and does not have ->mm set (or has ->mm set in the case of unuse_mm).

Also give the functions a kthread_ prefix to better document the use case.

---

 arch/powerpc/platforms/powernv/vas-fault.c |    4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h |    4 +-
 drivers/usb/gadget/function/f_fs.c         |    4 +-
 drivers/usb/gadget/legacy/inode.c          |    4 +-
 drivers/vfio/vfio_iommu_type1.c            |    4 +-
 drivers/vhost/vhost.c                      |    4 +-
 fs/io-wq.c                                 |    6 +--
 fs/io_uring.c                              |    4 +-
 include/linux/kthread.h                    |    4 +-
 kernel/kthread.c                           |   33 +++++++++----------
 mm/oom_kill.c                              |    6 +--
 mm/vmacache.c                              |    4 +-
 12 files changed, 40 insertions(+), 41 deletions(-)
arch/powerpc/platforms/powernv/vas-fault.c renamed from arch/powerpc/platforms/powernv/vas-fault.c~kernel-better-document-the-use_mm-unuse_mm-api-contract +2 -2
@@ -127,7 +127,7 @@ static void update_csb(struct vas_window
 		return;
 	}
 
-	use_mm(window->mm);
+	kthread_use_mm(window->mm);
 	rc = copy_to_user(csb_addr, &csb, sizeof(csb));
 	/*
 	 * User space polls on csb.flags (first byte). So add barrier
@@ -139,7 +139,7 @@ static void update_csb(struct vas_window
 		smp_mb();
 		rc = copy_to_user(csb_addr, &csb, sizeof(u8));
 	}
-	unuse_mm(window->mm);
+	kthread_unuse_mm(window->mm);
 	put_task_struct(tsk);
 
 	/* Success */
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h renamed from drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h~kernel-better-document-the-use_mm-unuse_mm-api-contract +2 -2
@@ -197,9 +197,9 @@ uint8_t amdgpu_amdkfd_get_xgmi_hops_coun
 			if ((mmptr) == current->mm) {			\
 				valid = !get_user((dst), (wptr));	\
 			} else if (current->mm == NULL) {		\
-				use_mm(mmptr);				\
+				kthread_use_mm(mmptr);			\
 				valid = !get_user((dst), (wptr));	\
-				unuse_mm(mmptr);			\
+				kthread_unuse_mm(mmptr);		\
 			}						\
 			pagefault_enable();				\
 		}							\
drivers/usb/gadget/function/f_fs.c renamed from drivers/usb/gadget/function/f_fs.c~kernel-better-document-the-use_mm-unuse_mm-api-contract +2 -2
@@ -827,9 +827,9 @@ static void ffs_user_copy_worker(struct
 		mm_segment_t oldfs = get_fs();
 
 		set_fs(USER_DS);
-		use_mm(io_data->mm);
+		kthread_use_mm(io_data->mm);
 		ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
-		unuse_mm(io_data->mm);
+		kthread_unuse_mm(io_data->mm);
 		set_fs(oldfs);
 	}
 
drivers/usb/gadget/legacy/inode.c renamed from drivers/usb/gadget/legacy/inode.c~kernel-better-document-the-use_mm-unuse_mm-api-contract +2 -2
@@ -462,9 +462,9 @@ static void ep_user_copy_worker(struct w
 	struct kiocb *iocb = priv->iocb;
 	size_t ret;
 
-	use_mm(mm);
+	kthread_use_mm(mm);
 	ret = copy_to_iter(priv->buf, priv->actual, &priv->to);
-	unuse_mm(mm);
+	kthread_unuse_mm(mm);
 	if (!ret)
 		ret = -EFAULT;
 
drivers/vfio/vfio_iommu_type1.c renamed from drivers/vfio/vfio_iommu_type1.c~kernel-better-document-the-use_mm-unuse_mm-api-contract +2 -2
@@ -2817,7 +2817,7 @@ static int vfio_iommu_type1_dma_rw_chunk
 		return -EPERM;
 
 	if (kthread)
-		use_mm(mm);
+		kthread_use_mm(mm);
 	else if (current->mm != mm)
 		goto out;
 
@@ -2844,7 +2844,7 @@ static int vfio_iommu_type1_dma_rw_chunk
 		*copied = copy_from_user(data, (void __user *)vaddr,
 					   count) ? 0 : count;
 	if (kthread)
-		unuse_mm(mm);
+		kthread_unuse_mm(mm);
 out:
 	mmput(mm);
 	return *copied ? 0 : -EFAULT;
drivers/vhost/vhost.c renamed from drivers/vhost/vhost.c~kernel-better-document-the-use_mm-unuse_mm-api-contract +2 -2
@@ -332,7 +332,7 @@ static int vhost_worker(void *data)
 	mm_segment_t oldfs = get_fs();
 
 	set_fs(USER_DS);
-	use_mm(dev->mm);
+	kthread_use_mm(dev->mm);
 
 	for (;;) {
 		/* mb paired w/ kthread_stop */
@@ -360,7 +360,7 @@ static int vhost_worker(void *data)
 				schedule();
 		}
 	}
-	unuse_mm(dev->mm);
+	kthread_unuse_mm(dev->mm);
 	set_fs(oldfs);
 	return 0;
 }
fs/io_uring.c renamed from fs/io_uring.c~kernel-better-document-the-use_mm-unuse_mm-api-contract +2 -2
@@ -5866,7 +5866,7 @@ static int io_init_req(struct io_ring_ct
 	if (io_op_defs[req->opcode].needs_mm && !current->mm) {
 		if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
 			return -EFAULT;
-		use_mm(ctx->sqo_mm);
+		kthread_use_mm(ctx->sqo_mm);
 	}
 
 	sqe_flags = READ_ONCE(sqe->flags);
@@ -5980,7 +5980,7 @@ static inline void io_sq_thread_drop_mm(
 	struct mm_struct *mm = current->mm;
 
 	if (mm) {
-		unuse_mm(mm);
+		kthread_unuse_mm(mm);
 		mmput(mm);
 	}
 }
fs/io-wq.c renamed from fs/io-wq.c~kernel-better-document-the-use_mm-unuse_mm-api-contract +3 -3
@@ -170,7 +170,7 @@ static bool __io_worker_unuse(struct io_
 		}
 		__set_current_state(TASK_RUNNING);
 		set_fs(KERNEL_DS);
-		unuse_mm(worker->mm);
+		kthread_unuse_mm(worker->mm);
 		mmput(worker->mm);
 		worker->mm = NULL;
 	}
@@ -417,7 +417,7 @@ static struct io_wq_work *io_get_next_wo
 static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
 {
 	if (worker->mm) {
-		unuse_mm(worker->mm);
+		kthread_unuse_mm(worker->mm);
 		mmput(worker->mm);
 		worker->mm = NULL;
 	}
@@ -426,7 +426,7 @@ static void io_wq_switch_mm(struct io_wo
 		return;
 	}
 	if (mmget_not_zero(work->mm)) {
-		use_mm(work->mm);
+		kthread_use_mm(work->mm);
 		if (!worker->mm)
 			set_fs(USER_DS);
 		worker->mm = work->mm;
include/linux/kthread.h renamed from include/linux/kthread.h~kernel-better-document-the-use_mm-unuse_mm-api-contract +2 -2
@@ -200,8 +200,8 @@ bool kthread_cancel_delayed_work_sync(st
 
 void kthread_destroy_worker(struct kthread_worker *worker);
 
-void use_mm(struct mm_struct *mm);
-void unuse_mm(struct mm_struct *mm);
+void kthread_use_mm(struct mm_struct *mm);
+void kthread_unuse_mm(struct mm_struct *mm);
 
 struct cgroup_subsys_state;
 
kernel/kthread.c renamed from kernel/kthread.c~kernel-better-document-the-use_mm-unuse_mm-api-contract +16 -17
@@ -1208,18 +1208,18 @@ void kthread_destroy_worker(struct kthre
 }
 EXPORT_SYMBOL(kthread_destroy_worker);
 
-/*
- * use_mm
- *	Makes the calling kernel thread take on the specified
- *	mm context.
- *	(Note: this routine is intended to be called only
- *	from a kernel thread context)
+/**
+ * kthread_use_mm - make the calling kthread operate on an address space
+ * @mm: address space to operate on
  */
-void use_mm(struct mm_struct *mm)
+void kthread_use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
 
+	WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
+	WARN_ON_ONCE(tsk->mm);
+
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	if (active_mm != mm) {
@@ -1236,20 +1236,19 @@ void use_mm(struct mm_struct *mm)
 	if (active_mm != mm)
 		mmdrop(active_mm);
 }
-EXPORT_SYMBOL_GPL(use_mm);
+EXPORT_SYMBOL_GPL(kthread_use_mm);
 
-/*
- * unuse_mm
- *	Reverses the effect of use_mm, i.e. releases the
- *	specified mm context which was earlier taken on
- *	by the calling kernel thread
- *	(Note: this routine is intended to be called only
- *	from a kernel thread context)
+/**
+ * kthread_unuse_mm - reverse the effect of kthread_use_mm()
+ * @mm: address space to operate on
  */
-void unuse_mm(struct mm_struct *mm)
+void kthread_unuse_mm(struct mm_struct *mm)
 {
 	struct task_struct *tsk = current;
 
+	WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
+	WARN_ON_ONCE(!tsk->mm);
+
 	task_lock(tsk);
 	sync_mm_rss(mm);
 	tsk->mm = NULL;
@@ -1257,7 +1256,7 @@ void unuse_mm(struct mm_struct *mm)
 	enter_lazy_tlb(mm, tsk);
 	task_unlock(tsk);
 }
-EXPORT_SYMBOL_GPL(unuse_mm);
+EXPORT_SYMBOL_GPL(kthread_unuse_mm);
 
 #ifdef CONFIG_BLK_CGROUP
 /**
mm/oom_kill.c renamed from mm/oom_kill.c~kernel-better-document-the-use_mm-unuse_mm-api-contract +3 -3
@@ -126,7 +126,7 @@ static bool oom_cpuset_eligible(struct t
 
 /*
  * The process p may have detached its own ->mm while exiting or through
- * use_mm(), but one or more of its subthreads may still have a valid
+ * kthread_use_mm(), but one or more of its subthreads may still have a valid
  * pointer.  Return p, or any of its subthreads with a valid ->mm, with
  * task_lock() held.
  */
@@ -919,8 +919,8 @@ static void __oom_kill_process(struct ta
 			continue;
 		}
 		/*
-		 * No use_mm() user needs to read from the userspace so we are
-		 * ok to reap it.
+		 * No kthead_use_mm() user needs to read from the userspace so
+		 * we are ok to reap it.
 		 */
 		if (unlikely(p->flags & PF_KTHREAD))
 			continue;
mm/vmacache.c renamed from mm/vmacache.c~kernel-better-document-the-use_mm-unuse_mm-api-contract +2 -2
@@ -24,8 +24,8 @@
  * task's vmacache pertains to a different mm (ie, its own).  There is
  * nothing we can do here.
  *
- * Also handle the case where a kernel thread has adopted this mm via use_mm().
- * That kernel thread's vmacache is not applicable to this mm.
+ * Also handle the case where a kernel thread has adopted this mm via
+ * kthread_use_mm(). That kernel thread's vmacache is not applicable to this mm.
  */
 static inline bool vmacache_valid_mm(struct mm_struct *mm)
 {

_

author maintainer
#17 lore raw

PATCH 16/25 kernel: set USER_DS in kthread_use_mm

In reply to Andrew Morton

From: Christoph Hellwig <[email protected]>
Subject: kernel: set USER_DS in kthread_use_mm

Some architectures like arm64 and s390 require USER_DS to be set for kernel threads to access user address space, which is the whole purpose of kthread_use_mm, but other like x86 don't. That has lead to a huge mess where some callers are fixed up once they are tested on said architectures, while others linger around and yet other like io_uring try to do "clever" optimizations for what usually is just a trivial asignment to a member in the thread_struct for most architectures.

Make kthread_use_mm set USER_DS, and kthread_unuse_mm restore to the previous value instead.

---

 drivers/usb/gadget/function/f_fs.c |    4 ----
 drivers/vhost/vhost.c              |    3 ---
 fs/io-wq.c                         |    8 ++------
 fs/io_uring.c                      |    4 ----
 kernel/kthread.c                   |    6 ++++++
 5 files changed, 8 insertions(+), 17 deletions(-)
drivers/usb/gadget/function/f_fs.c renamed from drivers/usb/gadget/function/f_fs.c~kernel-set-user_ds-in-kthread_use_mm +0 -4
@@ -824,13 +824,9 @@ static void ffs_user_copy_worker(struct
 	bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
 
 	if (io_data->read && ret > 0) {
-		mm_segment_t oldfs = get_fs();
-
-		set_fs(USER_DS);
 		kthread_use_mm(io_data->mm);
 		ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
 		kthread_unuse_mm(io_data->mm);
-		set_fs(oldfs);
 	}
 
 	io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
drivers/vhost/vhost.c renamed from drivers/vhost/vhost.c~kernel-set-user_ds-in-kthread_use_mm +0 -3
@@ -329,9 +329,7 @@ static int vhost_worker(void *data)
 	struct vhost_dev *dev = data;
 	struct vhost_work *work, *work_next;
 	struct llist_node *node;
-	mm_segment_t oldfs = get_fs();
 
-	set_fs(USER_DS);
 	kthread_use_mm(dev->mm);
 
 	for (;;) {
@@ -361,7 +359,6 @@ static int vhost_worker(void *data)
 		}
 	}
 	kthread_unuse_mm(dev->mm);
-	set_fs(oldfs);
 	return 0;
 }
 
fs/io_uring.c renamed from fs/io_uring.c~kernel-set-user_ds-in-kthread_use_mm +0 -4
@@ -5989,15 +5989,12 @@ static int io_sq_thread(void *data)
 {
 	struct io_ring_ctx *ctx = data;
 	const struct cred *old_cred;
-	mm_segment_t old_fs;
 	DEFINE_WAIT(wait);
 	unsigned long timeout;
 	int ret = 0;
 
 	complete(&ctx->sq_thread_comp);
 
-	old_fs = get_fs();
-	set_fs(USER_DS);
 	old_cred = override_creds(ctx->creds);
 
 	timeout = jiffies + ctx->sq_thread_idle;
@@ -6102,7 +6099,6 @@ static int io_sq_thread(void *data)
 	if (current->task_works)
 		task_work_run();
 
-	set_fs(old_fs);
 	io_sq_thread_drop_mm(ctx);
 	revert_creds(old_cred);
 
fs/io-wq.c renamed from fs/io-wq.c~kernel-set-user_ds-in-kthread_use_mm +2 -6
@@ -169,7 +169,6 @@ static bool __io_worker_unuse(struct io_
 			dropped_lock = true;
 		}
 		__set_current_state(TASK_RUNNING);
-		set_fs(KERNEL_DS);
 		kthread_unuse_mm(worker->mm);
 		mmput(worker->mm);
 		worker->mm = NULL;
@@ -421,14 +420,11 @@ static void io_wq_switch_mm(struct io_wo
 		mmput(worker->mm);
 		worker->mm = NULL;
 	}
-	if (!work->mm) {
-		set_fs(KERNEL_DS);
+	if (!work->mm)
 		return;
-	}
+
 	if (mmget_not_zero(work->mm)) {
 		kthread_use_mm(work->mm);
-		if (!worker->mm)
-			set_fs(USER_DS);
 		worker->mm = work->mm;
 		/* hang on to this mm */
 		work->mm = NULL;
kernel/kthread.c renamed from kernel/kthread.c~kernel-set-user_ds-in-kthread_use_mm +6 -0
@@ -52,6 +52,7 @@ struct kthread {
 	unsigned long flags;
 	unsigned int cpu;
 	void *data;
+	mm_segment_t oldfs;
 	struct completion parked;
 	struct completion exited;
 #ifdef CONFIG_BLK_CGROUP
@@ -1235,6 +1236,9 @@ void kthread_use_mm(struct mm_struct *mm
 
 	if (active_mm != mm)
 		mmdrop(active_mm);
+
+	to_kthread(tsk)->oldfs = get_fs();
+	set_fs(USER_DS);
 }
 EXPORT_SYMBOL_GPL(kthread_use_mm);
 
@@ -1249,6 +1253,8 @@ void kthread_unuse_mm(struct mm_struct *
 	WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
 	WARN_ON_ONCE(!tsk->mm);
 
+	set_fs(to_kthread(tsk)->oldfs);
+
 	task_lock(tsk);
 	sync_mm_rss(mm);
 	tsk->mm = NULL;

_

author maintainer
#18 lore raw

PATCH 17/25 mm/madvise: pass task and mm to do_madvise

In reply to Andrew Morton

From: Minchan Kim <[email protected]>
Subject: mm/madvise: pass task and mm to do_madvise

Patch series "introduce memory hinting API for external process", v7.

Now, we have MADV_PAGEOUT and MADV_COLD as madvise hinting API. With that, application could give hints to kernel what memory range are preferred to be reclaimed. However, in some platform(e.g., Android), the information required to make the hinting decision is not known to the app. Instead, it is known to a centralized userspace daemon(e.g., ActivityManagerService), and that daemon must be able to initiate reclaim on its own without any app involvement.

To solve the concern, this patch introduces new syscall - process_madvise(2). Bascially, it's same with madvise(2) syscall but it has some differences.

1. It needs pidfd of target process to provide the hint

2. It supports only MADV_{COLD|PAGEOUT|MERGEABLE|UNMEREABLE} at this moment. Other hints in madvise will be opened when there are explicit requests from community to prevent unexpected bugs we couldn't support.

3. Only privileged processes can do something for other process's address space.

For more detail of the new API, please see "mm: introduce external memory hinting API" description in this patchset.

This patch (of 7):

In upcoming patches, do_madvise will be called from external process context so we shouldn't asssume "current" is always hinted process's task_struct.

Furthermore, we must not access mm_struct via task->mm, but obtain it via access_mm() once (in the following patch) and only use that pointer [1], so pass it to do_madvise() as well. Note the vma->vm_mm pointers are safe, so we can use them further down the call stack.

And let's pass *current* and current->mm as arguments of do_madvise so it shouldn't change existing behavior but prepare next patch to make review easy.

Note: io_madvise passes NULL as target_task argument of do_madvise because it couldn't know who is target.

[1] http://lore.kernel.org/r/CAG48ez27=pwm5m_N_988xT1huO7g7h6arTQL44zev6TD-h-7Tg@mail.gmail.com

[[email protected]: changelog tweak]
[[email protected]: use current->mm for io_uring]
[[email protected]: fix it for upstream changes]
[[email protected]: add missing includes]
Cc Jens Axboe <[email protected]>
Cc Jann Horn <[email protected]>
Cc Tim Murray <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc John Dias <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Alexander Duyck <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Kirill Tkhai <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Christian Brauner <[email protected]>

---

 fs/io_uring.c      |    2 +-
 include/linux/mm.h |    3 ++-
 mm/madvise.c       |   40 +++++++++++++++++++++++-----------------
 3 files changed, 26 insertions(+), 19 deletions(-)
fs/io_uring.c renamed from fs/io_uring.c~mm-pass-task-and-mm-to-do_madvise +1 -1
@@ -3321,7 +3321,7 @@ static int io_madvise(struct io_kiocb *r
 	if (force_nonblock)
 		return -EAGAIN;
 
-	ret = do_madvise(ma->addr, ma->len, ma->advice);
+	ret = do_madvise(NULL, current->mm, ma->addr, ma->len, ma->advice);
 	if (ret < 0)
 		req_set_fail_links(req);
 	io_cqring_add_event(req, ret);
include/linux/mm.h renamed from include/linux/mm.h~mm-pass-task-and-mm-to-do_madvise +2 -1
@@ -2585,7 +2585,8 @@ extern int __do_munmap(struct mm_struct
 		       struct list_head *uf, bool downgrade);
 extern int do_munmap(struct mm_struct *, unsigned long, size_t,
 		     struct list_head *uf);
-extern int do_madvise(unsigned long start, size_t len_in, int behavior);
+extern int do_madvise(struct task_struct *target_task, struct mm_struct *mm,
+		unsigned long start, size_t len_in, int behavior);
 
 static inline unsigned long
 do_mmap_pgoff(struct file *file, unsigned long addr,
mm/madvise.c renamed from mm/madvise.c~mm-pass-task-and-mm-to-do_madvise +23 -17
@@ -22,12 +22,14 @@
 #include <linux/file.h>
 #include <linux/blkdev.h>
 #include <linux/backing-dev.h>
+#include <linux/compat.h>
 #include <linux/pagewalk.h>
 #include <linux/swap.h>
 #include <linux/swapops.h>
 #include <linux/shmem_fs.h>
 #include <linux/mmu_notifier.h>
 #include <linux/sched/mm.h>
+#include <linux/uio.h>
 
 #include <asm/tlb.h>
 
@@ -255,6 +257,7 @@ static long madvise_willneed(struct vm_a
 			     struct vm_area_struct **prev,
 			     unsigned long start, unsigned long end)
 {
+	struct mm_struct *mm = vma->vm_mm;
 	struct file *file = vma->vm_file;
 	loff_t offset;
 
@@ -289,12 +292,12 @@ static long madvise_willneed(struct vm_a
 	 */
 	*prev = NULL;	/* tell sys_madvise we drop mmap_lock */
 	get_file(file);
-	mmap_read_unlock(current->mm);
+	mmap_read_unlock(mm);
 	offset = (loff_t)(start - vma->vm_start)
 			+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
 	vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
 	fput(file);
-	mmap_read_lock(current->mm);
+	mmap_read_lock(mm);
 	return 0;
 }
 
@@ -683,7 +686,6 @@ out:
 	if (nr_swap) {
 		if (current->mm == mm)
 			sync_mm_rss(mm);
-
 		add_mm_counter(mm, MM_SWAPENTS, nr_swap);
 	}
 	arch_leave_lazy_mmu_mode();
@@ -763,6 +765,8 @@ static long madvise_dontneed_free(struct
 				  unsigned long start, unsigned long end,
 				  int behavior)
 {
+	struct mm_struct *mm = vma->vm_mm;
+
 	*prev = vma;
 	if (!can_madv_lru_vma(vma))
 		return -EINVAL;
@@ -770,8 +774,8 @@ static long madvise_dontneed_free(struct
 	if (!userfaultfd_remove(vma, start, end)) {
 		*prev = NULL; /* mmap_lock has been dropped, prev is stale */
 
-		mmap_read_lock(current->mm);
-		vma = find_vma(current->mm, start);
+		mmap_read_lock(mm);
+		vma = find_vma(mm, start);
 		if (!vma)
 			return -ENOMEM;
 		if (start < vma->vm_start) {
@@ -825,6 +829,7 @@ static long madvise_remove(struct vm_are
 	loff_t offset;
 	int error;
 	struct file *f;
+	struct mm_struct *mm = vma->vm_mm;
 
 	*prev = NULL;	/* tell sys_madvise we drop mmap_lock */
 
@@ -852,13 +857,13 @@ static long madvise_remove(struct vm_are
 	get_file(f);
 	if (userfaultfd_remove(vma, start, end)) {
 		/* mmap_lock was not released by userfaultfd_remove() */
-		mmap_read_unlock(current->mm);
+		mmap_read_unlock(mm);
 	}
 	error = vfs_fallocate(f,
 				FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
 				offset, end - start);
 	fput(f);
-	mmap_read_lock(current->mm);
+	mmap_read_lock(mm);
 	return error;
 }
 
@@ -1051,7 +1056,8 @@ madvise_behavior_valid(int behavior)
  *  -EBADF  - map exists, but area maps something that isn't a file.
  *  -EAGAIN - a kernel resource was temporarily unavailable.
  */
-int do_madvise(unsigned long start, size_t len_in, int behavior)
+int do_madvise(struct task_struct *target_task, struct mm_struct *mm,
+		unsigned long start, size_t len_in, int behavior)
 {
 	unsigned long end, tmp;
 	struct vm_area_struct *vma, *prev;
@@ -1089,7 +1095,7 @@ int do_madvise(unsigned long start, size
 
 	write = madvise_need_mmap_write(behavior);
 	if (write) {
-		if (mmap_write_lock_killable(current->mm))
+		if (mmap_write_lock_killable(mm))
 			return -EINTR;
 
 		/*
@@ -1104,12 +1110,12 @@ int do_madvise(unsigned long start, size
 		 * but for now we have the mmget_still_valid()
 		 * model.
 		 */
-		if (!mmget_still_valid(current->mm)) {
-			mmap_write_unlock(current->mm);
+		if (!mmget_still_valid(mm)) {
+			mmap_write_unlock(mm);
 			return -EINTR;
 		}
 	} else {
-		mmap_read_lock(current->mm);
+		mmap_read_lock(mm);
 	}
 
 	/*
@@ -1117,7 +1123,7 @@ int do_madvise(unsigned long start, size
 	 * ranges, just ignore them, but return -ENOMEM at the end.
 	 * - different from the way of handling in mlock etc.
 	 */
-	vma = find_vma_prev(current->mm, start, &prev);
+	vma = find_vma_prev(mm, start, &prev);
 	if (vma && start > vma->vm_start)
 		prev = vma;
 
@@ -1154,19 +1160,19 @@ int do_madvise(unsigned long start, size
 		if (prev)
 			vma = prev->vm_next;
 		else	/* madvise_remove dropped mmap_lock */
-			vma = find_vma(current->mm, start);
+			vma = find_vma(mm, start);
 	}
 out:
 	blk_finish_plug(&plug);
 	if (write)
-		mmap_write_unlock(current->mm);
+		mmap_write_unlock(mm);
 	else
-		mmap_read_unlock(current->mm);
+		mmap_read_unlock(mm);
 
 	return error;
 }
 
 SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
 {
-	return do_madvise(start, len_in, behavior);
+	return do_madvise(current, current->mm, start, len_in, behavior);
 }

_

author maintainer
#19 lore raw

PATCH 18/25 mm/madvise: introduce process_madvise() syscall: an external memory hinting API

In reply to Andrew Morton

From: Minchan Kim <[email protected]>
Subject: mm/madvise: introduce process_madvise() syscall: an external memory hinting API

There is usecase that System Management Software(SMS) want to give a memory hint like MADV_[COLD|PAGEEOUT] to other processes and in the case of Android, it is the ActivityManagerService.

The information required to make the reclaim decision is not known to the app. Instead, it is known to the centralized userspace daemon(ActivityManagerService), and that daemon must be able to initiate reclaim on its own without any app involvement.

To solve the issue, this patch introduces a new syscall process_madvise(2). It uses pidfd of an external process to give the hint.

int process_madvise(int pidfd, void *addr, size_t length, int advice,

			unsigned long flags);

Since it could affect other process's address range, only privileged process(CAP_SYS_PTRACE) or something else(e.g., being the same UID) gives it the right to ptrace the process could use it successfully. The flag argument is reserved for future use if we need to extend the API.

I think supporting all hints madvise has/will supported/support to process_madvise is rather risky. Because we are not sure all hints make sense from external process and implementation for the hint may rely on the caller being in the current context so it could be error-prone. Thus, I just limited hints as MADV_[COLD|PAGEOUT] in this patch.

If someone want to add other hints, we could hear hear the usecase and review it for each hint. It's safer for maintenance rather than introducing a buggy syscall but hard to fix it later.

Q.1 - Why does any external entity have better knowledge?

Quote from Sandeep

"For Android, every application (including the special SystemServer) are forked from Zygote. The reason of course is to share as many libraries and classes between the two as possible to benefit from the preloading during boot.

After applications start, (almost) all of the APIs end up calling into this SystemServer process over IPC (binder) and back to the application.

In a fully running system, the SystemServer monitors every single process periodically to calculate their PSS / RSS and also decides which process is "important" to the user for interactivity.

So, because of how these processes start _and_ the fact that the SystemServer is looping to monitor each process, it does tend to *know* which address range of the application is not used / useful.

Besides, we can never rely on applications to clean things up themselves. We've had the "hey app1, the system is low on memory, please trim your memory usage down" notifications for a long time[1]. They rely on applications honoring the broadcasts and very few do.

So, if we want to avoid the inevitable killing of the application and restarting it, some way to be able to tell the OS about unimportant memory in these applications will be useful.

- ssp

Q.2 - How to guarantee the race(i.e., object validation) between when giving a hint from an external process and get the hint from the target process?

process_madvise operates on the target process's address space as it exists at the instant that process_madvise is called. If the space target process can run between the time the process_madvise process inspects the target process address space and the time that process_madvise is actually called, process_madvise may operate on memory regions that the calling process does not expect. It's the responsibility of the process calling process_madvise to close this race condition. For example, the calling process can suspend the target process with ptrace, SIGSTOP, or the freezer cgroup so that it doesn't have an opportunity to change its own address space before process_madvise is called. Another option is to operate on memory regions that the caller knows a priori will be unchanged in the target process. Yet another option is to accept the race for certain process_madvise calls after reasoning that mistargeting will do no harm. The suggested API itself does not provide synchronization. It also apply other APIs like move_pages, process_vm_write.

The race isn't really a problem though. Why is it so wrong to require that callers do their own synchronization in some manner? Nobody objects to write(2) merely because it's possible for two processes to open the same file and clobber each other's writes --- instead, we tell people to use flock or something. Think about mmap. It never guarantees newly allocated address space is still valid when the user tries to access it because other threads could unmap the memory right before. That's where we need synchronization by using other API or design from userside. It shouldn't be part of API itself. If someone needs more fine-grained synchronization rather than process level, there were two ideas suggested - cookie[2] and anon-fd[3]. Both are applicable via using last reserved argument of the API but I don't think it's necessary right now since we have already ways to prevent the race so don't want to add additional complexity with more fine-grained optimization model.

To make the API extend, it reserved an unsigned long as last argument so we could support it in future if someone really needs it.

Q.3 - Why doesn't ptrace work?

Injecting an madvise in the target process using ptrace would not work for us because such injected madvise would have to be executed by the target process, which means that process would have to be runnable and that creates the risk of the abovementioned race and hinting a wrong VMA. Furthermore, we want to act the hint in caller's context, not the callee's, because the callee is usually limited in cpuset/cgroups or even freezed state so they can't act by themselves quick enough, which causes more thrashing/kill. It doesn't work if the target process are ptraced(e.g., strace, debugger, minidump) because a process can have at most one ptracer.

[1] https://developer.android.com/topic/performance/memory"

[2] process_getinfo for getting the cookie which is updated whenever vma of process address layout are changed - Daniel Colascione -
https://lore.kernel.org/lkml/[email protected]/T/#m7694416fd179b2066a2c62b5b139b14e3894e224

[3] anonymous fd which is used for the object(i.e., address range) validation - Michal Hocko -
https://lore.kernel.org/lkml/[email protected]/

[[email protected]: fix process_madvise build break for arm64]
[[email protected]: fix build error for mips of process_madvise]
[[email protected]: fix patch ordering issue]
Cc Alexander Duyck <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc John Dias <[email protected]>
Cc Kirill Tkhai <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc SeongJae Park <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Tim Murray <[email protected]>
Cc Christian Brauner <[email protected]>

---

 arch/alpha/kernel/syscalls/syscall.tbl      |    1 
 arch/arm/tools/syscall.tbl                  |    1 
 arch/arm64/include/asm/unistd.h             |    2 
 arch/arm64/include/asm/unistd32.h           |    2 
 arch/ia64/kernel/syscalls/syscall.tbl       |    1 
 arch/m68k/kernel/syscalls/syscall.tbl       |    1 
 arch/microblaze/kernel/syscalls/syscall.tbl |    1 
 arch/mips/kernel/syscalls/syscall_n32.tbl   |    1 
 arch/mips/kernel/syscalls/syscall_n64.tbl   |    1 
 arch/mips/kernel/syscalls/syscall_o32.tbl   |    1 
 arch/parisc/kernel/syscalls/syscall.tbl     |    1 
 arch/powerpc/kernel/syscalls/syscall.tbl    |    1 
 arch/s390/kernel/syscalls/syscall.tbl       |    1 
 arch/sh/kernel/syscalls/syscall.tbl         |    1 
 arch/sparc/kernel/syscalls/syscall.tbl      |    1 
 arch/x86/entry/syscalls/syscall_32.tbl      |    1 
 arch/x86/entry/syscalls/syscall_64.tbl      |    1 
 arch/xtensa/kernel/syscalls/syscall.tbl     |    1 
 include/linux/syscalls.h                    |    2 
 include/uapi/asm-generic/unistd.h           |    4 -
 kernel/sys_ni.c                             |    1 
 mm/madvise.c                                |   64 ++++++++++++++++++
 22 files changed, 89 insertions(+), 2 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl renamed from arch/alpha/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -478,3 +478,4 @@
 547	common	openat2				sys_openat2
 548	common	pidfd_getfd			sys_pidfd_getfd
 549	common	faccessat2			sys_faccessat2
+550	common	process_madvise			sys_process_madvise
arch/arm64/include/asm/unistd32.h renamed from arch/arm64/include/asm/unistd32.h~mm-introduce-external-memory-hinting-api +2 -0
@@ -885,6 +885,8 @@ __SYSCALL(__NR_openat2, sys_openat2)
 __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd)
 #define __NR_faccessat2 439
 __SYSCALL(__NR_faccessat2, sys_faccessat2)
+#define __NR_process_madvise 440
+__SYSCALL(__NR_process_madvise, sys_process_madvise)
 
 /*
  * Please add new compat syscalls above this comment and update
arch/arm64/include/asm/unistd.h renamed from arch/arm64/include/asm/unistd.h~mm-introduce-external-memory-hinting-api +1 -1
@@ -38,7 +38,7 @@
 #define __ARM_NR_compat_set_tls		(__ARM_NR_COMPAT_BASE + 5)
 #define __ARM_NR_COMPAT_END		(__ARM_NR_COMPAT_BASE + 0x800)
 
-#define __NR_compat_syscalls		440
+#define __NR_compat_syscalls		441
 #endif
 
 #define __ARCH_WANT_SYS_CLONE
arch/arm/tools/syscall.tbl renamed from arch/arm/tools/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -452,3 +452,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/ia64/kernel/syscalls/syscall.tbl renamed from arch/ia64/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -359,3 +359,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/m68k/kernel/syscalls/syscall.tbl renamed from arch/m68k/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -438,3 +438,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/microblaze/kernel/syscalls/syscall.tbl renamed from arch/microblaze/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -444,3 +444,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/mips/kernel/syscalls/syscall_n32.tbl renamed from arch/mips/kernel/syscalls/syscall_n32.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -377,3 +377,4 @@
 437	n32	openat2				sys_openat2
 438	n32	pidfd_getfd			sys_pidfd_getfd
 439	n32	faccessat2			sys_faccessat2
+440	n32	process_madvise			sys_process_madvise
arch/mips/kernel/syscalls/syscall_n64.tbl renamed from arch/mips/kernel/syscalls/syscall_n64.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -353,3 +353,4 @@
 437	n64	openat2				sys_openat2
 438	n64	pidfd_getfd			sys_pidfd_getfd
 439	n64	faccessat2			sys_faccessat2
+440	n64	process_madvise			sys_process_madvise
arch/mips/kernel/syscalls/syscall_o32.tbl renamed from arch/mips/kernel/syscalls/syscall_o32.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -426,3 +426,4 @@
 437	o32	openat2				sys_openat2
 438	o32	pidfd_getfd			sys_pidfd_getfd
 439	o32	faccessat2			sys_faccessat2
+440	o32	process_madvise			sys_process_madvise
arch/parisc/kernel/syscalls/syscall.tbl renamed from arch/parisc/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -436,3 +436,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/powerpc/kernel/syscalls/syscall.tbl renamed from arch/powerpc/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -528,3 +528,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/s390/kernel/syscalls/syscall.tbl renamed from arch/s390/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -441,3 +441,4 @@
 437  common	openat2			sys_openat2			sys_openat2
 438  common	pidfd_getfd		sys_pidfd_getfd			sys_pidfd_getfd
 439  common	faccessat2		sys_faccessat2			sys_faccessat2
+440  common	process_madvise		sys_process_madvise		sys_process_madvise
arch/sh/kernel/syscalls/syscall.tbl renamed from arch/sh/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -441,3 +441,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/sparc/kernel/syscalls/syscall.tbl renamed from arch/sparc/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -484,3 +484,4 @@
 437	common	openat2			sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/x86/entry/syscalls/syscall_32.tbl renamed from arch/x86/entry/syscalls/syscall_32.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -443,3 +443,4 @@
 437	i386	openat2			sys_openat2
 438	i386	pidfd_getfd		sys_pidfd_getfd
 439	i386	faccessat2		sys_faccessat2
+440	i386	process_madvise		sys_process_madvise
arch/x86/entry/syscalls/syscall_64.tbl renamed from arch/x86/entry/syscalls/syscall_64.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -360,6 +360,7 @@
 437	common	openat2			sys_openat2
 438	common	pidfd_getfd		sys_pidfd_getfd
 439	common	faccessat2		sys_faccessat2
+440	common	process_madvise		sys_process_madvise
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
arch/xtensa/kernel/syscalls/syscall.tbl renamed from arch/xtensa/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -409,3 +409,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
include/linux/syscalls.h renamed from include/linux/syscalls.h~mm-introduce-external-memory-hinting-api +2 -0
@@ -878,6 +878,8 @@ asmlinkage long sys_munlockall(void);
 asmlinkage long sys_mincore(unsigned long start, size_t len,
 				unsigned char __user * vec);
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
+asmlinkage long sys_process_madvise(int pidfd, unsigned long start,
+			size_t len, int behavior, unsigned long flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
 			unsigned long prot, unsigned long pgoff,
 			unsigned long flags);
include/uapi/asm-generic/unistd.h renamed from include/uapi/asm-generic/unistd.h~mm-introduce-external-memory-hinting-api +3 -1
@@ -857,9 +857,11 @@ __SYSCALL(__NR_openat2, sys_openat2)
 __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd)
 #define __NR_faccessat2 439
 __SYSCALL(__NR_faccessat2, sys_faccessat2)
+#define __NR_process_madvise 440
+__SYSCALL(__NR_process_madvise, sys_process_madvise)
 
 #undef __NR_syscalls
-#define __NR_syscalls 440
+#define __NR_syscalls 441
 
 /*
  * 32 bit systems traditionally used different
kernel/sys_ni.c renamed from kernel/sys_ni.c~mm-introduce-external-memory-hinting-api +1 -0
@@ -280,6 +280,7 @@ COND_SYSCALL(mlockall);
 COND_SYSCALL(munlockall);
 COND_SYSCALL(mincore);
 COND_SYSCALL(madvise);
+COND_SYSCALL(process_madvise);
 COND_SYSCALL(remap_file_pages);
 COND_SYSCALL(mbind);
 COND_SYSCALL_COMPAT(mbind);
mm/madvise.c renamed from mm/madvise.c~mm-introduce-external-memory-hinting-api +64 -0

85 lines not on this page · raw message

_

author maintainer
#20 lore raw

PATCH 19/25 mm/madvise: check fatal signal pending of target process

In reply to Andrew Morton

From: Minchan Kim <[email protected]>
Subject: mm/madvise: check fatal signal pending of target process

Bail out to prevent unnecessary CPU overhead if target process has pending fatal signal during (MADV_COLD|MADV_PAGEOUT) operation.

Cc Alexander Duyck <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc John Dias <[email protected]>
Cc Kirill Tkhai <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc SeongJae Park <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Tim Murray <[email protected]>
Cc Christian Brauner <[email protected]>

---

 mm/madvise.c |   29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
mm/madvise.c renamed from mm/madvise.c~mm-check-fatal-signal-pending-of-target-process +21 -8
@@ -39,6 +39,7 @@
 struct madvise_walk_private {
 	struct mmu_gather *tlb;
 	bool pageout;
+	struct task_struct *target_task;
 };
 
 /*
@@ -319,6 +320,10 @@ static int madvise_cold_or_pageout_pte_r
 	if (fatal_signal_pending(current))
 		return -EINTR;
 
+	if (private->target_task &&
+			fatal_signal_pending(private->target_task))
+		return -EINTR;
+
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 	if (pmd_trans_huge(*pmd)) {
 		pmd_t orig_pmd;
@@ -480,12 +485,14 @@ static const struct mm_walk_ops cold_wal
 };
 
 static void madvise_cold_page_range(struct mmu_gather *tlb,
+			     struct task_struct *task,
 			     struct vm_area_struct *vma,
 			     unsigned long addr, unsigned long end)
 {
 	struct madvise_walk_private walk_private = {
 		.pageout = false,
 		.tlb = tlb,
+		.target_task = task,
 	};
 
 	tlb_start_vma(tlb, vma);
@@ -493,7 +500,8 @@ static void madvise_cold_page_range(stru
 	tlb_end_vma(tlb, vma);
 }
 
-static long madvise_cold(struct vm_area_struct *vma,
+static long madvise_cold(struct task_struct *task,
+			struct vm_area_struct *vma,
 			struct vm_area_struct **prev,
 			unsigned long start_addr, unsigned long end_addr)
 {
@@ -506,19 +514,21 @@ static long madvise_cold(struct vm_area_
 
 	lru_add_drain();
 	tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
-	madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
+	madvise_cold_page_range(&tlb, task, vma, start_addr, end_addr);
 	tlb_finish_mmu(&tlb, start_addr, end_addr);
 
 	return 0;
 }
 
 static void madvise_pageout_page_range(struct mmu_gather *tlb,
+			     struct task_struct *task,
 			     struct vm_area_struct *vma,
 			     unsigned long addr, unsigned long end)
 {
 	struct madvise_walk_private walk_private = {
 		.pageout = true,
 		.tlb = tlb,
+		.target_task = task,
 	};
 
 	tlb_start_vma(tlb, vma);
@@ -542,7 +552,8 @@ static inline bool can_do_pageout(struct
 		inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0;
 }
 
-static long madvise_pageout(struct vm_area_struct *vma,
+static long madvise_pageout(struct task_struct *task,
+			struct vm_area_struct *vma,
 			struct vm_area_struct **prev,
 			unsigned long start_addr, unsigned long end_addr)
 {
@@ -558,7 +569,7 @@ static long madvise_pageout(struct vm_ar
 
 	lru_add_drain();
 	tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
-	madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
+	madvise_pageout_page_range(&tlb, task, vma, start_addr, end_addr);
 	tlb_finish_mmu(&tlb, start_addr, end_addr);
 
 	return 0;
@@ -938,7 +949,8 @@ static int madvise_inject_error(int beha
 #endif
 
 static long
-madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
+madvise_vma(struct task_struct *task, struct vm_area_struct *vma,
+		struct vm_area_struct **prev,
 		unsigned long start, unsigned long end, int behavior)
 {
 	switch (behavior) {
@@ -947,9 +959,9 @@ madvise_vma(struct vm_area_struct *vma,
 	case MADV_WILLNEED:
 		return madvise_willneed(vma, prev, start, end);
 	case MADV_COLD:
-		return madvise_cold(vma, prev, start, end);
+		return madvise_cold(task, vma, prev, start, end);
 	case MADV_PAGEOUT:
-		return madvise_pageout(vma, prev, start, end);
+		return madvise_pageout(task, vma, prev, start, end);
 	case MADV_FREE:
 	case MADV_DONTNEED:
 		return madvise_dontneed_free(vma, prev, start, end, behavior);
@@ -1166,7 +1178,8 @@ int do_madvise(struct task_struct *targe
 			tmp = end;
 
 		/* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
-		error = madvise_vma(vma, &prev, start, tmp, behavior);
+		error = madvise_vma(target_task, vma, &prev,
+					start, tmp, behavior);
 		if (error)
 			goto out;
 		start = tmp;

_

author maintainer
#21 lore raw

PATCH 20/25 pid: move pidfd_get_pid() to pid.c

In reply to Andrew Morton

From: Minchan Kim <[email protected]>
Subject: pid: move pidfd_get_pid() to pid.c

process_madvise syscall needs pidfd_get_pid function to translate pidfd to pid so this patch move the function to kernel/pid.c.

---

 include/linux/pid.h |    1 +
 kernel/exit.c       |   17 -----------------
 kernel/pid.c        |   17 +++++++++++++++++
 3 files changed, 18 insertions(+), 17 deletions(-)
include/linux/pid.h renamed from include/linux/pid.h~pid-move-pidfd_get_pid-function-to-pidc +1 -0
@@ -77,6 +77,7 @@ extern const struct file_operations pidf
 struct file;
 
 extern struct pid *pidfd_pid(const struct file *file);
+struct pid *pidfd_get_pid(unsigned int fd);
 
 static inline struct pid *get_pid(struct pid *pid)
 {
kernel/exit.c renamed from kernel/exit.c~pid-move-pidfd_get_pid-function-to-pidc +0 -17
@@ -1474,23 +1474,6 @@ end:
 	return retval;
 }
 
-static struct pid *pidfd_get_pid(unsigned int fd)
-{
-	struct fd f;
-	struct pid *pid;
-
-	f = fdget(fd);
-	if (!f.file)
-		return ERR_PTR(-EBADF);
-
-	pid = pidfd_pid(f.file);
-	if (!IS_ERR(pid))
-		get_pid(pid);
-
-	fdput(f);
-	return pid;
-}
-
 static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
 			  int options, struct rusage *ru)
 {
kernel/pid.c renamed from kernel/pid.c~pid-move-pidfd_get_pid-function-to-pidc +17 -0
@@ -518,6 +518,23 @@ struct pid *find_ge_pid(int nr, struct p
 	return idr_get_next(&ns->idr, &nr);
 }
 
+struct pid *pidfd_get_pid(unsigned int fd)
+{
+	struct fd f;
+	struct pid *pid;
+
+	f = fdget(fd);
+	if (!f.file)
+		return ERR_PTR(-EBADF);
+
+	pid = pidfd_pid(f.file);
+	if (!IS_ERR(pid))
+		get_pid(pid);
+
+	fdput(f);
+	return pid;
+}
+
 /**
  * pidfd_create() - Create a new pid file descriptor.
  *

_

author maintainer
#22 lore raw

PATCH 21/25 mm/madvise: support both pid and pidfd for process_madvise

In reply to Andrew Morton

From: Minchan Kim <[email protected]>
Subject: mm/madvise: support both pid and pidfd for process_madvise

There is a demand[1] to support pid as well pidfd for process_madvise to reduce unnecessary syscall to get pidfd if the user has control of the target process (ie, they could guarantee the process is not gone or pid is not reused).

This patch aims for supporting both options like waitid(2). So, the syscall is currently,

int process_madvise(idtype_t idtype, id_t id, void *addr,
size_t length, int advice, unsigned long flags);

@which is actually idtype_t for userspace library and currently, it supports P_PID and P_PIDFD.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Cc Christian Brauner <[email protected]>
Cc Alexander Duyck <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc John Dias <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc SeongJae Park <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Tim Murray <[email protected]>
Cc Christian Brauner <[email protected]>

---

 include/linux/syscalls.h |    3 ++-
 mm/madvise.c             |   36 +++++++++++++++++++++++-------------
 2 files changed, 25 insertions(+), 14 deletions(-)
include/linux/syscalls.h renamed from include/linux/syscalls.h~mm-support-both-pid-and-pidfd-for-process_madvise +2 -1
@@ -878,7 +878,8 @@ asmlinkage long sys_munlockall(void);
 asmlinkage long sys_mincore(unsigned long start, size_t len,
 				unsigned char __user * vec);
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
-asmlinkage long sys_process_madvise(int pidfd, unsigned long start,
+
+asmlinkage long sys_process_madvise(int which, pid_t pid, unsigned long start,
 			size_t len, int behavior, unsigned long flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
 			unsigned long prot, unsigned long pgoff,
mm/madvise.c renamed from mm/madvise.c~mm-support-both-pid-and-pidfd-for-process_madvise +23 -13
@@ -1208,11 +1208,10 @@ SYSCALL_DEFINE3(madvise, unsigned long,
 	return do_madvise(current, current->mm, start, len_in, behavior);
 }
 
-SYSCALL_DEFINE5(process_madvise, int, pidfd, unsigned long, start,
+SYSCALL_DEFINE6(process_madvise, int, which, pid_t, upid, unsigned long, start,
 		size_t, len_in, int, behavior, unsigned long, flags)
 {
 	int ret;
-	struct fd f;
 	struct pid *pid;
 	struct task_struct *task;
 	struct mm_struct *mm;
@@ -1223,20 +1222,31 @@ SYSCALL_DEFINE5(process_madvise, int, pi
 	if (!process_madvise_behavior_valid(behavior))
 		return -EINVAL;
 
-	f = fdget(pidfd);
-	if (!f.file)
-		return -EBADF;
-
-	pid = pidfd_pid(f.file);
-	if (IS_ERR(pid)) {
-		ret = PTR_ERR(pid);
-		goto fdput;
+	switch (which) {
+	case P_PID:
+		if (upid <= 0)
+			return -EINVAL;
+
+		pid = find_get_pid(upid);
+		if (!pid)
+			return -ESRCH;
+		break;
+	case P_PIDFD:
+		if (upid < 0)
+			return -EINVAL;
+
+		pid = pidfd_get_pid(upid);
+		if (IS_ERR(pid))
+			return PTR_ERR(pid);
+		break;
+	default:
+		return -EINVAL;
 	}
 
 	task = get_pid_task(pid, PIDTYPE_PID);
 	if (!task) {
 		ret = -ESRCH;
-		goto fdput;
+		goto put_pid;
 	}
 
 	mm = mm_access(task, PTRACE_MODE_ATTACH_FSCREDS);
@@ -1249,7 +1259,7 @@ SYSCALL_DEFINE5(process_madvise, int, pi
 	mmput(mm);
 release_task:
 	put_task_struct(task);
-fdput:
-	fdput(f);
+put_pid:
+	put_pid(pid);
 	return ret;
 }

_

author maintainer
#23 lore raw

PATCH 22/25 mm/madvise: allow KSM hints for remote API

In reply to Andrew Morton

From: Oleksandr Natalenko <[email protected]>
Subject: mm/madvise: allow KSM hints for remote API

It all began with the fact that KSM works only on memory that is marked by madvise(). And the only way to get around that is to either:

* use LD_PRELOAD; or
* patch the kernel with something like UKSM or PKSM.

(i skip ptrace can of worms here intentionally)

To overcome this restriction, lets employ a new remote madvise API. This can be used by some small userspace helper daemon that will do auto-KSM job for us.

I think of two major consumers of remote KSM hints:

* hosts, that run containers, especially similar ones and especially in

    a trusted environment, sharing the same runtime like Node.js;

* heavy applications, that can be run in multiple instances, not limited to opensource ones like Firefox, but also those that cannot be modified since they are binary-only and, maybe, statically linked.

Speaking of statistics, more numbers can be found in the very first submission, that is related to this one [1]. For my current setup with two Firefox instances I get 100 to 200 MiB saved for the second instance depending on the amount of tabs.

1 FF instance with 15 tabs:

   $ echo "$(cat /sys/kernel/mm/ksm/pages_sharing) * 4 / 1024" | bc
   410

2 FF instances, second one has 12 tabs (all the tabs are different):

   $ echo "$(cat /sys/kernel/mm/ksm/pages_sharing) * 4 / 1024" | bc
   592

At the very moment I do not have specific numbers for containerised workload, but those should be comparable in case the containers share similar/same runtime.

[1] https://lore.kernel.org/patchwork/patch/1012142/

Cc Alexander Duyck <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc John Dias <[email protected]>
Cc Kirill Tkhai <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tim Murray <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Christian Brauner <[email protected]>

---

 mm/madvise.c |    4 ++++
 1 file changed, 4 insertions(+)
mm/madvise.c renamed from mm/madvise.c~mm-madvise-allow-ksm-hints-for-remote-api +4 -0
@@ -1014,6 +1014,10 @@ process_madvise_behavior_valid(int behav
 	switch (behavior) {
 	case MADV_COLD:
 	case MADV_PAGEOUT:
+#ifdef CONFIG_KSM
+	case MADV_MERGEABLE:
+	case MADV_UNMERGEABLE:
+#endif
 		return true;
 	default:
 		return false;

_

author maintainer
#24 lore raw

PATCH 23/25 mm: support vector address ranges for process_madvise

In reply to Andrew Morton

From: Minchan Kim <[email protected]>
Subject: mm: support vector address ranges for process_madvise

This patch changes process_madvise interface:

a) support vector address ranges in a system call
b) support the vector address ranges to local process as well as external process
c) remove pid but keep only pidfd in argument - [1][2]
d) change type of flags with unsgined int

Android app has thousands of vmas due to zygote so it's totally waste of CPU and power if we should call the syscall one by one for each vma. (With testing 2000-vma syscall vs 1-vector syscall, it showed 15% performance improvement. I think it would be bigger in real practice because the testing ran very cache friendly environment).

Another potential use case for the vector range is to amortize the cost of TLB shootdowns for multiple ranges when using MADV_DONTNEED; this could benefit users like TCP receive zerocopy and malloc implementations. In future, we could find more usecases for other advises so let's make it happens as API since we introduce a new syscall at this moment. With that, existing madvise(2) user could replace it with process_madvise(2) with their own pid if they want to have batch address ranges support feature.

So finally, the API is as follows,

ssize_t process_madvise(int pidfd, const struct iovec *iovec,
unsigned long vlen, int advice, unsigned int flags);

DESCRIPTION
The process_madvise() system call is used to give advice or directions to the kernel about the address ranges from external process as well as local process. It provides the advice to address ranges of process described by iovec and vlen. The goal of such advice is to improve system or application performance.

The pidfd selects the process referred to by the PID file descriptor specified in pidfd. (See pidofd_open(2) for further information)

The pointer iovec points to an array of iovec structures, defined in <sys/uio.h> as:

struct iovec {
void *iov_base; /* starting address */
size_t iov_len; /* number of bytes to be advised */
};

The iovec describes address ranges beginning at address(iov_base) and with size length of bytes(iov_len).

The vlen represents the number of elements in iovec.

The advice is indicated in the advice argument, which is one of the following at this moment if the target process specified by pidfd is external.

MADV_COLD
MADV_PAGEOUT
MADV_MERGEABLE
MADV_UNMERGEABLE

Permission to provide a hint to external process is governed by a ptrace access mode PTRACE_MODE_ATTACH_FSCREDS check; see ptrace(2).

The process_madvise supports every advice madvise(2) has if target process is in same thread group with calling process so user could use process_madvise(2) to extend existing madvise(2) to support vector address ranges.

RETURN VALUE
On success, process_madvise() returns the number of bytes advised. This return value may be less than the total number of requested bytes, if an error occurred. The caller should check return value to determine whether a partial advice occurred.

[1] https://lore.kernel.org/linux-mm/20200509124817.xmrvsrq3mla6b76k@wittgenstein/
[2] https://lore.kernel.org/linux-mm/[email protected]/

[[email protected]: support compat_sys_process_madvise]
[[email protected]: fix process_madvise prototype]
[[email protected]: make do_process_madvise() static]
[[email protected]: fix s390 compat build error]
[[email protected]: add compat_sys_process_madvise to mips syscall table]
Cc David Rientjes <[email protected]>
Cc Arjun Roy <[email protected]>
Cc Tim Murray <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc John Dias <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Christian Brauner <[email protected]>

---

 arch/arm64/include/asm/unistd32.h         |    2 
 arch/mips/kernel/syscalls/syscall_n32.tbl |    2 
 arch/mips/kernel/syscalls/syscall_o32.tbl |    2 
 arch/parisc/kernel/syscalls/syscall.tbl   |    2 
 arch/powerpc/kernel/syscalls/syscall.tbl  |    2 
 arch/s390/kernel/syscalls/syscall.tbl     |    2 
 arch/sparc/kernel/syscalls/syscall.tbl    |    2 
 arch/x86/entry/syscalls/syscall_32.tbl    |    2 
 arch/x86/entry/syscalls/syscall_64.tbl    |    4 -
 include/linux/compat.h                    |    4 +
 include/linux/syscalls.h                  |    6 -
 include/uapi/asm-generic/unistd.h         |    3 
 kernel/sys_ni.c                           |    1 
 mm/madvise.c                              |   80 ++++++++++++++++++--
 14 files changed, 93 insertions(+), 21 deletions(-)
arch/arm64/include/asm/unistd32.h renamed from arch/arm64/include/asm/unistd32.h~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -886,7 +886,7 @@ __SYSCALL(__NR_pidfd_getfd, sys_pidfd_ge
 #define __NR_faccessat2 439
 __SYSCALL(__NR_faccessat2, sys_faccessat2)
 #define __NR_process_madvise 440
-__SYSCALL(__NR_process_madvise, sys_process_madvise)
+__SYSCALL(__NR_process_madvise, compat_sys_process_madvise)
 
 /*
  * Please add new compat syscalls above this comment and update
arch/mips/kernel/syscalls/syscall_n32.tbl renamed from arch/mips/kernel/syscalls/syscall_n32.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -377,4 +377,4 @@
 437	n32	openat2				sys_openat2
 438	n32	pidfd_getfd			sys_pidfd_getfd
 439	n32	faccessat2			sys_faccessat2
-440	n32	process_madvise			sys_process_madvise
+440	n32	process_madvise			compat_sys_process_madvise
arch/mips/kernel/syscalls/syscall_o32.tbl renamed from arch/mips/kernel/syscalls/syscall_o32.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -426,4 +426,4 @@
 437	o32	openat2				sys_openat2
 438	o32	pidfd_getfd			sys_pidfd_getfd
 439	o32	faccessat2			sys_faccessat2
-440	o32	process_madvise			sys_process_madvise
+440	o32	process_madvise			sys_process_madvise		compat_sys_process_madvise
arch/parisc/kernel/syscalls/syscall.tbl renamed from arch/parisc/kernel/syscalls/syscall.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -436,4 +436,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
-440	common	process_madvise			sys_process_madvise
+440	common	process_madvise			sys_process_madvise		compat_sys_process_madvise
arch/powerpc/kernel/syscalls/syscall.tbl renamed from arch/powerpc/kernel/syscalls/syscall.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -528,4 +528,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
-440	common	process_madvise			sys_process_madvise
+440	common	process_madvise			sys_process_madvise		compat_sys_process_madvise
arch/s390/kernel/syscalls/syscall.tbl renamed from arch/s390/kernel/syscalls/syscall.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -441,4 +441,4 @@
 437  common	openat2			sys_openat2			sys_openat2
 438  common	pidfd_getfd		sys_pidfd_getfd			sys_pidfd_getfd
 439  common	faccessat2		sys_faccessat2			sys_faccessat2
-440  common	process_madvise		sys_process_madvise		sys_process_madvise
+440  common	process_madvise		sys_process_madvise		compat_sys_process_madvise
arch/sparc/kernel/syscalls/syscall.tbl renamed from arch/sparc/kernel/syscalls/syscall.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -484,4 +484,4 @@
 437	common	openat2			sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
-440	common	process_madvise			sys_process_madvise
+440	common	process_madvise			sys_process_madvise		compat_sys_process_madvise
arch/x86/entry/syscalls/syscall_32.tbl renamed from arch/x86/entry/syscalls/syscall_32.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -443,4 +443,4 @@
 437	i386	openat2			sys_openat2
 438	i386	pidfd_getfd		sys_pidfd_getfd
 439	i386	faccessat2		sys_faccessat2
-440	i386	process_madvise		sys_process_madvise
+440	i386	process_madvise		sys_process_madvise		compat_sys_process_madvise
arch/x86/entry/syscalls/syscall_64.tbl renamed from arch/x86/entry/syscalls/syscall_64.tbl~mm-support-vector-address-ranges-for-process_madvise +2 -2
@@ -360,8 +360,7 @@
 437	common	openat2			sys_openat2
 438	common	pidfd_getfd		sys_pidfd_getfd
 439	common	faccessat2		sys_faccessat2
-440	common	process_madvise		sys_process_madvise
-
+440	64	process_madvise		sys_process_madvise
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
 # for native 64-bit operation. The __x32_compat_sys stubs are created
@@ -404,3 +403,4 @@
 545	x32	execveat		compat_sys_execveat
 546	x32	preadv2			compat_sys_preadv64v2
 547	x32	pwritev2		compat_sys_pwritev64v2
+548	x32	process_madvise		compat_sys_process_madvise
include/linux/compat.h renamed from include/linux/compat.h~mm-support-vector-address-ranges-for-process_madvise +4 -0
@@ -827,6 +827,10 @@ asmlinkage long compat_sys_pwritev64v2(u
 		unsigned long vlen, loff_t pos, rwf_t flags);
 #endif
 
+asmlinkage ssize_t compat_sys_process_madvise(compat_int_t which,
+		compat_pid_t upid, const struct compat_iovec __user *vec,
+		compat_ulong_t vlen, compat_int_t behavior,
+		compat_ulong_t flags);
 
 /*
  * Deprecated system calls which are still defined in
include/linux/syscalls.h renamed from include/linux/syscalls.h~mm-support-vector-address-ranges-for-process_madvise +3 -3
@@ -878,9 +878,9 @@ asmlinkage long sys_munlockall(void);
 asmlinkage long sys_mincore(unsigned long start, size_t len,
 				unsigned char __user * vec);
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
-
-asmlinkage long sys_process_madvise(int which, pid_t pid, unsigned long start,
-			size_t len, int behavior, unsigned long flags);
+asmlinkage long sys_process_madvise(int which, pid_t upid,
+		const struct iovec __user *vec, unsigned long vlen,
+		int behavior, unsigned long flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
 			unsigned long prot, unsigned long pgoff,
 			unsigned long flags);
include/uapi/asm-generic/unistd.h renamed from include/uapi/asm-generic/unistd.h~mm-support-vector-address-ranges-for-process_madvise +2 -1
@@ -858,7 +858,8 @@ __SYSCALL(__NR_pidfd_getfd, sys_pidfd_ge
 #define __NR_faccessat2 439
 __SYSCALL(__NR_faccessat2, sys_faccessat2)
 #define __NR_process_madvise 440
-__SYSCALL(__NR_process_madvise, sys_process_madvise)
+__SC_COMP(__NR_process_madvise, sys_process_madvise, \
+		compat_sys_process_madvise)
 
 #undef __NR_syscalls
 #define __NR_syscalls 441
kernel/sys_ni.c renamed from kernel/sys_ni.c~mm-support-vector-address-ranges-for-process_madvise +1 -0
@@ -281,6 +281,7 @@ COND_SYSCALL(munlockall);
 COND_SYSCALL(mincore);
 COND_SYSCALL(madvise);
 COND_SYSCALL(process_madvise);
+COND_SYSCALL_COMPAT(process_madvise);
 COND_SYSCALL(remap_file_pages);
 COND_SYSCALL(mbind);
 COND_SYSCALL_COMPAT(mbind);
mm/madvise.c renamed from mm/madvise.c~mm-support-vector-address-ranges-for-process_madvise +73 -7

109 lines not on this page · raw message

_

author maintainer
#25 lore raw

PATCH 24/25 mm: use only pidfd for process_madvise syscall

In reply to Andrew Morton

From: Minchan Kim <[email protected]>
Subject: mm: use only pidfd for process_madvise syscall

Based on discussion[1], people didn't feel we need to support both pid and pidfd for every new coming API[2] so this patch keeps only pidfd. This patch also changes flags's type with "unsigned int".

[1] https://lore.kernel.org/linux-mm/20200509124817.xmrvsrq3mla6b76k@wittgenstein/
[2] https://lore.kernel.org/linux-mm/[email protected]/

[[email protected]: return EBADF if pidfd is invalid]
Cc David Rientjes <[email protected]>
Cc Arjun Roy <[email protected]>
Cc Tim Murray <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc John Dias <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Christian Brauner <[email protected]>

---

 include/linux/compat.h   |    6 ++---
 include/linux/syscalls.h |    5 +---
 mm/madvise.c             |   41 +++++++++----------------------------
 3 files changed, 16 insertions(+), 36 deletions(-)
include/linux/compat.h renamed from include/linux/compat.h~mm-use-only-pidfd-for-process_madvise-syscall +3 -3
@@ -827,10 +827,10 @@ asmlinkage long compat_sys_pwritev64v2(u
 		unsigned long vlen, loff_t pos, rwf_t flags);
 #endif
 
-asmlinkage ssize_t compat_sys_process_madvise(compat_int_t which,
-		compat_pid_t upid, const struct compat_iovec __user *vec,
+asmlinkage ssize_t compat_sys_process_madvise(compat_int_t pidfd,
+		const struct compat_iovec __user *vec,
 		compat_ulong_t vlen, compat_int_t behavior,
-		compat_ulong_t flags);
+		compat_int_t flags);
 
 /*
  * Deprecated system calls which are still defined in
include/linux/syscalls.h renamed from include/linux/syscalls.h~mm-use-only-pidfd-for-process_madvise-syscall +2 -3
@@ -878,9 +878,8 @@ asmlinkage long sys_munlockall(void);
 asmlinkage long sys_mincore(unsigned long start, size_t len,
 				unsigned char __user * vec);
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
-asmlinkage long sys_process_madvise(int which, pid_t upid,
-		const struct iovec __user *vec, unsigned long vlen,
-		int behavior, unsigned long flags);
+asmlinkage long sys_process_madvise(int pidfd, const struct iovec __user *vec,
+		unsigned long vlen, int behavior, unsigned int flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
 			unsigned long prot, unsigned long pgoff,
 			unsigned long flags);
mm/madvise.c renamed from mm/madvise.c~mm-use-only-pidfd-for-process_madvise-syscall +11 -30

80 lines not on this page · raw message

_

#26 lore raw

Re: [patch 24/25] mm: use only pidfd for process_madvise syscall

Quoted 3 lines · Andrew Morton

From: Minchan Kim <[email protected]>
Subject: mm: use only pidfd for process_madvise syscall

No.

I'm not taking this crazy series.

First we have patch 18/25, which introduces process_madvise() that takes a pidfd.

Then we have patch 21/25, which says

"There is a demand[1] to support pid as well pidfd for process_madvise.."

and points to

https://lore.kernel.org/linux-mm/[email protected]/

And then finally we have patch 24/25, which says

"Based on discussion[1], people didn't feel we need to support both
pid and pidfd for every new coming API[2] so this patch keeps only pidfd"

and points to

https://lore.kernel.org/linux-mm/20200509124817.xmrvsrq3mla6b76k@wittgenstein/

Fine. Discussion is good. But this patch-series is crazy, and I refuse to take this kind of schizophrenic patches that can't make up their mind.

Make up your mind, dammit! Don't send me patches that vacillate between two standpoints and make the history and the eventual final end result really hard to tell.

What will it be tomorrow? Another two patches that decide to go the other way and then back?

I'm going to flush this whole madvise patch-set down the toilet,
because after having this kind of whip-lash looking through it, I can't take it any more.

So all of 17-25 are just going in the garbage until people can make up their minds.

Linus

#27 lore raw

Re: [patch 24/25] mm: use only pidfd for process_madvise syscall

Quoted 41 lines · Linus Torvalds
Quoted 3 lines · Andrew Morton

From: Minchan Kim <[email protected]>
Subject: mm: use only pidfd for process_madvise syscall

No.

I'm not taking this crazy series.

First we have patch 18/25, which introduces process_madvise() that takes a pidfd.

Then we have patch 21/25, which says

"There is a demand[1] to support pid as well pidfd for process_madvise.."

and points to

https://lore.kernel.org/linux-mm/[email protected]/

And then finally we have patch 24/25, which says

"Based on discussion[1], people didn't feel we need to support both
pid and pidfd for every new coming API[2] so this patch keeps only pidfd"

and points to

https://lore.kernel.org/linux-mm/20200509124817.xmrvsrq3mla6b76k@wittgenstein/

Fine. Discussion is good. But this patch-series is crazy, and I refuse to take this kind of schizophrenic patches that can't make up their mind.

Make up your mind, dammit! Don't send me patches that vacillate between two standpoints and make the history and the eventual final end result really hard to tell.

What will it be tomorrow? Another two patches that decide to go the other way and then back?

Quoted 6 lines

I'm going to flush this whole madvise patch-set down the toilet,
because after having this kind of whip-lash looking through it, I can't take it any more.

So all of 17-25 are just going in the garbage until people can make up their minds.

At next revision, I will merge 18 and 23 into a patch with dropping 21 and 24.
Andrew, I will resend it after rc window is closed. Thanks.

[patch 18/25] mm/madvise: introduce process_madvise() syscall: an external memory hinting API [patch 21/25] mm/madvise: support both pid and pidfd for process_madvise [patch 23/25] mm: support vector address ranges for process_madvise
[patch 24/25] mm: use only pidfd for process_madvise syscall

author maintainer
#28 lore raw

PATCH 25/25 mm/madvise.c: remove duplicated include

In reply to Andrew Morton

From: YueHaibing <[email protected]>
Subject: mm/madvise.c: remove duplicated include

Remove duplicated include.

---

 mm/madvise.c |    1 -
 1 file changed, 1 deletion(-)
mm/madvise.c renamed from mm/madvise.c~mm-remove-duplicated-include-from-madvisec +0 -1
@@ -29,7 +29,6 @@
 #include <linux/swapops.h>
 #include <linux/shmem_fs.h>
 #include <linux/mmu_notifier.h>
-#include <linux/sched/mm.h>
 #include <linux/uio.h>
 
 #include <asm/tlb.h>

_

author maintainer tree notice
#29 lore raw

[to-be-updated] mm-pass-task-and-mm-to-do_madvise.patch removed from -mm tree

In reply to Andrew Morton
The patch titled Subject: mm/madvise: pass task and mm to do_madvise has been removed from the -mm tree. Its filename was mm-pass-task-and-mm-to-do_madvise.patch

The patch titled
Subject: mm/madvise: pass task and mm to do_madvise
has been removed from the -mm tree. Its filename was
mm-pass-task-and-mm-to-do_madvise.patch

This patch was dropped because an updated version will be merged

------------------------------------------------------
From: Minchan Kim <[email protected]>
Subject: mm/madvise: pass task and mm to do_madvise

Patch series "introduce memory hinting API for external process", v7.

Now, we have MADV_PAGEOUT and MADV_COLD as madvise hinting API. With that, application could give hints to kernel what memory range are preferred to be reclaimed. However, in some platform(e.g., Android), the information required to make the hinting decision is not known to the app. Instead, it is known to a centralized userspace daemon(e.g., ActivityManagerService), and that daemon must be able to initiate reclaim on its own without any app involvement.

To solve the concern, this patch introduces new syscall - process_madvise(2). Bascially, it's same with madvise(2) syscall but it has some differences.

1. It needs pidfd of target process to provide the hint

2. It supports only MADV_{COLD|PAGEOUT|MERGEABLE|UNMEREABLE} at this moment. Other hints in madvise will be opened when there are explicit requests from community to prevent unexpected bugs we couldn't support.

3. Only privileged processes can do something for other process's address space.

For more detail of the new API, please see "mm: introduce external memory hinting API" description in this patchset.

This patch (of 7):

In upcoming patches, do_madvise will be called from external process context so we shouldn't asssume "current" is always hinted process's task_struct.

Furthermore, we must not access mm_struct via task->mm, but obtain it via access_mm() once (in the following patch) and only use that pointer [1], so pass it to do_madvise() as well. Note the vma->vm_mm pointers are safe, so we can use them further down the call stack.

And let's pass *current* and current->mm as arguments of do_madvise so it shouldn't change existing behavior but prepare next patch to make review easy.

Note: io_madvise passes NULL as target_task argument of do_madvise because it couldn't know who is target.

[1] http://lore.kernel.org/r/CAG48ez27=pwm5m_N_988xT1huO7g7h6arTQL44zev6TD-h-7Tg@mail.gmail.com

[[email protected]: changelog tweak]
[[email protected]: use current->mm for io_uring]
[[email protected]: fix it for upstream changes]
[[email protected]: add missing includes]
Cc Jens Axboe <[email protected]>
Cc Jann Horn <[email protected]>
Cc Tim Murray <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc John Dias <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Alexander Duyck <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Kirill Tkhai <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Christian Brauner <[email protected]>

---

 fs/io_uring.c      |    2 +-
 include/linux/mm.h |    3 ++-
 mm/madvise.c       |   40 +++++++++++++++++++++++-----------------
 3 files changed, 26 insertions(+), 19 deletions(-)
fs/io_uring.c renamed from fs/io_uring.c~mm-pass-task-and-mm-to-do_madvise +1 -1
@@ -3321,7 +3321,7 @@ static int io_madvise(struct io_kiocb *r
 	if (force_nonblock)
 		return -EAGAIN;
 
-	ret = do_madvise(ma->addr, ma->len, ma->advice);
+	ret = do_madvise(NULL, current->mm, ma->addr, ma->len, ma->advice);
 	if (ret < 0)
 		req_set_fail_links(req);
 	io_cqring_add_event(req, ret);
include/linux/mm.h renamed from include/linux/mm.h~mm-pass-task-and-mm-to-do_madvise +2 -1
@@ -2585,7 +2585,8 @@ extern int __do_munmap(struct mm_struct
 		       struct list_head *uf, bool downgrade);
 extern int do_munmap(struct mm_struct *, unsigned long, size_t,
 		     struct list_head *uf);
-extern int do_madvise(unsigned long start, size_t len_in, int behavior);
+extern int do_madvise(struct task_struct *target_task, struct mm_struct *mm,
+		unsigned long start, size_t len_in, int behavior);
 
 static inline unsigned long
 do_mmap_pgoff(struct file *file, unsigned long addr,
mm/madvise.c renamed from mm/madvise.c~mm-pass-task-and-mm-to-do_madvise +23 -17
@@ -22,12 +22,14 @@
 #include <linux/file.h>
 #include <linux/blkdev.h>
 #include <linux/backing-dev.h>
+#include <linux/compat.h>
 #include <linux/pagewalk.h>
 #include <linux/swap.h>
 #include <linux/swapops.h>
 #include <linux/shmem_fs.h>
 #include <linux/mmu_notifier.h>
 #include <linux/sched/mm.h>
+#include <linux/uio.h>
 
 #include <asm/tlb.h>
 
@@ -255,6 +257,7 @@ static long madvise_willneed(struct vm_a
 			     struct vm_area_struct **prev,
 			     unsigned long start, unsigned long end)
 {
+	struct mm_struct *mm = vma->vm_mm;
 	struct file *file = vma->vm_file;
 	loff_t offset;
 
@@ -289,12 +292,12 @@ static long madvise_willneed(struct vm_a
 	 */
 	*prev = NULL;	/* tell sys_madvise we drop mmap_lock */
 	get_file(file);
-	mmap_read_unlock(current->mm);
+	mmap_read_unlock(mm);
 	offset = (loff_t)(start - vma->vm_start)
 			+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
 	vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
 	fput(file);
-	mmap_read_lock(current->mm);
+	mmap_read_lock(mm);
 	return 0;
 }
 
@@ -683,7 +686,6 @@ out:
 	if (nr_swap) {
 		if (current->mm == mm)
 			sync_mm_rss(mm);
-
 		add_mm_counter(mm, MM_SWAPENTS, nr_swap);
 	}
 	arch_leave_lazy_mmu_mode();
@@ -763,6 +765,8 @@ static long madvise_dontneed_free(struct
 				  unsigned long start, unsigned long end,
 				  int behavior)
 {
+	struct mm_struct *mm = vma->vm_mm;
+
 	*prev = vma;
 	if (!can_madv_lru_vma(vma))
 		return -EINVAL;
@@ -770,8 +774,8 @@ static long madvise_dontneed_free(struct
 	if (!userfaultfd_remove(vma, start, end)) {
 		*prev = NULL; /* mmap_lock has been dropped, prev is stale */
 
-		mmap_read_lock(current->mm);
-		vma = find_vma(current->mm, start);
+		mmap_read_lock(mm);
+		vma = find_vma(mm, start);
 		if (!vma)
 			return -ENOMEM;
 		if (start < vma->vm_start) {
@@ -825,6 +829,7 @@ static long madvise_remove(struct vm_are
 	loff_t offset;
 	int error;
 	struct file *f;
+	struct mm_struct *mm = vma->vm_mm;
 
 	*prev = NULL;	/* tell sys_madvise we drop mmap_lock */
 
@@ -852,13 +857,13 @@ static long madvise_remove(struct vm_are
 	get_file(f);
 	if (userfaultfd_remove(vma, start, end)) {
 		/* mmap_lock was not released by userfaultfd_remove() */
-		mmap_read_unlock(current->mm);
+		mmap_read_unlock(mm);
 	}
 	error = vfs_fallocate(f,
 				FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
 				offset, end - start);
 	fput(f);
-	mmap_read_lock(current->mm);
+	mmap_read_lock(mm);
 	return error;
 }
 
@@ -1051,7 +1056,8 @@ madvise_behavior_valid(int behavior)
  *  -EBADF  - map exists, but area maps something that isn't a file.
  *  -EAGAIN - a kernel resource was temporarily unavailable.
  */
-int do_madvise(unsigned long start, size_t len_in, int behavior)
+int do_madvise(struct task_struct *target_task, struct mm_struct *mm,
+		unsigned long start, size_t len_in, int behavior)
 {
 	unsigned long end, tmp;
 	struct vm_area_struct *vma, *prev;
@@ -1089,7 +1095,7 @@ int do_madvise(unsigned long start, size
 
 	write = madvise_need_mmap_write(behavior);
 	if (write) {
-		if (mmap_write_lock_killable(current->mm))
+		if (mmap_write_lock_killable(mm))
 			return -EINTR;
 
 		/*
@@ -1104,12 +1110,12 @@ int do_madvise(unsigned long start, size
 		 * but for now we have the mmget_still_valid()
 		 * model.
 		 */
-		if (!mmget_still_valid(current->mm)) {
-			mmap_write_unlock(current->mm);
+		if (!mmget_still_valid(mm)) {
+			mmap_write_unlock(mm);
 			return -EINTR;
 		}
 	} else {
-		mmap_read_lock(current->mm);
+		mmap_read_lock(mm);
 	}
 
 	/*
@@ -1117,7 +1123,7 @@ int do_madvise(unsigned long start, size
 	 * ranges, just ignore them, but return -ENOMEM at the end.
 	 * - different from the way of handling in mlock etc.
 	 */
-	vma = find_vma_prev(current->mm, start, &prev);
+	vma = find_vma_prev(mm, start, &prev);
 	if (vma && start > vma->vm_start)
 		prev = vma;
 
@@ -1154,19 +1160,19 @@ int do_madvise(unsigned long start, size
 		if (prev)
 			vma = prev->vm_next;
 		else	/* madvise_remove dropped mmap_lock */
-			vma = find_vma(current->mm, start);
+			vma = find_vma(mm, start);
 	}
 out:
 	blk_finish_plug(&plug);
 	if (write)
-		mmap_write_unlock(current->mm);
+		mmap_write_unlock(mm);
 	else
-		mmap_read_unlock(current->mm);
+		mmap_read_unlock(mm);
 
 	return error;
 }
 
 SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
 {
-	return do_madvise(start, len_in, behavior);
+	return do_madvise(current, current->mm, start, len_in, behavior);
 }

_

Patches currently in -mm which might be from [email protected] are

mm-introduce-external-memory-hinting-api.patch mm-check-fatal-signal-pending-of-target-process.patch pid-move-pidfd_get_pid-function-to-pidc.patch mm-support-both-pid-and-pidfd-for-process_madvise.patch mm-support-vector-address-ranges-for-process_madvise.patch mm-use-only-pidfd-for-process_madvise-syscall.patch

author maintainer tree notice
#30 lore raw

[to-be-updated] mm-introduce-external-memory-hinting-api.patch removed from -mm tree

In reply to Andrew Morton
The patch titled Subject: mm/madvise: introduce process_madvise() syscall: an external memory hinting API has been removed from the -mm tree. Its filename was…

The patch titled
Subject: mm/madvise: introduce process_madvise() syscall: an external memory hinting API
has been removed from the -mm tree. Its filename was
mm-introduce-external-memory-hinting-api.patch

This patch was dropped because an updated version will be merged

------------------------------------------------------
From: Minchan Kim <[email protected]>
Subject: mm/madvise: introduce process_madvise() syscall: an external memory hinting API

There is usecase that System Management Software(SMS) want to give a memory hint like MADV_[COLD|PAGEEOUT] to other processes and in the case of Android, it is the ActivityManagerService.

The information required to make the reclaim decision is not known to the app. Instead, it is known to the centralized userspace daemon(ActivityManagerService), and that daemon must be able to initiate reclaim on its own without any app involvement.

To solve the issue, this patch introduces a new syscall process_madvise(2). It uses pidfd of an external process to give the hint.

int process_madvise(int pidfd, void *addr, size_t length, int advice,

			unsigned long flags);

Since it could affect other process's address range, only privileged process(CAP_SYS_PTRACE) or something else(e.g., being the same UID) gives it the right to ptrace the process could use it successfully. The flag argument is reserved for future use if we need to extend the API.

I think supporting all hints madvise has/will supported/support to process_madvise is rather risky. Because we are not sure all hints make sense from external process and implementation for the hint may rely on the caller being in the current context so it could be error-prone. Thus, I just limited hints as MADV_[COLD|PAGEOUT] in this patch.

If someone want to add other hints, we could hear hear the usecase and review it for each hint. It's safer for maintenance rather than introducing a buggy syscall but hard to fix it later.

Q.1 - Why does any external entity have better knowledge?

Quote from Sandeep

"For Android, every application (including the special SystemServer) are forked from Zygote. The reason of course is to share as many libraries and classes between the two as possible to benefit from the preloading during boot.

After applications start, (almost) all of the APIs end up calling into this SystemServer process over IPC (binder) and back to the application.

In a fully running system, the SystemServer monitors every single process periodically to calculate their PSS / RSS and also decides which process is "important" to the user for interactivity.

So, because of how these processes start _and_ the fact that the SystemServer is looping to monitor each process, it does tend to *know* which address range of the application is not used / useful.

Besides, we can never rely on applications to clean things up themselves. We've had the "hey app1, the system is low on memory, please trim your memory usage down" notifications for a long time[1]. They rely on applications honoring the broadcasts and very few do.

So, if we want to avoid the inevitable killing of the application and restarting it, some way to be able to tell the OS about unimportant memory in these applications will be useful.

- ssp

Q.2 - How to guarantee the race(i.e., object validation) between when giving a hint from an external process and get the hint from the target process?

process_madvise operates on the target process's address space as it exists at the instant that process_madvise is called. If the space target process can run between the time the process_madvise process inspects the target process address space and the time that process_madvise is actually called, process_madvise may operate on memory regions that the calling process does not expect. It's the responsibility of the process calling process_madvise to close this race condition. For example, the calling process can suspend the target process with ptrace, SIGSTOP, or the freezer cgroup so that it doesn't have an opportunity to change its own address space before process_madvise is called. Another option is to operate on memory regions that the caller knows a priori will be unchanged in the target process. Yet another option is to accept the race for certain process_madvise calls after reasoning that mistargeting will do no harm. The suggested API itself does not provide synchronization. It also apply other APIs like move_pages, process_vm_write.

The race isn't really a problem though. Why is it so wrong to require that callers do their own synchronization in some manner? Nobody objects to write(2) merely because it's possible for two processes to open the same file and clobber each other's writes --- instead, we tell people to use flock or something. Think about mmap. It never guarantees newly allocated address space is still valid when the user tries to access it because other threads could unmap the memory right before. That's where we need synchronization by using other API or design from userside. It shouldn't be part of API itself. If someone needs more fine-grained synchronization rather than process level, there were two ideas suggested - cookie[2] and anon-fd[3]. Both are applicable via using last reserved argument of the API but I don't think it's necessary right now since we have already ways to prevent the race so don't want to add additional complexity with more fine-grained optimization model.

To make the API extend, it reserved an unsigned long as last argument so we could support it in future if someone really needs it.

Q.3 - Why doesn't ptrace work?

Injecting an madvise in the target process using ptrace would not work for us because such injected madvise would have to be executed by the target process, which means that process would have to be runnable and that creates the risk of the abovementioned race and hinting a wrong VMA. Furthermore, we want to act the hint in caller's context, not the callee's, because the callee is usually limited in cpuset/cgroups or even freezed state so they can't act by themselves quick enough, which causes more thrashing/kill. It doesn't work if the target process are ptraced(e.g., strace, debugger, minidump) because a process can have at most one ptracer.

[1] https://developer.android.com/topic/performance/memory"

[2] process_getinfo for getting the cookie which is updated whenever vma of process address layout are changed - Daniel Colascione -
https://lore.kernel.org/lkml/[email protected]/T/#m7694416fd179b2066a2c62b5b139b14e3894e224

[3] anonymous fd which is used for the object(i.e., address range) validation - Michal Hocko -
https://lore.kernel.org/lkml/[email protected]/

[[email protected]: fix process_madvise build break for arm64]
[[email protected]: fix build error for mips of process_madvise]
[[email protected]: fix patch ordering issue]
Cc Alexander Duyck <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc John Dias <[email protected]>
Cc Kirill Tkhai <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc SeongJae Park <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Tim Murray <[email protected]>
Cc Christian Brauner <[email protected]>

---

 arch/alpha/kernel/syscalls/syscall.tbl      |    1 
 arch/arm/tools/syscall.tbl                  |    1 
 arch/arm64/include/asm/unistd.h             |    2 
 arch/arm64/include/asm/unistd32.h           |    2 
 arch/ia64/kernel/syscalls/syscall.tbl       |    1 
 arch/m68k/kernel/syscalls/syscall.tbl       |    1 
 arch/microblaze/kernel/syscalls/syscall.tbl |    1 
 arch/mips/kernel/syscalls/syscall_n32.tbl   |    1 
 arch/mips/kernel/syscalls/syscall_n64.tbl   |    1 
 arch/mips/kernel/syscalls/syscall_o32.tbl   |    1 
 arch/parisc/kernel/syscalls/syscall.tbl     |    1 
 arch/powerpc/kernel/syscalls/syscall.tbl    |    1 
 arch/s390/kernel/syscalls/syscall.tbl       |    1 
 arch/sh/kernel/syscalls/syscall.tbl         |    1 
 arch/sparc/kernel/syscalls/syscall.tbl      |    1 
 arch/x86/entry/syscalls/syscall_32.tbl      |    1 
 arch/x86/entry/syscalls/syscall_64.tbl      |    1 
 arch/xtensa/kernel/syscalls/syscall.tbl     |    1 
 include/linux/syscalls.h                    |    2 
 include/uapi/asm-generic/unistd.h           |    4 -
 kernel/sys_ni.c                             |    1 
 mm/madvise.c                                |   64 ++++++++++++++++++
 22 files changed, 89 insertions(+), 2 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl renamed from arch/alpha/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -478,3 +478,4 @@
 547	common	openat2				sys_openat2
 548	common	pidfd_getfd			sys_pidfd_getfd
 549	common	faccessat2			sys_faccessat2
+550	common	process_madvise			sys_process_madvise
arch/arm64/include/asm/unistd32.h renamed from arch/arm64/include/asm/unistd32.h~mm-introduce-external-memory-hinting-api +2 -0
@@ -885,6 +885,8 @@ __SYSCALL(__NR_openat2, sys_openat2)
 __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd)
 #define __NR_faccessat2 439
 __SYSCALL(__NR_faccessat2, sys_faccessat2)
+#define __NR_process_madvise 440
+__SYSCALL(__NR_process_madvise, sys_process_madvise)
 
 /*
  * Please add new compat syscalls above this comment and update
arch/arm64/include/asm/unistd.h renamed from arch/arm64/include/asm/unistd.h~mm-introduce-external-memory-hinting-api +1 -1
@@ -38,7 +38,7 @@
 #define __ARM_NR_compat_set_tls		(__ARM_NR_COMPAT_BASE + 5)
 #define __ARM_NR_COMPAT_END		(__ARM_NR_COMPAT_BASE + 0x800)
 
-#define __NR_compat_syscalls		440
+#define __NR_compat_syscalls		441
 #endif
 
 #define __ARCH_WANT_SYS_CLONE
arch/arm/tools/syscall.tbl renamed from arch/arm/tools/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -452,3 +452,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/ia64/kernel/syscalls/syscall.tbl renamed from arch/ia64/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -359,3 +359,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/m68k/kernel/syscalls/syscall.tbl renamed from arch/m68k/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -438,3 +438,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/microblaze/kernel/syscalls/syscall.tbl renamed from arch/microblaze/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -444,3 +444,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/mips/kernel/syscalls/syscall_n32.tbl renamed from arch/mips/kernel/syscalls/syscall_n32.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -377,3 +377,4 @@
 437	n32	openat2				sys_openat2
 438	n32	pidfd_getfd			sys_pidfd_getfd
 439	n32	faccessat2			sys_faccessat2
+440	n32	process_madvise			sys_process_madvise
arch/mips/kernel/syscalls/syscall_n64.tbl renamed from arch/mips/kernel/syscalls/syscall_n64.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -353,3 +353,4 @@
 437	n64	openat2				sys_openat2
 438	n64	pidfd_getfd			sys_pidfd_getfd
 439	n64	faccessat2			sys_faccessat2
+440	n64	process_madvise			sys_process_madvise
arch/mips/kernel/syscalls/syscall_o32.tbl renamed from arch/mips/kernel/syscalls/syscall_o32.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -426,3 +426,4 @@
 437	o32	openat2				sys_openat2
 438	o32	pidfd_getfd			sys_pidfd_getfd
 439	o32	faccessat2			sys_faccessat2
+440	o32	process_madvise			sys_process_madvise
arch/parisc/kernel/syscalls/syscall.tbl renamed from arch/parisc/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -436,3 +436,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/powerpc/kernel/syscalls/syscall.tbl renamed from arch/powerpc/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -528,3 +528,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/s390/kernel/syscalls/syscall.tbl renamed from arch/s390/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -441,3 +441,4 @@
 437  common	openat2			sys_openat2			sys_openat2
 438  common	pidfd_getfd		sys_pidfd_getfd			sys_pidfd_getfd
 439  common	faccessat2		sys_faccessat2			sys_faccessat2
+440  common	process_madvise		sys_process_madvise		sys_process_madvise
arch/sh/kernel/syscalls/syscall.tbl renamed from arch/sh/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -441,3 +441,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/sparc/kernel/syscalls/syscall.tbl renamed from arch/sparc/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -484,3 +484,4 @@
 437	common	openat2			sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
arch/x86/entry/syscalls/syscall_32.tbl renamed from arch/x86/entry/syscalls/syscall_32.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -443,3 +443,4 @@
 437	i386	openat2			sys_openat2
 438	i386	pidfd_getfd		sys_pidfd_getfd
 439	i386	faccessat2		sys_faccessat2
+440	i386	process_madvise		sys_process_madvise
arch/x86/entry/syscalls/syscall_64.tbl renamed from arch/x86/entry/syscalls/syscall_64.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -360,6 +360,7 @@
 437	common	openat2			sys_openat2
 438	common	pidfd_getfd		sys_pidfd_getfd
 439	common	faccessat2		sys_faccessat2
+440	common	process_madvise		sys_process_madvise
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
arch/xtensa/kernel/syscalls/syscall.tbl renamed from arch/xtensa/kernel/syscalls/syscall.tbl~mm-introduce-external-memory-hinting-api +1 -0
@@ -409,3 +409,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
+440	common	process_madvise			sys_process_madvise
include/linux/syscalls.h renamed from include/linux/syscalls.h~mm-introduce-external-memory-hinting-api +2 -0
@@ -878,6 +878,8 @@ asmlinkage long sys_munlockall(void);
 asmlinkage long sys_mincore(unsigned long start, size_t len,
 				unsigned char __user * vec);
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
+asmlinkage long sys_process_madvise(int pidfd, unsigned long start,
+			size_t len, int behavior, unsigned long flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
 			unsigned long prot, unsigned long pgoff,
 			unsigned long flags);
include/uapi/asm-generic/unistd.h renamed from include/uapi/asm-generic/unistd.h~mm-introduce-external-memory-hinting-api +3 -1
@@ -857,9 +857,11 @@ __SYSCALL(__NR_openat2, sys_openat2)
 __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd)
 #define __NR_faccessat2 439
 __SYSCALL(__NR_faccessat2, sys_faccessat2)
+#define __NR_process_madvise 440
+__SYSCALL(__NR_process_madvise, sys_process_madvise)
 
 #undef __NR_syscalls
-#define __NR_syscalls 440
+#define __NR_syscalls 441
 
 /*
  * 32 bit systems traditionally used different
kernel/sys_ni.c renamed from kernel/sys_ni.c~mm-introduce-external-memory-hinting-api +1 -0
@@ -280,6 +280,7 @@ COND_SYSCALL(mlockall);
 COND_SYSCALL(munlockall);
 COND_SYSCALL(mincore);
 COND_SYSCALL(madvise);
+COND_SYSCALL(process_madvise);
 COND_SYSCALL(remap_file_pages);
 COND_SYSCALL(mbind);
 COND_SYSCALL_COMPAT(mbind);
mm/madvise.c renamed from mm/madvise.c~mm-introduce-external-memory-hinting-api +64 -0

85 lines not on this page · raw message

_

Patches currently in -mm which might be from [email protected] are

mm-check-fatal-signal-pending-of-target-process.patch pid-move-pidfd_get_pid-function-to-pidc.patch mm-support-both-pid-and-pidfd-for-process_madvise.patch mm-support-vector-address-ranges-for-process_madvise.patch mm-use-only-pidfd-for-process_madvise-syscall.patch

author maintainer tree notice
#31 lore raw

[to-be-updated] mm-check-fatal-signal-pending-of-target-process.patch removed from -mm tree

In reply to Andrew Morton
The patch titled Subject: mm/madvise: check fatal signal pending of target process has been removed from the -mm tree. Its filename was mm-check-fatal-signal-pending-of-target-process.patch

The patch titled
Subject: mm/madvise: check fatal signal pending of target process
has been removed from the -mm tree. Its filename was
mm-check-fatal-signal-pending-of-target-process.patch

This patch was dropped because an updated version will be merged

------------------------------------------------------
From: Minchan Kim <[email protected]>
Subject: mm/madvise: check fatal signal pending of target process

Bail out to prevent unnecessary CPU overhead if target process has pending fatal signal during (MADV_COLD|MADV_PAGEOUT) operation.

Cc Alexander Duyck <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc John Dias <[email protected]>
Cc Kirill Tkhai <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc SeongJae Park <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Tim Murray <[email protected]>
Cc Christian Brauner <[email protected]>

---

 mm/madvise.c |   29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
mm/madvise.c renamed from mm/madvise.c~mm-check-fatal-signal-pending-of-target-process +21 -8
@@ -39,6 +39,7 @@
 struct madvise_walk_private {
 	struct mmu_gather *tlb;
 	bool pageout;
+	struct task_struct *target_task;
 };
 
 /*
@@ -319,6 +320,10 @@ static int madvise_cold_or_pageout_pte_r
 	if (fatal_signal_pending(current))
 		return -EINTR;
 
+	if (private->target_task &&
+			fatal_signal_pending(private->target_task))
+		return -EINTR;
+
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 	if (pmd_trans_huge(*pmd)) {
 		pmd_t orig_pmd;
@@ -480,12 +485,14 @@ static const struct mm_walk_ops cold_wal
 };
 
 static void madvise_cold_page_range(struct mmu_gather *tlb,
+			     struct task_struct *task,
 			     struct vm_area_struct *vma,
 			     unsigned long addr, unsigned long end)
 {
 	struct madvise_walk_private walk_private = {
 		.pageout = false,
 		.tlb = tlb,
+		.target_task = task,
 	};
 
 	tlb_start_vma(tlb, vma);
@@ -493,7 +500,8 @@ static void madvise_cold_page_range(stru
 	tlb_end_vma(tlb, vma);
 }
 
-static long madvise_cold(struct vm_area_struct *vma,
+static long madvise_cold(struct task_struct *task,
+			struct vm_area_struct *vma,
 			struct vm_area_struct **prev,
 			unsigned long start_addr, unsigned long end_addr)
 {
@@ -506,19 +514,21 @@ static long madvise_cold(struct vm_area_
 
 	lru_add_drain();
 	tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
-	madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
+	madvise_cold_page_range(&tlb, task, vma, start_addr, end_addr);
 	tlb_finish_mmu(&tlb, start_addr, end_addr);
 
 	return 0;
 }
 
 static void madvise_pageout_page_range(struct mmu_gather *tlb,
+			     struct task_struct *task,
 			     struct vm_area_struct *vma,
 			     unsigned long addr, unsigned long end)
 {
 	struct madvise_walk_private walk_private = {
 		.pageout = true,
 		.tlb = tlb,
+		.target_task = task,
 	};
 
 	tlb_start_vma(tlb, vma);
@@ -542,7 +552,8 @@ static inline bool can_do_pageout(struct
 		inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0;
 }
 
-static long madvise_pageout(struct vm_area_struct *vma,
+static long madvise_pageout(struct task_struct *task,
+			struct vm_area_struct *vma,
 			struct vm_area_struct **prev,
 			unsigned long start_addr, unsigned long end_addr)
 {
@@ -558,7 +569,7 @@ static long madvise_pageout(struct vm_ar
 
 	lru_add_drain();
 	tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
-	madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
+	madvise_pageout_page_range(&tlb, task, vma, start_addr, end_addr);
 	tlb_finish_mmu(&tlb, start_addr, end_addr);
 
 	return 0;
@@ -938,7 +949,8 @@ static int madvise_inject_error(int beha
 #endif
 
 static long
-madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
+madvise_vma(struct task_struct *task, struct vm_area_struct *vma,
+		struct vm_area_struct **prev,
 		unsigned long start, unsigned long end, int behavior)
 {
 	switch (behavior) {
@@ -947,9 +959,9 @@ madvise_vma(struct vm_area_struct *vma,
 	case MADV_WILLNEED:
 		return madvise_willneed(vma, prev, start, end);
 	case MADV_COLD:
-		return madvise_cold(vma, prev, start, end);
+		return madvise_cold(task, vma, prev, start, end);
 	case MADV_PAGEOUT:
-		return madvise_pageout(vma, prev, start, end);
+		return madvise_pageout(task, vma, prev, start, end);
 	case MADV_FREE:
 	case MADV_DONTNEED:
 		return madvise_dontneed_free(vma, prev, start, end, behavior);
@@ -1166,7 +1178,8 @@ int do_madvise(struct task_struct *targe
 			tmp = end;
 
 		/* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
-		error = madvise_vma(vma, &prev, start, tmp, behavior);
+		error = madvise_vma(target_task, vma, &prev,
+					start, tmp, behavior);
 		if (error)
 			goto out;
 		start = tmp;

_

Patches currently in -mm which might be from [email protected] are

pid-move-pidfd_get_pid-function-to-pidc.patch mm-support-both-pid-and-pidfd-for-process_madvise.patch mm-support-vector-address-ranges-for-process_madvise.patch mm-use-only-pidfd-for-process_madvise-syscall.patch

author maintainer tree notice
#32 lore raw

[to-be-updated] pid-move-pidfd_get_pid-function-to-pidc.patch removed from -mm tree

In reply to Andrew Morton
The patch titled Subject: pid: move pidfd_get_pid() to pid.c has been removed from the -mm tree. Its filename was pid-move-pidfd_get_pid-function-to-pidc.patch

The patch titled
Subject: pid: move pidfd_get_pid() to pid.c
has been removed from the -mm tree. Its filename was
pid-move-pidfd_get_pid-function-to-pidc.patch

This patch was dropped because an updated version will be merged

------------------------------------------------------
From: Minchan Kim <[email protected]>
Subject: pid: move pidfd_get_pid() to pid.c

process_madvise syscall needs pidfd_get_pid function to translate pidfd to pid so this patch move the function to kernel/pid.c.

---

 include/linux/pid.h |    1 +
 kernel/exit.c       |   17 -----------------
 kernel/pid.c        |   17 +++++++++++++++++
 3 files changed, 18 insertions(+), 17 deletions(-)
include/linux/pid.h renamed from include/linux/pid.h~pid-move-pidfd_get_pid-function-to-pidc +1 -0
@@ -77,6 +77,7 @@ extern const struct file_operations pidf
 struct file;
 
 extern struct pid *pidfd_pid(const struct file *file);
+struct pid *pidfd_get_pid(unsigned int fd);
 
 static inline struct pid *get_pid(struct pid *pid)
 {
kernel/exit.c renamed from kernel/exit.c~pid-move-pidfd_get_pid-function-to-pidc +0 -17
@@ -1474,23 +1474,6 @@ end:
 	return retval;
 }
 
-static struct pid *pidfd_get_pid(unsigned int fd)
-{
-	struct fd f;
-	struct pid *pid;
-
-	f = fdget(fd);
-	if (!f.file)
-		return ERR_PTR(-EBADF);
-
-	pid = pidfd_pid(f.file);
-	if (!IS_ERR(pid))
-		get_pid(pid);
-
-	fdput(f);
-	return pid;
-}
-
 static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
 			  int options, struct rusage *ru)
 {
kernel/pid.c renamed from kernel/pid.c~pid-move-pidfd_get_pid-function-to-pidc +17 -0
@@ -518,6 +518,23 @@ struct pid *find_ge_pid(int nr, struct p
 	return idr_get_next(&ns->idr, &nr);
 }
 
+struct pid *pidfd_get_pid(unsigned int fd)
+{
+	struct fd f;
+	struct pid *pid;
+
+	f = fdget(fd);
+	if (!f.file)
+		return ERR_PTR(-EBADF);
+
+	pid = pidfd_pid(f.file);
+	if (!IS_ERR(pid))
+		get_pid(pid);
+
+	fdput(f);
+	return pid;
+}
+
 /**
  * pidfd_create() - Create a new pid file descriptor.
  *

_

Patches currently in -mm which might be from [email protected] are

mm-support-both-pid-and-pidfd-for-process_madvise.patch mm-support-vector-address-ranges-for-process_madvise.patch mm-use-only-pidfd-for-process_madvise-syscall.patch

author maintainer tree notice
#33 lore raw

[to-be-updated] mm-support-both-pid-and-pidfd-for-process_madvise.patch removed from -mm tree

In reply to Andrew Morton
The patch titled Subject: mm/madvise: support both pid and pidfd for process_madvise has been removed from the -mm tree. Its filename was mm-support-both-pid-and-pidfd-for-process_madvise.patch

The patch titled
Subject: mm/madvise: support both pid and pidfd for process_madvise
has been removed from the -mm tree. Its filename was
mm-support-both-pid-and-pidfd-for-process_madvise.patch

This patch was dropped because an updated version will be merged

------------------------------------------------------
From: Minchan Kim <[email protected]>
Subject: mm/madvise: support both pid and pidfd for process_madvise

There is a demand[1] to support pid as well pidfd for process_madvise to reduce unnecessary syscall to get pidfd if the user has control of the target process (ie, they could guarantee the process is not gone or pid is not reused).

This patch aims for supporting both options like waitid(2). So, the syscall is currently,

int process_madvise(idtype_t idtype, id_t id, void *addr,
size_t length, int advice, unsigned long flags);

@which is actually idtype_t for userspace library and currently, it supports P_PID and P_PIDFD.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Cc Christian Brauner <[email protected]>
Cc Alexander Duyck <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc John Dias <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc SeongJae Park <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Tim Murray <[email protected]>
Cc Christian Brauner <[email protected]>

---

 include/linux/syscalls.h |    3 ++-
 mm/madvise.c             |   36 +++++++++++++++++++++++-------------
 2 files changed, 25 insertions(+), 14 deletions(-)
include/linux/syscalls.h renamed from include/linux/syscalls.h~mm-support-both-pid-and-pidfd-for-process_madvise +2 -1
@@ -878,7 +878,8 @@ asmlinkage long sys_munlockall(void);
 asmlinkage long sys_mincore(unsigned long start, size_t len,
 				unsigned char __user * vec);
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
-asmlinkage long sys_process_madvise(int pidfd, unsigned long start,
+
+asmlinkage long sys_process_madvise(int which, pid_t pid, unsigned long start,
 			size_t len, int behavior, unsigned long flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
 			unsigned long prot, unsigned long pgoff,
mm/madvise.c renamed from mm/madvise.c~mm-support-both-pid-and-pidfd-for-process_madvise +23 -13
@@ -1208,11 +1208,10 @@ SYSCALL_DEFINE3(madvise, unsigned long,
 	return do_madvise(current, current->mm, start, len_in, behavior);
 }
 
-SYSCALL_DEFINE5(process_madvise, int, pidfd, unsigned long, start,
+SYSCALL_DEFINE6(process_madvise, int, which, pid_t, upid, unsigned long, start,
 		size_t, len_in, int, behavior, unsigned long, flags)
 {
 	int ret;
-	struct fd f;
 	struct pid *pid;
 	struct task_struct *task;
 	struct mm_struct *mm;
@@ -1223,20 +1222,31 @@ SYSCALL_DEFINE5(process_madvise, int, pi
 	if (!process_madvise_behavior_valid(behavior))
 		return -EINVAL;
 
-	f = fdget(pidfd);
-	if (!f.file)
-		return -EBADF;
-
-	pid = pidfd_pid(f.file);
-	if (IS_ERR(pid)) {
-		ret = PTR_ERR(pid);
-		goto fdput;
+	switch (which) {
+	case P_PID:
+		if (upid <= 0)
+			return -EINVAL;
+
+		pid = find_get_pid(upid);
+		if (!pid)
+			return -ESRCH;
+		break;
+	case P_PIDFD:
+		if (upid < 0)
+			return -EINVAL;
+
+		pid = pidfd_get_pid(upid);
+		if (IS_ERR(pid))
+			return PTR_ERR(pid);
+		break;
+	default:
+		return -EINVAL;
 	}
 
 	task = get_pid_task(pid, PIDTYPE_PID);
 	if (!task) {
 		ret = -ESRCH;
-		goto fdput;
+		goto put_pid;
 	}
 
 	mm = mm_access(task, PTRACE_MODE_ATTACH_FSCREDS);
@@ -1249,7 +1259,7 @@ SYSCALL_DEFINE5(process_madvise, int, pi
 	mmput(mm);
 release_task:
 	put_task_struct(task);
-fdput:
-	fdput(f);
+put_pid:
+	put_pid(pid);
 	return ret;
 }

_

Patches currently in -mm which might be from [email protected] are

mm-support-vector-address-ranges-for-process_madvise.patch mm-use-only-pidfd-for-process_madvise-syscall.patch

author maintainer tree notice
#34 lore raw

[to-be-updated] mm-madvise-allow-ksm-hints-for-remote-api.patch removed from -mm tree

In reply to Andrew Morton
The patch titled Subject: mm/madvise: allow KSM hints for remote API has been removed from the -mm tree. Its filename was mm-madvise-allow-ksm-hints-for-remote-api.patch

The patch titled
Subject: mm/madvise: allow KSM hints for remote API
has been removed from the -mm tree. Its filename was
mm-madvise-allow-ksm-hints-for-remote-api.patch

This patch was dropped because an updated version will be merged

------------------------------------------------------
From: Oleksandr Natalenko <[email protected]>
Subject: mm/madvise: allow KSM hints for remote API

It all began with the fact that KSM works only on memory that is marked by madvise(). And the only way to get around that is to either:

* use LD_PRELOAD; or
* patch the kernel with something like UKSM or PKSM.

(i skip ptrace can of worms here intentionally)

To overcome this restriction, lets employ a new remote madvise API. This can be used by some small userspace helper daemon that will do auto-KSM job for us.

I think of two major consumers of remote KSM hints:

* hosts, that run containers, especially similar ones and especially in

    a trusted environment, sharing the same runtime like Node.js;

* heavy applications, that can be run in multiple instances, not limited to opensource ones like Firefox, but also those that cannot be modified since they are binary-only and, maybe, statically linked.

Speaking of statistics, more numbers can be found in the very first submission, that is related to this one [1]. For my current setup with two Firefox instances I get 100 to 200 MiB saved for the second instance depending on the amount of tabs.

1 FF instance with 15 tabs:

   $ echo "$(cat /sys/kernel/mm/ksm/pages_sharing) * 4 / 1024" | bc
   410

2 FF instances, second one has 12 tabs (all the tabs are different):

   $ echo "$(cat /sys/kernel/mm/ksm/pages_sharing) * 4 / 1024" | bc
   592

At the very moment I do not have specific numbers for containerised workload, but those should be comparable in case the containers share similar/same runtime.

[1] https://lore.kernel.org/patchwork/patch/1012142/

Cc Alexander Duyck <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc John Dias <[email protected]>
Cc Kirill Tkhai <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tim Murray <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Christian Brauner <[email protected]>

---

 mm/madvise.c |    4 ++++
 1 file changed, 4 insertions(+)
mm/madvise.c renamed from mm/madvise.c~mm-madvise-allow-ksm-hints-for-remote-api +4 -0
@@ -1014,6 +1014,10 @@ process_madvise_behavior_valid(int behav
 	switch (behavior) {
 	case MADV_COLD:
 	case MADV_PAGEOUT:
+#ifdef CONFIG_KSM
+	case MADV_MERGEABLE:
+	case MADV_UNMERGEABLE:
+#endif
 		return true;
 	default:
 		return false;

_

Patches currently in -mm which might be from [email protected] are

author maintainer tree notice
#35 lore raw

[to-be-updated] mm-support-vector-address-ranges-for-process_madvise.patch removed from -mm tree

In reply to Andrew Morton
The patch titled Subject: mm: support vector address ranges for process_madvise has been removed from the -mm tree. Its filename was mm-support-vector-address-ranges-for-process_madvise.patch

The patch titled
Subject: mm: support vector address ranges for process_madvise
has been removed from the -mm tree. Its filename was
mm-support-vector-address-ranges-for-process_madvise.patch

This patch was dropped because an updated version will be merged

------------------------------------------------------
From: Minchan Kim <[email protected]>
Subject: mm: support vector address ranges for process_madvise

This patch changes process_madvise interface:

a) support vector address ranges in a system call
b) support the vector address ranges to local process as well as external process
c) remove pid but keep only pidfd in argument - [1][2]
d) change type of flags with unsgined int

Android app has thousands of vmas due to zygote so it's totally waste of CPU and power if we should call the syscall one by one for each vma. (With testing 2000-vma syscall vs 1-vector syscall, it showed 15% performance improvement. I think it would be bigger in real practice because the testing ran very cache friendly environment).

Another potential use case for the vector range is to amortize the cost of TLB shootdowns for multiple ranges when using MADV_DONTNEED; this could benefit users like TCP receive zerocopy and malloc implementations. In future, we could find more usecases for other advises so let's make it happens as API since we introduce a new syscall at this moment. With that, existing madvise(2) user could replace it with process_madvise(2) with their own pid if they want to have batch address ranges support feature.

So finally, the API is as follows,

ssize_t process_madvise(int pidfd, const struct iovec *iovec,
unsigned long vlen, int advice, unsigned int flags);

DESCRIPTION
The process_madvise() system call is used to give advice or directions to the kernel about the address ranges from external process as well as local process. It provides the advice to address ranges of process described by iovec and vlen. The goal of such advice is to improve system or application performance.

The pidfd selects the process referred to by the PID file descriptor specified in pidfd. (See pidofd_open(2) for further information)

The pointer iovec points to an array of iovec structures, defined in <sys/uio.h> as:

struct iovec {
void *iov_base; /* starting address */
size_t iov_len; /* number of bytes to be advised */
};

The iovec describes address ranges beginning at address(iov_base) and with size length of bytes(iov_len).

The vlen represents the number of elements in iovec.

The advice is indicated in the advice argument, which is one of the following at this moment if the target process specified by pidfd is external.

MADV_COLD
MADV_PAGEOUT
MADV_MERGEABLE
MADV_UNMERGEABLE

Permission to provide a hint to external process is governed by a ptrace access mode PTRACE_MODE_ATTACH_FSCREDS check; see ptrace(2).

The process_madvise supports every advice madvise(2) has if target process is in same thread group with calling process so user could use process_madvise(2) to extend existing madvise(2) to support vector address ranges.

RETURN VALUE
On success, process_madvise() returns the number of bytes advised. This return value may be less than the total number of requested bytes, if an error occurred. The caller should check return value to determine whether a partial advice occurred.

[1] https://lore.kernel.org/linux-mm/20200509124817.xmrvsrq3mla6b76k@wittgenstein/
[2] https://lore.kernel.org/linux-mm/[email protected]/

[[email protected]: support compat_sys_process_madvise]
[[email protected]: fix process_madvise prototype]
[[email protected]: make do_process_madvise() static]
[[email protected]: fix s390 compat build error]
[[email protected]: add compat_sys_process_madvise to mips syscall table]
Cc David Rientjes <[email protected]>
Cc Arjun Roy <[email protected]>
Cc Tim Murray <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc John Dias <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Christian Brauner <[email protected]>

---

 arch/arm64/include/asm/unistd32.h         |    2 
 arch/mips/kernel/syscalls/syscall_n32.tbl |    2 
 arch/mips/kernel/syscalls/syscall_o32.tbl |    2 
 arch/parisc/kernel/syscalls/syscall.tbl   |    2 
 arch/powerpc/kernel/syscalls/syscall.tbl  |    2 
 arch/s390/kernel/syscalls/syscall.tbl     |    2 
 arch/sparc/kernel/syscalls/syscall.tbl    |    2 
 arch/x86/entry/syscalls/syscall_32.tbl    |    2 
 arch/x86/entry/syscalls/syscall_64.tbl    |    4 -
 include/linux/compat.h                    |    4 +
 include/linux/syscalls.h                  |    6 -
 include/uapi/asm-generic/unistd.h         |    3 
 kernel/sys_ni.c                           |    1 
 mm/madvise.c                              |   80 ++++++++++++++++++--
 14 files changed, 93 insertions(+), 21 deletions(-)
arch/arm64/include/asm/unistd32.h renamed from arch/arm64/include/asm/unistd32.h~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -886,7 +886,7 @@ __SYSCALL(__NR_pidfd_getfd, sys_pidfd_ge
 #define __NR_faccessat2 439
 __SYSCALL(__NR_faccessat2, sys_faccessat2)
 #define __NR_process_madvise 440
-__SYSCALL(__NR_process_madvise, sys_process_madvise)
+__SYSCALL(__NR_process_madvise, compat_sys_process_madvise)
 
 /*
  * Please add new compat syscalls above this comment and update
arch/mips/kernel/syscalls/syscall_n32.tbl renamed from arch/mips/kernel/syscalls/syscall_n32.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -377,4 +377,4 @@
 437	n32	openat2				sys_openat2
 438	n32	pidfd_getfd			sys_pidfd_getfd
 439	n32	faccessat2			sys_faccessat2
-440	n32	process_madvise			sys_process_madvise
+440	n32	process_madvise			compat_sys_process_madvise
arch/mips/kernel/syscalls/syscall_o32.tbl renamed from arch/mips/kernel/syscalls/syscall_o32.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -426,4 +426,4 @@
 437	o32	openat2				sys_openat2
 438	o32	pidfd_getfd			sys_pidfd_getfd
 439	o32	faccessat2			sys_faccessat2
-440	o32	process_madvise			sys_process_madvise
+440	o32	process_madvise			sys_process_madvise		compat_sys_process_madvise
arch/parisc/kernel/syscalls/syscall.tbl renamed from arch/parisc/kernel/syscalls/syscall.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -436,4 +436,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
-440	common	process_madvise			sys_process_madvise
+440	common	process_madvise			sys_process_madvise		compat_sys_process_madvise
arch/powerpc/kernel/syscalls/syscall.tbl renamed from arch/powerpc/kernel/syscalls/syscall.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -528,4 +528,4 @@
 437	common	openat2				sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
-440	common	process_madvise			sys_process_madvise
+440	common	process_madvise			sys_process_madvise		compat_sys_process_madvise
arch/s390/kernel/syscalls/syscall.tbl renamed from arch/s390/kernel/syscalls/syscall.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -441,4 +441,4 @@
 437  common	openat2			sys_openat2			sys_openat2
 438  common	pidfd_getfd		sys_pidfd_getfd			sys_pidfd_getfd
 439  common	faccessat2		sys_faccessat2			sys_faccessat2
-440  common	process_madvise		sys_process_madvise		sys_process_madvise
+440  common	process_madvise		sys_process_madvise		compat_sys_process_madvise
arch/sparc/kernel/syscalls/syscall.tbl renamed from arch/sparc/kernel/syscalls/syscall.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -484,4 +484,4 @@
 437	common	openat2			sys_openat2
 438	common	pidfd_getfd			sys_pidfd_getfd
 439	common	faccessat2			sys_faccessat2
-440	common	process_madvise			sys_process_madvise
+440	common	process_madvise			sys_process_madvise		compat_sys_process_madvise
arch/x86/entry/syscalls/syscall_32.tbl renamed from arch/x86/entry/syscalls/syscall_32.tbl~mm-support-vector-address-ranges-for-process_madvise +1 -1
@@ -443,4 +443,4 @@
 437	i386	openat2			sys_openat2
 438	i386	pidfd_getfd		sys_pidfd_getfd
 439	i386	faccessat2		sys_faccessat2
-440	i386	process_madvise		sys_process_madvise
+440	i386	process_madvise		sys_process_madvise		compat_sys_process_madvise
arch/x86/entry/syscalls/syscall_64.tbl renamed from arch/x86/entry/syscalls/syscall_64.tbl~mm-support-vector-address-ranges-for-process_madvise +2 -2
@@ -360,8 +360,7 @@
 437	common	openat2			sys_openat2
 438	common	pidfd_getfd		sys_pidfd_getfd
 439	common	faccessat2		sys_faccessat2
-440	common	process_madvise		sys_process_madvise
-
+440	64	process_madvise		sys_process_madvise
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
 # for native 64-bit operation. The __x32_compat_sys stubs are created
@@ -404,3 +403,4 @@
 545	x32	execveat		compat_sys_execveat
 546	x32	preadv2			compat_sys_preadv64v2
 547	x32	pwritev2		compat_sys_pwritev64v2
+548	x32	process_madvise		compat_sys_process_madvise
include/linux/compat.h renamed from include/linux/compat.h~mm-support-vector-address-ranges-for-process_madvise +4 -0
@@ -827,6 +827,10 @@ asmlinkage long compat_sys_pwritev64v2(u
 		unsigned long vlen, loff_t pos, rwf_t flags);
 #endif
 
+asmlinkage ssize_t compat_sys_process_madvise(compat_int_t which,
+		compat_pid_t upid, const struct compat_iovec __user *vec,
+		compat_ulong_t vlen, compat_int_t behavior,
+		compat_ulong_t flags);
 
 /*
  * Deprecated system calls which are still defined in
include/linux/syscalls.h renamed from include/linux/syscalls.h~mm-support-vector-address-ranges-for-process_madvise +3 -3
@@ -878,9 +878,9 @@ asmlinkage long sys_munlockall(void);
 asmlinkage long sys_mincore(unsigned long start, size_t len,
 				unsigned char __user * vec);
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
-
-asmlinkage long sys_process_madvise(int which, pid_t pid, unsigned long start,
-			size_t len, int behavior, unsigned long flags);
+asmlinkage long sys_process_madvise(int which, pid_t upid,
+		const struct iovec __user *vec, unsigned long vlen,
+		int behavior, unsigned long flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
 			unsigned long prot, unsigned long pgoff,
 			unsigned long flags);
include/uapi/asm-generic/unistd.h renamed from include/uapi/asm-generic/unistd.h~mm-support-vector-address-ranges-for-process_madvise +2 -1
@@ -858,7 +858,8 @@ __SYSCALL(__NR_pidfd_getfd, sys_pidfd_ge
 #define __NR_faccessat2 439
 __SYSCALL(__NR_faccessat2, sys_faccessat2)
 #define __NR_process_madvise 440
-__SYSCALL(__NR_process_madvise, sys_process_madvise)
+__SC_COMP(__NR_process_madvise, sys_process_madvise, \
+		compat_sys_process_madvise)
 
 #undef __NR_syscalls
 #define __NR_syscalls 441
kernel/sys_ni.c renamed from kernel/sys_ni.c~mm-support-vector-address-ranges-for-process_madvise +1 -0
@@ -281,6 +281,7 @@ COND_SYSCALL(munlockall);
 COND_SYSCALL(mincore);
 COND_SYSCALL(madvise);
 COND_SYSCALL(process_madvise);
+COND_SYSCALL_COMPAT(process_madvise);
 COND_SYSCALL(remap_file_pages);
 COND_SYSCALL(mbind);
 COND_SYSCALL_COMPAT(mbind);
mm/madvise.c renamed from mm/madvise.c~mm-support-vector-address-ranges-for-process_madvise +73 -7

109 lines not on this page · raw message

_

Patches currently in -mm which might be from [email protected] are

mm-use-only-pidfd-for-process_madvise-syscall.patch

author maintainer tree notice
#36 lore raw

[to-be-updated] mm-use-only-pidfd-for-process_madvise-syscall.patch removed from -mm tree

In reply to Andrew Morton
The patch titled Subject: mm: use only pidfd for process_madvise syscall has been removed from the -mm tree. Its filename was mm-use-only-pidfd-for-process_madvise-syscall.patch

The patch titled
Subject: mm: use only pidfd for process_madvise syscall
has been removed from the -mm tree. Its filename was
mm-use-only-pidfd-for-process_madvise-syscall.patch

This patch was dropped because an updated version will be merged

------------------------------------------------------
From: Minchan Kim <[email protected]>
Subject: mm: use only pidfd for process_madvise syscall

Based on discussion[1], people didn't feel we need to support both pid and pidfd for every new coming API[2] so this patch keeps only pidfd. This patch also changes flags's type with "unsigned int".

[1] https://lore.kernel.org/linux-mm/20200509124817.xmrvsrq3mla6b76k@wittgenstein/
[2] https://lore.kernel.org/linux-mm/[email protected]/

[[email protected]: return EBADF if pidfd is invalid]
Cc David Rientjes <[email protected]>
Cc Arjun Roy <[email protected]>
Cc Tim Murray <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Sonny Rao <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc John Dias <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Sandeep Patil <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Christian Brauner <[email protected]>

---

 include/linux/compat.h   |    6 ++---
 include/linux/syscalls.h |    5 +---
 mm/madvise.c             |   41 +++++++++----------------------------
 3 files changed, 16 insertions(+), 36 deletions(-)
include/linux/compat.h renamed from include/linux/compat.h~mm-use-only-pidfd-for-process_madvise-syscall +3 -3
@@ -827,10 +827,10 @@ asmlinkage long compat_sys_pwritev64v2(u
 		unsigned long vlen, loff_t pos, rwf_t flags);
 #endif
 
-asmlinkage ssize_t compat_sys_process_madvise(compat_int_t which,
-		compat_pid_t upid, const struct compat_iovec __user *vec,
+asmlinkage ssize_t compat_sys_process_madvise(compat_int_t pidfd,
+		const struct compat_iovec __user *vec,
 		compat_ulong_t vlen, compat_int_t behavior,
-		compat_ulong_t flags);
+		compat_int_t flags);
 
 /*
  * Deprecated system calls which are still defined in
include/linux/syscalls.h renamed from include/linux/syscalls.h~mm-use-only-pidfd-for-process_madvise-syscall +2 -3
@@ -878,9 +878,8 @@ asmlinkage long sys_munlockall(void);
 asmlinkage long sys_mincore(unsigned long start, size_t len,
 				unsigned char __user * vec);
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
-asmlinkage long sys_process_madvise(int which, pid_t upid,
-		const struct iovec __user *vec, unsigned long vlen,
-		int behavior, unsigned long flags);
+asmlinkage long sys_process_madvise(int pidfd, const struct iovec __user *vec,
+		unsigned long vlen, int behavior, unsigned int flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
 			unsigned long prot, unsigned long pgoff,
 			unsigned long flags);
mm/madvise.c renamed from mm/madvise.c~mm-use-only-pidfd-for-process_madvise-syscall +11 -30

80 lines not on this page · raw message

_

Patches currently in -mm which might be from [email protected] are