Received: by 10.192.165.156 with SMTP id m28csp1004762imm; Mon, 16 Apr 2018 12:19:36 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/OB/+7IBV5ulP5HcsXoaLjXmzicnde3OmAUS9lEVT//bCQxXw2GWykDpp5Q1FJF//UlopY X-Received: by 10.98.32.87 with SMTP id g84mr22630110pfg.28.1523906376344; Mon, 16 Apr 2018 12:19:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523906376; cv=none; d=google.com; s=arc-20160816; b=X9guSwsOof2E+EdwsIZLs6YXq2sCWnOkSEQ6P0W8+vhehRFf02ditKmbNmIb5VcLuA 38TFofTXHPYVLirALLOt7wmDYJNkw9gqdv3ZPqwitwE6/ocKfs+VXBRqnlo6yuhQtEYQ J0j1IqrdH5kTaV5ygUMu4p/YKTJ3rIaQlnevVXqC3Fg3EUBGDjd9sTVPz5eWM7B1XQkg ZzUi5Av27Fq6kdtHgplyEHtFfnVK2b58v7kvo6FpQguRA6/CwIGIJd0BX6UK9CQIzdYg SrE4SRI9Nzhtw0Lvv/Igai/H+27o+Qg1rUcKuJCKCBLsxaRMHwjUFgk7pYg0/9ioj6mm 8Ftg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=Jy3FSIxbk8qmasehz4wQG5qDq3I2RGVg9jrxg8WnuIg=; b=qN3I49GXIAafqM5jZr19FQVNiG/VZVp9vLSl1n4kC9WOO+Rd95iMyelTzfvPByDM/Q c+wucLdRdkovc0w3D4Quvr4Gmy+sjo8wiEVeQ+YU3LFAltuTGK8H8SjNq0bWVFnpN+PJ iwnpxfOJYsNNmIfkXgrIgg16vkrjiV1yASKxwHZuAQymO9xDl59LRKbnyhxRW4r5y7u/ 6xjU9/5TGFdvoeeFk6SRWTOXlm4fek1lITvVETT5qxuCDUvQphJTvgKihizX1+aFs+4R zuuAiI9ndYyf1dzw6blnxUkdvg0a+K5zPwvFj2drW+4Ctv0VzbEtWd6dyBpGt09VKx42 +4Ww== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b3-v6si12832512pld.2.2018.04.16.12.19.20; Mon, 16 Apr 2018 12:19:36 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753355AbeDPTSL (ORCPT + 99 others); Mon, 16 Apr 2018 15:18:11 -0400 Received: from mx2.suse.de ([195.135.220.15]:50662 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753182AbeDPTSI (ORCPT ); Mon, 16 Apr 2018 15:18:08 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id D5D85AF3C; Mon, 16 Apr 2018 19:18:06 +0000 (UTC) Date: Mon, 16 Apr 2018 21:18:05 +0200 From: Michal Hocko To: Jann Horn Cc: "Michael Kerrisk (man-pages)" , 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 Message-ID: <20180416191805.GS17484@dhcp22.suse.cz> References: <9c714917-fc29-4d12-b5e8-cff28761a2c1@gmail.com> <20180413064917.GC17484@dhcp22.suse.cz> <20180413160435.GA17484@dhcp22.suse.cz> <20180416100736.GG17484@dhcp22.suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon 16-04-18 15:55:36, Jann Horn wrote: > On Mon, Apr 16, 2018 at 12:07 PM, Michal Hocko wrote: > > On Fri 13-04-18 18:17:36, Jann Horn wrote: > >> 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. > > > > I could have been more specific, sorry. > > > >> 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 - > > > > Yes, this all is true. Except the atomicity is guaranteed only for the > > syscall. Once you return to the userspace any error handling is error > > prone and racy because your mapping might change under you feet. So... > > Can you please elaborate on why you think anything could change the > mapping returned by mmap() under the caller's feet? Because as soon as the mmap_sem is dropped then any other thread can modify the shared address space. > When mmap() returns a memory area to the caller, that memory area > belongs to the caller. No unrelated code will touch it, unless that > code is buggy. 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. -- Michal Hocko SUSE Labs