Received: by 2002:a19:771d:0:0:0:0:0 with SMTP id s29csp1239910lfc; Wed, 1 Jun 2022 12:45:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxh6x8YC52eOH/tspJN3D067wCWLlzzbYxoI9Ouj8r0tU0LWEHdos3Zuudy5v8dslXiJsix X-Received: by 2002:a17:90b:304:b0:1e3:4489:193c with SMTP id ay4-20020a17090b030400b001e34489193cmr9437900pjb.78.1654112755868; Wed, 01 Jun 2022 12:45:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654112755; cv=none; d=google.com; s=arc-20160816; b=UcOMru3JKXrSYPEnPnsfTOxHGLD9xFkVsamgBaTqtoe4kFk96OtuXXwykMLCzqLLBD Hmcv6pGTFdrUlvd9kU1uyh0abRkJQO2HgtcTkMy8A0toLQVVIxOzmD/pmO1jj0jtwG2s b4zew2QIzfyh1u0eQWfqlC/kEMXWNRdSghkQ9K00/S3BzJ42IHWoOFPrnP3kD5tySsn+ teSisydTVty17/rP7wQelM2CXPI5moNmrWS+lG/Rf3MK+6DOvz5qg3JPV7sZPItNXAHq H9xV2ZEqu1tpQVF11a4iCQhYituEsJXEVdv44f3tdz2zxP/f8jYkAX1s7igBRdBkuw6q 2s5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=Ih36/gycPe5v27oOsRC0FfKWnoaY9b8aOJdexOvR9ik=; b=FHtIS+SYBNbT/5MlepE+T/Yr2SqoTvKRx5DOvUuIR4uI6CXlp23Vjd6Q+BebFxXUMG uWKZXnX3oM9jFncTQPxp3OYKEknWRy4NQL6gg8/GNH4zwl8Izk4R9iX3/0oLjQbuHaJW KFoPwn9LdRCekVp+Wt12klf/H6Lo/usfTbktchfCqFqLodRCAKq6cLJAMV8xiGoqLxSE rIAZsbY0Xu8TDinUm/s7vy/j6I9feeT5Byo8j0kkoQOjj6GjoSgNzw6uzAMhwn9m782L N6r4NMDHLPfQ3j9Fn0Lyg+RNMTOG6L/5CSiRlmsIECYWrpeu+W3J3vFtL6IBM3lbe83V bZFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=loBw4lDu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id h2-20020a63e142000000b003fc75297698si3304302pgk.36.2022.06.01.12.45.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 12:45:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=loBw4lDu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D15991DF125; Wed, 1 Jun 2022 12:10:46 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348749AbiEaXpt (ORCPT + 99 others); Tue, 31 May 2022 19:45:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348856AbiEaXpi (ORCPT ); Tue, 31 May 2022 19:45:38 -0400 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B6F39CF7C; Tue, 31 May 2022 16:45:34 -0700 (PDT) Received: by mail-pl1-x635.google.com with SMTP id u18so186171plb.3; Tue, 31 May 2022 16:45:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=Ih36/gycPe5v27oOsRC0FfKWnoaY9b8aOJdexOvR9ik=; b=loBw4lDu2zlmYiEiDfFp/7PSreINSjGVtMPRhUMsvq01MK7Z24fDXVITXdqBxOyjrJ kin08LrU2rPoO7rLQLpUURuFf1SOK9X4Wm49oXn8CRWnNtTSFljqs/Ad8whOWOiEQz/b PbzzTBJvBtXCIrybe8MOS7kQXLhBaN7urG2sxfP6NmVjToYcBbh1MKmnHQgiO7WEwOEa Dxpm65dX4sa18f+Qyy/g2v4lnUWoNs7XBPNEk06gDmT6mFOrgXh74gFZ3sSd0kfU4SEB +lVLvB/vx7I9vSy+5VHhFVMSdRfWuQCz7oG4px1gflWULtI0X5ptdfwGRhjKkvXIjtJd 2+tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=Ih36/gycPe5v27oOsRC0FfKWnoaY9b8aOJdexOvR9ik=; b=DTeNKmYC3oxors0IS5aT59hLLy3hzGZKQBtFjgwggkeRWXfdDGEjgXV2dx5qds+JhO fZjqdlB5rtE5rAMLEG93HUKeFJ9+PwbJ/TmVd4rLUfmKYlEm7rRgHdARztLdesANEGCF wIspvCbrgNN00aQyCkihezQj9ZF9nC5sRBfk0fK1SrBCUZ436aqwu1viQOVaf3yfsBhd i2+NYbCp8T7pmCizxlTeFyfQzPrp3OhrQ/9aDgziVRnucJG1O3qPSmbn7XYb77YTrNNh JEmsSnJA3xSCdF38n7jNknaUdVPWSrLeW/82N4W07sVIu5CY880HELRKkeKziSwhvxF1 ccTg== X-Gm-Message-State: AOAM533KfcwGbhULw51FPOGiiLRX0m5D0B3P3+pGppHTXa6HupVkBICw JjzRJaU63B13zKm+nmPJrWc= X-Received: by 2002:a17:902:b58b:b0:162:2e01:9442 with SMTP id a11-20020a170902b58b00b001622e019442mr42900573pls.6.1654040734054; Tue, 31 May 2022 16:45:34 -0700 (PDT) Received: from ?IPV6:2620:15c:2c1:200:6be8:4de7:1334:9fa2? ([2620:15c:2c1:200:6be8:4de7:1334:9fa2]) by smtp.gmail.com with ESMTPSA id a15-20020a62e20f000000b0051bac6d2603sm6459pfi.214.2022.05.31.16.45.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 31 May 2022 16:45:33 -0700 (PDT) Message-ID: Date: Tue, 31 May 2022 16:45:31 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH v3] mm: page_frag: Warn_on when frag_alloc size is bigger than PAGE_SIZE Content-Language: en-US To: Chen Lin , akpm@linux-foundation.org Cc: kuba@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, alexander.duyck@gmail.com, netdev@vger.kernel.org References: <20220530130705.29c5fa4a5225265d3736bfa4@linux-foundation.org> <1654008188-3183-1-git-send-email-chen45464546@163.com> From: Eric Dumazet In-Reply-To: <1654008188-3183-1-git-send-email-chen45464546@163.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5/31/22 07:43, Chen Lin wrote: > netdev_alloc_frag->page_frag_alloc may cause memory corruption in > the following process: > > 1. A netdev_alloc_frag function call need alloc 200 Bytes to build a skb. > > 2. Insufficient memory to alloc PAGE_FRAG_CACHE_MAX_ORDER(32K) in > __page_frag_cache_refill to fill frag cache, then one page(eg:4K) > is allocated, now current frag cache is 4K, alloc is success, > nc->pagecnt_bias--. > > 3. Then this 200 bytes skb in step 1 is freed, page->_refcount--. > > 4. Another netdev_alloc_frag function call need alloc 5k, page->_refcount > is equal to nc->pagecnt_bias, reset page count bias and offset to > start of new frag. page_frag_alloc will return the 4K memory for a > 5K memory request. > > 5. The caller write on the extra 1k memory which is not actual allocated > will cause memory corruption. > > page_frag_alloc is for fragmented allocation. We should warn the caller > to avoid memory corruption. > > When fragsz is larger than one page, we report the failure and return. > I don't think it is a good idea to make efforts to support the > allocation of more than one page in this function because the total > frag cache size(PAGE_FRAG_CACHE_MAX_SIZE 32768) is relatively small. > When the request is larger than one page, the caller should switch to > use other kernel interfaces, such as kmalloc and alloc_Pages. > > This bug is mainly caused by the reuse of the previously allocated > frag cache memory by the following LARGER allocations. This bug existed > before page_frag_alloc was ported from __netdev_alloc_frag in > net/core/skbuff.c, so most Linux versions have this problem. > > Signed-off-by: Chen Lin > --- > mm/page_alloc.c | 9 +++++++++ > 1 file changed, 9 insertions(+) > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index e008a3d..ffc42b5 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -5574,6 +5574,15 @@ void *page_frag_alloc_align(struct page_frag_cache *nc, > struct page *page; > int offset; > > + /* > + * frag_alloc is not suitable for memory alloc which fragsz > + * is bigger than PAGE_SIZE, use kmalloc or alloc_pages instead. > + */ > + if (WARN_ONCE(fragz > PAGE_SIZE, > + "alloc fragsz(%d) > PAGE_SIZE(%ld) not supported, alloc fail\n", > + fragsz, PAGE_SIZE)) > + return NULL; > + > if (unlikely(!nc->va)) { > refill: > page = __page_frag_cache_refill(nc, gfp_mask); I do not think this patch is needed, nor correct. (panic_on_warn=1 will panic the box) Or provide a stack trace ? Please fix the caller (presumably a network driver ?), and provide an appropriate Fixes: tag. Thanks.