Received: by 10.192.165.148 with SMTP id m20csp614283imm; Wed, 2 May 2018 06:07:25 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrgXCIobTCY8JP2PLqTclsZPWzIE2IpyXDEhFp742FYsDSAPHNzsbZ6DzZCaytZK4DPwu+P X-Received: by 2002:a65:5b8a:: with SMTP id i10-v6mr16474498pgr.431.1525266445027; Wed, 02 May 2018 06:07:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525266444; cv=none; d=google.com; s=arc-20160816; b=Ia7FFtnyRuvfs3slJD4oC1spdZjUkwMRfhyaVs5uYvFhqRbdaj8IVx3IpJwTEW0Aaf VJwNsWupU8DiAA5vIQzpAudqstWdRKZvTaLUpls+2EWYJbHFJZ26722QrpzHAlyHL0Jc 4BBdCUqsZIBuFK4bgF//WFRMVCbmsOU97VnEga+5hI6LPqYtIumzI4jHk/Zh2OYr3W6z zmdJzkEfPO/H+DCw8yfxIMHI5PLA+mE/WqDVVKzKPlEB1W2/H2celdmwIwE0nyWw6IZ5 aOpAVL7sXMBOi6vKeZGXQScQa3yBetu8Yb6ZrNIWsy6RtEPpRUgvcI+L20Yxx8Icvvcq Q+jQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:to:subject:cc:dkim-signature :arc-authentication-results; bh=iUNbzwNcgw4ArWrHXDf6MvP/fTC6eQzw1QToy1gIfRk=; b=RAf89p4t0ZZRlHnd6o9aba7CEhGQY1A9Z8sXM4SJlr0c+hiPKpqMH2wfk5agIBI0UW PrKt6TNyuJw/YjeaJa6i9Zoq7ipkPN8uT8HCfO7yCRDZ46HCTLSIrS4LupF0ZBR+Ncnn 95t1OnWozPWXv/oF3B8Ew00DPHb66WiK8KXe8Y9i7gEXMt/sCeo7W+FiVf46qI6wG9cV bN5aONnT8RHDI8ni94chfWlVfuyRws47Yol5bVzxiJk0PW/FE015lkGMPL9Gm6BfMET9 DeP4jv02NGgy15giKbwzJXeR/DFg5+m6sysUbMoU0qHURt4/o9pMT7YN2YUF9dUcZeY9 Rmug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=UGJUgef7; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h61-v6si11064766pld.228.2018.05.02.06.07.02; Wed, 02 May 2018 06:07:24 -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=@gmail.com header.s=20161025 header.b=UGJUgef7; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751434AbeEBNGo (ORCPT + 99 others); Wed, 2 May 2018 09:06:44 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:53611 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751073AbeEBNGj (ORCPT ); Wed, 2 May 2018 09:06:39 -0400 Received: by mail-wm0-f67.google.com with SMTP id a67so15042608wmf.3; Wed, 02 May 2018 06:06:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=cc:subject:to:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=iUNbzwNcgw4ArWrHXDf6MvP/fTC6eQzw1QToy1gIfRk=; b=UGJUgef7K9wzra4WVlS5M3iXDKHR0OKws/pUyQ6gud3YAM1aqu+Jh9szDPkgSPjnRd GyG1H2dMGUbt2b+PVwAT2kkgTSeP2RnYUbHubeVb87jKU9CqP4tB6hzHl00VQE/N6F30 u767NWttRm25IsTtpE2T0EJzSpH+RMxWA5Ubp5qdjAg8kqNdR7aPw6DpU2lDasJRz7dB wWdxu8zlTcXei/PiirplQCWEH51zseSHXVsgmb+8Z9bHYrGSaZAdg/DuyWe25tivv5Sw Vo+RfVMW54B/PAcYAUotLY1EbjpEcgP/PboqvSqoCFKOQKZc3vYZfmvtGb5g0ZncXVby tuBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:cc:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=iUNbzwNcgw4ArWrHXDf6MvP/fTC6eQzw1QToy1gIfRk=; b=a8NxN6erxv6BPrlCD35hp9nMZP2It0tNOP9L+vWMI7sbcsd1P2EuHuvK4oSIjGpnwK UpRd1bpHGZmQikQOaWf/0lWXtOujv6jxpVgjfqjf42RmcgkK5fC2BAtYvTcM+RzlLxtb A5KA9UkeYkNsbFkWu2Gx4MWCeEAI3gaLyv+iGvi7tM3SHcBUKYJtuDHaOdYH3MdGUHa9 RkHYbECi1eWZLvVCihhyWDXF5uokaPu0XcCPsE/Nmo1C1I5r8aI9DuxUc5/haNhmaaqK LlMJ+z4NAFRfw9Gro+2o4qSLGpwXqkrpX7ZwmRuHieA8IVjeIvHz5sNrcXJTB7fGkJGU ZbUQ== X-Gm-Message-State: ALQs6tCwa2RzzKlQh6/t0B0c3GJOD4y3OIghQZsPw1K5Z/b29nOnKALG Nvh/u6Rr09Uu/hJptYofrdiBucyL X-Received: by 10.28.186.136 with SMTP id k130mr11768183wmf.101.1525266397347; Wed, 02 May 2018 06:06:37 -0700 (PDT) Received: from [192.168.51.234] ([84.16.30.22]) by smtp.gmail.com with ESMTPSA id y42-v6sm15582529wry.21.2018.05.02.06.06.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 May 2018 06:06:36 -0700 (PDT) Cc: mtk.manpages@gmail.com, John Hubbard , linux-man , Andrew Morton , Linux-MM , lkml , Linux API Subject: Re: [PATCH] mmap.2: MAP_FIXED is okay if the address range has been reserved To: Michal Hocko , Jann Horn References: <20180413160435.GA17484@dhcp22.suse.cz> <20180416100736.GG17484@dhcp22.suse.cz> <20180416191805.GS17484@dhcp22.suse.cz> <20180416195726.GT17484@dhcp22.suse.cz> <20180416211115.GU17484@dhcp22.suse.cz> From: "Michael Kerrisk (man-pages)" Message-ID: <43db1d63-fa64-4b4a-f3c3-edc73892bd23@gmail.com> Date: Wed, 2 May 2018 15:06:34 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <20180416211115.GU17484@dhcp22.suse.cz> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Jann, Micha, On 04/16/2018 11:11 PM, Michal Hocko wrote: > On Mon 16-04-18 22:17:40, Jann Horn wrote: >> On Mon, Apr 16, 2018 at 9:57 PM, Michal Hocko wrote: >>> On Mon 16-04-18 21:30:09, Jann Horn wrote: >>>> On Mon, Apr 16, 2018 at 9:18 PM, Michal Hocko wrote: >>> [...] >>>>> Yes, reasonably well written application will not have this problem. >>>>> That, however, requires an external synchronization and that's why >>>>> called it error prone and racy. I guess that was the main motivation for >>>>> that part of the man page. >>>> >>>> What requires external synchronization? I still don't understand at >>>> all what you're talking about. >>>> >>>> The following code: >>>> >>>> void *try_to_alloc_addr(void *hint, size_t len) { >>>> char *x = mmap(hint, len, ...); >>>> if (x == MAP_FAILED) return NULL; >>>> if (x == hint) return x; >>> >>> Any other thread can modify the address space at this moment. >> >> But not parts of the address space that were returned by this mmap() call. > ? >>> Just >>> consider that another thread would does mmap(x, MAP_FIXED) (or any other >>> address overlapping [x, x+len] range) >> >> If the other thread does that without previously having created a >> mapping covering the area in question, that would be a bug in the >> other thread. > > MAP_FIXED is sometimes used without preallocated address ranges. > >> MAP_FIXED on an unmapped address is almost always a bug >> (excluding single-threaded cases with no library code, and even then >> it's quite weird) - for example, any malloc() call could also cause >> libc to start using the memory range you're trying to map with >> MAP_FIXED. > > Yeah and that's why we there is such a large paragraph in the man page > ;) > >>> becaus it is seemingly safe as x >>> != hint. >> >> I don't understand this part. Are you talking about a hypothetical >> scenario in which a programmer attempts to segment the virtual memory >> space into areas that are exclusively used by threads without creating >> memory mappings for those areas? > > Yeah, that doesn't sound all that over-exaggerated, right? And yes, > such a code would be subtle and most probably buggy. I am not trying to > argue for those hypothetical cases. All I am saying is that MAP_FIXED is > subtle. > > I _do_ agree that using it solely on the preallocated and _properly_ > managed address ranges is safe. I still maintain my position on error > prone though. And besides that there are usecases which do not operate > on preallocated address ranges so people really have to be careful. > > I do not really care what is the form. I find the current wording quite > informative and showing examples of how things might be broken. I do > agree with your remark that "MAP_FIXED on preallocated ranges is safe" > should be added. But MAP_FIXED is dangerous API and should have few big > fat warnings. So, at this stage, is any further patch needed to the text describing MAP_FIXED/MAP_FIXED_REPLACE? Cheers, Michael -- Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/