Received: by 10.192.165.156 with SMTP id m28csp948836imm; Wed, 11 Apr 2018 09:44:08 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/gpGIXJXZ5L+pZzbvUBccH5noKgtC+eSv/8X5dC/7TkH2/QDkcL0UHREkUNEYdQU7QKeJX X-Received: by 10.101.67.72 with SMTP id k8mr1721147pgq.433.1523465048864; Wed, 11 Apr 2018 09:44:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523465048; cv=none; d=google.com; s=arc-20160816; b=AKKeeg0NVatyNL8BLug6vzdcT45vMS23du1Hu8MpnqifBcFrIh4PEzaAoEDpELGeZs ia7aNYGDe3yR5F90AuULCmad/XOdu8wB4iGe+npfEq/O32Th50R516awyrUonahhi2GS ualYZrOhqPKaahruFulLRHvtrDhDVni1lf/SNfXuqkdq8LUXByjaaftVrhtJBtVdubau KvTuTsBYf/61rAiAHqqjNe6bQys9kPSPwmDhMGVd1GBd75KERvJS2vDAqDZwdRhNv2P9 F3a9QX0SS9yVICNcOjCsH99R4SNvm0tY3uXL71TxyTWKExhAmkm9XbveP7rM6+kOHA/n pgjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=aEgKU5i6EEHTxTrmPvS3i6f1XiP1zK2zS2eH5vqKZ+U=; b=QVDMyyayfKE+Vx0VsdYp8d6sMl9LZ0wo0riLLx6qlJeIu80GPu0f+kkT8Az9TqMrms LuBeOJcjvfg9zYvcsPMjU3W1VMX77BbT4NHOvv2uGMIxc/w5jUuOGoPZPmV88PlOv/Ci gwJplPcOh9j4XEWG6NmphAYoqU1ssTRaWqxeXiLUajJZvDKO6Bg5+0jWBAs3N5qRXT4c DfLBSvdXD3O4S+egFmTM7O2Il3eLndewDSqTI3FzmZTWhc7EttKNj7rZO34DCOhJD65T 7lZMHOZ1xR0HPhDQO9GJ5oDfSb4TczCtxMWjLHm+2F37VDaEOfgFUQ++fz5ESQJk2UW6 RZNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kdzeRIL/; 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 c10-v6si1501942plr.398.2018.04.11.09.43.31; Wed, 11 Apr 2018 09:44:08 -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=kdzeRIL/; 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 S1753106AbeDKQkd (ORCPT + 99 others); Wed, 11 Apr 2018 12:40:33 -0400 Received: from mail-ot0-f196.google.com ([74.125.82.196]:45522 "EHLO mail-ot0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752664AbeDKQkb (ORCPT ); Wed, 11 Apr 2018 12:40:31 -0400 Received: by mail-ot0-f196.google.com with SMTP id v6-v6so2704730otj.12 for ; Wed, 11 Apr 2018 09:40:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=aEgKU5i6EEHTxTrmPvS3i6f1XiP1zK2zS2eH5vqKZ+U=; b=kdzeRIL/8Ud2llc1BtizqvHUrQQjSjZZuROyxYZRWnpSYozCP9XhACHS5vWzJeH6Lw dMQFfVM2FVenJp1kaduBQnoCV89ZgWxUO7ag55Vu8ypyrPa82ypj/ne7RQ5ZIwoUfEJ7 mB7yDTUG5S54GsPTp8AK5acZEYen5jZTYTeM3l9yhB+mxINg9lpPcbG+gjwLSLx6bWBX hW5d0tgvx6DiyOnePAsZ80l/uhxiDMYaJ2S3mHu9tQGKv96St1DRLGwzc5lFXna3kpQq koCK3P7DH7NE2FkVOwoIXj/b4gK7+YA0MtCyUgw15e8dEceJI3AYuBzWKQ0061z4X6CC PkIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=aEgKU5i6EEHTxTrmPvS3i6f1XiP1zK2zS2eH5vqKZ+U=; b=tng9LTaUT8ThWKf18RUltInmGRhWB+ZnecpNEP9zgkbdNK0HQiJwXTSGJXeNJAMxO6 NxUjEEXxm3Ydy4RG6AQ2DQJZIAk952G75YHetCCtJv9KOjZ/8JPF8A0/zwh+R8ynfnNw zQ9JNl2UeT3sJ5w8pg7o9MwPJ36efsWH2xV0Oq6k3vM1vXlLN1dspWXUL7IL995KzHj4 7cYVCfN2+ZuiewbGixu5O4y/6ibkvXlgEyr7XI2KNxesT/L6vev9aFboktB4L7ldYn+W HrfIt/cO70FQvrNmSOmhXyfsU/ZOaLf0MxAvRj+qVSjmUxUR3xxJ2D7yVie9EDAWWCV4 lalw== X-Gm-Message-State: ALQs6tAQEVBRcTRGD41pgqbzb7I6aGxo95yiBDz/wRZcdkLVsHqtw12C H6g+elCw0Uq5EWnlqs6klIKC+rE+75hR2wqChZdCeQ== X-Received: by 2002:a9d:1691:: with SMTP id c17-v6mr3801858ote.115.1523464830610; Wed, 11 Apr 2018 09:40:30 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.73.133 with HTTP; Wed, 11 Apr 2018 09:40:09 -0700 (PDT) In-Reply-To: <20180411163631.GL23400@dhcp22.suse.cz> References: <20180411120452.1736-1-mhocko@kernel.org> <20180411163631.GL23400@dhcp22.suse.cz> From: Jann Horn Date: Wed, 11 Apr 2018 18:40:09 +0200 Message-ID: Subject: Re: [PATCH] mmap.2: document new MAP_FIXED_NOREPLACE flag To: Michal Hocko Cc: Michael Kerrisk , John Hubbard , Andrew Morton , Linux-MM , LKML , Linux API 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 On Wed, Apr 11, 2018 at 6:36 PM, Michal Hocko wrote: > On Wed 11-04-18 17:37:46, Jann Horn wrote: >> On Wed, Apr 11, 2018 at 2:04 PM, wrote: >> > From: Michal Hocko >> > >> > 4.17+ kernels offer a new MAP_FIXED_NOREPLACE flag which allows the caller to >> > atomicaly probe for a given address range. >> > >> > [wording heavily updated by John Hubbard ] >> > Signed-off-by: Michal Hocko >> > --- >> > Hi, >> > Andrew's sent the MAP_FIXED_NOREPLACE to Linus for the upcoming merge >> > window. So here we go with the man page update. >> > >> > man2/mmap.2 | 27 +++++++++++++++++++++++++++ >> > 1 file changed, 27 insertions(+) >> > >> > diff --git a/man2/mmap.2 b/man2/mmap.2 >> > index ea64eb8f0dcc..f702f3e4eba2 100644 >> > --- a/man2/mmap.2 >> > +++ b/man2/mmap.2 >> > @@ -261,6 +261,27 @@ Examples include >> > and the PAM libraries >> > .UR http://www.linux-pam.org >> > .UE . >> > +Newer kernels >> > +(Linux 4.17 and later) have a >> > +.B MAP_FIXED_NOREPLACE >> > +option that avoids the corruption problem; if available, MAP_FIXED_NOREPLACE >> > +should be preferred over MAP_FIXED. >> >> This still looks wrong to me. There are legitimate uses for MAP_FIXED, >> and for most users of MAP_FIXED that I'm aware of, MAP_FIXED_NOREPLACE >> wouldn't work while MAP_FIXED works perfectly well. >> >> MAP_FIXED is for when you have already reserved the targeted memory >> area using another VMA; MAP_FIXED_NOREPLACE is for when you haven't. >> Please don't make it sound as if MAP_FIXED is always wrong. > > Well, this was suggested by John. I think, nobody is objecting that > MAP_FIXED has legitimate usecases. The above text just follows up on > the previous section which emphasises the potential memory corruption > problems and it suggests that a new flag is safe with that regards. > > If you have specific wording that would be better I am open for changes. I guess I'd probably also want to change the previous text; so I should probably send a followup patch once this one has landed. >> > +.TP >> > +.BR MAP_FIXED_NOREPLACE " (since Linux 4.17)" >> > +Similar to MAP_FIXED with respect to the >> > +.I >> > +addr >> > +enforcement, but different in that MAP_FIXED_NOREPLACE never clobbers a pre-existing >> > +mapped range. If the requested range would collide with an existing >> > +mapping, then this call fails with >> > +.B EEXIST. >> > +This flag can therefore be used as a way to atomically (with respect to other >> > +threads) attempt to map an address range: one thread will succeed; all others >> > +will report failure. Please note that older kernels which do not recognize this >> > +flag will typically (upon detecting a collision with a pre-existing mapping) >> > +fall back to a "non-MAP_FIXED" type of behavior: they will return an address that >> > +is different than the requested one. Therefore, backward-compatible software >> > +should check the returned address against the requested address. >> > .TP >> > .B MAP_GROWSDOWN >> > This flag is used for stacks. >> > @@ -487,6 +508,12 @@ is not a valid file descriptor (and >> > .B MAP_ANONYMOUS >> > was not set). >> > .TP >> > +.B EEXIST >> > +range covered by >> > +.IR addr , >> > +.IR length >> > +is clashing with an existing mapping. >> >> Maybe add something like ", and MAP_FIXED_NOREPLACE was specified"? I >> think most manpages explicitly document which error conditions can be >> triggered by which flags. > > sure, no objection from me. > > -- > Michal Hocko > SUSE Labs