Received: by 10.192.165.156 with SMTP id m28csp877683imm; Fri, 13 Apr 2018 09:19:17 -0700 (PDT) X-Google-Smtp-Source: AIpwx48OoBPx9dkpbm8eGql6XwmzYNVNawE+gNr21qiD1BrfgaCYLh3Qe2gBOpVsljNozBouCBzz X-Received: by 2002:a17:902:28e4:: with SMTP id f91-v6mr5746076plb.336.1523636357109; Fri, 13 Apr 2018 09:19:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523636357; cv=none; d=google.com; s=arc-20160816; b=h9Hhz5rR5149zIpNIDcXt22WnGv4iLhBCfuqdYYP+oePjWzwbkYn39prmOvg33vyyN 4USxirlcrkfOTDds0JuOMB598x1nENbAiLltUzdngojcpLJLYzkcU9R5Drx+ggn/fV37 sW9Qwiiwp/ukYhicZcCS+FL15AcJ6mRkmxxeyzHvzNj1vCgBGkCIosCBlolJgOb1MkLq MuQlvP9j2MaosT99pBj3SuTqWGOIoNSk6uJQfTAMSC2Jlv7lqjmE9Hw7zfoM44+31HdH N7aHBOIguxu/bRngsHsbSL8Ma4oTKlzyLUvibcnj3TY+ITd++rdK7b+zafkhPEc0BBTS bgTw== 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=kgVm3ANtK/kMoEzLQub1eTsRk5EFy0TDOdvln1nDu+g=; b=ocD/aDu3dr5gypPgC9NHfEHUetnxRRXgV6W/cM7y16l9fkJoWbIscuF6MaFhEbT8L3 sMg63uh3ZcXCfZbycJtjtRJ3T6HeY9hFuUnTRmctbrVbVfek4inzgwM+1Bz70pXw0acz YD51vdkOg/pfl1oquHo/I5XFfwxuq9gTQcAzizDNa4vLYiAjpjIQ/e35yxiRaAoCY9cO 68+icVMpi/fA6oOuKseetmaS3s41P+GHCF8Xl3jVfHHeEsqPwMxxR3EkxtczKhB9FhYC 4Dvu+n7HrXvYa4yx/s41u3t7D7FQRst7OxbFm96D01HlyAX6HCDMOm+ynLAjEBH6WjE2 Opxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ZJRMKLcr; 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 i81si5026534pfd.261.2018.04.13.09.19.02; Fri, 13 Apr 2018 09:19:17 -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=ZJRMKLcr; 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 S1752696AbeDMQSA (ORCPT + 99 others); Fri, 13 Apr 2018 12:18:00 -0400 Received: from mail-ot0-f196.google.com ([74.125.82.196]:43815 "EHLO mail-ot0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751864AbeDMQR6 (ORCPT ); Fri, 13 Apr 2018 12:17:58 -0400 Received: by mail-ot0-f196.google.com with SMTP id d9-v6so10449776oth.10 for ; Fri, 13 Apr 2018 09:17:57 -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=kgVm3ANtK/kMoEzLQub1eTsRk5EFy0TDOdvln1nDu+g=; b=ZJRMKLcr0qiaphgM5hts6eVeXvtkNBDsW+S/DVJOjk/9lpU0Jb1bUbExrsq2ejR7Sz 5AKRdBtlP0hX3/LtSSB5nFLRupqqu4xH1FcPWKo/YkL72mvhKO7oeo40/a8+e8XWmdZ4 GX0prGuaPLbqUpA3AvxgmpQi7PnzVRkBqyGplF7C2GNec6FmC2ZtiMdnj1QGCDIZLBXw Eid+NSybbsgrJGIPZgMB8nBXCuTQdou1PByQIpGdj84LpKzX4yo81XOeLVoS3gjXQ4iF P59Hh279rC6dkpiNWOv/SEgz0Fp16RSmFNj3TCeBeVtG7ZIsDi6MBujtEhCWP+40wTG3 MsrA== 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=kgVm3ANtK/kMoEzLQub1eTsRk5EFy0TDOdvln1nDu+g=; b=ADu6Y3UkSK4o7dG22pWwy39Ewxqbgo4mYZ2s1zGub95zAQLq0sL+PhtvvQAP2PAWe9 Z+eawrV+aJPOm5oRjgdACiqhrjcGVtwQyDxhYEBw7etbBRNvNWH9aeWo+x6EidukQa0T JlTRMJWz3UL8aMDMDGHIp0VUPo9aj+O5Sq0uRsg4w1/1OVGUvrzzYLMZlfvKvU/ZyhwB zSJ/7S6taLcaqtysJ1hRE2nsbr0IBZa6wbu8HfwNfySNieg5Xe8SgBlShzIwRnlQJooX XcEizHVQFtC01SVTeKUs/mxvcf9yIROiRxBqUlAfUO/t9/JwVrwxzTOTYNaxSrIfcHy5 2QrA== X-Gm-Message-State: ALQs6tBNNVfe8g/+7jq3MfHz7lZPmj87n35iEo9if7qYN4s2PTAa1i1E oDP7Set8XJbPQcIArUetEGVALDPc9QqEZVEwgBanNw== X-Received: by 2002:a9d:f28:: with SMTP id 37-v6mr3887872ott.0.1523636277095; Fri, 13 Apr 2018 09:17:57 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.73.133 with HTTP; Fri, 13 Apr 2018 09:17:36 -0700 (PDT) In-Reply-To: References: <20180412153941.170849-1-jannh@google.com> <13801e2a-c44d-e940-f872-890a0612a483@nvidia.com> <9c714917-fc29-4d12-b5e8-cff28761a2c1@gmail.com> <20180413064917.GC17484@dhcp22.suse.cz> <20180413160435.GA17484@dhcp22.suse.cz> From: Jann Horn Date: Fri, 13 Apr 2018 18:17:36 +0200 Message-ID: Subject: Re: [PATCH] mmap.2: MAP_FIXED is okay if the address range has been reserved To: Michal Hocko Cc: "Michael Kerrisk (man-pages)" , John Hubbard , linux-man , 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 Fri, Apr 13, 2018 at 6:05 PM, Jann Horn wrote: > On Fri, Apr 13, 2018 at 6:04 PM, Michal Hocko wrote: >> On Fri 13-04-18 17:04:09, Jann Horn wrote: >>> On Fri, Apr 13, 2018 at 8:49 AM, Michal Hocko wrote: >>> > On Fri 13-04-18 08:43:27, Michael Kerrisk wrote: >>> > [...] >>> >> So, you mean remove this entire paragraph: >>> >> >>> >> 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 MAP_FIXED_NOREPLACE that >>> >> should be used instead; older kernels require the caller to >>> >> use addr as a hint (without MAP_FIXED) and take appropriate >>> >> action if the kernel places the new mapping at a different >>> >> address. >>> >> >>> >> It seems like some version of the first half of the paragraph is worth >>> >> keeping, though, so as to point the reader in the direction of a remedy. >>> >> How about replacing that text with the following: >>> >> >>> >> Since Linux 4.17, the MAP_FIXED_NOREPLACE flag can be used >>> >> in a multithreaded program to avoid the hazard described >>> >> above. >>> > >>> > Yes, that sounds reasonable to me. >>> >>> But that kind of sounds as if you can't avoid it before Linux 4.17, >>> when actually, you just have to call mmap() with the address as hint, >>> and if mmap() returns a different address, munmap() it and go on your >>> normal error path. >> >> This is still racy in multithreaded application which is the main point >> of the whole section, no? > > No, it isn't. mmap() with a hint (without MAP_FIXED) will always non-racily allocate a memory region for you or return an error code. If it does allocate a memory region, it belongs to you until you deallocate it. It might be at a different address than you requested - in that case you can emulate MAP_FIXED_NOREPLACE by calling munmap() and treating it as an error; or you can do something else with it. MAP_FIXED_NOREPLACE is just a performance optimization.