From patchwork Wed Jun 5 13:32:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686903 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3DCCC27C5E for ; Wed, 5 Jun 2024 13:36:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6EFC56B008A; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 69DF86B008C; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 564E56B0092; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 355136B008C for ; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D0218140D82 for ; Wed, 5 Jun 2024 13:36:15 +0000 (UTC) X-FDA: 82196933910.25.F7DF074 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf09.hostedemail.com (Postfix) with ESMTP id 2D14514000C for ; Wed, 5 Jun 2024 13:36:12 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; spf=pass (imf09.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717594573; a=rsa-sha256; cv=none; b=vzj0SXe7L+noTuV99DlPD4irR0y06GXRJ8clhmSZ9w/KN414IjRjsr6HP1/J59E2PLtHtP ZzkbDzCXS0XRVW8kngOKduKIPuTT9GUjRdlJDZrQbgMunfJSw11SjsSGGeH2DZAJmfzv22 KdjWrqjc/vWItcdSUwcbiIGsGGRdJlY= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; spf=pass (imf09.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717594573; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=651YPQJY1ccuAoTSZ6mx1jH9FKczANbdWKeYsrewnwc=; b=A9nAhRykzozUyDGAHT6yKhdav8buqM4xICaQ+iMZVOxS4yPcCxVqmfmD8Hr5fBryr5oqFW WdWcMzgwocNC6xqS8rgrJSnu6w1T/wfI1e3sjO9kYFxzRYd8h2HseHxvfDjgUycNjx+yOK zRlmw6BqW9tl0ST5pH7riMAT6VOLWaA= Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VvT0F1TzXzsSlt; Wed, 5 Jun 2024 21:32:13 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 1D360180AA6; Wed, 5 Jun 2024 21:36:10 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Wed, 5 Jun 2024 21:36:09 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v6 05/15] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Wed, 5 Jun 2024 21:32:55 +0800 Message-ID: <20240605133306.11272-6-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: 2D14514000C X-Rspam-User: X-Rspamd-Server: rspam12 X-Stat-Signature: 96qg8pfo3wqg3771gzfxmeqwmso3y4sk X-HE-Tag: 1717594572-213123 X-HE-Meta: U2FsdGVkX1+kNoTd76QaLjdY9Y/Kh0CY8pnD4eG/FFKhuQ5t2+FkGabA2z9DpbI0n1fYPN/54KJ27vfYSX5MRfID4r6AcHqsYXGK6BAqrEq5czabBT2ZWdm6JJ8otbM0Epocwjw+Kt9Kgy8ipNZFmvI1UBdO6gzS3qmL8oVdNo4XSYOARUiGrKGBLt6WtGSwdh9PenlMW9fT/YqttoJXy0TqUWPhfI26jko4xM69H3ak9xg0ZHEFHMS2ugDjHxT4Gh6wRjWZQ2uQao1I4sjrpgaAA7mpLntAQyOBI4hVP8ZhH2K0aEhoS9rI5RP97G3FkU2Pbo2YiINhTt/2pycAgb/ibKRQ8c8h5Y2DAXvdThFzuE1Yzi12jOcuDTZhmIrf64ea4yNhQQh29B+emXDfVLy0sfilz7/YYUrfeIsq9oMu8bv4YoGkqvuqpfBTAL3jOj5sdTOO6NPOsrmqATrMXtfURKAtxrwb2KILQKHbPcoyJcf/nC9ITpfv41ReC7qpDi6K4QnNM3Cahn44bv+j9LgVtxLf4AEqI0qBtP62F8gTcOEVKvtxlVSzKVSAt7R4pq9ogKZY+ZISIYUV2UVNtO60rRhFFfLhfV0nYM49Scwel7B/iMx98kYsfhhxoEAIcn3ZNGO+1PW5d9pOzZtwXLCKBi7POaFRpcaWjknnPUBAT5RyrCI4YeqTvdf0SyT0e5PBitF2oSJZCT4Avd3wwCn0cmdDfwsDAw4znCB8TqZFdKPnjxagrUCaMV2W1yFziFF20q29MBr1bjL4dzspN7hxkqgyEeHt1tbeejZMM68712hGqE6T2MRvh2C5AoUqNBniZb8eYV+p8WScL0+dtTEt5bZdR67GuIdp6HCVvK8bOs78kogVths1imzlRjZ/NL++ZguHiJcUK7+gsj7EZF/oG356abWdSQX6+pFjFGAlviw+XFy1lkIuNVcrcPLi5BvTIsLfxd2QqlpOATN zqGUoJQj O2wxwSRkQB1q+J6Y2jlQ148zvV396rkBXONw4k0VNOZ7qe9v96l615yqYKV9EMjY9JY86h/ZnYDAGADsD4V5Nyz1MfjSApsdWwre0nCiX5sIfnP3t+oitGnuDMYjMj3KzbAEWrJmxlJampKQmHFLp3o3gUmp9BL/CYe5FxuiQNg7YIMzIYE17BQUicMyR9hWLGyF1+CyYykeBi1RsJKyeRCUR2/tIxK9cH+6eiq+SwbhWxEdpfmrKzFHIzn+85iwhqM15hqul5rWPIpeq6HKJxaufr3+VqjIQH/07c7SPScTruEzA27WiAhh+cfMH1R8+EZ+yeAFG45c61L1iGK/HGouZfA== 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: List-Subscribe: List-Unsubscribe: We are above to use page_frag_alloc_*() API to not just allocate memory for skb->data, but also use them to do the memory allocation for skb frag too. Currently the implementation of page_frag in mm subsystem is running the offset as a countdown rather than count-up value, there may have several advantages to that as mentioned in [1], but it may have some disadvantages, for example, it may disable skb frag coaleasing and more correct cache prefetching We have a trade-off to make in order to have a unified implementation and API for page_frag, so use a initial zero offset in this patch, and the following patch will try to make some optimization to aovid the disadvantages as much as possible. As offsets is added due to alignment requirement before actually checking if the cache is enough, which might make it exploitable if caller passes a align value bigger than 32K mistakenly. As we are allowing order 3 page allocation to fail easily under low memory condition, align value bigger than PAGE_SIZE is not really allowed, so add a 'align > PAGE_SIZE' checking in page_frag_alloc_va_align() to catch that. 1. https://lore.kernel.org/all/f4abe71b3439b39d17a6fb2d410180f367cadf5c.camel@gmail.com/ CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 2 +- include/linux/skbuff.h | 4 ++-- mm/page_frag_cache.c | 26 +++++++++++--------------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 3a44bfc99750..b9411f0db25a 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -32,7 +32,7 @@ static inline void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align)); + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); } diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index bc1a64c6a436..fbff80a03224 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3294,7 +3294,7 @@ static inline void *netdev_alloc_frag(unsigned int fragsz) static inline void *netdev_alloc_frag_align(unsigned int fragsz, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align)); + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); return __netdev_alloc_frag_align(fragsz, -align); } @@ -3365,7 +3365,7 @@ static inline void *napi_alloc_frag(unsigned int fragsz) static inline void *napi_alloc_frag_align(unsigned int fragsz, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align)); + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); return __napi_alloc_frag_align(fragsz, -align); } diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 64993b5d1243..d569021376d4 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -75,10 +75,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, if (!page) return NULL; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif /* Even if we own the page, we do not use atomic_set(). * This would break get_page_unless_zero() users. */ @@ -87,11 +83,16 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pfmemalloc = page_is_pfmemalloc(page); nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = size; + nc->offset = 0; } - offset = nc->offset - fragsz; - if (unlikely(offset < 0)) { +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size = nc->size; +#endif + + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(offset + fragsz > size)) { page = virt_to_page(nc->va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) @@ -102,17 +103,13 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, goto refill; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif /* OK, page count is 0, we can safely set it */ set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = size - fragsz; - if (unlikely(offset < 0)) { + offset = 0; + if (unlikely(fragsz > PAGE_SIZE)) { /* * The caller is trying to allocate a fragment * with fragsz > PAGE_SIZE but the cache isn't big @@ -127,8 +124,7 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, } nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; + nc->offset = offset + fragsz; return nc->va + offset; }