Received: by 10.192.165.156 with SMTP id m28csp2106627imm; Thu, 12 Apr 2018 08:43:18 -0700 (PDT) X-Google-Smtp-Source: AIpwx49KZEpuDgHuk3fB5m+FsCKKBoizRr7Ymakg0ruDxvxi2CIbnsI/sZlQSUnqJl3FPB9m+NgL X-Received: by 10.98.157.7 with SMTP id i7mr7947693pfd.85.1523547798169; Thu, 12 Apr 2018 08:43:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523547798; cv=none; d=google.com; s=arc-20160816; b=cM0dQ67VCkTVj45CrTfq0zHmBIKcwFF52ctpW1VzNBwzBVL15cRqRzcP6yw0lHhc59 eVx+wGISp2yhs2wNM6KRZcz4wrX52GqqkvFxpJfyW1PTcgbAde7LBjzQlDegocufBgn+ jCKnjdiHDjqX8dhTxMZizQFofjcXSHpdUUHsYd4CT0uqbAtHp0f4nUwOL69ndnMR1TT2 KXLq0MeMCAtaX/IjcdpU6Oguv/BJSLt+mLIs4wSt6KdUPMSIYhzwyJGU1wCmw/VYP3kS bt7JnXmU+PJ7yb4z92goqUYHJoTUgAvzyy77Agw6KHh8HSgOkf/edbEDJZH9AThTcbUz 3AFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:message-id:date :mime-version:dkim-signature:arc-authentication-results; bh=utS+oijFK853nm+zGHWcaoN6OTbausQ6IlGvJYCLEC4=; b=E+IK6QPlkrQXlbzDCjQtexC/5DMYZCOFfBm32TLM5YXymO0yQIEQK5Wz4GsPwxjxNi SoL2Hq4N4G+bU+xmu2NBOxQ/0zsIK1acwaEEQxe1yH1lX2PPvFTzcl2FBl87/ZD6VC3S Ny+CVSoo5T+MggE2rSCV9WsB7Q+gD/0tBR7RThSUA4A77t8c3vShairF0Icxu4NP1qS/ x0u7huUK8fqXc7JEeTsy+2E/9wqKObq9nYFRQmqZcx7jllsELXdCHh++rS4o++9kEMCa I1xQyfcWWqpSMAxaWPeuHE3zQDRg1p1OdVdLA9/5WsaS9TL+E4Vv5Qbmj24KG3jehODY NXsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=PqTD6RBf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j8-v6si3483006pli.9.2018.04.12.08.42.41; Thu, 12 Apr 2018 08:43:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=PqTD6RBf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753239AbeDLPjy (ORCPT + 99 others); Thu, 12 Apr 2018 11:39:54 -0400 Received: from mail-qt0-f201.google.com ([209.85.216.201]:46964 "EHLO mail-qt0-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753040AbeDLPjv (ORCPT ); Thu, 12 Apr 2018 11:39:51 -0400 Received: by mail-qt0-f201.google.com with SMTP id n5so4039146qtl.13 for ; Thu, 12 Apr 2018 08:39:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:date:message-id:subject:from:to:cc; bh=utS+oijFK853nm+zGHWcaoN6OTbausQ6IlGvJYCLEC4=; b=PqTD6RBf7ROXxmThyhrazC8rFlQbovKnRUNStuIoHfgW+xEeqzvexOWG8XkZydGd74 ysDFdy1TWcr2HDccuG071gR3MueUB70eSGol9Tv+WRkJcY34ow5dSxKR/WxAJpOd8NY1 WmkcWgV1aw3wu/4wAEj63vUVDNZPo1dufhPkBKzB8zgWXwaDPJMU9SlDlJt7GIAtf38Y qHtCkyt7mAif8bEYyIJQqPIx24PjelhgNqIFs/0dZCgdLjHuO7YGTcNJd6wUAFO2O/Ta nJ+kM1/fDoY2wNRfqei5VyMk89bB17Xr2PYEa2c/79GjavgsSPbq19tm7DMQiswqvUIA 8gKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:date:message-id:subject:from:to:cc; bh=utS+oijFK853nm+zGHWcaoN6OTbausQ6IlGvJYCLEC4=; b=JjJ+2/HSsXsiBEAUnxejXsrp2aPaQqVL71rKfas8QkEzMBcSsAmMPclqwxLOt6F6TS CubPcjfxKgIpFsdn6NzrzuM/2gJKjbrlmFG5gX4boCkF+avjnvMWi4Vrah0O14j2adJG aDuV1GnWcJDmemGhBcG/3HhU3jJMfCodmKGD7ZDUbq3dsEtjVaKxZQlykOAFTTwUoI9c AFcdRmflU1hHA26zbY6ooWnCM4M8BKCoO+RedXTm1Ou4QgxRrsBeTiHbX/nkWmvHyZTg SUfPKzBiQli+y8z86f/v2mNdg92VT6NlVpK2Hx5clLilpWTBq1N4VSrlbnxF/COMLebL Xjhg== X-Gm-Message-State: ALQs6tD69Bc2+NgIYc3IjRrbyLI0fDx6pyaStGiXnQdmu1fi9Xe/ewmx FIBAi0Z6kWXAm/gTwOJLdVYlWRFgiQ== MIME-Version: 1.0 X-Received: by 10.55.124.131 with SMTP id x125mr971136qkc.22.1523547590486; Thu, 12 Apr 2018 08:39:50 -0700 (PDT) Date: Thu, 12 Apr 2018 17:39:41 +0200 Message-Id: <20180412153941.170849-1-jannh@google.com> X-Mailer: git-send-email 2.17.0.484.g0c8726318c-goog Subject: [PATCH] mmap.2: MAP_FIXED is okay if the address range has been reserved From: Jann Horn To: mtk.manpages@gmail.com, linux-man@vger.kernel.org, mhocko@kernel.org, jhubbard@nvidia.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, jannh@google.com Cc: linux-man@vger.kernel.org, Michal Hocko , John Hubbard , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Clarify that MAP_FIXED is appropriate if the specified address range has been reserved using an existing mapping, but shouldn't be used otherwise. Signed-off-by: Jann Horn --- man2/mmap.2 | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/man2/mmap.2 b/man2/mmap.2 index bef8b4432..80c9ec285 100644 --- a/man2/mmap.2 +++ b/man2/mmap.2 @@ -253,8 +253,9 @@ Software that aspires to be portable should use this option with care, keeping in mind that the exact layout of a process's memory mappings is allowed to change significantly between kernel versions, C library versions, and operating system releases. -Furthermore, this option is extremely hazardous (when used on its own), -because it forcibly removes preexisting mappings, +This option should only be used when the specified memory region has +already been reserved using another mapping; otherwise, it is extremely +hazardous because it forcibly removes preexisting mappings, making it easy for a multithreaded process to corrupt its own address space. .IP For example, suppose that thread A looks through @@ -284,13 +285,15 @@ and the PAM libraries .UR http://www.linux-pam.org .UE . .IP -Newer kernels -(Linux 4.17 and later) have a +For cases in which the specified memory region has not been reserved using an +existing mapping, newer kernels (Linux 4.17 and later) provide an option .B MAP_FIXED_NOREPLACE -option that avoids the corruption problem; if available, -.B MAP_FIXED_NOREPLACE -should be preferred over -.BR MAP_FIXED . +that should be used instead; older kernels require the caller to use +.I addr +as a hint (without +.BR MAP_FIXED ) +and take appropriate action if the kernel places the new mapping at a +different address. .TP .BR MAP_FIXED_NOREPLACE " (since Linux 4.17)" .\" commit a4ff8e8620d3f4f50ac4b41e8067b7d395056843 -- 2.17.0.484.g0c8726318c-goog