Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp89111rwd; Fri, 19 May 2023 15:59:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7Afq+/gCnSuo2viYcc7CvVaLhJrGb6GnNF+iEf5Rf014xKa/ThDc5lyjHY9+Y4Uqel7ivm X-Received: by 2002:a17:902:eccc:b0:1ac:76a6:a1f with SMTP id a12-20020a170902eccc00b001ac76a60a1fmr8460477plh.16.1684537187427; Fri, 19 May 2023 15:59:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684537187; cv=none; d=google.com; s=arc-20160816; b=oOm38kTcnrpdISg8XYOf4X96Jt3h3T1syVqAiWwwoO3lD4XkNrZ+HyX+REeOrqkzl6 lkg7YucNH0eO7gHo4y6/03reP/WTWP/Htzu+0hPF67z6lbJKF7GAd+T4pBh4u7nNgGPd Ell0IMGpKxwnZy6+z1IK6xUdfkv2fMSIkHxqr0mdgx2N36NBQVouliGR4HT6xRTbYuWu io3XH6KEwb0da43z8GKhFYG9kE2bkY2D0t+aNPzTptzym4GnkZvq117pX2VpBe8q51qr zEh02OGjcREtszyAA0KUo0ZUyrrKlRU6f0kyoiHmbjfw5M2u1g/zLMUP25WbAXyu0WRn zmGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=+/3ocS24eoYwGfIiVCx+gEjnY3f1UvtwZkxMJpeF2sY=; b=w625IvYyN9sJtzrZ2R2mdc4cRjnHtoTKvyR0T2xZz1PXHzZ8u3Tzi6x1lbqe35B+Fa TByC1ubURP1kVckaudvW7uzHA5XsakytHVL3FJoTYaYgKt/DOC1W7ahkNiMtXY0hM9YD XHbmlgkCQChXq15eImAXa6Em4+utOdZ/rjbvslcfVtuOVD+/WSRTHHzIm8UDur5DExA+ m++F9Onqq6nzfe5m0YnEb2lrR2VWWODRKPX4uV78kSrbccj8qzPazWqoKcei7ikrcjAq 4MFeWc/TDUXpeE0aHhuCmCX3LPvF5Kcm0UjrQxwmXVw9cVyZgrMlbnjsoJQKSELO/75L QRKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=Bm7Gn5uU; 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 a10-20020a170902ee8a00b001ac82e8491dsi231694pld.345.2023.05.19.15.59.35; Fri, 19 May 2023 15:59:47 -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=@joelfernandes.org header.s=google header.b=Bm7Gn5uU; 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 S229508AbjESWwn (ORCPT + 99 others); Fri, 19 May 2023 18:52:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229534AbjESWwm (ORCPT ); Fri, 19 May 2023 18:52:42 -0400 Received: from mail-yw1-x112b.google.com (mail-yw1-x112b.google.com [IPv6:2607:f8b0:4864:20::112b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5DA7101 for ; Fri, 19 May 2023 15:52:40 -0700 (PDT) Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-55db055b412so21065477b3.0 for ; Fri, 19 May 2023 15:52:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1684536760; x=1687128760; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=+/3ocS24eoYwGfIiVCx+gEjnY3f1UvtwZkxMJpeF2sY=; b=Bm7Gn5uUyfgUHvaKPJ4LmsdJPZK2xKqqBjYyzSawEtA1LUx1q99FH0bUZ8bhJXkqx0 BjSWlp0rUtqb3tM3m9ZuvGEVk+kVvX/XEEubgANORpKrLPx4JR4A+3HXCeNbJPmfCoLt s76O1K/E4hO/SxtsdXpVaFvXHjR+cf5KY0bJ4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684536760; x=1687128760; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+/3ocS24eoYwGfIiVCx+gEjnY3f1UvtwZkxMJpeF2sY=; b=B6dbgfFTjpSoMuaWAlYWWqYa8gvXdet/nUQoQbb9lIT2NKS9pBTj0t/RmJxQOAPu7e +gzvbCcvIbTy9BlBi2983IpYQzNHsibSKMa4h1GSrl9xV0Xm2JbZBJOe668M63lmkS3c Hz0fqwwGpwm1g9Z2Z/QFopsA81C1GZ65mvILEzozhrpxTO5ZTRGTDlQa9PiYA3djSLys yrlUoemX9Tnat9FFsLYlj4PdaQrXVTu9nxdNppvsdKYXuDIn4W/LUkaXAxmb3oMktJ8I 9LVxjSNaBLAWAH7PxpksbjK1rd2CCl4FdAzofANCXlYlYRK3WvMIUHlaQy1kNrHJZhYY kurA== X-Gm-Message-State: AC+VfDw1+sirFZm3f3BEAygayD8uI7tkqi3peLBPdrbbi4Yb1h1FuSAZ bsuSUgSPTUgctvRsw+6p7Bmpsc1Ew466HL6muijmXA== X-Received: by 2002:a81:9b0a:0:b0:561:b5c7:d764 with SMTP id s10-20020a819b0a000000b00561b5c7d764mr3602262ywg.18.1684536759920; Fri, 19 May 2023 15:52:39 -0700 (PDT) MIME-Version: 1.0 References: <20230519190934.339332-1-joel@joelfernandes.org> <20230519190934.339332-2-joel@joelfernandes.org> In-Reply-To: From: Joel Fernandes Date: Fri, 19 May 2023 18:52:28 -0400 Message-ID: Subject: Re: [PATCH v2 1/4] mm/mremap: Optimize the start addresses in move_page_tables() To: Linus Torvalds Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Lorenzo Stoakes , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,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 Hi Linus, On Fri, May 19, 2023 at 3:21=E2=80=AFPM Linus Torvalds wrote: > > On Fri, May 19, 2023 at 12:09=E2=80=AFPM Joel Fernandes (Google) > wrote: > > > > +static bool check_addr_in_prev(struct vm_area_struct *vma, unsigned lo= ng addr, > > + unsigned long mask) > > +{ > > + int addr_masked =3D addr & mask; > > + struct vm_area_struct *prev =3D NULL, *cur =3D NULL; > > + > > + /* If the masked address is within vma, there is no prev mappin= g of concern. */ > > + if (vma->vm_start <=3D addr_masked) > > + return false; > > Hmm. > > I should have caught this last time, but I didn't. > > That test smells bad to me. Or maybe it's just the comment. > > I *suspect* that the test is literally just for the stack movement > case by execve, where it catches the case where we're doing the > movement entirely within the one vma we set up. Yes that's right, the test is only for the stack movement case. For the regular mremap case, I don't think there is a way for it to trigger. > But in the *general* case I think the above is horribly wrong: if you > want to move pages within an existing mapping, the page moving code > can't just randomly say "I'll expand the area you wanted to move". > Again, in that general mremap() case (as opposed to the special stack > moving case for execve), I *think* that the caller has already split > the vma's at the point of the move, and this test simply cannot ever > trigger. Yes, the test simply cannot ever trigger for mremap() but we still need the test for the stack case. That's why I had considered adding it and I had indeed reviewed the stack case when adding the test. I could update the comment to mention that, if you want. > So I think the _code_ works, but I think the comment in particular is > questionable, and I'm a bit surprised about the code too,. because I > thought execve() only expanded to exactly the moving area. It expands to cover both the new start and the old end of the stack AFAICS: /* * cover the whole range: [new_start, old_end) */ if (vma_expand(&vmi, vma, new_start, old_end, vma->vm_pgoff, NULL= )) return -ENOMEM; In this case, it will trigger for the stack because this same expanded vma is passed as both the new vma and the old vma to move_page_tables(). */ if (length !=3D move_page_tables(vma, old_start, vma, new_start, length, false)) return -ENOMEM; So AFAICS, it is possible that old_start will start later than this newly expanded VMA. So for such a situation, old_start can be realigned to PMD and the test allows that by saying it need not worry about aligning down to an existing VMA. > End result: I think the patch on the whole looks nice, and smaller > than I expected. I also suspect it works in practice, but I'd like > that test clarified. Does it *actually* trigger for the stack moving > case? Because I think it must (never* trigger for the mremap case? You are right that the test will not trigger for the mremap case. But from a correctness standpoint, I thought of leaving it there for the stack (and who knows for what other future reasons the test may be needed). I can update the comment describing the stack if you like. > And maybe I'm the one confused here, and all I really need is an > explanation with small words and simple grammar starting with "No, > Linus, this is for case xyz" Hopefully it is clear now and you agree. Let me know if you want me to do something more. I can make some time next week to trace the stack case a bit more if you like and report back on any behaviors, however the mremap tests I did are looking good and working as expected. thanks, - Joel