Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2077366ybv; Fri, 14 Feb 2020 11:02:03 -0800 (PST) X-Google-Smtp-Source: APXvYqzladsDIBcATkbnmyLoV7pL/4ECjFYpZBwhd5Hp1lBhV1tZgKCOwvFWEtAVVvZyF9nnwlaR X-Received: by 2002:a9d:7c99:: with SMTP id q25mr3522085otn.105.1581706923053; Fri, 14 Feb 2020 11:02:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581706923; cv=none; d=google.com; s=arc-20160816; b=d8x1iJdLmki48TffRgk1TKEZYTehj0Ay062bM4tkkxeuucqR3LTVjTmRkqmvxKRv7P Khl14ZbuvQFYIlEHYf/Rm1ZYrN/1XzPZ9qC8EurdIpmnJJHN7D/DTDHnitN2l/LoXnqv flGrYNufut7zM3pnSLIUZhPltX53mMJeh4vgJ8HNSdZ5xgKDn/YbaorhwY5giU0aFveb JALrHUu7eIfqgPUZlk4a9Kk16QK2v59kkX02k/fFLqFNBCnSZeGKLGXyTsq/ukru+S3u zKadan5ihiO6Yfgvx0DbhLCaNcPHQyrDkCoAaHd4qyVgv0Xy1tNYz9Mr4/V6YZ9xvUY1 EIuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=SiWqUpdh4i6ijlaIGQ6h9I93/rzJCmHhBbrF7O5i8NM=; b=LGUguJSx2FStFpXG0MA8SuGVn1/n9JSYCEfv2RsoQot7h/AGsL4GRhEwZVnMaGlQm3 9aiJE0aamIHvFitpJw5lmbuTpO+3cr8QpTrhk4sHH4D9UUxMhXIEpNHByz3weieJPKtv tRUeMsGTY7dgXOiQ37x87P9LKjHPIw/HSy2WWjcmYbgan2m2k2pwpW/Dpid5w80yC69A 03Ayl6CUrpxthAgAHM0NNKStTW1sok8FrLAFZZehiu6VtuxMHx60ocblxLNB6LDv240T pYmAAchvikW4GKpeKTKRAcUDWqMkLdbJDPPjdTNjyWrmLP/+cjmYz9ZjFeGjxEpdQ/3Q kqHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Q0XhyH6X; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d26si3198072otc.6.2020.02.14.11.01.50; Fri, 14 Feb 2020 11:02:02 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Q0XhyH6X; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389486AbgBNSrN (ORCPT + 99 others); Fri, 14 Feb 2020 13:47:13 -0500 Received: from mail-ed1-f53.google.com ([209.85.208.53]:33220 "EHLO mail-ed1-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729556AbgBNSrM (ORCPT ); Fri, 14 Feb 2020 13:47:12 -0500 Received: by mail-ed1-f53.google.com with SMTP id r21so12342393edq.0 for ; Fri, 14 Feb 2020 10:47:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=SiWqUpdh4i6ijlaIGQ6h9I93/rzJCmHhBbrF7O5i8NM=; b=Q0XhyH6Xlhrpl2gwB631HswEjt4oCyWy3lLVptoDwWxl0HG3Q3C88cYuIRC4OBPf6w auhtMnSkhu8J6PeB+HyjnZFro/At+yFVhwPKlWilHic9FKPWhKmFaP4DkfrtfAuum9mS LUqDX+lJPCC892vzT6mJ5j1dI9Xpb7ZjQU85WsW6rnIZ4F4jbixPzQ6fZYsdSYa/0g53 jUmfiKCGOYHTOgfi/GWQ02YtcPM5tphrucdmwLiHMuMdGPgTQCfhlIT5CyRdNbli20Sh T7HcP9jyAHwBdOPmJdubumHSDbsffx622LiG2f5QAwsrbLB7JjhMhSLPs6tG9rngpZjJ O3gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SiWqUpdh4i6ijlaIGQ6h9I93/rzJCmHhBbrF7O5i8NM=; b=B5/zUrtG5zZfwESOhsu/yaT5oX1eAhBtiteQdGKh4Km7DFWOOWvuHQOUnDUzevOctU oL4fVMs61qJ+htDc8fHe5EliT2TFn4nXe3XgmDzFNIsRq2rLTxoKnfYuVggRrG8wz1pd 1hhc75Gz8YqV3zvAUoIqPx6Alp6rJpyjF0IQFbFLZ/SiLeenuCcZ9zLRGyRdN2Ak+qZp qMUQhBYJAA92iBCb5pbkrfX4rInpmGd48wH9O+AG09wCakPMhxNAb2xabmTgTt0YR1C2 So1wPX5WYu085/+1d2OASxia8qoCWpoUHP8spybesBrkTqS+CO38l6UUNap3tz3Ogwn1 RXJQ== X-Gm-Message-State: APjAAAUqDg2UqlemTeKf2mgNJnFurXrQpmFWhAKS+8TSOJfYGc9Itj7B Vy7nrqhzHMFf5YjqcfmlhnPym3tHxiLoKZVcNWBNSg== X-Received: by 2002:a05:6402:61a:: with SMTP id n26mr3817911edv.135.1581706029817; Fri, 14 Feb 2020 10:47:09 -0800 (PST) MIME-Version: 1.0 References: <20200207201856.46070-1-bgeffon@google.com> <20200214040952.43195-1-bgeffon@google.com> <20200214142857.kcmjiequhfl3sot2@box> In-Reply-To: <20200214142857.kcmjiequhfl3sot2@box> From: Brian Geffon Date: Fri, 14 Feb 2020 10:46:43 -0800 Message-ID: Subject: Re: [PATCH v5 1/2] mm: Add MREMAP_DONTUNMAP to mremap(). To: "Kirill A. Shutemov" Cc: Andrew Morton , "Michael S . Tsirkin" , Arnd Bergmann , LKML , linux-mm , Linux API , Andy Lutomirski , Will Deacon , Andrea Arcangeli , Sonny Rao , Minchan Kim , Joel Fernandes , Yu Zhao , Jesse Barnes , Nathan Chancellor , Florian Weimer Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Kirill, > > - if (vm_flags & VM_LOCKED) { > > - mm->locked_vm += new_len >> PAGE_SHIFT; > > - *locked = true; > > - } > > - > > Ah. You moved this piece. Why? Because we're not unmapping, do_munmap would have adjusted mm->locked_vm by decreasing it by old_len so then we have to add back in the new_len in the normal case (non. MREMAP_DONTUNMAP), but since we're not doing the unmap I want to skip the increase by new_len and just adjust accordingly. In the MREMAP_DONTUNMAP case if the VMA got smaller then the do_munmap on that portion would have decreased it by new_len - old_len, and the accounting is correct. In the case of an unchanged VMA size there is nothing to do, but in the case where it grows we're responsible for adding new_len - old_len because of the decision to jump that block and now the accounting is right for all cases. If we were to leave the original block and not jump over it then we would have to remove old_len bytes and then we're doing the same thing but now special casing the situation where new_len < old_len because the unmap on the removed part would have reduced it by new_len - old_len so backing old_len would be too much and we'd have to add back in new_len - old_len. I hope that explains it all. By doing it this way, IMO it makes it easier to see how the locked_vm accounting is happening because the vm_locked incrementing happens in only one of two places based on the type of remap that is happening. But I definitely can clean up the code a bit to drop the levels of indentation, maybe this: /* * locked_vm accounting: if the mapping remained the same size * it will have just moved and we don't need to touch locked_vm * because we skip the do_unmap. If the mapping shrunk before * being moved then the do_unmap on that portion will have * adjusted vm_locked. Only if the mapping grows do we need to * do something special; the reason is locked_vm only accounts * for old_len, but we're now adding new_len - old_len locked * bytes to the new mapping. */ if (vm_flags & VM_LOCKED && new_len > old_len) { mm->locked_vm += (new_len - old_len) >> PAGE_SHIFT; *locked = true; } /* We always clear VM_LOCKED[ONFAULT] on the old vma */ vma->vm_flags &= VM_LOCKED_CLEAR_MASK; goto out; } Having only one place where locked_vm is accounted and adjusted based on the type of remap seems like it will be easier to follow and less error prone later. What do you think about this? > > + if (flags & MREMAP_FIXED) { > > I think it has to be > > if (!(flags & MREMAP_DONTUNMAP)) { > > No? No. Because we dropped the requirement to use MREMAP_FIXED with MREMAP_DONTUNMAP, if we're not using MREMAP_FIXED we don't need to unmap anything at dest if it already exists because get_unmapped_area() below will not be using the MAP_FIXED flag either, instead it will search for a new unmapped area. If we were to change it then we wouldn't be able to do MREMAP_FIXED | MREMAP_DONTUNMAP, so I think this is correct. > > - if (flags & MREMAP_FIXED) { > > + if (flags & MREMAP_FIXED || flags & MREMAP_DONTUNMAP) { > > if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) { Sure, I can change that. If you're good with all of that I can mail a new patch today. Thanks again, Brian