From mboxrd@z Thu Jan 1 00:00:00 1970 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 ACF03D339A9 for ; Mon, 28 Oct 2024 17:54:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 42C9E6B0095; Mon, 28 Oct 2024 13:54:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DCAA6B0096; Mon, 28 Oct 2024 13:54:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A4176B0098; Mon, 28 Oct 2024 13:54:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 0D18F6B0095 for ; Mon, 28 Oct 2024 13:54:14 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 964441C4F8C for ; Mon, 28 Oct 2024 17:54:13 +0000 (UTC) X-FDA: 82723759440.05.43AC187 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) by imf11.hostedemail.com (Postfix) with ESMTP id DFDB740027 for ; Mon, 28 Oct 2024 17:53:42 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=PpBRFKFY; spf=pass (imf11.hostedemail.com: domain of alexander.duyck@gmail.com designates 209.85.221.46 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730137997; 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:dkim-signature; bh=u6UuyopKZ4h5skfASyHzs29ZguAavZNLDhMYIlEaFJI=; b=YAKHyUlaVGJnfZQwOKSIzFPrS19vOZdx5KenAn54Zrv/VuiBmSCAjhe7ahtOQPYyp8jUOS NljswSMMkaqGmwyvWTPm2QqWWp7h48YpBXOOB3itOc2Ovhyixu3TTlk+hQ9cDPvut/hD1S JZhDm18Zv3wZ9jRzAU4eAJp1xghc5wI= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=PpBRFKFY; spf=pass (imf11.hostedemail.com: domain of alexander.duyck@gmail.com designates 209.85.221.46 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730137997; a=rsa-sha256; cv=none; b=w2j2LAP0HmNuNF8//Vq38dDumc86RShyokZz26bEPHuTYzA06FF4BBU6Ah9k6FzBSCWtDg LQkFzrt7ahvqFuTTvF1j4kEvjwpygPZ8fsQPzOx24ywiqUN6kkDrXAKlg/uqPnZhPKQZ0r agfWz7JYNc33oUO1bL0QwEkPyuuit0A= Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-37d55f0cf85so3457467f8f.3 for ; Mon, 28 Oct 2024 10:54:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730138050; x=1730742850; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=u6UuyopKZ4h5skfASyHzs29ZguAavZNLDhMYIlEaFJI=; b=PpBRFKFYBgQNsD1mypS7CA2GitL1BqQYRZCzkFf+0loILDFXAT9hk9hn52BOGJ3ppY NfFIP18hWxBsm1ammzyJNgbry2BWzeiY9rRkpLtQqvBEniDkPQwZLLAX/4JL0dqAXonI r8ojUIZYK4atVdVefpNXTNWlX0UVz2hc5UnICNLpxg6qT5kV2pCGFw5rqFyYPmGYJZtq 0Zwy2bC40fFGaddXa9w0dIJ8zchi5wBAFtu009jM+fExRKXttMSYDdB9/gF86ezVyiJM DXVKtk0BvGM+C1H249vRELMWgZAWpLXhQ8A3C0OdlEUxQ6+8yZO4g8cK+Go9JDww6npI fRqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730138050; x=1730742850; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=u6UuyopKZ4h5skfASyHzs29ZguAavZNLDhMYIlEaFJI=; b=vSVcpmmSS4zM/L0rLNM42gFBeXxhMSxyPXnCVFNY3wvUgriQ2/lN4HNNZ4muQc8P4W jRP3Zy0gCsGzmqjiyDJCFkRxoS7NIcL0LYo8CVVmciugyj7D4TpKbFntKNG3AW6L6i1E iDxerweT3tI5gPyaR1eDuIy1xnyC79rLg+tTxdJptPBA36pKP0LbtNAYgvJUHHNuNK5R 53vO0NBt9t0vBrF+OZcVDigAEKCPol+HhuuZcfHBV9azZKwSII6TXWbO/MQdXkMCrcUO bb7I9853PW50HBJbEisoGkMIzwVBmAP/vOoJU7WFNzAzAtcrn1uc3/vaOO+0kgyTiltr jMlw== X-Forwarded-Encrypted: i=1; AJvYcCUHu3hmgizurMLi88L5aq612GBgvxsAVhGN9DM27Q/lhe+d0ItpqAKhtkOr9RsxJRnswqoet7a2Kg==@kvack.org X-Gm-Message-State: AOJu0YwnLymxyi04pjD2yyQOIn6Nq1SG5Gg93JY86uTWO2k3b0QTLTJw ArGJ1xqgjIetQ8402lPsazlCibt6vgM8JZkTxqQlQ6PE+ti/pGiHijLUMf/4+NLFtWz38Mm1BMN 6NpvA+KdBTthGEf9miawcnnifwWg= X-Google-Smtp-Source: AGHT+IHDF4FKUdkCw3jvLqzaE+MRg8G7+oBY30wWy8t6CmWPzpsWrLHilSvHlQUUJLdmkQ9yX3dPsu9z4QHqCFKcAWo= X-Received: by 2002:adf:cf06:0:b0:37d:4afe:8c98 with SMTP id ffacd0b85a97d-38061159506mr6015970f8f.26.1730138050108; Mon, 28 Oct 2024 10:54:10 -0700 (PDT) MIME-Version: 1.0 References: <20241028115850.3409893-1-linyunsheng@huawei.com> <20241028115850.3409893-5-linyunsheng@huawei.com> In-Reply-To: <20241028115850.3409893-5-linyunsheng@huawei.com> From: Alexander Duyck Date: Mon, 28 Oct 2024 10:53:33 -0700 Message-ID: Subject: Re: [PATCH RFC 04/10] mm: page_frag: introduce page_frag_alloc_abort() related API To: Yunsheng Lin Cc: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Andrew Morton , Linux-MM , Jonathan Corbet , linux-doc@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Stat-Signature: qmuw3yf5mh1on8rpz6utu8ypsgfr1q9r X-Rspamd-Queue-Id: DFDB740027 X-Rspamd-Server: rspam11 X-HE-Tag: 1730138022-988019 X-HE-Meta: U2FsdGVkX1+T6rFkFdhFTTU2bAqlhvNVjS+Up9K1u/1Y1e/oCNeeFC642KXDMsjSSBoZd7dhmupQlNZXefKEiy9+43jyrWVUB93VGcIkGqbIgpxm7+hPpH3r7XDDxPysDnS6FopR6jV3oap0JyBDtF7Sj9HontRe5uwBS5imF+r0SRZbuYL3+wECGywjEO0zHRVEvu03pUoeK9gTrCwgVdVO6azkLhDBNkf2RV6WSKqdxc3JgcMmDpUWA48aOwnqlSPDUaDpQHsW2uM5iXjaC2E35esKrLgajXaTo8YEYzUfGndwybvy16/Yu0z/0VzAk+jXM1A9Y+RMcR++gTXKlyWDLaqw3N64rmH4X9lQameqhh/pweIwKk+6tsX5J1Nd7/y7QJeGFYVtivnx8wfrVlObkUJRZhxPk/cLxm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GlvIoSP/suleC0o9HIZAEdlOLFEeoZV1EbZwOyubS31sw5vsFz1p2a7pQkcrcsw== 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: On Mon, Oct 28, 2024 at 5:05=E2=80=AFAM Yunsheng Lin wrote: > > For some case as tun_build_skb() without the needing of > using complicated prepare & commit API, add the abort API to > abort the operation of page_frag_alloc_*() related API for > error handling knowing that no one else is taking extra > reference to the just allocated fragment, and add abort_ref > API to only abort the reference counting of the allocated > fragment if it is already referenced by someone else. > > CC: Alexander Duyck > CC: Andrew Morton > CC: Linux-MM > Signed-off-by: Yunsheng Lin > --- > Documentation/mm/page_frags.rst | 7 +++++-- > include/linux/page_frag_cache.h | 20 ++++++++++++++++++++ > mm/page_frag_cache.c | 21 +++++++++++++++++++++ > 3 files changed, 46 insertions(+), 2 deletions(-) > > diff --git a/Documentation/mm/page_frags.rst b/Documentation/mm/page_frag= s.rst > index 34e654c2956e..339e641beb53 100644 > --- a/Documentation/mm/page_frags.rst > +++ b/Documentation/mm/page_frags.rst > @@ -114,9 +114,10 @@ fragsz if there is an alignment requirement for the = size of the fragment. > .. kernel-doc:: include/linux/page_frag_cache.h > :identifiers: page_frag_cache_init page_frag_cache_is_pfmemalloc > __page_frag_alloc_align page_frag_alloc_align page_frag_= alloc > + page_frag_alloc_abort > > .. kernel-doc:: mm/page_frag_cache.c > - :identifiers: page_frag_cache_drain page_frag_free > + :identifiers: page_frag_cache_drain page_frag_free page_frag_alloc_ab= ort_ref > > Coding examples > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > @@ -143,8 +144,10 @@ Allocation & freeing API > goto do_error; > > err =3D do_something(va, size); > - if (err) > + if (err) { > + page_frag_alloc_abort(nc, va, size); > goto do_error; > + } > > ... > > diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_ca= che.h > index a2b1127e8ac8..c3347c97522c 100644 > --- a/include/linux/page_frag_cache.h > +++ b/include/linux/page_frag_cache.h > @@ -141,5 +141,25 @@ static inline void *page_frag_alloc(struct page_frag= _cache *nc, > } > > void page_frag_free(void *addr); > +void page_frag_alloc_abort_ref(struct page_frag_cache *nc, void *va, > + unsigned int fragsz); > + > +/** > + * page_frag_alloc_abort - Abort the page fragment allocation. > + * @nc: page_frag cache to which the page fragment is aborted back > + * @va: virtual address of page fragment to be aborted > + * @fragsz: size of the page fragment to be aborted > + * > + * It is expected to be called from the same context as the allocation A= PI. > + * Mostly used for error handling cases to abort the fragment allocation= knowing > + * that no one else is taking extra reference to the just aborted fragme= nt, so > + * that the aborted fragment can be reused. > + */ > +static inline void page_frag_alloc_abort(struct page_frag_cache *nc, voi= d *va, > + unsigned int fragsz) > +{ > + page_frag_alloc_abort_ref(nc, va, fragsz); > + nc->offset -=3D fragsz; > +} > > #endif > diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c > index d014130fb893..4d5626da42ed 100644 > --- a/mm/page_frag_cache.c > +++ b/mm/page_frag_cache.c > @@ -201,3 +201,24 @@ void page_frag_free(void *addr) > free_unref_page(page, compound_order(page)); > } > EXPORT_SYMBOL(page_frag_free); > + > +/** > + * page_frag_alloc_abort_ref - Abort the reference of allocated fragment= . > + * @nc: page_frag cache to which the page fragment is aborted back > + * @va: virtual address of page fragment to be aborted > + * @fragsz: size of the page fragment to be aborted > + * > + * It is expected to be called from the same context as the allocation A= PI. > + * Mostly used for error handling cases to abort the reference of alloca= ted > + * fragment if the fragment has been referenced for other usages, to aov= id the > + * atomic operation of page_frag_free() API. > + */ > +void page_frag_alloc_abort_ref(struct page_frag_cache *nc, void *va, > + unsigned int fragsz) > +{ > + VM_BUG_ON(va + fragsz !=3D > + encoded_page_decode_virt(nc->encoded_page) + nc->offset= ); > + > + nc->pagecnt_bias++; > +} > +EXPORT_SYMBOL(page_frag_alloc_abort_ref); It isn't clear to me why you split this over two functions. It seems like you could just update the offset in this lower function rather than do it in the upper one since you are passing all the arguments here anyway.