Received: by 10.192.165.156 with SMTP id m28csp1102581imm; Mon, 16 Apr 2018 14:13:02 -0700 (PDT) X-Google-Smtp-Source: AIpwx49syGH9dg3QoIYIqCTdqd7E8VB+R0PLiVeB2AvgGF77mQ8fpmPvFribPTJHJ41/9+sHFNoP X-Received: by 2002:a17:902:3381:: with SMTP id b1-v6mr9637713plc.248.1523913182408; Mon, 16 Apr 2018 14:13:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523913182; cv=none; d=google.com; s=arc-20160816; b=1C+K3sHBkqqO8v4fZrSadu7p4nL3bDPO6yqigK1kWZi0o48xbKO83KP34lydjKrt6u ZPrnLc95tFFnAKukeJ+s6pSz+Le2m1+32nhC/DiwuBskhFlD/NN1DkPPe/1rFEdJQ18W JeNyehrj8bWfXEH2/RF9UkwwsOPfDzim8AKeIZ3CEpCG8D2en80+OchPJZr+bm9OKjdN ldk35XQj4KaNGlfbJWqmi3X1N/aYmjDcShNZmVr8q042RGNT1Pgyb7lN9HLNVaju2eL8 gjByF5ye2VeClg1EBfn1YVBjGzGglF0A6a3wkG1h4JX02CheoxGBTJL0PWzsd2K9PmvN ZJtg== 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=SXBBeatNTZysmIIxOcjaPKanwClmpvgsuqb4RlSohrw=; b=G7OYmtDApYdzjAT4qToR6h1aS1/gxCOVwyV7yfTqNAVbtvfAvJ0iQLNPNru/J+68R/ BRF0d1yzlLzMgyikK4JyiWYR1jInZjSLO4gyevdsqwgU+gpLOivdiYrrkSZfqPyj2WEg XS9M8j4UA1XrcrCb8XIWKwZgJv7QurmlCyJI34sFQNDWZUxNxUvTe2cp2bEyGFOKnKj+ 0y1J655A2UPYEe3lFqiZVKIsmniw+5hnULIFAKOxk07g6ai+HONgsA4lQ4G1RJTMBhgc QoAQTs/eVbQL/jPpXdkT5q2Fgu+QJFydJAuSwzUdRs3UACtUdDdsW4T9qad7Zr7jx1PI 4+rQ== 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 73si2206347pfb.204.2018.04.16.14.12.48; Mon, 16 Apr 2018 14:13:02 -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 S1751210AbeDPVLU (ORCPT + 99 others); Mon, 16 Apr 2018 17:11:20 -0400 Received: from mx2.suse.de ([195.135.220.15]:60070 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751489AbeDPVLS (ORCPT ); Mon, 16 Apr 2018 17:11:18 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id A2D74AE86; Mon, 16 Apr 2018 21:11:16 +0000 (UTC) Date: Mon, 16 Apr 2018 23:11:15 +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: <20180416211115.GU17484@dhcp22.suse.cz> References: <20180413160435.GA17484@dhcp22.suse.cz> <20180416100736.GG17484@dhcp22.suse.cz> <20180416191805.GS17484@dhcp22.suse.cz> <20180416195726.GT17484@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 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. -- Michal Hocko SUSE Labs