Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp857227rdb; Thu, 30 Nov 2023 23:39:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IFLX0+tp2laBBkzKdI17sX+gAjG5L5hiZAjux8DRwwyRG0O8v5IZQMxz8pARD84oKd94jWT X-Received: by 2002:a17:902:d5cb:b0:1cf:5629:a0c6 with SMTP id g11-20020a170902d5cb00b001cf5629a0c6mr20959180plh.13.1701416375438; Thu, 30 Nov 2023 23:39:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701416375; cv=none; d=google.com; s=arc-20160816; b=POBcd1KwH7+EDCyktvOCrK9OORP+o2ny7TisVbhc6CdV6PuFiQYHi5tqgsmOil3PZN 3Z3Dxf/PFeYOY5HRGqm/pc1HbxjmqBPRNqMwAujsBYGPxogOkLRUIvbfOm9vemzUcZpW acuwdDDFKG9Xd4W5mQX3LZ3+ue2h9BzbqCCrifUi0ft+Opj721N6KB96f5fuT0LYm3ze tKitCpKKdkFp5VxD65b1LzeNbPx+rVssA/E6Q9qpIOdv28worEK78UDeQDisG0mma8Nd MlNb8H+cddHL+kGyjoDJgLAZckFZ8Q0CozoS5xqqJOfKYyzk7gq05mKWr6pu+jzKv6y6 rZUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=mnJ59VAhsdbO2avTSjDqatw8/IsdchoyFGlEW3iQESU=; fh=658IzL/FnSGry58xenYrijK6lVYrX2ZKm8jUOjcb14s=; b=V9x9FJPq/eArd7Wie1dozJ48KQXXdfzs7I8Q5YGyJ9MbgOMZI/de0RqtOywNumNOFf Tt1H4TMjYzI0VBHfkk/gjOIAlU7n4pUnK6SzVaHWAOy2b7sRLtxN4Pyzal/4mr0GEEdT KZ4RRt3xQDIlfFentAzQc4fTQhXS9a1tPsHg+PahbJdcoCL2TD3PVGzVqCCTxyvUYnkj oCjbWE7Fv45tg1Fm1g7uVGuAJBU5mcgdWXQnI0Hihh/c1VwcmMDK6ySv0QLFKTOIGFcQ YGsMx3lR9SucEwcBBKLm7+eHx1gkXkF6dw6eqBA5vtTIHw8i43S4src4jgBhG65CY9/V 8ncA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qRVomScx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id l2-20020a170903244200b001cc15d1ac7csi2852389pls.563.2023.11.30.23.39.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 23:39:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qRVomScx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id CB8FA801B81E; Thu, 30 Nov 2023 23:39:32 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377727AbjLAHjS (ORCPT + 99 others); Fri, 1 Dec 2023 02:39:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229496AbjLAHjR (ORCPT ); Fri, 1 Dec 2023 02:39:17 -0500 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB0C110F3 for ; Thu, 30 Nov 2023 23:39:23 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6cdd13c586fso1706067b3a.0 for ; Thu, 30 Nov 2023 23:39:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701416363; x=1702021163; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=mnJ59VAhsdbO2avTSjDqatw8/IsdchoyFGlEW3iQESU=; b=qRVomScxrXIjbm/Bb9otC5RcEmqHv/K0QizV31f/WQUwIKJrEDglK/dsDcip3aPJCT 7eKjn31YPgAvtYsW2tDZpTaz35lj4xdYZULXG4xgfn8T/MQwvazJYRdZntsvlLMicsfA 8YEHq/pK4XYYTlSSAJC0KRWeZxBOLj6Bs+HVgFNIiJm8705F7WYDThcAcUqZ6/6ZPsOm gavLipju/yRz/dGR8p/3H4ZFIJLVQLfdA9QdxqJtkVHBMCL2SyM1IiDD0uWbVR8nMx+1 OkaOKvtD6apmyf9Bt2THR/9IzFVE8LbuVF0z47nz8n1+qZUlnxrWQUDFucN3nlSCqcGa l8Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701416363; x=1702021163; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=mnJ59VAhsdbO2avTSjDqatw8/IsdchoyFGlEW3iQESU=; b=RJ80ti93zw0s59JdGnC5g8xL4PMJcce4q3q1Fx+Eb/ZHFRooAjKHdnMPX+1UjNU04f tAErkgEmD/3ermf/XAzLsVuJkb0dLGAkTBy15masznHuuevc5EtEHXmX0IHfsTt32JYP sscZuHTuBSrsQoM0T5RzuZTn1ofFYl2Rts3h8onJPD8wBJ/7aEVanTEAZS8DFh2oepP3 pq3tB4mxJ8Kh4A3B5nWOJZSQK92nfMe8npHEG6EbFkNNeGttahqw15kWlRpzYHTgE5E6 lyp9vJFV4i9AAzA3WAK4YLjRRUfxXEbKjuYz/MS/eYZh+uaIenN3a/xMppl1+UVQvlnS ZqsA== X-Gm-Message-State: AOJu0Yxhyp4so1DWOWWsC/iRwXoEzJjjNAybhkKI8gx/hfT9PcXrNbiN 5+4VY0SkoYf1fHeTDOVaA2SKfQ== X-Received: by 2002:a05:6a00:2d83:b0:6cd:e320:b0d5 with SMTP id fb3-20020a056a002d8300b006cde320b0d5mr6073415pfb.12.1701416363180; Thu, 30 Nov 2023 23:39:23 -0800 (PST) Received: from google.com (170.102.105.34.bc.googleusercontent.com. [34.105.102.170]) by smtp.gmail.com with ESMTPSA id k24-20020a63f018000000b0057825bd3448sm2411730pgh.51.2023.11.30.23.39.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 23:39:22 -0800 (PST) Date: Fri, 1 Dec 2023 07:39:19 +0000 From: Carlos Llamas To: Alice Ryhl Cc: Arve =?iso-8859-1?B?SGr4bm5lduVn?= , Christian Brauner , Greg Kroah-Hartman , Joel Fernandes , kernel-team@android.com, linux-kernel@vger.kernel.org, Martijn Coenen , Suren Baghdasaryan , Todd Kjos Subject: Re: [PATCH 19/21] binder: perform page allocation outside of locks Message-ID: References: <20231102185934.773885-20-cmllamas@google.com> <20231107090843.261410-1-aliceryhl@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231107090843.261410-1-aliceryhl@google.com> X-Spam-Status: No, score=-4.6 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FSL_HELO_FAKE,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SORTED_RECIPS,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 30 Nov 2023 23:39:32 -0800 (PST) On Tue, Nov 07, 2023 at 09:08:43AM +0000, Alice Ryhl wrote: > I would really like a comment on each function explaining that: > > * The binder_allocate_page_range function ensures that existing pages > will not be reclaimed by the shrinker. > * The binder_get_page_range function ensures that missing pages are > allocated and inserted. Ok, I think I rather go for a better naming than compensating through comments, so I came up with the following names: - binder_lru_freelist_{add,del}() - binder_install_buffer_pages() There will be more details in the v2. The new names give a clear separation of the scope of these function. > > mmap_write_lock(alloc->mm); > > + if (lru_page->page_ptr) > > + goto out; > > Another comment that I'd like to see somewhere is one that says > something along these lines: > > Multiple processes may call `binder_get_user_page_remote` on the > same page in parallel. When this happens, one of them will allocate > the page and insert it, and the other process will use the mmap > write lock to wait for the insertion to complete. This means that we > can't use a mmap read lock here. > I've added a shorter version of this to v2, thanks. > > + /* mark page insertion complete and safe to acquire */ > > + smp_store_release(&lru_page->page_ptr, page); > > [snip] > > + /* check if page insertion is marked complete by release */ > > + if (smp_load_acquire(&page->page_ptr)) > > + continue; > > We already discussed this when I asked you to make this an acquire / > release operation so that it isn't racy, but it could use a comment > explaining its purpose. I've wrapped these calls into inline functions with better names in v2. The purpose should now be evident. > > > mmap_write_lock(alloc->mm); > > + if (lru_page->page_ptr) > > + goto out; > > + > > if (!alloc->vma) { > > pr_err("%d: %s failed, no vma\n", alloc->pid, __func__); > > ret = -ESRCH; > > goto out; > > } > > > > page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); > > if (!page) { > > pr_err("%d: failed to allocate page\n", alloc->pid); > > ret = -ENOMEM; > > goto out; > > } > > Maybe it would be worth to allocate the page before taking the mmap > write lock? It has the disadvantage that you may have to immediately > deallocate it if we trigger the `if (lru_page->page_ptr) goto out` > branch, but that shouldn't happen that often, and it would reduce the > amount of time we spend holding the mmap write lock. If we sleep on alloc_page() then chances are that having other tasks allocating more pages could create more memory pressure. In some cases this would be unecessary (e.g. if it's the same page). I do think this could happen often since buffer requests tend to be < PAGE_SIZE and adjecent too. I'll look into this with more detail and send a follow up patch if needed. Thanks! -- Carlos Llamas