From owner-freebsd-stable@freebsd.org Wed Oct 21 15:28:54 2020 Return-Path: Delivered-To: freebsd-stable@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 5053F44EB05 for ; Wed, 21 Oct 2020 15:28:54 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4CGZCT2T55z4R5D for ; Wed, 21 Oct 2020 15:28:52 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.16.1/8.16.1) with ESMTPS id 09LFSid2086991 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 21 Oct 2020 18:28:47 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 09LFSid2086991 Received: (from kostik@localhost) by tom.home (8.16.1/8.16.1/Submit) id 09LFSibJ086990; Wed, 21 Oct 2020 18:28:44 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 21 Oct 2020 18:28:44 +0300 From: Konstantin Belousov To: Nick Kostirya Cc: freebsd-stable@freebsd.org Subject: Re: mmap and MAP_STACK Message-ID: <20201021152844.GK2643@kib.kiev.ua> References: <20201021165311.186bd606@i11.co> <20201021141657.GJ2643@kib.kiev.ua> <20201021181850.49126cdf@i11.co> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201021181850.49126cdf@i11.co> X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on tom.home X-Rspamd-Queue-Id: 4CGZCT2T55z4R5D X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=gmail.com (policy=none); spf=softfail (mx1.freebsd.org: 2001:470:d5e7:1::1 is neither permitted nor denied by domain of kostikbel@gmail.com) smtp.mailfrom=kostikbel@gmail.com X-Spamd-Result: default: False [0.21 / 15.00]; RCVD_TLS_ALL(0.00)[]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; NEURAL_HAM_LONG(-0.13)[-0.129]; MIME_GOOD(-0.10)[text/plain]; HAS_XAW(0.00)[]; R_SPF_SOFTFAIL(0.00)[~all]; NEURAL_SPAM_MEDIUM(0.09)[0.089]; TO_MATCH_ENVRCPT_SOME(0.00)[]; NEURAL_SPAM_SHORT(0.25)[0.254]; RCPT_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US]; RCVD_COUNT_TWO(0.00)[2]; MAILMAN_DEST(0.00)[freebsd-stable]; DMARC_POLICY_SOFTFAIL(0.10)[gmail.com : No valid SPF, No valid DKIM,none] X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Production branch of FreeBSD source code List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Oct 2020 15:28:54 -0000 On Wed, Oct 21, 2020 at 06:18:50PM +0300, Nick Kostirya via freebsd-stable wrote: > On Wed, 21 Oct 2020 17:16:57 +0300 > Konstantin Belousov wrote: > > > On Wed, Oct 21, 2020 at 04:53:11PM +0300, Nick Kostirya via freebsd-stable wrote: > > > Hello. > > > I have question about mmap. > > > > > > void *OSMem::AllocateDataArea(size_t &space) > > > { > > > // Round up to an integral number of pages. > > > space = (space + pageSize-1) & ~(pageSize-1); > > > int fd = -1; // This value is required by FreeBSD. Linux doesn't care > > > int flags = MAP_PRIVATE | MAP_ANON; > > > #ifdef MAP_STACK > > > if (memUsage == UsageStack) flags |= MAP_STACK; // OpenBSD seems to require this > > > #endif > > > void *result = mmap(0, space, PROT_READ|PROT_WRITE, flags, fd, 0); > > > // Convert MAP_FAILED (-1) into NULL > > > if (result == MAP_FAILED) > > > return 0; > > > return result; > > > } > > > > > > > > > When MAP_STACK is used, "insufficient memory" error occurs. > > > When MAP_STACK removed, it is all right. > > > > > > Please tell me why. > > Show ktrace/kdump output of the mmap(2) without and with MAP_STACK. > > > > Or provide a minimal self-contained C source that demonstrates your > > issue. > > kdump with MAP_STACK. > > 87183 polyimport CALL mmap(0,0x1000,0x3,0x1402,0xffffffff,0,0) > 87183 polyimport RET mmap -1 errno 22 Invalid argument So it is anything but 'insufficient memory' (I suspected ENOMEM). EINVAL there is because sysctl security.bsd.stack_guard_page default value is 1, which means that at least one page of the stack is reserved as guard. Kernel does not allow to map stack that would have no data pages (all pages are guard). Your mapping request is for one page, and one page is due to guard, so you get EINVAL. Generally MAP_STACK is magic and requires caller to know what it does. > > > kdump without MAP_STACK. > > 93712 polyimport CALL mmap(0,0x1000,0x3,0x1002,0xffffffff,0,0) > 93712 polyimport RET mmap 547053568/0x209b6000 > > > > _______________________________________________ > freebsd-stable@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-stable > To unsubscribe, send any mail to "freebsd-stable-unsubscribe@freebsd.org"