From patchwork Tue May 28 12:55: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: 13676633 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 9E7E5C41513 for ; Tue, 28 May 2024 12:59:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5E39F6B0099; Tue, 28 May 2024 08:59:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4CC1A6B009A; Tue, 28 May 2024 08:59:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3939C6B009D; Tue, 28 May 2024 08:59:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1DA3A6B0099 for ; Tue, 28 May 2024 08:59:03 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C2261A12D4 for ; Tue, 28 May 2024 12:59:02 +0000 (UTC) X-FDA: 82167809724.10.5A272FF Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf24.hostedemail.com (Postfix) with ESMTP id 4326218001A for ; Tue, 28 May 2024 12:58:59 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716901140; 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=bvLD0CsYKevuI0vA2cj7f8gmWD+74UtJHbTTmT4Ptcs=; b=k4s2IBQiRIF/Hb3iYhRNj4P2WomRT5w8qteAV3mWEvs7lf3daRVG/u+ISWYLcm8HlHt3bx vFXtrgv3hCZlubYSy9UZyRVq/uNyGJUIXaTt/EP9gcqhe5pb8MsoobFynvCVe+Fl7lkz+R WcYA18leasWsFKRymkxgnXlKjsxK0C8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716901140; a=rsa-sha256; cv=none; b=3mbpNZdntrvYGhcWmtgR1VmxrA+dYzP61wx3HkWZqmkDFLrQ0Ypb/f/x6/DhzD9drY0aBL sQf0biGL2x2jZHEMXs4Zz/k8KNM5DBOpYUAT/D6g0dzgA241KIWI9RW1+W9ddq4S29Oak+ 25+aAXFpV02EBc29PGGQp+q6mC5SD3s= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VpXYC3PCXzwQPW; Tue, 28 May 2024 20:55:11 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id B336018007F; Tue, 28 May 2024 20:58:57 +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; Tue, 28 May 2024 20:58:57 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v5 05/13] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Tue, 28 May 2024 20:55:55 +0800 Message-ID: <20240528125604.63048-6-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240528125604.63048-1-linyunsheng@huawei.com> References: <20240528125604.63048-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm500005.china.huawei.com (7.185.36.74) X-Stat-Signature: jr3ps4bmwz5zxuaacd63f8ny8ssit5ds X-Rspamd-Queue-Id: 4326218001A X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1716901139-869360 X-HE-Meta: U2FsdGVkX1+DsPgdzati7RG1GHB9qy4mRkJbbphiT43qbUtWZ8C+DZ5CfrF17PP2SF9E26ks7ZJvEoa1S4CB0kljabAk5AKHRfsnGzrJ72e1O71iQfMcxxrNP+PZEb6AJzxdtRdL11Zg8BDvXWRQXy8UhwjlLBYmACsS9G/NuE432dJQE2DjjAV2JC/aZ/bEGuKwMrsfzwXuhrbrRuFgK01bkgNLGBKfYELJRwyiDVNddBTU4tuuHhHOxH+jD8v0nlrR147jFif/eWBP2EN6ZMkWIrM6vwYp6v0yG1p0pVaTC7FuFUmGTKTApRrsWIYiX4H7O1KAHrrhLgZrNxBbA9AuvhEkmCuf2cY5bYJuDeI+9ArHGY8hGhcwFiLD3KlQmeLxG3ZqpRbfWbAkLxC+uX+RA1oiQHqKeGIxwPY6hXTZxXpb9PUWuVxB1lQbHHk748VXnCc8k35jIcpgPYzSJ+khGenfY4mn6GX42b4kyvL8D5DS6IhOcVffH8aSVRG2RrWvUK8HJYmzw7KEDfEGJDgQYAc9JjjwhisnK6E3AdwmZ5I6buCBkKphu4eerjnobOV7KoQN6Gg2I5jhhtId8uzu9XD/pRcl0h73KZGP94MOBxLnUH1zghc3ZSKVQWYRUhzil2ubJKvEmipJo+M8LNZR0M0ce6z02dP2cv9q8NujCgQSNmULp94ZJ9uuaJYQhGTjhXf7k/LCiygXtsOHRR6BroVaCw8hKYmh7tHV/z34VAviFGQOutafjkE8gKXbzOZegyIo8wRXnMTg4m/bcMBTBxyIcFMgi6ncGpEoWMgIKyiH0wgHQzYF4Fu1O5/eVnNsABMn40EMS+7XPg3ZzJBu/ta2jZW1koyobjdxqbZD9k8bVHWPXFo0wBmbyXJQGSdt3vUMsK7b9f5D1Ce8jvjoCAxp0TF8CsOoV+66ZrQINoSzOXJouK6CGcq2/S2WPaqUo3HUeGGSnFzlX7a vSxxFYLo zVIv3KTY9SW9BL18iaz1qOmWA7eqeaW6C/fLvb8ZoyUz4I4ECU8Xo4hoq9H7Ly/3lg7OFoQ7MkVZajcSuDTWXUnNIVeMX2sBB0szBt1MfVoapI3H+Vv0hsVtbuwz+76G+Af7xpKpNgWAYZ76WfUTajaUIrQU3L84PBGDpprUmvuEShpwUKM2uvSJsASfdD6agijK0ncq4oHStgUB19r6OriKqqfAxcIC7hzoHXc4vvt8eKkyqKfmC2avo0REAEov4ioYy0oPVPtziLS5wCGLDryuyjWCfCYnjxGhPT2S8B8GrbbghI0pk/XOAmgYPzsxb7riMwunndDcYaO54Dz7Hj3nw6A== 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 | 27 ++++++++++++--------------- 3 files changed, 15 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..a20f608d857e 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,14 @@ 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 (WARN_ON_ONCE(fragsz > PAGE_SIZE)) { /* * The caller is trying to allocate a fragment * with fragsz > PAGE_SIZE but the cache isn't big @@ -127,8 +125,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; }