From patchwork Tue Oct 8 11:20:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13826284 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 6B176CEF157 for ; Tue, 8 Oct 2024 11:27:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 556146B0089; Tue, 8 Oct 2024 07:27:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 505376B008A; Tue, 8 Oct 2024 07:27:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 380026B008C; Tue, 8 Oct 2024 07:27:12 -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 19BB96B0089 for ; Tue, 8 Oct 2024 07:27:12 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9CA63A1346 for ; Tue, 8 Oct 2024 11:27:09 +0000 (UTC) X-FDA: 82650208662.28.8A1B7C8 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf20.hostedemail.com (Postfix) with ESMTP id 1EAEB1C0008 for ; Tue, 8 Oct 2024 11:27:08 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 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=1728386694; 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=PBbxlB0kz24iy7iokK1TfN2qwdTa4QMZvXDcqO85e8U=; b=MVr/dPIDlLWv0uu+soBuJpaXMnXn4sfgysnxYe/ZwpB5dm/8jbc78mcxaiXJ7CvIUI+x1c 4+Mh+liHkGckHkcCntl+qtOOfYzOHE86VuVkk29aoMFnuelF/9ahNf/BtRkUo4pl79FwH9 k4lNsQk5ZPBB11v5ea3Rd+tMT/QlImI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728386694; a=rsa-sha256; cv=none; b=8d6NVD2xPaPs4ohdaVb9uxwFHdq9wtsmShZju8xnoXCaSbhYJWEtt8N8IuH3hhoyzSo2yX 5eZHushusPHGnyyVPCGSju+F92bjlVSI4AHigpu7dUJJ6ByybhB80IbIxxEXUKqHvIikn1 tnsvdwXyY9RF9oirsC+2GFXa1I2QqAw= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4XNDHR5TVzzCt4M; Tue, 8 Oct 2024 19:26:27 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 7586118009B; Tue, 8 Oct 2024 19:27:03 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) 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; Tue, 8 Oct 2024 19:27:03 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v20 03/14] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Tue, 8 Oct 2024 19:20:37 +0800 Message-ID: <20241008112049.2279307-4-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241008112049.2279307-1-linyunsheng@huawei.com> References: <20241008112049.2279307-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemf200006.china.huawei.com (7.185.36.61) X-Stat-Signature: dxab69ykg8xioes7tpisp9w9wusegi86 X-Rspamd-Queue-Id: 1EAEB1C0008 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728386828-898372 X-HE-Meta: U2FsdGVkX1+FLz287M4on+RYTyn2Lo8AHU7xaXMvveR2ETnKW9wi1RiTDLqW6TLA9bB7lkEFjMltlF+klW5og/UndDl9aK1PCzxlMX59ORIq3doQQfL0QfuQzc9rzfFh7uNaVGURX3qvknwagwOJjx1ZS6RhxuCodlFunj3vwFHmMwj0npdmEEneTk9GeLIYYulshx3cHsZJvyrHyd1fmEljWlE4YaNTg967CXd0fJRKfm6i4xL7tpCh8lRtIHkTnU3P7p13TivIDeS/N65SM5/3uOQOEDlZ/FMI4gwfy4eUYu8CNUOFVZGe9hqiL3kAA+dZYbNDNMzzR+mTuCBEllSZE2nNQslTAf56fJs5uuQRx+VBaeg++0cGZ30Oo+ZyZCp6t9ms/+i+sbeOLa8Xj14uDRMt7T8fvYAxZaGBZ80mUz4XE/P3SlnUgWnYW7d8tpfJfALC1+xbQCoz3a2mi/waIM2aScqMAk+jEsQXsLmRZjLpIfxvhSDk7ij+6IrwBJtChIiDryYFMWb13QxWrOm8iI2O8pegZhvIbI6UuAgLG9dvs9A90G27bjWYB1bY1ntujpJQ61mTrtZRWHWUkdVLcmVi5sxP2m36gOzBbFU743/rLvuAY3Zsgutwg7EC3z7eyfX5yDjZWK9s459loytwbnaG0u+daSHtM4y/D8gXZ7l86taX+BqeqYldk1MuIUpFegqhsSzwUdHof0a8/2o6/XPQMJRGGYzAWmzoqM/dSfa6/eSeALZPi6C4eNYC5admYTm4l1bkhe6/WreQ7Rn9FjUxd6l5GMCTc0t5rPxzujgzQ2LsKxYeQ8NWI+mtuApheEZIvObgg7xU+PusNZMUF8RM/DkMPVwH1oz7i921qWDaIf77Xq1jBggLCkyeK59t+Du+EDBvQj4tqJlCNvhQdu7lYbvzPx4RuHp2QsgYL1sbfpOB9UP0LQlOQzsx2jmYKIYhxcwAopzFwEl UFbS0HaC +FqrVN7JgSwV0PAPfdMYdt9HlrK9nS08LtC8hesHZpbv5kNAf8XONi3/a1/ypNRxojZrlOswKU0QVfNw7ypuwNryMV4zS4O0bZe1O0u/Go2JLPkraXfaZbpLbvYILLwGNsU64H4hBuE4n9g/gItCAvVCUrmNts9QlC/HZ4jAYMFb0/4lwdQVB3PCC78bwm5qH3BT5SDSVwGGbrzIlmcGEjvs20eIQyCdt7k6Yc4VGWxBsstqkYAzDGWlkkldayDQemjLFF5aUtZaU+bz/GtTS2in2/TPuOmJitp4dy7Cw+ce7KYXHZ8xmHQVRUMpQKv1hqyypja8lAQlQOmA= 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 about 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 coalescing 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 avoid the disadvantages as much as possible. 1. https://lore.kernel.org/all/f4abe71b3439b39d17a6fb2d410180f367cadf5c.camel@gmail.com/ CC: Alexander Duyck Signed-off-by: Yunsheng Lin Reviewed-by: Alexander Duyck --- mm/page_frag_cache.c | 46 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 609a485cd02a..4c8e04379cb3 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -63,9 +63,13 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + unsigned int size = nc->size; +#else unsigned int size = PAGE_SIZE; +#endif + unsigned int offset; struct page *page; - int offset; if (unlikely(!nc->va)) { refill: @@ -85,11 +89,24 @@ 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)) { + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(offset + fragsz > size)) { + if (unlikely(fragsz > PAGE_SIZE)) { + /* + * 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; + } + page = virt_to_page(nc->va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) @@ -100,33 +117,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; }