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 A0DC1C19F32 for ; Fri, 7 Mar 2025 14:15:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04D98280002; Fri, 7 Mar 2025 09:15:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F18AF280001; Fri, 7 Mar 2025 09:15:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB995280002; Fri, 7 Mar 2025 09:15:31 -0500 (EST) 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 BBB32280001 for ; Fri, 7 Mar 2025 09:15:31 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1958F55E09 for ; Fri, 7 Mar 2025 14:15:33 +0000 (UTC) X-FDA: 83194952946.22.F0041C9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 9E83D180017 for ; Fri, 7 Mar 2025 14:15:29 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cfnaHWqT; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf24.hostedemail.com: domain of toke@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=toke@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1741356930; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3O3AnmpQI3jBSfzVaFR0gisRYPo1AftdSa2GSlKHZsw=; b=UkgUQWH6JQpjLgeL07nnpgtvnkJFxXdhpdTNm3rR+rth0i/esVxBQlDZJUJTttzpqRXsN3 1MLN/B3qmbLZaSxJMH98XcxKDOcFrgAzX/SHMGABlCI8Ayx3wCCmd/Zjdo40sfNheX1zgs uLC/N3lr/AQlbYR0IbaCiRYllxi0hmU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cfnaHWqT; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf24.hostedemail.com: domain of toke@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=toke@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1741356930; a=rsa-sha256; cv=none; b=NPPScW4caNiGbgNoe/E00mWbdDxKhicm5cP5EvhNyShfoEWqNju6qPBvQ8YWwC7urRMA/B KK3XZYwkGsIRKBZtImL/WVHqR7HfZ3rHKNFsZQGaYA8tcp+Xicgil79Kl+5OexvLvlASX7 S8CH2/Iiw++QAb/x8UtinnFnw4QgOW8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741356928; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=3O3AnmpQI3jBSfzVaFR0gisRYPo1AftdSa2GSlKHZsw=; b=cfnaHWqTYy/EwlluAIJkTzfOyvI3cxT9/zynGveLQJGUofxU0lmNzsasATVtFllCB45uVc nfLatL3fCRG6wW+HbfrdvphwD4Sq90Q5Ts0FRXQlOr4crE23URb1xmkooUXNQROQLJ5yZ8 36Wtj6FIMk6zPud0cM4jC6p+3HLzqsY= Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-133-Rh3qHo04MU2E0bfBj-gMhA-1; Fri, 07 Mar 2025 09:15:27 -0500 X-MC-Unique: Rh3qHo04MU2E0bfBj-gMhA-1 X-Mimecast-MFC-AGG-ID: Rh3qHo04MU2E0bfBj-gMhA_1741356926 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-5493a71ae78so1240073e87.0 for ; Fri, 07 Mar 2025 06:15:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741356926; x=1741961726; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3O3AnmpQI3jBSfzVaFR0gisRYPo1AftdSa2GSlKHZsw=; b=BNDMg9igo8g5v+tlk8UvnTxA7OKK7CEHRbnO72r/37Vad//UdWjEexsigEJQmVE6pX PE9tf7nd8nYXI5uSaqM7k2yEaC0OauYp9duMfKXcYXs8M0TKxnWgRgk6siFiBE2NvCd+ j2e8wocnbh0+yn5ZTFWUGZpwjnooKFbD7pF2AYZY+8tagfYwf2kJz2+lXRzTJlSyuCxU g1bwMm+Y1NfSzlIeRiJ8rg3aKYl3vP1Wx4SDqhWK0yXMcj9ly//k45qzE4Rj4UxdXOjO CYkTIVudIZStkmLhZ+urWpbdyjiRnOKz2m8DRvdzdfGQ31XcTzXv88Htdg+DTWZc+0KQ PNTA== X-Forwarded-Encrypted: i=1; AJvYcCVUPbnu/Oj9fcgBL5Dn7TohNnxfjfnZl8NBhaoNdImAPpSnVOm4cgCeEm13hwlvIOTQv3Dg+bxS1w==@kvack.org X-Gm-Message-State: AOJu0YylKDyqU/3cB6a9TViu/q95Waw65Yff7wIp2CpNyZS2YBcEiogG 2dMTfQtWzQsD6gT+g8IjPX1b54dd/y8CqF7d0Qp0Pr6K7LyOaSRW8snHF4rVLYgUPD5ammCkcEB G6CejeDr7waY2E2i0WfIaImGFFyLdF/GkKBUzcif9tjiay9TR X-Gm-Gg: ASbGncsb8h38Krg13BotJwSOi1RjDebkWV2Yij0C2OfrBiusBPthtyBC8FkSR9AcW+r oWIYfTUk41UoSJymD9sL6n6SxnnE1Bhr7zkvz9kJxfRfjVUae6OqP+EIRuiuC/Dc+j9cHu5WLw3 38SwlGjmv7Q1uEFHrA8Zbq8Gvw1HUV/e3xqu4dI+QVBCNBXA6LC5tnV8XfeD6bZ7sxrPtQR28ay cbe1Nl7ox2TZdPl1A+s/tNnYzB+Fz/6dsJIntRi27ov73Rb5vKiLF53XGr6dWYQqG6aWnMr8GHf PuvyMdEcLg8I X-Received: by 2002:a05:6512:3e08:b0:549:8c0c:f059 with SMTP id 2adb3069b0e04-549903f7501mr1328606e87.10.1741356925931; Fri, 07 Mar 2025 06:15:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IE3xCZIBKOSDK+lcZFrDyk8zQiKyadlLEz2VUX4gZrVM2Ry982AQyl3kHsVBYxqC5TNxfqK0A== X-Received: by 2002:a05:6512:3e08:b0:549:8c0c:f059 with SMTP id 2adb3069b0e04-549903f7501mr1328584e87.10.1741356925465; Fri, 07 Mar 2025 06:15:25 -0800 (PST) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5498ae58e4fsm500345e87.82.2025.03.07.06.15.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Mar 2025 06:15:24 -0800 (PST) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id C544C18B8B34; Fri, 07 Mar 2025 15:15:22 +0100 (CET) From: Toke =?utf-8?Q?H=C3=B8iland-J=C3=B8rgensen?= To: Yunsheng Lin , davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com Cc: zhangkun09@huawei.com, liuyonglong@huawei.com, fanghaiqing@huawei.com, Yunsheng Lin , Alexander Lobakin , Robin Murphy , Alexander Duyck , Andrew Morton , Gaurav Batra , Matthew Rosato , IOMMU , MM , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Matthias Brugger , AngeloGioacchino Del Regno , netdev@vger.kernel.org, intel-wired-lan@lists.osuosl.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org Subject: Re: [PATCH net-next v11 0/4] fix the DMA API misuse problem for page_pool In-Reply-To: <20250307092356.638242-1-linyunsheng@huawei.com> References: <20250307092356.638242-1-linyunsheng@huawei.com> X-Clacks-Overhead: GNU Terry Pratchett Date: Fri, 07 Mar 2025 15:15:22 +0100 Message-ID: <87v7slvsed.fsf@toke.dk> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 3RhS0aP4TThm6V0DWa__1hZv9YNkPd57C3uySQeSaoc_1741356926 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9E83D180017 X-Rspam-User: X-Stat-Signature: yxjieqf3psxq6zgn9xa3n363gwhhqza7 X-HE-Tag: 1741356929-843951 X-HE-Meta: U2FsdGVkX18q1JGQcJUdfaQ1N9pPSmYM32awNMq59tvkq8houSWEQmHmsIjS9Ma/lT5ebEwgrvh871dzTg/5wIp1LlWN0EuYBIhDptYlImncj+nzO5GSnjNh7MPP6lIpv4TqVlY9kN1CGmWARrtTTKmioQClfQofYB1H9JNP277Cy5p9ASoUCqWRmENGPF1daoAuOqtYN8xaWIBK9XRp5d6MZ/PqVIPZWg9cdv2NirTUfoJKGTLC2DRo7EB5uqgn4ny+hauqgu+DkkWT/9bMAil1IfGH0gGKuK8q6xkqZSXJg3ea7jr1QRWUraL1bO52nN2MzgDCBONf9yW0AzY31mbVN9/VES8rZbYkCHN74uh7/dxXmPj/C8JPmhPxdGNMwS1vdBgQd3nrJM+whTFyoxXvHyMS+k9dOi9MVEZpbz1rnH/vQ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z1NfR1InctDqxzlqLiPH6qnpnEcbq9V37JlF7Lv+tAKjX4hz9VFUx5olP2lBGb0sXcJO4WXQi+GTp/02XQj3ZQhyXMInxJ2xwsH5QA== 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: Yunsheng Lin writes: > This patchset fix the dma API misuse problem as below: > Networking driver with page_pool support may hand over page > still with dma mapping to network stack and try to reuse that > page after network stack is done with it and passes it back > to page_pool to avoid the penalty of dma mapping/unmapping. > With all the caching in the network stack, some pages may be > held in the network stack without returning to the page_pool > soon enough, and with VF disable causing the driver unbound, > the page_pool does not stop the driver from doing it's > unbounding work, instead page_pool uses workqueue to check > if there is some pages coming back from the network stack > periodically, if there is any, it will do the dma unmmapping > related cleanup work. > > As mentioned in [1], attempting DMA unmaps after the driver > has already unbound may leak resources or at worst corrupt > memory. Fundamentally, the page pool code cannot allow DMA > mappings to outlive the driver they belong to. > > By using the 'struct page_pool_item' referenced by page->pp_item, > page_pool is not only able to keep track of the inflight page to > do dma unmmaping if some pages are still handled in networking > stack when page_pool_destroy() is called, and networking stack is > also able to find the page_pool owning the page when returning > pages back into page_pool: > 1. When a page is added to the page_pool, an item is deleted from > pool->hold_items and set the 'pp_netmem' pointing to that page > and set item->state and item->pp_netmem accordingly in order to > keep track of that page, refill from pool->release_items when > pool->hold_items is empty or use the item from pool->slow_items > when fast items run out. > 2. When a page is released from the page_pool, it is able to tell > which page_pool this page belongs to by masking off the lower > bits of the pointer to page_pool_item *item, as the 'struct > page_pool_item_block' is stored in the top of a struct page. > And after clearing the pp_item->state', the item for the > released page is added back to pool->release_items so that it > can be reused for new pages or just free it when it is from the > pool->slow_items. > 3. When page_pool_destroy() is called, item->state is used to tell > if a specific item is being used/dma mapped or not by scanning > all the item blocks in pool->item_blocks, then item->netmem can > be used to do the dma unmmaping if the corresponding inflight > page is dma mapped. You are making this incredibly complicated. You've basically implemented a whole new slab allocator for those page_pool_item objects, and you're tracking every page handed out by the page pool instead of just the ones that are DMA-mapped. None of this is needed. I took a stab at implementing the xarray-based tracking first suggested by Mina[0]: https://git.kernel.org/toke/c/e87e0edf9520 And, well, it's 50 lines of extra code, none of which are in the fast path. Jesper has kindly helped with testing that it works for normal packet processing, but I haven't yet verified that it resolves the original crash. Will post the patch to the list once I have verified this (help welcome!). -Toke [0] https://lore.kernel.org/all/CAHS8izPg7B5DwKfSuzz-iOop_YRbk3Sd6Y4rX7KBG9DcVJcyWg@mail.gmail.com/