From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43F17C282DE for ; Mon, 10 Mar 2025 15:12:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D522B280005; Mon, 10 Mar 2025 11:12:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D007A280004; Mon, 10 Mar 2025 11:12:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B7E1D280005; Mon, 10 Mar 2025 11:12:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 991DE280004 for ; Mon, 10 Mar 2025 11:12:04 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E8AA4121146 for ; Mon, 10 Mar 2025 15:12:04 +0000 (UTC) X-FDA: 83205981768.19.D30C481 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf01.hostedemail.com (Postfix) with ESMTP id 08B2E4000D for ; Mon, 10 Mar 2025 15:12:01 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b="nNv/ixMA"; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=If69XtfG; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=IZlzDh+5; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=ClK6Yobt; dmarc=none; spf=pass (imf01.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1741619522; h=from:from:sender:reply-to:subject:subject: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:dkim-signature; bh=tyvzamKgKRqyxRt5U6QPdn/vabNysXQhbfHATPS8cPY=; b=4IJtwBTmDoE0kf5eVwaRoPdHvN6dhcYXesXWtx0GKJtBd84YRgY6hcSJg7Omk39/AKlf2g /+mPIJjbFmJjO14eJj2BEKpWSDV3dr5BTZB1q3a+Zke1AXnsxDB1N8HoSo8I1FgcAxl9sM VzYh349JlsA9yYGhItdMgnQ/0l9nU/0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1741619522; a=rsa-sha256; cv=none; b=ePhZGR7qHYv1msS2BFVKCky6nfmJlXreqxLDlqKZKGmbhQosM5ExZB+aTPvf1oGTfGra7u OMQeuGBfGj7MpOJyZn6e4SKZNEwe2Y6D99NMRO1knpxDcZ+ybtnqNd4JJiA8h5F0teXFYT vXQL2nrzYZLny7gU11WesYW5QjrfwuY= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b="nNv/ixMA"; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=If69XtfG; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=IZlzDh+5; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=ClK6Yobt; dmarc=none; spf=pass (imf01.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104: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-out2.suse.de (Postfix) with ESMTPS id EFF121F441; Mon, 10 Mar 2025 15:11:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1741619520; 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=tyvzamKgKRqyxRt5U6QPdn/vabNysXQhbfHATPS8cPY=; b=nNv/ixMAXtw5zZW7wRyqkTqnDHPVmvTQjluHZ8Eoy+3pRHn5TNaDnfOeMk9VNsSPphNdWw eC7zZs1ywRDGnSRK287KnLyIx+zICE5Q3a8tTQAk5kMrKnE1o812j1yiV1/fnZpnLZyBjt bGKCxg08/04Iuv7l4psCvSv1zjUsArs= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1741619520; 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=tyvzamKgKRqyxRt5U6QPdn/vabNysXQhbfHATPS8cPY=; b=If69XtfGXGpne+R5FJTRZgorUjnuVxrjeL8LxRyZ7K4WYsjkD59rPQ1ytu7NJdTVDUJQTo 72FjJwb026eIbSBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1741619519; 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=tyvzamKgKRqyxRt5U6QPdn/vabNysXQhbfHATPS8cPY=; b=IZlzDh+53qGN29M9vgdmB0y6ZGROZu6ynz+smH3ytfkgh/jJVrcTdy8H185u4lQ3pWfeKL +4iU/W/AWiL6ElP75/p16aqXjZLTEl+xcUQDFeKcIyIEQhhMj9k88s6TI0OubSx/uB+HBS vo10v+47WH5B4x73ZJ4TXCbgmYqh3ow= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1741619519; 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=tyvzamKgKRqyxRt5U6QPdn/vabNysXQhbfHATPS8cPY=; b=ClK6YobtRI92Py7ruEYqR+C8AuyfKOHojTiwRa7A8IplFAMydvflG/30u5WMNFiptqg1m4 +FGNfHet1kVcWdBw== 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 D2BA11399F; Mon, 10 Mar 2025 15:11:59 +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 XBjRMj8Bz2e4RgAAD6G6ig (envelope-from ); Mon, 10 Mar 2025 15:11:59 +0000 Message-ID: <99922cca-ed8e-4996-8833-a89264783b28@suse.cz> Date: Mon, 10 Mar 2025 16:11:59 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 5/7] mm/mremap: complete refactor of move_vma() Content-Language: en-US To: Lorenzo Stoakes , Andrew Morton Cc: "Liam R . Howlett" , Jann Horn , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Harry Yoo , Yosry Ahmed References: From: Vlastimil Babka In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Action: no action X-Stat-Signature: nskk4do47d6xizzmzqo8hpg3jbuyeujo X-Rspam-User: X-Rspamd-Queue-Id: 08B2E4000D X-Rspamd-Server: rspam04 X-HE-Tag: 1741619521-814591 X-HE-Meta: U2FsdGVkX1/4XerJXNZcBGvSJdPlykXsDUK1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UaD7avyFFy5KHxm2edeCl3WKPfW1OwYtiU5jGKqbEGDsSNUUuJBdO+jdrz2+DO+WJ44EDbmK3LGflTglmbVbAUOizn4uKfP5TA2Oy9j/KeBdHul6G3nNiOfIN1eqjaa5uarkPQWy3ZKsFwS0nAyAoRk9yka+pMPD2Q77F5IniapY2YhXkTeyVmg+7E2zRC+KRNaClkn9U3zTFrZQcjgAiPGQ3eZ/ulyac7kWYlJw X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 3/6/25 11:34, Lorenzo Stoakes wrote: > We invoke ksm_madvise() with an intentionally dummy flags field, so no > need to pass around. > > Additionally, the code tries to be 'clever' with account_start, > account_end, using these to both check that vma->vm_start != 0 and that we > ought to account the newly split portion of VMA post-move, either before > or after it. > > We need to do this because we intentionally removed VM_ACCOUNT on the VMA > prior to unmapping, so we don't erroneously unaccount memory (we have > already calculated the correct amount to account and accounted it, any > subsequent subtraction will be incorrect). > > This patch significantly expands the comment (from 2002!) about > 'concealing' the flag to make it abundantly clear what's going on, as well > as adding and expanding a number of other comments also. > > We can remove account_start, account_end by instead tracking when we > account (i.e. vma->vm_flags has the VM_ACCOUNT flag set, and this is not > an MREMAP_DONTUNMAP operation), and figuring out when to reinstate the > VM_ACCOUNT flag on prior/subsequent VMAs separately. > > We additionally break the function into logical pieces and attack the very > confusing error handling logic (where, for instance, new_addr is set to > err). > > After this change the code is considerably more readable and easy to > manipulate. > > Signed-off-by: Lorenzo Stoakes Reviewed-by: Vlastimil Babka Some nits below: > +/* > + * Unmap source VMA for VMA move, turning it from a copy to a move, being > + * careful to ensure we do not underflow memory account while doing so if an > + * accountable move. > + * > + * This is best effort, if we fail to unmap then we simply try this looks like an unfinished sentence? > @@ -1007,51 +1157,15 @@ static unsigned long move_vma(struct vma_remap_struct *vrm) > */ > hiwater_vm = mm->hiwater_vm; This... > - /* Tell pfnmap has moved from this vma */ > - if (unlikely(vma->vm_flags & VM_PFNMAP)) > - untrack_pfn_clear(vma); > - > - if (unlikely(!err && (vrm->flags & MREMAP_DONTUNMAP))) { > - /* We always clear VM_LOCKED[ONFAULT] on the old vma */ > - vm_flags_clear(vma, VM_LOCKED_MASK); > - > - /* > - * anon_vma links of the old vma is no longer needed after its page > - * table has been moved. > - */ > - if (new_vma != vma && vma->vm_start == old_addr && > - vma->vm_end == (old_addr + old_len)) > - unlink_anon_vmas(vma); > - > - /* Because we won't unmap we don't need to touch locked_vm */ > - vrm_stat_account(vrm, new_len); > - return new_addr; > - } > - > - vrm_stat_account(vrm, new_len); > - > - vma_iter_init(&vmi, mm, old_addr); > - if (do_vmi_munmap(&vmi, mm, old_addr, old_len, vrm->uf_unmap, false) < 0) { > - /* OOM: unable to split vma, just get accounts right */ > - if (vm_flags & VM_ACCOUNT && !(vrm->flags & MREMAP_DONTUNMAP)) > - vm_acct_memory(old_len >> PAGE_SHIFT); > - account_start = account_end = false; > - } > + vrm_stat_account(vrm, vrm->new_len); > + if (unlikely(!err && (vrm->flags & MREMAP_DONTUNMAP))) > + dontunmap_complete(vrm, new_vma); > + else > + unmap_source_vma(vrm); > > mm->hiwater_vm = hiwater_vm; ... and this AFAICS only applies to the unmap_source_vma() case. And from the comment it seems only to the "undo destination vma" variant. BTW this also means the unmap_source_vma() name is rather misleading? So I think the whole handling of that hiwater_vm could move to unmap_source_vma(). It should not matter if we take the snapshot of hiwater_vm only after vrm_stat_account() bumps the total_vm. Nobody else can be racing with us to permanently turn that total_vm to a hiwater_vm. (this is probably a potential cleanup for a followup-patch anyway) > > - /* Restore VM_ACCOUNT if one or two pieces of vma left */ > - if (account_start) { > - vma = vma_prev(&vmi); > - vm_flags_set(vma, VM_ACCOUNT); > - } > - > - if (account_end) { > - vma = vma_next(&vmi); > - vm_flags_set(vma, VM_ACCOUNT); > - } > - > - return new_addr; > + return err ? (unsigned long)err : vrm->new_addr; > } > > /* > -- > 2.48.1