From nobody Sat Jul 10 13:54:17 2021 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id B66361241A68 for ; Sat, 10 Jul 2021 13:54:33 +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 4GMWjh69tlz3MYF; Sat, 10 Jul 2021 13:54:32 +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 16ADsHmU048955 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Sat, 10 Jul 2021 16:54:20 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 16ADsHmU048955 Received: (from kostik@localhost) by tom.home (8.16.1/8.16.1/Submit) id 16ADsHL4048954; Sat, 10 Jul 2021 16:54:17 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Sat, 10 Jul 2021 16:54:17 +0300 From: Konstantin Belousov To: Vitaliy Gusev Cc: freebsd-hackers@freebsd.org, gljennjohn@gmail.com, Mark Johnston , Peter Grehan Subject: Re: madvise(MADV_FREE) doesn't work in some cases? Message-ID: References: <20210703065420.6dbafb5f@ernst.home> <2390FA9B-319E-45D4-BEA7-10878E43AD4B@gmail.com> <2A7A1E60-766D-427B-8288-E92B4090022E@gmail.com> List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: 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.5 X-Spam-Checker-Version: SpamAssassin 3.4.5 (2021-03-20) on tom.home X-Rspamd-Queue-Id: 4GMWjh69tlz3MYF 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 [1.70 / 15.00]; RCVD_COUNT_TWO(0.00)[2]; TO_DN_SOME(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; HAS_XAW(0.00)[]; R_SPF_SOFTFAIL(0.00)[~all:c]; RCPT_COUNT_FIVE(0.00)[5]; FREEMAIL_TO(0.00)[gmail.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US]; R_DKIM_NA(0.00)[]; RBL_DBL_DONT_QUERY_IPS(0.00)[2001:470:d5e7:1::1:from]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-0.29)[-0.291]; FROM_HAS_DN(0.00)[]; NEURAL_SPAM_SHORT(1.00)[1.000]; NEURAL_HAM_LONG(-0.01)[-0.011]; TAGGED_RCPT(0.00)[]; MIME_GOOD(-0.10)[text/plain]; SPAMHAUS_ZRD(0.00)[2001:470:d5e7:1::1:from:127.0.2.255]; SUBJECT_ENDS_QUESTION(1.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; FREEMAIL_CC(0.00)[freebsd.org,gmail.com]; RCVD_TLS_ALL(0.00)[]; MAILMAN_DEST(0.00)[freebsd-hackers]; DMARC_POLICY_SOFTFAIL(0.10)[gmail.com : No valid SPF, No valid DKIM,none] X-Spam: Yes X-ThisMailContainsUnwantedMimeParts: N On Tue, Jul 06, 2021 at 01:21:17AM +0300, Vitaliy Gusev wrote: > Comments are below, > > > commit 0392eb3c93b7dacc31dbdf8ec2fc40fa5ba67c62 > > Author: Konstantin Belousov > > > Date: Mon Jul 5 21:53:22 2021 +0300 > > > > madvise(MADV_FREE): try harder to handle shadow chain > > > > In particular, collapse top object and see if there is no backing object > > after, which means that we would not revert to older content if drop the > > top object. > > > > diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c > > index 1ac4ccf72f11..80abac223f29 100644 > > --- a/sys/vm/vm_map.c > > +++ b/sys/vm/vm_map.c > > @@ -3033,6 +3033,7 @@ vm_map_madvise( > > entry = vm_map_entry_succ(entry); > > for (; entry->start < end; > > entry = vm_map_entry_succ(entry)) { > > + vm_object_t obj; > > vm_offset_t useEnd, useStart; > > > > if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) > > @@ -3046,9 +3047,16 @@ vm_map_madvise( > > * backing object can change. > > */ > > if (behav == MADV_FREE && > > - entry->object.vm_object != NULL && > > - entry->object.vm_object->backing_object != NULL) > > - continue; > > + (obj = entry->object.vm_object) != NULL && > > + obj->backing_object != NULL) { > > + VM_OBJECT_WLOCK(obj); > > + if ((obj->flags & OBJ_DEAD) != 0) > > + continue; > > Here is object is left locked, however I didn’t met this condition. Right. > > > + vm_object_collapse(obj); > > + VM_OBJECT_WUNLOCK(obj); > > + if (obj->backing_object != NULL) > > + continue; > > After testing it looks that backing_object is not null here, memory is not freed and mmapfork test program is still killed. Any ideas? > I finally looked at this thing again. You do madvise(PAGE_SIZE). With my patch (updated version below) issue madvise() for the whole region once. commit b183ec9e985961bd0b450d9e7bdbbcde751c830b Author: Konstantin Belousov Date: Mon Jul 5 21:53:22 2021 +0300 madvise(MADV_FREE): try harder to handle shadow chain In particular, collapse top object and see if there is no backing object after, which means that we would not revert to older content if drop the top object. diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c index 1ac4ccf72f11..188b37560e0e 100644 --- a/sys/vm/vm_map.c +++ b/sys/vm/vm_map.c @@ -3033,6 +3033,7 @@ vm_map_madvise( entry = vm_map_entry_succ(entry); for (; entry->start < end; entry = vm_map_entry_succ(entry)) { + vm_object_t obj; vm_offset_t useEnd, useStart; if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) @@ -3046,9 +3047,18 @@ vm_map_madvise( * backing object can change. */ if (behav == MADV_FREE && - entry->object.vm_object != NULL && - entry->object.vm_object->backing_object != NULL) - continue; + (obj = entry->object.vm_object) != NULL && + obj->backing_object != NULL) { + VM_OBJECT_WLOCK(obj); + if ((obj->flags & OBJ_DEAD) != 0) { + VM_OBJECT_WUNLOCK(obj); + continue; + } + vm_object_collapse(obj); + VM_OBJECT_WUNLOCK(obj); + if (obj->backing_object != NULL) + continue; + } pstart = OFF_TO_IDX(entry->offset); pend = pstart + atop(entry->end - entry->start);