From mboxrd@z Thu Jan 1 00:00:00 1970 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=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 007DDC433E0 for ; Tue, 2 Feb 2021 16:20:08 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6B8BB64F65 for ; Tue, 2 Feb 2021 16:20:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6B8BB64F65 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 AD3D16B006C; Tue, 2 Feb 2021 11:20:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A5D556B0071; Tue, 2 Feb 2021 11:20:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94CD86B0073; Tue, 2 Feb 2021 11:20:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7E62B6B006C for ; Tue, 2 Feb 2021 11:20:06 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 47CD9824999B for ; Tue, 2 Feb 2021 16:20:06 +0000 (UTC) X-FDA: 77773839612.13.5325767 Received: from mail-io1-f46.google.com (mail-io1-f46.google.com [209.85.166.46]) by imf02.hostedemail.com (Postfix) with ESMTP id 3A2AB4091439 for ; Tue, 2 Feb 2021 16:20:05 +0000 (UTC) Received: by mail-io1-f46.google.com with SMTP id f6so1720015ioz.5 for ; Tue, 02 Feb 2021 08:20:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0Da19mOLKmJyRjSXgpGZWU4OOIXigtDd8FLvo0i6X24=; b=jb5DKhEAl6leh7r1gGeddbngBnqJXZL/aura7fPimgD6Cflo8LfGJr9lpaeOLWHsxQ fzD4QQBV6EFMGycTlkuCNCl0GutMzSxFx1hHRfIkJip8fyzuAVFLutLZTyvS7TG0Frbh fj4+5lOngmC6lIDh9eG5+FqQfwI8cc1hOsTzEbjJTSvzXFV4G45yQkFy10ekSaaNBk8r 4Ixk/AQQP8Vw+oGCJS/VLqBoks7Q341EYD3GS9CnpZiy60gXTv6vx7ZWuEzVon43ilur C3FVbNcKsKelYg+iIMTtcmC1PIcyv2YxgdlHdgCsdYFtzy9PrbmAvgUCJxR9ohoVWcDw Mitg== 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=0Da19mOLKmJyRjSXgpGZWU4OOIXigtDd8FLvo0i6X24=; b=UHsm+nEX0SqZQO70EPjN9RLPxDm9BYZGCJJq0Vd3NeIO3hJEk2MAq2HlTOWTmF2PdF rIiRKz8HhXtcSDOJJV16dhaussld5SmS0l1uxXhiKJSy14LijUUmFmnOsBSqtBiyce+X 53xscp+oqR3KMWJz4Lz8d1D4chekHM8SRt7qdmTAWICkCg4C+LzGqXejDIvsMeh1lotX ZOF9/nH8r/7K1V26F1PWrF7HR5JJQey4QDR468GI81aV/dxDKE1u42HIqxaH8fUtMOZW 3Zp4IYQkBRGwpucT5s9WvuzMcWU9knFtqiGOwIy4VEWsAMjKA9L5lk4Vwl0FT9f2HC05 alrg== X-Gm-Message-State: AOAM531FTShpnlNgMrN+ZUTJl7mkgkg8KUkH4n9eKHNV1X7t9wM9q9eL VHDCtfz1tFsvVEO0YjVYuxugqZzEeXFaOKkESxY= X-Google-Smtp-Source: ABdhPJzgiYkMbxueZ9+tOHERxUDsWzJJifUYbboj6iiuldrT0aG9w/GhlMkFeGk2p5lawoQCBWKauMBgiIxRdcJ3ufk= X-Received: by 2002:a6b:f904:: with SMTP id j4mr7922451iog.138.1612282804958; Tue, 02 Feb 2021 08:20:04 -0800 (PST) MIME-Version: 1.0 References: <20210131074426.44154-1-haokexin@gmail.com> <20210131074426.44154-2-haokexin@gmail.com> In-Reply-To: <20210131074426.44154-2-haokexin@gmail.com> From: Alexander Duyck Date: Tue, 2 Feb 2021 08:19:54 -0800 Message-ID: Subject: Re: [PATCH net-next v2 1/4] mm: page_frag: Introduce page_frag_alloc_align() To: Kevin Hao Cc: "David S . Miller" , Jakub Kicinski , Andrew Morton , Netdev , linux-mm , Vlastimil Babka , Eric Dumazet Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 3A2AB4091439 X-Stat-Signature: oe3n5kryzhzxbzd3jurswz5rstctujcx Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf02; identity=mailfrom; envelope-from=""; helo=mail-io1-f46.google.com; client-ip=209.85.166.46 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1612282805-458166 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: On Sat, Jan 30, 2021 at 11:54 PM Kevin Hao wrote: > > 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 > --- > v2: > - Inline page_frag_alloc() > - Adopt Vlastimil's suggestion and add his Acked-by > > 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 6e479e9c48ce..39f4b3070d09 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, > + int align); > + > +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, 0); > +} > + > 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 519a60d5b6f7..4667e7b6993b 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -5137,8 +5137,8 @@ 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, int align) I would make "align" unsigned since really we are using it as a mask. Actually passing it as a mask might be even better. More on that below. > { > unsigned int size = PAGE_SIZE; > struct page *page; > @@ -5190,11 +5190,13 @@ void *page_frag_alloc(struct page_frag_cache *nc, > } > > nc->pagecnt_bias--; > + if (align) > + offset = ALIGN_DOWN(offset, align); > 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. Rather than using the conditional branch it might be better to just do "offset &= align_mask". Then you would be adding at most 1 instruction which can likely occur in parallel with the other work that is going on versus the conditional branch which requires a test, jump, and then the 3 alignment instructions to do the subtraction, inversion, and AND. However it would ripple through the other patches as you would also need to update you other patches to assume ~0 in the unaligned case, however with your masked cases you could just use the negative alignment value to generate your mask which would likely be taken care of by the compiler.