From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A5ED2F0C6B for ; Mon, 22 Dec 2025 13:09:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766408943; cv=none; b=TqR0Ib1t+k16R9m1Tzm0QErkaA1Sb3e8AIrV46AihuNIJuhpzg626YNC0bjA/ttM50/bfHA8Kd/IoUmYRPtSvCPsu1UGQT4S81Yzttzmk28S2Q4UT0S6cT5X0QrV4widhfGpbCkBGAcFMEiyOEJ7nVIFxfWZDXszBirOCVQ1otI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766408943; c=relaxed/simple; bh=BWxLr1weDifKxuoKiuRn8B7wkUOXcwizFFSB7Cut1+0=; h=From:Date:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=fj3p05aYPE7Ty3wC4Ha3yJAbuh+ux6hFvLJNQLwnKLC2HjODKjUJXoZ16DIm/ioz3vMA+fEDtOw2cifXzTNx++YIiTohYXeJrMnPXw4/78Fa2vJXRUKY/ZvEvFw/XxpgAOlOfiMUCi4DKqOymEl8Z+rC+gFpcYqlqYxUFsVv66c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=BScemfHq; arc=none smtp.client-ip=209.85.167.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BScemfHq" Received: by mail-lf1-f54.google.com with SMTP id 2adb3069b0e04-5943d20f352so4318059e87.0 for ; Mon, 22 Dec 2025 05:09:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766408939; x=1767013739; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:date:from:from:to :cc:subject:date:message-id:reply-to; bh=TqkA3QTeqvfIHTaFJmrTviJUHc15iycYlD6ucgB4o6w=; b=BScemfHqEv+vyqZQP9PNXAQTsYZ66ix8sc6RNUkUrJmXQ1gaPoBSp+5gIWSdll9KrU qpI89JFvA4uHzZEcbBboZ2yaow2abJopE4v/h7J4tJ0PA+NUMiBnIp8FaZKZ4tGxef22 ZEeIKmlPpQSAmUUdO+BBNLgrQdozfMFDwg2vCO6Dgkc6xCjkk0MPqbBzd57IbOZkGsWb haS3odRe+4CtihAUSjYH5v+gio+E/HGXFeBAUPXwm/U4H21nnEZpkQRRJ6cuin00hqr9 e3AEJAiRB+rYgFd/+y1LGT/MAKEk3LT5hmQlOIlxlR1Aodj/V83LxFXx6sIP6c0gL0CS kKKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766408939; x=1767013739; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TqkA3QTeqvfIHTaFJmrTviJUHc15iycYlD6ucgB4o6w=; b=qCzHDfO0hf0EcMsTQ7WY7WovwG3SjFKu+DEFAsQWzZFkA5ev3Wlnji0sZYyWs2S2nG bkSt/HXfssv+BQT6nO/ZLpqqOnZTA/8FJdU8Y1eVz2eKcZfwEIF8ip+xg2kRcUbR8F+/ AEEYUgrr6TY6+yMrRJ8y7NsGiB26tcTj+6cJ/Rm+ZDg7eidIuHc3NFyuqFdahmAzT21Z SxsOF0DizocdHOq2U8cFMNrIOwGm9B2rVhgNznz6uqIQPXCIGmIVGcFoSXs8jvzhLFCq 5RP/l3XUGLgn7qLdPjIvB6HgI0pH2yTI5RNKyVvN/i5cgMlpDYmBok5CK7yP0YA4i7SM 84pg== X-Forwarded-Encrypted: i=1; AJvYcCUvumCT82HzAzbZN5NCbyIdJw7vRFGEHuEa1PKla/lGq06FCJ1pmJKadUmkUzdjiSo1X5oMIhEGCGAdyeE=@vger.kernel.org X-Gm-Message-State: AOJu0YxQYvwr257wenCpegIjP9dbXlMcGdVW2Ft03k1YwgLuR8lpesJ+ 25iS5+hHy5qT6ilW5s2jdFok3006WpLbI+dVSVQmdtg/vsRNVh5GB1V3 X-Gm-Gg: AY/fxX7UpLi/PNMJi2BDb8rP8F6+P4Q/AFPi1BzFhM6iZR+FZyv4hXTuLdMm/I1x1/d Z+SSoXCRyKaphP8fITEHon/BdIRg3o2xcEE2shY/ivKxuvzFSZD02YkW9/hu3auVrsBa2G4zAer matSQeN4gFEjAcGQsXBf9znnNj4PAAqVFkodlHQtFIMTKiYtc22B9e+J2a2AB4waJIEpr3Or8FR M4FS6ZgxMM/yEJd9/X5uuUi6pnyroI5LQoDoPHtXRuBrZUYPCT4BAE4DjkzHNHhDwXJrsMFoACr uZDECvV9L3MaMAHrcosBx0zPE9mYQ1UQKg+jmSpBY4hbvqXmAlDOSjkd/xkzg0b7G+nHIswB4/D 3yBb2ddKOT3cx+/tH5Vd5Vu4mvoP9R06XfPJBjg6IkFHqMMe1oict X-Google-Smtp-Source: AGHT+IEoHaD9txNltw8AYIh3bvKWbd6Ld21Uc2hEUyNiNte2MbtG2FdcMNsmNf298a2tH2qY7Uf5Kw== X-Received: by 2002:a05:6512:3f1a:b0:598:e39b:d628 with SMTP id 2adb3069b0e04-59a17d7271amr3858414e87.5.1766408939248; Mon, 22 Dec 2025 05:08:59 -0800 (PST) Received: from milan ([2001:9b1:d5a0:a500::24b]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-59a1861f4e4sm3115532e87.83.2025.12.22.05.08.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Dec 2025 05:08:58 -0800 (PST) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Mon, 22 Dec 2025 14:08:56 +0100 To: Barry Song <21cnbao@gmail.com> Cc: urezki@gmail.com, akpm@linux-foundation.org, david@kernel.org, dri-devel@lists.freedesktop.org, jstultz@google.com, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linux-mm@kvack.org, mripard@kernel.org, sumit.semwal@linaro.org, v-songbaohua@oppo.com, zhengtangquan@oppo.com Subject: Re: [PATCH] mm/vmalloc: map contiguous pages in batches for vmap() whenever possible Message-ID: References: <20251218212436.17142-1-21cnbao@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20251218212436.17142-1-21cnbao@gmail.com> On Fri, Dec 19, 2025 at 05:24:36AM +0800, Barry Song wrote: > On Thu, Dec 18, 2025 at 9:55 PM Uladzislau Rezki wrote: > > > > On Thu, Dec 18, 2025 at 02:01:56PM +0100, David Hildenbrand (Red Hat) wrote: > > > On 12/15/25 06:30, Barry Song wrote: > > > > From: Barry Song > > > > > > > > In many cases, the pages passed to vmap() may include high-order > > > > pages allocated with __GFP_COMP flags. For example, the systemheap > > > > often allocates pages in descending order: order 8, then 4, then 0. > > > > Currently, vmap() iterates over every page individually—even pages > > > > inside a high-order block are handled one by one. > > > > > > > > This patch detects high-order pages and maps them as a single > > > > contiguous block whenever possible. > > > > > > > > An alternative would be to implement a new API, vmap_sg(), but that > > > > change seems to be large in scope. > > > > > > > > When vmapping a 128MB dma-buf using the systemheap, this patch > > > > makes system_heap_do_vmap() roughly 17× faster. > > > > > > > > W/ patch: > > > > [   10.404769] system_heap_do_vmap took 2494000 ns > > > > [   12.525921] system_heap_do_vmap took 2467008 ns > > > > [   14.517348] system_heap_do_vmap took 2471008 ns > > > > [   16.593406] system_heap_do_vmap took 2444000 ns > > > > [   19.501341] system_heap_do_vmap took 2489008 ns > > > > > > > > W/o patch: > > > > [    7.413756] system_heap_do_vmap took 42626000 ns > > > > [    9.425610] system_heap_do_vmap took 42500992 ns > > > > [   11.810898] system_heap_do_vmap took 42215008 ns > > > > [   14.336790] system_heap_do_vmap took 42134992 ns > > > > [   16.373890] system_heap_do_vmap took 42750000 ns > > > > > > > > > > That's quite a speedup. > > > > > > > Cc: David Hildenbrand > > > > Cc: Uladzislau Rezki > > > > Cc: Sumit Semwal > > > > Cc: John Stultz > > > > Cc: Maxime Ripard > > > > Tested-by: Tangquan Zheng > > > > Signed-off-by: Barry Song > > > > --- > > > >   * diff with rfc: > > > >   Many code refinements based on David's suggestions, thanks! > > > >   Refine comment and changelog according to Uladzislau, thanks! > > > >   rfc link: > > > >   https://lore.kernel.org/linux-mm/20251122090343.81243-1-21cnbao@gmail.com/ > > > > > > > >   mm/vmalloc.c | 45 +++++++++++++++++++++++++++++++++++++++------ > > > >   1 file changed, 39 insertions(+), 6 deletions(-) > > > > > > > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > > > > index 41dd01e8430c..8d577767a9e5 100644 > > > > --- a/mm/vmalloc.c > > > > +++ b/mm/vmalloc.c > > > > @@ -642,6 +642,29 @@ static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, > > > >     return err; > > > >   } > > > > +static inline int get_vmap_batch_order(struct page **pages, > > > > +           unsigned int stride, unsigned int max_steps, unsigned int idx) > > > > +{ > > > > +   int nr_pages = 1; > > > > > > unsigned int, maybe > > Right > > > > > > > Why are you initializing nr_pages when you overwrite it below? > > Right, initializing nr_pages can be dropped. > > > > > > > > + > > > > +   /* > > > > +    * Currently, batching is only supported in vmap_pages_range > > > > +    * when page_shift == PAGE_SHIFT. > > > > > > I don't know the code so realizing how we go from page_shift to stride too > > > me a second. Maybe only talk about stride here? > > > > > > OTOH, is "stride" really the right terminology? > > > > > > we calculate it as > > > > > >       stride = 1U << (page_shift - PAGE_SHIFT); > > > > > > page_shift - PAGE_SHIFT should give us an "order". So is this a > > > "granularity" in nr_pages? > > This is the case where vmalloc() may realize that it has > high-order pages and therefore calls > vmap_pages_range_noflush() with a page_shift larger than > PAGE_SHIFT. For vmap(), we take a pages array, so > page_shift is always PAGE_SHIFT. > > > > > > > Again, I don't know this code, so sorry for the question. > > > > > To me "stride" also sounds unclear. > > Thanks, David and Uladzislau. On second thought, this stride may be > redundant, and it should be possible to drop it entirely. This results > in the code below: > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > index 41dd01e8430c..3962bdcb43e5 100644 > --- a/mm/vmalloc.c > +++ b/mm/vmalloc.c > @@ -642,6 +642,20 @@ static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, > return err; > } > > +static inline int get_vmap_batch_order(struct page **pages, > + unsigned int max_steps, unsigned int idx) > +{ > + unsigned int nr_pages = compound_nr(pages[idx]); > + > + if (nr_pages == 1 || max_steps < nr_pages) > + return 0; > + > + if (num_pages_contiguous(&pages[idx], nr_pages) == nr_pages) > + return compound_order(pages[idx]); > + return 0; > +} > + > > /* > * vmap_pages_range_noflush is similar to vmap_pages_range, but does not > * flush caches. > @@ -658,20 +672,35 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, > > WARN_ON(page_shift < PAGE_SHIFT); > > + /* > + * For vmap(), users may allocate pages from high orders down to > + * order 0, while always using PAGE_SHIFT as the page_shift. > + * We first check whether the initial page is a compound page. If so, > + * there may be an opportunity to batch multiple pages together. > + */ > if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) || > - page_shift == PAGE_SHIFT) > + (page_shift == PAGE_SHIFT && !PageCompound(pages[0]))) > return vmap_small_pages_range_noflush(addr, end, prot, pages); Hm.. If first few pages are order-0 and the rest are compound then we do nothing. > > - for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) { > + for (i = 0; i < nr; ) { > + unsigned int shift = page_shift; > int err; > > - err = vmap_range_noflush(addr, addr + (1UL << page_shift), > + /* > + * For vmap() cases, page_shift is always PAGE_SHIFT, even > + * if the pages are physically contiguous, they may still > + * be mapped in a batch. > + */ > + if (page_shift == PAGE_SHIFT) > + shift += get_vmap_batch_order(pages, nr - i, i); > + err = vmap_range_noflush(addr, addr + (1UL << shift), > page_to_phys(pages[i]), prot, > - page_shift); > + shift); > if (err) > return err; > > - addr += 1UL << page_shift; > + addr += 1UL << shift; > + i += 1U << shift; > } > > return 0; > > Does this look clearer? > The concern is we mix it with a huge page mapping path. If we want to batch v-mapping for page_shift == PAGE_SHIFT case, where "pages" array may contain compound pages(folio)(corner case to me), i think we should split it. -- Uladzislau Rezki