From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0927C4338F for ; Mon, 23 Aug 2021 12:07:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5E81C61184 for ; Mon, 23 Aug 2021 12:07:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 5E81C61184 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id B0BED8D0001; Mon, 23 Aug 2021 08:07:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A95646B0072; Mon, 23 Aug 2021 08:07:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 935BE8D0001; Mon, 23 Aug 2021 08:07:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0059.hostedemail.com [216.40.44.59]) by kanga.kvack.org (Postfix) with ESMTP id 722BC6B006C for ; Mon, 23 Aug 2021 08:07:03 -0400 (EDT) Received: from smtpin34.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id F1F0321243 for ; Mon, 23 Aug 2021 12:07:02 +0000 (UTC) X-FDA: 78506219484.34.5B96F81 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 6AE5CB00009D for ; Mon, 23 Aug 2021 12:07:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1629720422; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=iPSPC+NwQeFpMWMRRBx4FR2HEbNT0VSAkYeTuMe/Ulg=; b=c51XJqImrskQODodRaFz+QVL5mFvLHIph7SCaTDHuZfU9tgUNHf34F9rkvrbtYMVJ+evgy vcUb2fGzm1TZHzBr1eMQVPLjvYJJo+Ekmfpeyz6V8UlZMH72aP5W7JAsNLXwoRtO4wu39p a18zDC7YwAlaxbH1zcl3e4HjwVlPYkM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-420-nS38xyqLML2fy8_STjSe1g-1; Mon, 23 Aug 2021 08:06:57 -0400 X-MC-Unique: nS38xyqLML2fy8_STjSe1g-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3714E801A92; Mon, 23 Aug 2021 12:06:53 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.57]) by smtp.corp.redhat.com (Postfix) with ESMTP id DDFFA189C7; Mon, 23 Aug 2021 12:06:46 +0000 (UTC) From: David Hildenbrand To: linux-man@vger.kernel.org Cc: David Hildenbrand , Pankaj Gupta , Alejandro Colomar , Michael Kerrisk , Andrew Morton , Michal Hocko , Oscar Salvador , Jann Horn , Mike Rapoport , Linux API , linux-mm@kvack.org Subject: [PATCH v3] madvise.2: Document MADV_POPULATE_READ and MADV_POPULATE_WRITE Date: Mon, 23 Aug 2021 14:06:45 +0200 Message-Id: <20210823120645.8223-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=c51XJqIm; spf=none (imf24.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 6AE5CB00009D X-Stat-Signature: 3szgt1ancx3wcaqejhwc9xf375ch3zft X-HE-Tag: 1629720422-578757 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: MADV_POPULATE_READ and MADV_POPULATE_WRITE have been merged into upstream Linux via commit 4ca9b3859dac ("mm/madvise: introduce MADV_POPULATE_(READ|WRITE) to prefault page tables"), part of v5.14-rc1. Further, commit eb2faa513c24 ("mm/madvise: report SIGBUS as -EFAULT for MADV_POPULATE_(READ|WRITE)"), part of v5.14-rc6, made sure that SIGBUS is converted to -EFAULT instead of -EINVAL. Let's document the behavior and error conditions of these new madvise() options. Acked-by: Pankaj Gupta Cc: Alejandro Colomar Cc: Michael Kerrisk Cc: Andrew Morton Cc: Michal Hocko Cc: Oscar Salvador Cc: Jann Horn Cc: Mike Rapoport Cc: Linux API Cc: linux-mm@kvack.org Signed-off-by: David Hildenbrand --- v2 -> v3: - Refine what "populating readable/writable" means - Compare each version with MAP_POPULATE and give an example use case - Reword SIGBUS handling - Reword comment regarding special mappings and also add memfd_secret(2) - Reference MADV_HWPOISON when talking about HW poisoned pages - Minor cosmetic fixes v1 -> v2: - Use semantic newlines in all cases - Add two missing " - Document -EFAULT handling - Rephrase some parts to make it more generic: VM_PFNMAP and VM_IO are on= ly examples for special mappings --- man2/madvise.2 | 156 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/man2/madvise.2 b/man2/madvise.2 index f1f384c0c..37f6dd6fa 100644 --- a/man2/madvise.2 +++ b/man2/madvise.2 @@ -469,6 +469,106 @@ If a page is file-backed and dirty, it will be writ= ten back to the backing storage. The advice might be ignored for some pages in the range when it is not applicable. +.TP +.BR MADV_POPULATE_READ " (since Linux 5.14)" +"Populate (prefault) page tables readable, +faulting in all pages in the range just as if manually reading from each= page; +however, +avoid the actual memory access that would have been performed after hand= ling +the fault. +.IP +In contrast to +.BR MAP_POPULATE , +.B MADV_POPULATE_READ +does not hide errors, +can be applied to (parts of) existing mappings and will always populate +(prefault) page tables readable. +One example use case is prefaulting a file mapping, +reading all file content from disk; +however, +pages won't be dirtied and consequently won't have to be written back to= disk +when evicting the pages from memory. +.IP +Depending on the underlying mapping, +map the shared zeropage, +preallocate memory or read the underlying file; +files with holes might or might not preallocate blocks. +If populating fails, +a +.B SIGBUS +signal is not generated; instead, an error is returned. +.IP +If +.B MADV_POPULATE_READ +succeeds, +all page tables have been populated (prefaulted) readable once. +If +.B MADV_POPULATE_READ +fails, +some page tables might have been populated. +.IP +.B MADV_POPULATE_READ +cannot be applied to mappings without read permissions +and special mappings, +for example, +mappings marked with kernel-internal flags such as +.B VM_PFNMAP +or +.BR VM_IO , +or secret memory regions created using +.BR memfd_secret(2) . +.IP +Note that with +.BR MADV_POPULATE_READ , +the process can be killed at any moment when the system runs out of memo= ry. +.TP +.BR MADV_POPULATE_WRITE " (since Linux 5.14)" +Populate (prefault) page tables writable, +faulting in all pages in the range just as if manually writing to each +each page; +however, +avoid the actual memory access that would have been performed after hand= ling +the fault. +.IP +In contrast to +.BR MAP_POPULATE , +MADV_POPULATE_WRITE does not hide errors, +can be applied to (parts of) existing mappings and will always populate +(prefault) page tables writable. +One example use case is preallocating memory, +breaking any CoW (Copy on Write). +.IP +Depending on the underlying mapping, +preallocate memory or read the underlying file; +files with holes will preallocate blocks. +If populating fails, +a +.B SIGBUS +signal is not generated; instead, an error is returned. +.IP +If +.B MADV_POPULATE_WRITE +succeeds, +all page tables have been populated (prefaulted) writable once. +If +.B MADV_POPULATE_WRITE +fails, +some page tables might have been populated. +.IP +.B MADV_POPULATE_WRITE +cannot be applied to mappings without write permissions +and special mappings, +for example, +mappings marked with kernel-internal flags such as +.B VM_PFNMAP +or +.BR VM_IO , +or secret memory regions created using +.BR memfd_secret(2) . +.IP +Note that with +.BR MADV_POPULATE_WRITE , +the process can be killed at any moment when the system runs out of memo= ry. .SH RETURN VALUE On success, .BR madvise () @@ -490,6 +590,22 @@ A kernel resource was temporarily unavailable. .B EBADF The map exists, but the area maps something that isn't a file. .TP +.B EFAULT +.I advice +is +.B MADV_POPULATE_READ +or +.BR MADV_POPULATE_WRITE , +and populating (prefaulting) page tables failed because a +.B SIGBUS +would have been generated on actual memory access and the reason is not = a +HW poisoned page +(HW poisoned pages can, +for example, +be created using the +.B MADV_HWPOISON +flag described elsewhere in this page). +.TP .B EINVAL .I addr is not page-aligned or @@ -533,6 +649,22 @@ or .BR VM_PFNMAP ranges. .TP +.B EINVAL +.I advice +is +.B MADV_POPULATE_READ +or +.BR MADV_POPULATE_WRITE , +but the specified address range includes ranges with insufficient permis= sions +or special mappings, +for example, +mappings marked with kernel-internal flags such a +.B VM_IO +or +.BR VM_PFNMAP , +or secret memory regions created using +.BR memfd_secret(2) . +.TP .B EIO (for .BR MADV_WILLNEED ) @@ -548,6 +680,15 @@ Not enough memory: paging in failed. Addresses in the specified range are not currently mapped, or are outside the address space of the process. .TP +.B ENOMEM +.I advice +is +.B MADV_POPULATE_READ +or +.BR MADV_POPULATE_WRITE , +and populating (prefaulting) page tables failed because there was not en= ough +memory. +.TP .B EPERM .I advice is @@ -555,6 +696,20 @@ is but the caller does not have the .B CAP_SYS_ADMIN capability. +.TP +.B EHWPOISON +.I advice +is +.B MADV_POPULATE_READ +or +.BR MADV_POPULATE_WRITE , +and populating (prefaulting) page tables failed because a HW poisoned pa= ge +(HW poisoned pages can, +for example, +be created using the +.B MADV_HWPOISON +flag described elsewhere in this page) +was encountered. .SH VERSIONS Since Linux 3.18, .\" commit d3ac21cacc24790eb45d735769f35753f5b56ceb @@ -602,6 +757,7 @@ from the system call, as it should). .\" function first appeared in 4.4BSD. .SH SEE ALSO .BR getrlimit (2), +.BR memfd_secret(2), .BR mincore (2), .BR mmap (2), .BR mprotect (2), --=20 2.31.1