From patchwork Fri Jun 7 12:38:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689829 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 B67F7C27C53 for ; Fri, 7 Jun 2024 12:41:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47FE36B00A7; Fri, 7 Jun 2024 08:41:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 42DEE6B00A8; Fri, 7 Jun 2024 08:41:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F6E16B00A9; Fri, 7 Jun 2024 08:41:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 08EE86B00A7 for ; Fri, 7 Jun 2024 08:41:26 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8AE0C14184A for ; Fri, 7 Jun 2024 12:41:25 +0000 (UTC) X-FDA: 82204053330.24.D7D3934 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf01.hostedemail.com (Postfix) with ESMTP id DFA774001F for ; Fri, 7 Jun 2024 12:41:22 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 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=1717764083; 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=7SPNZMzmZwzE/snTI4LAwNFjAVMQpBbCTMUnLeNfsPk=; b=Moojqy89bD/ozKfCnBPIe0XE0h6JYk1tDv5cfcvNPOu+i4Kw5ceuSmqwPcoGIwMIZgnFDs +rqXY73dZiuW5y4jgZNt+KRQ54pu9pBcS4FKLceqB61Wm6xlozEhrMX9OgyYYeqdgw/yTb sniZ1vFYsdThqDb7NmMHlCJl71aCRgY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717764083; a=rsa-sha256; cv=none; b=2NCjul7QdymKzJB09hpAhdXXPCkdcukiVodKwTLVZSYhDOUBUCJUNzh1yFQXKI20spSPOm w0nJDcFjkme+H+zncugPk64d5NTSJF0F9GLEHnB6r71+bW/mEV6ox7JMLkxBW+dlS6xlB6 COeFS4wSfZEsCJrAXbUGoKBx8+7N3m8= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Vwghm4V3zz1yt5C; Fri, 7 Jun 2024 20:38:00 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id DB5941400D3; Fri, 7 Jun 2024 20:41:19 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:19 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v7 05/15] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Fri, 7 Jun 2024 20:38:08 +0800 Message-ID: <20240607123819.40694-6-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-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 dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Stat-Signature: 8cuan6b6yn7dy9yioqdg6f5o7myicksm X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DFA774001F X-HE-Tag: 1717764082-808175 X-HE-Meta: U2FsdGVkX18eo0Eizj/sZVcwcV0TLBkm/iGsHT6F8HbUqry1MeDz1aUFFj3P8ZGFKns9k/lVm8wrllL4yHnR2i2gRSup7HTnOXX4Sjf41l8isg5jKEkBlaT824uJL4GG6NK7BcJTB/cKrb1Eed3bMSgTJ0CHNVOIlVP2f3RtujJYtFxYMUNzgSeP3tjPNZPGHVImQ356/KxDu75LmpUMACqoc5J0oSfCTUAUQGP/gKASFi1UyNeOFQvhQwM9lq39HCrKaUoPKtTTWupCiIL3OG3rEeGnpgkxuoQQhfto8zwJ5BlD9AtM6g2WDhu640SJGJiTV6uWKWMmR1NL/GaA3zWqbwOCRFu8Eg05xItHxV79ORZvU4YxvjJeWkXWpwq2BLVR8sf0huuDFNRqqK3TihW42FzVsSNM/ZHrXX1Dbz8N9HfP8f9yDCJTCyhEmrT2Ogcyk21P98+mb68SEBMSewnmoHjPI5c447Jj2X6YQYSnkwKg5Bp7ToKlg6PaSS4DzpdAcWfxK1Hcv7913dqbpS14DQymgNk7Mv0idgYTa5s3HyNk4HcsRRUKHPSWsnIuhj/wQZ/l9jwQ1dQtbzr1O75WY2QQdcEOEB1QDGR93UFIl0fXne8YBBhwUbxqat/drSWYcNmh1NlYxkmwuel7LhDwcnqbLv94QG9xSMjoKxO28l76HfIjISIPoX/9sCoXCj5Dpji1//5d0csUB9qZgiNns48d9ofVAPNseVy7RyUITw4hByn7Hz0eEudF1Vyn2NuvfaB54uzAb2CA5GThO2Ah6KFZTHveVWVpv3X3Z/DOOq0bcWX4M7fkQ1F5RQAtEc/0JPsho+KfeftYVaEOD+E0qKz/sKWdQJtitoAZU4SkpUUgw60SGSyL7P4W5ARjDhmnyOINqLgekeGiX8HymkIY8VMX3tjaXCnQT1UgqyehoBOtU3vs2jTiVRKAPyRrxAVlByUihwvBWXu+Orv v1S//n75 WBWdG66TTHdY6GOJrjLmwVCkNp+wcMpPiIBfw7GRfM4y1HAvyIvqtyanTLdff9JDBW6eL7MU+Aoen50kYJB2awgRc2zNOP0fg6G39FH0qR3GQDAPNmr45pVeWDl0Y7Ef7oaFPD3wtJnbM0dgur38/5faq+Nh5k9UadyiV9K7d7mk+ogta6pnDaFYUo+O0R0KrANqAAdDS35UgGZY+ynMq3yR/sCRZyKIizd3blveY+PoEGYn41kVki2HLjH+uHmFlbRAkNSuBZdfOSYRjgZbI5othBLJ0LSuMgvOzTZA11rkwCM+FwJ3IRjvCahFrMbGpLmVRr9tHJwTaykjy2ku8Ato/Vw== 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 35f6e3f12565..6587dc1b3f70 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3300,7 +3300,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); } @@ -3371,7 +3371,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; }