Received: by 2002:ac8:156:0:b0:3e0:cd10:60c8 with SMTP id f22csp1801579qtg; Wed, 22 Mar 2023 02:26:50 -0700 (PDT) X-Google-Smtp-Source: AK7set8OdXBgUARFqs1N6IYoueHBTbWNQeiQb2WtW0PWvbTLQZ38tNmM3jrC6UpRlKi6zB5whAB7 X-Received: by 2002:a05:6402:44f:b0:500:4062:99f7 with SMTP id p15-20020a056402044f00b00500406299f7mr6371624edw.32.1679477209890; Wed, 22 Mar 2023 02:26:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679477209; cv=none; d=google.com; s=arc-20160816; b=mnz2NPW7o4K3P6Ap/aA5wCGZZJN2RUnWfCZoYQe0O0uy0G50AAx8elHDxOu86KDkVG hHWkrBIMGg7ZBMIhdiGbe1bzM5QJERaIPxgpceTk0cxKPLthqCyThdWnkNptJwrWTsNE 9EpFCc6VhOMC+AvmWWe/f+V6jyEgHsLeqJuKexIuduAYtH6iAlOmNRQXJqOBsK4z1Yqz 7Lu3TDqAu6mw0VZNjWo1r7T052Q1J6excZ5OUKlRRsYkesDBm2JoLaL4ZZIJN40wC3qI QivQrGGt+1SNah7RK0EWbXamPcYH6FfS1DDh7zDcn/xZCn3kXhqQrcKkblMpDEjnyGz4 NyGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-signature; bh=EChfgy37fr4GWr9eZoipNMqUs51/fVt96h5C7/x84K8=; b=CeMwtWO8qIheFNP7mR+lFFWaHd0DXqLbzQZ57zZYtgKY+ZTNdq9BZDGW2+F47KGQ4S OvBhgS8sRBWo92RjJL9BCEB4ZQScgoCIfCB0n5VjVnRCmZwgbRvmjUzZ1QTVzWkdtFxk y227rYY33JNgJ1qoV21GnVyMiX4B0gS7PyDu1oH0WtjGbFUrNt76mVHxq1Ay3Rq6qgi7 HOUdRO0H0PsIRPCbwLvDZd2t0PI94Vkz1jkUxN+nlx97FKXuLALQ/zs1ZcgA8xVg75tn bz76vL/wz/Fzkv4fdzGdYHL9UYHQtrGpYLcBIdcAtQyU42vvduONJi/K9kTMGeDD3RJ4 GCBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=X6Rkq2zz; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=W39bgPCw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p5-20020a170906838500b009316736fd97si14801851ejx.405.2023.03.22.02.26.23; Wed, 22 Mar 2023 02:26:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=X6Rkq2zz; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=W39bgPCw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230165AbjCVJTt (ORCPT + 99 others); Wed, 22 Mar 2023 05:19:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230022AbjCVJTr (ORCPT ); Wed, 22 Mar 2023 05:19:47 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B71761913E for ; Wed, 22 Mar 2023 02:19:46 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 74CC433935; Wed, 22 Mar 2023 09:19:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1679476785; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EChfgy37fr4GWr9eZoipNMqUs51/fVt96h5C7/x84K8=; b=X6Rkq2zz7r+8uqbHrYHtv03Kebo1ORUYKOpjX+mtYj+2PjsJUWiQemsiGgnIrHe5E369ip Tf0quc6Tc+6NTbcgfEdZTKmcD9ow00ySKEcB5bLVzAJ40/5sBmvEOgGq6W+B3uvzQHtPOY IUKnZdG5hvwQNR055bjH659J2ykapr4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1679476785; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EChfgy37fr4GWr9eZoipNMqUs51/fVt96h5C7/x84K8=; b=W39bgPCwjlNd2sXy1KS0mAvPmKafThNfz1NabrFVkR+3QZ53Zs1RtdY4In0X4E2PrNTsfW Gz0jo7YCDt0RTODg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 57C5E13416; Wed, 22 Mar 2023 09:19:45 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 5S/nFDHIGmTKYAAAMHmgww (envelope-from ); Wed, 22 Mar 2023 09:19:45 +0000 Message-ID: <7a63f275-be11-30b3-8281-fcfc1b1fca9c@suse.cz> Date: Wed, 22 Mar 2023 10:19:45 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH v3 3/4] mm/mmap/vma_merge: explicitly assign res, vma, extend invariants Content-Language: en-US To: Lorenzo Stoakes , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: David Hildenbrand , Matthew Wilcox , "Liam R . Howlett" , maple-tree@lists.infradead.org, Vernon Yang References: From: Vlastimil Babka In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/22/23 08:13, Lorenzo Stoakes wrote: > Previously, vma was an uninitialised variable which was only definitely > assigned as a result of the logic covering all possible input cases - for > it to have remained uninitialised, prev would have to be NULL, and next > would _have_ to be mergeable. > > The value of res defaults to NULL, so we can neatly eliminate the > assignment to res and vma in the if (prev) block and ensure that both res > and vma are both explicitly assigned, by just setting both to prev. > > In addition we add an explanation as to under what circumstances both might > change, and since we absolutely do rely on addr == curr->vm_start should > curr exist, assert that this is the case. Hm replied to v2 of this, sorry, so let me repeat that here: > Signed-off-by: Lorenzo Stoakes Reviewed-by: Vlastimil Babka Nit suggestion below. > --- > mm/mmap.c | 19 ++++++++++++++----- > 1 file changed, 14 insertions(+), 5 deletions(-) > > diff --git a/mm/mmap.c b/mm/mmap.c > index dbdbb92493b2..2a4f63716231 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -911,7 +911,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, > { > pgoff_t pglen = (end - addr) >> PAGE_SHIFT; > pgoff_t vma_pgoff; > - struct vm_area_struct *curr, *next, *res = NULL; > + struct vm_area_struct *curr, *next, *res; > struct vm_area_struct *vma, *adjust, *remove, *remove2; > int err = -1; > bool merge_prev = false; > @@ -939,14 +939,18 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, > else > next = NULL; /* case 5 */ > > - /* verify some invariant that must be enforced by the caller */ > + /* > + * By default, we return prev. Cases 3, 4, 8 will instead return next > + * and cases 3, 8 will also update vma to point at next. > + */ > + res = vma = prev; Later in the function there's a line: remove = remove2 = adjust = NULL; Now it would make sense to move it up here? > + > + /* Verify some invariant that must be enforced by the caller. */ > VM_WARN_ON(prev && addr <= prev->vm_start); > - VM_WARN_ON(curr && end > curr->vm_end); > + VM_WARN_ON(curr && (addr != curr->vm_start || end > curr->vm_end)); > VM_WARN_ON(addr >= end); > > if (prev) { > - res = prev; > - vma = prev; > vma_start = prev->vm_start; > vma_pgoff = prev->vm_pgoff; > /* Can we merge the predecessor? */ > @@ -957,6 +961,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, > vma_prev(vmi); > } > } > + > /* Can we merge the successor? */ > if (next && mpol_equal(policy, vma_policy(next)) && > can_vma_merge_before(next, vm_flags, > @@ -997,6 +1002,10 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, > adj_start = -(prev->vm_end - addr); > err = dup_anon_vma(next, prev); > } else { > + /* > + * Note that cases 3 and 8 are the ONLY ones where prev > + * is permitted to be (but is not necessarily) NULL. > + */ > vma = next; /* case 3 */ > vma_start = addr; > vma_end = next->vm_end;