From patchwork Wed May 8 13:34:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13658779 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 D6002C04FFE for ; Wed, 8 May 2024 13:36:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE2006B0089; Wed, 8 May 2024 09:36:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C3EB86B008C; Wed, 8 May 2024 09:36:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A41CD6B0092; Wed, 8 May 2024 09:36:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 803986B0089 for ; Wed, 8 May 2024 09:36:52 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3AFB9810FF for ; Wed, 8 May 2024 13:36:52 +0000 (UTC) X-FDA: 82095329064.07.A5B39E5 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf25.hostedemail.com (Postfix) with ESMTP id B3C8BA0007 for ; Wed, 8 May 2024 13:36:49 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 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=1715175410; 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=yCjipTYX6NeIbyvwvOFcZRg33GjA6EnXlF/ICoHJ5Fw=; b=1FoMTvASKDWwH9BJKJVFi7LJd6dq9cQyV3XOHOkIX0E4Gj65i0vSWsyCEfsI5jOd8F4r4R mKgbnO66G9uPpnVYHnCS63F1BIafCUwvo/lDyrGkap1kbQhTBBjAwLG8Azay65oHh4nPiU fYRg8CN2HO0qjLpnUC4TgGWzVUW5FJY= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 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=1715175410; a=rsa-sha256; cv=none; b=t+a5L4AWlgUVxuw3Ftcv1C4Qay3LUVFazreNXxiJdnMQ1IBV/ODNhLAvCdsVbtdi1UERo3 Q17efYnuwczVSRBgZbmUljzjTI19nQ2XCl5ohvDkDcm2PZ+i3XunKRZjxRkk13gDsWsaa4 Fu12y0IcEgWwwI798cj+7D/1xfLfwTA= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VZGKz65lVzYnCc; Wed, 8 May 2024 21:32:55 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id BD5C514011A; Wed, 8 May 2024 21:36:46 +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.35; Wed, 8 May 2024 21:36:46 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v3 05/13] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Wed, 8 May 2024 21:34:00 +0800 Message-ID: <20240508133408.54708-6-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: B3C8BA0007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: xng9ymjzcaom51oi5j13xxbc9j4b64pk X-HE-Tag: 1715175409-814452 X-HE-Meta: U2FsdGVkX18+wuz4byNAgqRGssBDXmABxI4tbI2khDLMU6e4mCKdY40iCiD07eg+2OebcmtUAJe/yZKf/8kyKiGQx7SS3tURZBptpLLzVgW1EMdaSHHFBgmNHYL5eeJ3ck2IoyYnxmp0LszS3Z7HN5zEdulENIBplD95swXejPTDVGJiUlUMrtLWxKAj7dHqnF2SaFxWTxnnsa8THZez0N5CF4rcLBPy18tz/XYIANBt8gleagjpo10HUTwj1ZEVd/rUb3RBJWSSXFmnvyB1YIk7zIHu02XKbkYV+ZOIqrmq5AQ+0ivn1J3I4Qq+LHPrRxPYkEyqtw/iKdzDzFw5fqBTAde7ly0fZP2yO/kaCUMBi8dLbDsZSMQhsgUR7xwzeRXsoofiW3YIhzxncg1bycvBsg+f1VtVjFeplTVyRTGAkdElGq6cxxrPYXSa1PyXDov3ENCtAqVugnO8DRFmqXlGPwESSDRVrGNr3LPAGbbWT3JWpxU9lhnxpxxq4Vr8k9JjaXC752+bqu+7IjsHsOLA6LiIsUsl9yCIpSiyemNQaiQpyoK1BPX2v0vs56fgWRNn0CUR1M4xh/YDF0HWHkGFKgahtLlKB8bGLT26vK6gNbHcfuyfsNBfvj4LFEQ5w3mr0X4ft1oXSf66zgWRboxmMcw5XO3/wxddEnlpvOUCarHX8pRM1OW7xVWOaCdqZpu2K+mWTwPdFUAB6OA+SOvwGkfeUXmsH8aG1tTs+eB424J0A7PF39KGGIviFAuIzv2TIOyfGy2NDs+wfXhY7aSyRr/G6GhQlTrgxW/c6oM6eKEgVjb8dw1SvIPcdhRdj8wp3kIiqr2yzFkUzJqShUu6PH1j22bQPvZFiD95VTdrrUMd/GaMLLpv++R53AltNF9cY93Ne8luoVT9y7OgbtObPqlgtHbwuqyViQMOXTevlTZ5e4a1qVueQEtppQGhiqisaFPa8KIHt0gFYet YHGA84p1 xECUfhwEURnsVYwu/yfuylmNvpds9PrmZKN5pu9ImQ8ofTzhi8Fh0nOcEyI5LNf2h51MUjUXdwhIdWe3Dlnc3tiwCj6mHjjVohRzLfMrlO19OFOtQvU2DKfgrXMdBi0VNixzz7537cPtKuIUcu/JtGCwosyomSi4YjKgx3pLRAXQ2FV3lb9HCGw5SGSW4Kh/XKzXHlzNIOdS0LiTTVNuoWJdRbb9euhFQWDWEMLogHRHNluwJbUvZ7kFX6tN3QgoStzqe3LFI8ab4NkQL53Y1Li8smEGD0yC1FNf7CwvfAucuPkrMGH+CpmzRMfJGYwZFoEh2CltIn/OowUQiHqRPWi+zng== 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 +- mm/page_frag_cache.c | 48 ++++++++++++++------------------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 635b67ceb939..9da7cbd0ee47 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/mm/page_frag_cache.c b/mm/page_frag_cache.c index 64993b5d1243..152ae5dec58a 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -65,9 +65,8 @@ 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; + unsigned int size, offset; struct page *page; - int offset; if (unlikely(!nc->va)) { refill: @@ -75,10 +74,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 +82,25 @@ 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; +#else + size = PAGE_SIZE; +#endif + + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(offset + fragsz > size)) { + /* fragsz is not supposed to be bigger than PAGE_SIZE as we are + * allowing order 3 page allocation to fail easily under low + * memory condition. + */ + if (WARN_ON_ONCE(fragsz > PAGE_SIZE)) + return NULL; + page = virt_to_page(nc->va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) @@ -102,33 +111,16 @@ 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)) { - /* - * The caller is trying to allocate a fragment - * with fragsz > PAGE_SIZE but the cache isn't big - * enough to satisfy the request, this may - * happen in low memory conditions. - * We don't release the cache page because - * it could make memory pressure worse - * so we simply return NULL here. - */ - return NULL; - } + offset = 0; } nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; + nc->offset = offset + fragsz; return nc->va + offset; }