From patchwork Thu Feb 4 10:56:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Hao X-Patchwork-Id: 12066997 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8A1B3C433E0 for ; Thu, 4 Feb 2021 11:06:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3BCD264F39 for ; Thu, 4 Feb 2021 11:06:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3BCD264F39 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id CE71F6B006C; Thu, 4 Feb 2021 06:06:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C985A6B006E; Thu, 4 Feb 2021 06:06:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B869D6B0070; Thu, 4 Feb 2021 06:06:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0114.hostedemail.com [216.40.44.114]) by kanga.kvack.org (Postfix) with ESMTP id 9B69B6B006C for ; Thu, 4 Feb 2021 06:06:27 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 654F08249980 for ; Thu, 4 Feb 2021 11:06:27 +0000 (UTC) X-FDA: 77780306814.12.knee88_17029d8275db Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id 490BD18056270 for ; Thu, 4 Feb 2021 11:06:27 +0000 (UTC) X-HE-Tag: knee88_17029d8275db X-Filterd-Recvd-Size: 5901 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf32.hostedemail.com (Postfix) with ESMTP for ; Thu, 4 Feb 2021 11:06:26 +0000 (UTC) Received: by mail-pf1-f182.google.com with SMTP id t29so1885365pfg.11 for ; Thu, 04 Feb 2021 03:06:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Bzd7HKkd3Ts7ESkt/aXKI/JaGnhZL+skqDucX4yLTCQ=; b=C3c5dyJb6O/yirl0WF84J4RT6j6OM2oYPdgzYChKvnn7e54EVcapMYrooxphRQCGtc EHnKKcL4T46bfTuSE5iSFN5XH6Gh/ONPzXb3LmrgV2UcE3ajgNE/zRs+EQt+c/aM3DRw q1f+9cOQuRfs++EeRILkxm4qzsfr+6pTSa09hZBe+F7NzGwhIuqo3OThZyeOeWlk12eO GMnD4svgDEm3KNS9aHJkY1yGOJYSUpX5jXHmUrz5bPZyC28e0qzshMfE/o51ztssDV55 y1I4YtZsJJ5hRlC0LJ87EzhiFMK9qIGtcpx2InevX16Jw6mlaSM07T/Eyhq52poqLorN KOLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Bzd7HKkd3Ts7ESkt/aXKI/JaGnhZL+skqDucX4yLTCQ=; b=iv8qCuxv3rI0DR8pc6MEiIOXc57rdI2IX0HTxmha+AUkRgjJ1x7PMimO/2JqSZo/xL 0ONo1M0AzSrdcIgW1OPgVbNco/fp2VFjgM44tp1gO39YT3utjMNjn5oSC4RCmGrSgpTD dcuZs+rvRe/DMHiwdZ5hGFLJTFZI5kKQlZEmRvfxed/YozvSW4cCPp4FIGnuty2GQb3s ujA0A3/+5LI2XQD7RzWgLPx8QvnJrH1bKiMm9Mr1Vt9MiCgBzHKR2QyGZeOzYqKNuOa+ LXqKRzQYgQr6p+Gm1S/ZgXFT/36mnjp1LswkouwqOy5ffWY4hJDlteD98GmCnYja0oW5 qNjA== X-Gm-Message-State: AOAM532RUnmivQuF28wcSZjy071zSg7sZsTCnwsvKXi8794V7WcZSdyI tOkJ7Lq0Ob8l7qeSZz+Rdlc= X-Google-Smtp-Source: ABdhPJwxg80JL7uzJYm6e8ZqRQQZBcVyE1xoR7h7pgijaY23L3+J6ubz96J9mZG3Ijj2ipr1a1XCTA== X-Received: by 2002:aa7:96bc:0:b029:1d3:3d93:5a11 with SMTP id g28-20020aa796bc0000b02901d33d935a11mr5652568pfk.4.1612436785982; Thu, 04 Feb 2021 03:06:25 -0800 (PST) Received: from pek-lpggp6.wrs.com (unknown-105-123.windriver.com. [147.11.105.123]) by smtp.gmail.com with ESMTPSA id y15sm5283351pju.20.2021.02.04.03.06.19 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 04 Feb 2021 03:06:25 -0800 (PST) From: Kevin Hao To: "David S . Miller" , Jakub Kicinski , Andrew Morton Cc: netdev@vger.kernel.org, linux-mm@kvack.org, Vlastimil Babka , Eric Dumazet , Alexander Duyck Subject: [PATCH net-next v3 1/4] mm: page_frag: Introduce page_frag_alloc_align() Date: Thu, 4 Feb 2021 18:56:35 +0800 Message-Id: <20210204105638.1584-2-haokexin@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210204105638.1584-1-haokexin@gmail.com> References: <20210204105638.1584-1-haokexin@gmail.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In the current implementation of page_frag_alloc(), it doesn't have any align guarantee for the returned buffer address. But for some hardwares they do require the DMA buffer to be aligned correctly, so we would have to use some workarounds like below if the buffers allocated by the page_frag_alloc() are used by these hardwares for DMA. buf = page_frag_alloc(really_needed_size + align); buf = PTR_ALIGN(buf, align); These codes seems ugly and would waste a lot of memories if the buffers are used in a network driver for the TX/RX. So introduce page_frag_alloc_align() to make sure that an aligned buffer address is returned. Signed-off-by: Kevin Hao Acked-by: Vlastimil Babka Reviewed-by: Alexander Duyck --- v3: Use align mask as suggested by Alexander. include/linux/gfp.h | 12 ++++++++++-- mm/page_alloc.c | 8 +++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 53caa9846854..52cd415b436c 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -583,8 +583,16 @@ extern void free_pages(unsigned long addr, unsigned int order); struct page_frag_cache; extern void __page_frag_cache_drain(struct page *page, unsigned int count); -extern void *page_frag_alloc(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask); +extern void *page_frag_alloc_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align_mask); + +static inline void *page_frag_alloc(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask) +{ + return page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); +} + extern void page_frag_free(void *addr); #define __free_page(page) __free_pages((page), 0) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index ad3ed3ec4dd5..3583c6accd88 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5137,8 +5137,9 @@ void __page_frag_cache_drain(struct page *page, unsigned int count) } EXPORT_SYMBOL(__page_frag_cache_drain); -void *page_frag_alloc(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask) +void *page_frag_alloc_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align_mask) { unsigned int size = PAGE_SIZE; struct page *page; @@ -5190,11 +5191,12 @@ void *page_frag_alloc(struct page_frag_cache *nc, } nc->pagecnt_bias--; + offset &= align_mask; nc->offset = offset; return nc->va + offset; } -EXPORT_SYMBOL(page_frag_alloc); +EXPORT_SYMBOL(page_frag_alloc_align); /* * Frees a page fragment allocated out of either a compound or order 0 page.