Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp621014pxb; Wed, 3 Feb 2021 13:20:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJyA28/wlvKm005keZQpwCru4O/BFMLyGdpNUbetAYe1vp2WHgZFNsnz+slRRPUOqG9TEA/N X-Received: by 2002:a17:906:1741:: with SMTP id d1mr5256504eje.182.1612387255830; Wed, 03 Feb 2021 13:20:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612387255; cv=none; d=google.com; s=arc-20160816; b=YQgwCC2Zr/Jt9P4qRFIy78ECH6jXbhGqbu4RHIIBt78HLLeasB5QfoD6ZbTqkTgnKj u8X+7TbRD90BZYuje8QZqJmJw6wsV/YRipoWSqPHjwQaP1T4t0sukThxU1MJR0vu/T/8 QNZNO85P7zTmu1tnxQiMQzvxpc+cqkpeICK3FdvCj//8WN557q4a8/6YSHfSGfiUmPDp StfSbO2vg7J3hTkEc7vHlCfHZEAB3lzUEp1hhUS6pxp7phCq63NnqqAYOEWF2hvobFBl QtP18e8D1H4atu/0IQa0nYwAEyDIMxPUgM9FA0e0isXDgJc9m+ChHQF6Zqic3YuXweAl tmBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=i+u6BTILoUVaAAqMvPP05q1+sYeBPgAq4q8dfomuEGM=; b=aepOI0bXEKjuYou3KlscDLpZ6h0g3CvfKKp/cZKO3G9pomYYYwlVZca5rVXLS42eTS /hpiQizVafb5vju223eaWa9Pz/9siL6/l9ZTjmMwVLAMWux0SaMjeMuQt6QQKW857ft3 0IIR9MAUlbokckRA7nwe7JgsxGd1OgowOCN8U3nHUXQN2kAgV5nT6CpGI0Em1p8dU2nD FEcDaRgjpGnIVRScdfWD3lMnnAiqMeKHXRI7p4eqlHYYQedJ9Z8rlQI8MqPB5dAhbdTD lJD7OlJOSqtlCxhTsXSviA/F+E3QVPdDgAg3kKmPvxMzbw320FztiXDCFAbwicV9qsV1 UHlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=WEU9PqqZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o26si2151633ejg.360.2021.02.03.13.20.30; Wed, 03 Feb 2021 13:20:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=WEU9PqqZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232226AbhBCVQG (ORCPT + 99 others); Wed, 3 Feb 2021 16:16:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231592AbhBCVQE (ORCPT ); Wed, 3 Feb 2021 16:16:04 -0500 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [IPv6:2a00:1450:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6436AC061573 for ; Wed, 3 Feb 2021 13:15:24 -0800 (PST) Received: by mail-lj1-x230.google.com with SMTP id m22so779187ljj.4 for ; Wed, 03 Feb 2021 13:15:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=i+u6BTILoUVaAAqMvPP05q1+sYeBPgAq4q8dfomuEGM=; b=WEU9PqqZm2T0n4ETvzj8mQa1byAVBWQqIYe3UmQcn18OJkbNKbZTIyyJA1vcHeB9Qh exvPK/JEtrSWN7eF8haKy1SWYJ64eE/yz8Kr6mQ/5HqBIf2LUWRyeyuaTGlpm3LtIdfi QdkMo2BqRJIdo5qA25F+mrnvHdLId1tZ6k2cY= 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=i+u6BTILoUVaAAqMvPP05q1+sYeBPgAq4q8dfomuEGM=; b=dx5LbTpkMWxic6dMgNGqOytyT2ZcOMAZpGu+DzZGTLE+IEz6iYmDQuauXai/84+ePW bdbzJMVpDvMQRTs7BS39wpY8mCxbY/VejuaS4MCYdKKHVb9SgfnY9wbuG60feoSWxAKP AVV2XFcTKMitKsfYKb3RGlJL4N2twvuiH/1lLtBSQHBcGPJ9Ai5QrYyVZ0Ikj5qr7e0l UVt009GeaypOrKWcxhMlnWdIGqcEKqMgLom88HfWow5EVKXInLCND6tCpnsYOucHQZXy 3ELhtVV6w4cT/c8EOqkV+EptzMvCk0wQV7y8l1bGe5PKp6wyBF+08k/sVkBOpRuoOsLG PJIg== X-Gm-Message-State: AOAM5314ST/tSdmesYu6I77zDpaMV5w8dbjWl3gwQIAzt8kwL4Vi3m+2 2uwbqUtyCxErKnzebpRzYjMiDWexptPg5g== X-Received: by 2002:a2e:54d:: with SMTP id 74mr2726052ljf.44.1612386922526; Wed, 03 Feb 2021 13:15:22 -0800 (PST) Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com. [209.85.167.47]) by smtp.gmail.com with ESMTPSA id q1sm349988lfu.48.2021.02.03.13.15.20 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Feb 2021 13:15:20 -0800 (PST) Received: by mail-lf1-f47.google.com with SMTP id v24so1260062lfr.7 for ; Wed, 03 Feb 2021 13:15:20 -0800 (PST) X-Received: by 2002:ac2:5502:: with SMTP id j2mr2651150lfk.421.1612386919848; Wed, 03 Feb 2021 13:15:19 -0800 (PST) MIME-Version: 1.0 References: <20210203210832.113685-1-peterx@redhat.com> <20210203210832.113685-5-peterx@redhat.com> In-Reply-To: <20210203210832.113685-5-peterx@redhat.com> From: Linus Torvalds Date: Wed, 3 Feb 2021 13:15:03 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 4/4] hugetlb: Do early cow when page pinned on src mm To: Peter Xu Cc: Linux Kernel Mailing List , Linux-MM , Wei Zhang , Matthew Wilcox , Jason Gunthorpe , Gal Pressman , Christoph Hellwig , Andrea Arcangeli , Jan Kara , Kirill Shutemov , David Gibson , Mike Rapoport , Mike Kravetz , Kirill Tkhai , Jann Horn , Andrew Morton Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 3, 2021 at 1:08 PM Peter Xu wrote: > > This is the last missing piece of the COW-during-fork effort when there're > pinned pages found. One can reference 70e806e4e645 ("mm: Do early cow for > pinned pages during fork() for ptes", 2020-09-27) for more information, since > we do similar things here rather than pte this time, but just for hugetlb. No issues with the code itself, but.. Comments are good, but the comments inside this block of code actually makes the code *much* harder to read, because now the actual logic is much more spread out and you can't see what it does so well. > + if (unlikely(page_needs_cow_for_dma(vma, ptepage))) { > + /* This is very possibly a pinned huge page */ > + if (!prealloc) { > + /* > + * Preallocate the huge page without > + * tons of locks since we could sleep. > + * Note: we can't use any reservation > + * because the page will be exclusively > + * owned by the child later. > + */ > + put_page(ptepage); > + spin_unlock(src_ptl); > + spin_unlock(dst_ptl); > + prealloc = alloc_huge_page(vma, addr, 0); > + if (!prealloc) { > + /* > + * hugetlb_cow() seems to be > + * more careful here than us. > + * However for fork() we could > + * be strict not only because > + * no one should be referencing > + * the child mm yet, but also > + * if resources are rare we'd > + * better simply fail the > + * fork() even earlier. > + */ > + ret = -ENOMEM; > + break; > + } > + goto again; > + } > + /* > + * We have page preallocated so that we can do > + * the copy right now. > + */ > + hugetlb_copy_page(vma, dst_pte, addr, ptepage, > + prealloc); > + put_page(ptepage); > + spin_unlock(src_ptl); > + spin_unlock(dst_ptl); > + prealloc = NULL; > + continue; > + } Can you move the comment above the code? And I _think_ the prealloc conditional could be split up to a helper function (which would help more), but maybe there are too many variables for that to be practical. Linus