From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C33195C96 for ; Wed, 29 Jan 2025 15:42:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738165330; cv=none; b=t/rV+33MTGKDEBB9a3Y8KOZPdlY0SR2h9SQTcQwpytuAILCtA5+9bdEEGgK5p+Uc9O3cwNSV17kqU0kq3WPuGazs4lEy3opryd/SYww6aC9b19u5hahi395dgjhbZ06kKAEGdLdIrsGmghztkkLBDmqxI0dQNrpbgKP4kSMYh54= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738165330; c=relaxed/simple; bh=gFLH46y+E64ax7wtymxdbeYf4LAa7YQOerkk0u9irvk=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=UyH1S5t4Jg/1E69QNmOWKIjghwGujRWLUKN51yV8qS9Sawy8QWIGE3JMzVMoxu2W2J0Mi845uZFbLpMbEl0hk3s6BiSA+SdjLLBPl/UOqimvgukvTyzSxZ/2vZ6+WUe/nZpL+qp1dsuGxVK1X3vyk1gotYwzNOQWUW9XfehJaoo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=a4DM3ijI; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=kI62ASIC; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=lQQbxVzc; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=jcOLewHl; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="a4DM3ijI"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="kI62ASIC"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="lQQbxVzc"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="jcOLewHl" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id DC49221108; Wed, 29 Jan 2025 15:42:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1738165327; 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:autocrypt:autocrypt; bh=RjoCqeoYv2p4Qu8XufkuxZfqUFjtKs8MuQnM7xQd+2g=; b=a4DM3ijIrlz81P8Zjdbl8rA4hr7zb5MCp/o/lYMYR15HOLsBx6K4AeyDWEv2ciHJye8ZIl 3Y5nrWnCYNkyhdZ9+xq56+RmVNH3EpTBJ3/bupqKFpCtOTJhlmkr/suZyB+0fJsjSaBfxD mmrzVEoSZF7YIw1uu0spc+vjijjQCOE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1738165327; 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:autocrypt:autocrypt; bh=RjoCqeoYv2p4Qu8XufkuxZfqUFjtKs8MuQnM7xQd+2g=; b=kI62ASICBkcbmuqfdYpUXet6MHaE8tryWoWw2xUgnGqfIKlVDzJUvbWmsC6blThxBZDPK0 NEowumvW7A0PZNCQ== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1738165326; 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:autocrypt:autocrypt; bh=RjoCqeoYv2p4Qu8XufkuxZfqUFjtKs8MuQnM7xQd+2g=; b=lQQbxVzcTe1yc0IBEWMPyCu3+dZ1SH0qbLCokp/NDmLqkybyOlYEKCG5YS9sOfTODwYnM0 mBOqQYu6BTx39a0q8IQEP4tOnWUOcNFflvgITUER8ifTVabSG1Hqlu7tTn1OdzIjHOjUCo zTzhJy/V0hJLTU5oVJP7K6NVpbnmlow= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1738165326; 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:autocrypt:autocrypt; bh=RjoCqeoYv2p4Qu8XufkuxZfqUFjtKs8MuQnM7xQd+2g=; b=jcOLewHlSq0jNxHZvOx1jh1tmbvPQ5bOuYQI56+hSNi5fr0NJel2ZWPhaucY17j45I9Ga9 LRNa2a5tZBmpitAw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id CB3EC137DB; Wed, 29 Jan 2025 15:42:06 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id VJlHMU5Mmmc0VgAAD6G6ig (envelope-from ); Wed, 29 Jan 2025 15:42:06 +0000 Message-ID: Date: Wed, 29 Jan 2025 16:42:06 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 5/5] mm: completely abstract unnecessary adj_start calculation Content-Language: en-US To: Lorenzo Stoakes , Andrew Morton Cc: "Liam R . Howlett" , Jann Horn , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: From: Vlastimil Babka Autocrypt: addr=vbabka@suse.cz; keydata= xsFNBFZdmxYBEADsw/SiUSjB0dM+vSh95UkgcHjzEVBlby/Fg+g42O7LAEkCYXi/vvq31JTB KxRWDHX0R2tgpFDXHnzZcQywawu8eSq0LxzxFNYMvtB7sV1pxYwej2qx9B75qW2plBs+7+YB 87tMFA+u+L4Z5xAzIimfLD5EKC56kJ1CsXlM8S/LHcmdD9Ctkn3trYDNnat0eoAcfPIP2OZ+ 9oe9IF/R28zmh0ifLXyJQQz5ofdj4bPf8ecEW0rhcqHfTD8k4yK0xxt3xW+6Exqp9n9bydiy tcSAw/TahjW6yrA+6JhSBv1v2tIm+itQc073zjSX8OFL51qQVzRFr7H2UQG33lw2QrvHRXqD Ot7ViKam7v0Ho9wEWiQOOZlHItOOXFphWb2yq3nzrKe45oWoSgkxKb97MVsQ+q2SYjJRBBH4 8qKhphADYxkIP6yut/eaj9ImvRUZZRi0DTc8xfnvHGTjKbJzC2xpFcY0DQbZzuwsIZ8OPJCc LM4S7mT25NE5kUTG/TKQCk922vRdGVMoLA7dIQrgXnRXtyT61sg8PG4wcfOnuWf8577aXP1x 6mzw3/jh3F+oSBHb/GcLC7mvWreJifUL2gEdssGfXhGWBo6zLS3qhgtwjay0Jl+kza1lo+Cv BB2T79D4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xNRkB IBOjPdF26dMP69BwePQao1M8Acrrex9sAHYjQGyVmReRjVEtv9iG4DoTsnIR3amKVk6si4Ea X/mrapJqSCcBUVYUFH8M7bsm4CSxier5ofy8jTEa/CfvkqpKThTMCQPNZKY7hke5qEq1CBk2 wxhX48ZrJEFf1v3NuV3OimgsF2odzieNABEBAAHCwXwEGAEKACYCGwwWIQSpQNQ0mSwujpkQ PVAiT6fnzIKmZAUCZAUSmwUJDK5EZgAKCRAiT6fnzIKmZOJGEACOKABgo9wJXsbWhGWYO7mD 8R8mUyJHqbvaz+yTLnvRwfe/VwafFfDMx5GYVYzMY9TWpA8psFTKTUIIQmx2scYsRBUwm5VI EurRWKqENcDRjyo+ol59j0FViYysjQQeobXBDDE31t5SBg++veI6tXfpco/UiKEsDswL1WAr tEAZaruo7254TyH+gydURl2wJuzo/aZ7Y7PpqaODbYv727Dvm5eX64HCyyAH0s6sOCyGF5/p eIhrOn24oBf67KtdAN3H9JoFNUVTYJc1VJU3R1JtVdgwEdr+NEciEfYl0O19VpLE/PZxP4wX PWnhf5WjdoNI1Xec+RcJ5p/pSel0jnvBX8L2cmniYnmI883NhtGZsEWj++wyKiS4NranDFlA HdDM3b4lUth1pTtABKQ1YuTvehj7EfoWD3bv9kuGZGPrAeFNiHPdOT7DaXKeHpW9homgtBxj 8aX/UkSvEGJKUEbFL9cVa5tzyialGkSiZJNkWgeHe+jEcfRT6pJZOJidSCdzvJpbdJmm+eED w9XOLH1IIWh7RURU7G1iOfEfmImFeC3cbbS73LQEFGe1urxvIH5K/7vX+FkNcr9ujwWuPE9b 1C2o4i/yZPLXIVy387EjA6GZMqvQUFuSTs/GeBcv0NjIQi8867H3uLjz+mQy63fAitsDwLmR EP+ylKVEKb0Q2A== In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Score: -4.30 X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MID_RHS_MATCH_FROM(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_FIVE(0.00)[6]; FROM_EQ_ENVFROM(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.cz:mid,suse.cz:email,imap1.dmz-prg2.suse.org:helo] X-Spam-Flag: NO X-Spam-Level: On 1/27/25 16:50, Lorenzo Stoakes wrote: > The adj_start calculation has been a constant source of confusion in the > VMA merge code. > > There are two cases to consider, one where we adjust the start of the > vmg->middle VMA (i.e. the __VMG_FLAG_ADJUST_MIDDLE_START merge flag is > set), in which case adj_start is calculated as: > > (1) adj_start = vmg->end - vmg->middle->vm_start > > And the case where we adjust the start of the vmg->next VMA (i.e.t he > __VMG_FLAG_ADJUST_NEXT_START merge flag is set), in which case adj_start is > calculated as: > > (2) adj_start = -(vmg->middle->vm_end - vmg->end) > > We apply (1) thusly: > > vmg->middle->vm_start = > vmg->middle->vm_start + vmg->end - vmg->middle->vm_start > > Which simplifies to: > > vmg->middle->vm_start = vmg->end > > Similarly, we apply (2) as: > > vmg->next->vm_start = > vmg->next->vm_start + -(vmg->middle->vm_end - vmg->end) > > Noting that for these VMAs to be mergeable vmg->middle->vm_end == > vmg->next->vm_start and so this simplifies to: > > vmg->next->vm_start = > vmg->next->vm_start + -(vmg->next->vm_start - vmg->end) > > Which simplifies to: > > vmg->next->vm_start = vmg->end > > Therefore in each case, we simply need to adjust the start of the VMA to > vmg->end (!) and can do away with this adj_start calculation. The only > caveat is that we must ensure we update the vm_pgoff field correctly. > > We therefore abstract this entire calculation to a new function > vmg_adjust_set_range() which performs this calculation and sets the > adjusted VMA's new range using the general vma_set_range() function. > > We also must update vma_adjust_trans_huge() which expects the > now-abstracted adj_start parameter. It turns out this is wholly > unnecessary. > > In vma_adjust_trans_huge() the relevant code is: > > if (adjust_next > 0) { > struct vm_area_struct *next = find_vma(vma->vm_mm, vma->vm_end); > unsigned long nstart = next->vm_start; > nstart += adjust_next; > split_huge_pmd_if_needed(next, nstart); > } > > The only case where this is relevant is when __VMG_FLAG_ADJUST_MIDDLE_START > is specified (in which case adj_next would have been positive), i.e. the > one in which the vma specified is vmg->prev and this the sought 'next' VMA > would be vmg->middle. > > We can therefore eliminate the find_vma() invocation altogether and simply > provide the vmg->middle VMA in this instance, or NULL otherwise. > > Again we have an adj_next offset calculation: > > next->vm_start + vmg->end - vmg->middle->vm_start > > Where next == vmg->middle this simplifies to vmg->end as previously > demonstrated. > > Therefore nstart is equal to vmg->end, which is already passed to > vma_adjust_trans_huge() via the 'end' parameter and so this code (rather > delightfully) simplifies to: > > if (next) > split_huge_pmd_if_needed(next, end); > > With these changes in place, it becomes silly for commit_merge() to return > vmg->target, as it is always the same and threaded through vmg, so we > finally change commit_merge() to return an error value once again. > > This patch has no change in functional behaviour. > > Signed-off-by: Lorenzo Stoakes Yeah this makes the preparations worth it. Nice! Reviewed-by: Vlastimil Babka > +/* > + * Actually perform the VMA merge operation. > + * > + * On success, returns the merged VMA. Otherwise returns NULL. Needs updating? > + */ > +static int commit_merge(struct vma_merge_struct *vmg) > +{ > + struct vm_area_struct *vma; > + struct vma_prepare vp; > + bool adj_middle = vmg->merge_flags & __VMG_FLAG_ADJUST_MIDDLE_START; > > - vma_iter_config(vmg->vmi, vmg->next->vm_start + adj_start, > - vmg->next->vm_end); > + if (vmg->merge_flags & __VMG_FLAG_ADJUST_NEXT_START) { > + /* In this case we manipulate middle and return next. */ Also we don't return next anymore? At least not here. vma_merge_existing_range() does, but here it's rather "the target is next"? > + vma = vmg->middle; > + vma_iter_config(vmg->vmi, vmg->end, vmg->next->vm_end); > } else {