From nobody Thu Dec 18 13:41:03 2025 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 2C05D45948 for ; Mon, 15 Dec 2025 05:31:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765776669; cv=none; b=Y7YWk8LQCb746JgUM6veMKXuWl7YSqUAXp0lvpMGLh9SFk3xKV86V0QX5Bu2wN4egHtc4hKor+5oLQ5lkCxiZ6E3YsnyDjgx8IQn+ZfyOaUcphtcNsYo7QEVrSPT1T8S4h21wuh8OGg7pp+Cy+W0nzfP4GNaC1Ya3tJDLBZDnTM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765776669; c=relaxed/simple; bh=hvyFOzkBTCpYGInMchnhx4kBJ3GczXHnlx2IyopFapU=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=X7aCe4q5mLgKSAd5eppSqd7GEPw1Tt78y9E6QQzZZC4df6rGCn0dam6A9oR6TV7NpE3t6mL64oZCW1ouRrTY8R8beezbXSvxwtzq/KcuG7JioLDNri9eCMTNiqBJQlIghSzD8PBw0rZKdByrXkeIoHF7eXCDrtC5IrqEjNq1iBQ= 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=NRhWlxBD; arc=none smtp.client-ip=209.85.210.173 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="NRhWlxBD" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-7e2762ad850so3137835b3a.3 for ; Sun, 14 Dec 2025 21:31:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765776667; x=1766381467; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=uz3AkyAC19O+S8FUO5AbqCgspWmgP9i0gngDDNAJrGk=; b=NRhWlxBDWJjnUuz+RwMxs98gqeixWia0PIrlxnjM/TdpR5FkWBwpbvQxacCSZxhUwx Izh7WJWc5H6oq3VlktzFUSWynWJZA1L5kuPI2TRU+bfYru2jMa/teJsCi+OR3XNFWuPo geNtemtx410liO+JEGNpUCo5LeKp5ftSDiNXPRMCf/JP16dP4cZzRxL8kcoTjSENWCvr 9iqy+qALh8MNH5Zmpn9GCkziwfDbsNXHvcAFFPcDcV6cRzUOS+arMxP0jMRyt2QjhQx9 WtQ/e8a/vH2iuJB6UiIbwyWdRMJav4F6Wr7HcFau+T1J2Y4nEm23fUb5STCUXMqwJfy/ VmFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765776667; x=1766381467; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=uz3AkyAC19O+S8FUO5AbqCgspWmgP9i0gngDDNAJrGk=; b=ZuZUqGJRqvKH2YEYPXKirUiXwoC40TGIsgiyzMRYd8+E+IzEvNmgm7MgZlVdeQfxwx FRtwF/7Y2NMQle7yf8mRNDpECVoFeCkEgD5GmEFw+w3b/0LrVYr2iLk2roE52qDcbHan 4QNgiOqhVbm45G0t4rRGuukPWyY3VKlb34CcviYmcs3uwAqGa6411t8LdS5OMgqRug9i CiA9OWdZ6LaLGvV1bK4+UGkz+90Z5Ow3bAbaUO2EjmyB54wG+sony+r7trr3ILuXaxCv Wvg2NGogYAFFRI6je8UKJZmLCsLdHcTXfQ7+A2g++H1yVBIGzsSQMJZne5WJ+maEY/+N 3mDQ== X-Forwarded-Encrypted: i=1; AJvYcCX8vcMUyFvZY6PCs/ro/vELFoHZ4+y4VZ4yjUm23bfivJ6b1w+uDEj4UqOToSN46DIvjowpNW1+7Wzphck=@vger.kernel.org X-Gm-Message-State: AOJu0Yxew8Dkqf/Pl+rGw6J6LDpt3IMiL19rQN2e5GDVSmYJkKt3OI1z dg9A5fhVUSBZC8w0X9IikhnVfTimBpqRaa3UczbWZIRrPIkzGrzZxalu X-Gm-Gg: AY/fxX7Lsy93amX9Mhw/dZrexYz5qNIrbmMm6c9JnUZ2ubcqV77V/KCJ2zHEcCXf+eV AYwQFe3d4Ru1Z3efCrZbxDXqLM8qE8WB11+kEA5nu++gEWmWGvXAqgU0RChoLJ8t2xJzVE88M2m sBlxFQnL8zqasMv3qvz0xdYTnYM5gR820OzsoKjuw6c73uZBB68h+VN7Sad3zafj1IQkCD9Hdk6 C4vXPLjZPrE7E1iAK7CaTK1RJaM3ItuUUwRDshL11oO3I4Vpe6OlAGgBxY8dbxiJSG7V/8JVDxl Lefe9270KrMUiteymqHugCynqjs0hucAYeJ9Y5NU+r/zQp9dUI7vHLwwxp9ZmT+A8x5xEc5pV9X mL8NSrWW/2YidxmIXVleg1iYhGAfF90xJJt1wmmA7FIyHxH23T73LyyGyDRURvJryCjO2iwNmSW AmFd8qzfUDzpAJmVtDguFkV5YoZg== X-Google-Smtp-Source: AGHT+IHNaawrkiaB8tEzN2LJvPo55x1qDp7wzj9Mb1MNnfFeFzZwkDr8l2UABw6Bq3fG7Z2E49LeWQ== X-Received: by 2002:a05:6a00:a381:b0:7e8:4433:8fa1 with SMTP id d2e1a72fcca58-7f6694aa660mr7586764b3a.41.1765776666944; Sun, 14 Dec 2025 21:31:06 -0800 (PST) Received: from Barrys-MBP.hub ([47.72.129.29]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7f4c509cb51sm11409151b3a.54.2025.12.14.21.31.02 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 14 Dec 2025 21:31:06 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: dri-devel@lists.freedesktop.org, jstultz@google.com, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Barry Song , David Hildenbrand , Uladzislau Rezki , Sumit Semwal , Maxime Ripard , Tangquan Zheng Subject: [PATCH] mm/vmalloc: map contiguous pages in batches for vmap() whenever possible Date: Mon, 15 Dec 2025 13:30:50 +0800 Message-Id: <20251215053050.11599-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) 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-Transfer-Encoding: quoted-printable 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=E2=80=94even 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=C3=97 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 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 lon= g addr, unsigned long end, return err; } =20 +static inline int get_vmap_batch_order(struct page **pages, + unsigned int stride, unsigned int max_steps, unsigned int idx) +{ + int nr_pages =3D 1; + + /* + * Currently, batching is only supported in vmap_pages_range + * when page_shift =3D=3D PAGE_SHIFT. + */ + if (stride !=3D 1) + return 0; + + nr_pages =3D compound_nr(pages[idx]); + if (nr_pages =3D=3D 1) + return 0; + if (max_steps < nr_pages) + return 0; + + if (num_pages_contiguous(&pages[idx], nr_pages) =3D=3D nr_pages) + return compound_order(pages[idx]); + return 0; +} + /* * vmap_pages_range_noflush is similar to vmap_pages_range, but does not * flush caches. @@ -655,23 +678,33 @@ int __vmap_pages_range_noflush(unsigned long addr, un= signed long end, pgprot_t prot, struct page **pages, unsigned int page_shift) { unsigned int i, nr =3D (end - addr) >> PAGE_SHIFT; + unsigned int stride; =20 WARN_ON(page_shift < PAGE_SHIFT); =20 + /* + * 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 =3D=3D PAGE_SHIFT) + (page_shift =3D=3D PAGE_SHIFT && !PageCompound(pages[0]))) return vmap_small_pages_range_noflush(addr, end, prot, pages); =20 - for (i =3D 0; i < nr; i +=3D 1U << (page_shift - PAGE_SHIFT)) { - int err; + stride =3D 1U << (page_shift - PAGE_SHIFT); + for (i =3D 0; i < nr; ) { + int err, order; =20 - err =3D vmap_range_noflush(addr, addr + (1UL << page_shift), + order =3D get_vmap_batch_order(pages, stride, nr - i, i); + err =3D vmap_range_noflush(addr, addr + (1UL << (page_shift + order)), page_to_phys(pages[i]), prot, - page_shift); + page_shift + order); if (err) return err; =20 - addr +=3D 1UL << page_shift; + addr +=3D 1UL << (page_shift + order); + i +=3D 1U << (order + page_shift - PAGE_SHIFT); } =20 return 0; --=20 2.39.3 (Apple Git-146)