Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1076013rwl; Wed, 5 Apr 2023 11:23:45 -0700 (PDT) X-Google-Smtp-Source: AKy350azbkfHmWq/WKpiMYxaIzQbLBfAVYB6Ie57osnDRUhDt+EWPrfNgCdZZIows9ScwpmxYOOX X-Received: by 2002:aa7:9ec4:0:b0:62b:47fc:a970 with SMTP id r4-20020aa79ec4000000b0062b47fca970mr6783975pfq.9.1680719025355; Wed, 05 Apr 2023 11:23:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680719025; cv=none; d=google.com; s=arc-20160816; b=LC6+rNm+wtCUUSNGvDO42OkB9tDXHAZvabTcl/NP82sx6N91NzOtzpNDhv6Q3yVKRR 5UXW2H4onYekb+hwrYz99XsVM4xXqwxslyJ5q5d1T0IK7GrVJS5EyhPoDWQV71wWUOkd QvHWAYKDGH8AMIT8CqTqPqkwn5f1ZWciccsWzM4ZSNBK7afLA4yRi5c4sD+a4A31Emxa kqJe3BHfjJOmnpaDbVPANX4W4T31Ig3ZaafBWwej6dHsTA7SDnsDVVfluhv6S3ZS5HCN hmHRWOko7wyM96A7RQ8Cjm/yvwlQDfFRljjUZIhDWA2Ty9zVwOmz/6FpLhBL2NuT/WQS 9FyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=T6Pry4pnx6T0Rh2iyz0LFzPUOm+SG3X585FahTXWqMA=; b=mjSAQv9QqaFrBUN2Bz8EPxYkJmBWiWiMxT9uRRqCAv+ZwD1TJ6FnMaLw9ywsFNldao ge9Vc1Xz2B6QSJU5NUO9iYEflv4TT3zZRyS/SaRQ3tm/mxlOlNLWA38+awTqMQINcEWd uDXD5DtVwTffyCognCtkWbz7giaLq1DY6cd9w47kAKgQ532Qq37Vh86Wxm0uLY6JxPFZ a1UqDG0MhCFHCmeWzLFkOygsjk2i718tUNf2DR/FYJUVFixxSBJ0nvk9IA8uFLj2cyKc JCJEfML4h9IYDmX3yFiywmuZUS9vVh/GxanH0wsoPIEhromEp5S03yt/y4R76+PmtEjv 9paw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=RSQcjQKv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d12-20020a630e0c000000b00513f16584d1si6594530pgl.465.2023.04.05.11.23.33; Wed, 05 Apr 2023 11:23:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=RSQcjQKv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229520AbjDESMY (ORCPT + 99 others); Wed, 5 Apr 2023 14:12:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234149AbjDESMS (ORCPT ); Wed, 5 Apr 2023 14:12:18 -0400 Received: from mail-yb1-xb2d.google.com (mail-yb1-xb2d.google.com [IPv6:2607:f8b0:4864:20::b2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D24BA72AE for ; Wed, 5 Apr 2023 11:12:14 -0700 (PDT) Received: by mail-yb1-xb2d.google.com with SMTP id d3so15710142ybu.1 for ; Wed, 05 Apr 2023 11:12:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1680718333; 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=T6Pry4pnx6T0Rh2iyz0LFzPUOm+SG3X585FahTXWqMA=; b=RSQcjQKvrB51Wu6LP5qpBbWrEpXPZLwAMc5n+F73LMsXOKDuaIg63rDtodk2BTJ9Fw hE8F3Y8iYI151CocFhQamQWQzdsf0NEtcISSmQxmUdM1Ig6NBoVEeBSZwc0n1w+tFRa/ rxZ3tdMBkR8VgyR6ZKkph/Q6CjZO7l6uOii3s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680718333; 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=T6Pry4pnx6T0Rh2iyz0LFzPUOm+SG3X585FahTXWqMA=; b=dEavj/A8NsjuWamJYun1swcknjDPNL1I2gGKv77aaEJ+GpqEyxaSO+jIw8xsfoUPeM H2E2aJ8wo2OwvyBwwNWypZKjpxSH5ptOU1BGFgKNkXgCnalNo39O58v7j5dbO9wRqC9u SPmXfj3Gkh/N5rL9WOXEM5AXwaUPNjT8360qZ8QfqgsZLz6RjAEFsZ6jRUZ3Ce/NcQ32 JAlBUEipZ38xKsOkECfqsh5KHI4yjM4Qc57iZMsvsRDsUM65GqrVf8nrn3pFFgASVvNl ytgigD1Vdn2y0NQdD8L9719BbMM2R8E5Irve+HSRpMZatbN7ziVmIk9UZAuc7lgsTiZr wZVA== X-Gm-Message-State: AAQBX9e4aL9gxcEjmVk/p4DxZBObNHS3/u4z2d92ngRfPiWV3+pS6nTo YUQQ4zJUnqMa5ZKXl4ScQ/vZiqWLv9MrQz5AOuvbpw== X-Received: by 2002:a25:ccc4:0:b0:b8a:5fd7:5180 with SMTP id l187-20020a25ccc4000000b00b8a5fd75180mr152195ybf.2.1680718333506; Wed, 05 Apr 2023 11:12:13 -0700 (PDT) MIME-Version: 1.0 References: <1680266529-28429-1-git-send-email-ziwei.dai@unisoc.com> In-Reply-To: From: Joel Fernandes Date: Wed, 5 Apr 2023 14:12:02 -0400 Message-ID: Subject: Re: [PATCH V2] rcu: Make sure new krcp free business is handled after the wanted rcu grace period. To: Ziwei Dai Cc: urezki@gmail.com, paulmck@kernel.org, frederic@kernel.org, quic_neeraju@quicinc.com, josh@joshtriplett.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, jiangshanlai@gmail.com, rcu@vger.kernel.org, linux-kernel@vger.kernel.org, shuang.wang@unisoc.com, yifan.xin@unisoc.com, ke.wang@unisoc.com, xuewen.yan@unisoc.com, zhiguo.niu@unisoc.com, zhaoyang.huang@unisoc.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_PDS_OTHER_BAD_TLD 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 Wed, Apr 5, 2023 at 1:39=E2=80=AFPM Joel Fernandes wrote: > > On Fri, Mar 31, 2023 at 8:43=E2=80=AFAM Ziwei Dai = wrote: > > > > In kfree_rcu_monitor(), new free business at krcp is attached to any fr= ee > > channel at krwp. kfree_rcu_monitor() is responsible to make sure new fr= ee > > business is handled after the rcu grace period. But if there is any non= e-free > > channel at krwp already, that means there is an on-going rcu work, > > which will cause the kvfree_call_rcu()-triggered free business is done > > before the wanted rcu grace period ends. > > > > This commit ignore krwp which has non-free channel at kfree_rcu_monitor= (), > > to fix the issue that kvfree_call_rcu() loses effectiveness. > > > > Below is the css_set obj "from_cset" use-after-free case caused by > > kvfree_call_rcu() losing effectiveness. > > CPU 0 calls rcu_read_lock(), then use "from_cset", then hard irq comes, > > the task is schedule out. > > CPU 1 calls kfree_rcu(cset, rcu_head), willing to free "from_cset" afte= r new gp. > > But "from_cset" is freed right after current gp end. "from_cset" is rea= llocated. > > CPU 0 's task arrives back, references "from_cset"'s member, which caus= es crash. > > > > CPU 0 CPU 1 > > count_memcg_event_mm() > > |rcu_read_lock() <--- > > |mem_cgroup_from_task() > > |// css_set_ptr is the "from_cset" mentioned on CPU 1 > > |css_set_ptr =3D rcu_dereference((task)->cgroups) > > |// Hard irq comes, current task is scheduled out. > > > > cgroup_attach_task() > > |cgroup_migrate() > > |cgroup_migrate_execute() > > |css_set_move_task(task, from_c= set, to_cset, true) > > |cgroup_move_task(task, to_cset= ) > > |rcu_assign_pointer(.., to_cset= ) > > |... > > |cgroup_migrate_finish() > > |put_css_set_locked(from_cset) > > |from_cset->refcount return 0 > > |kfree_rcu(cset, rcu_head) // m= eans to free from_cset after new gp > > |add_ptr_to_bulk_krc_lock() > > |schedule_delayed_work(&krcp->m= onitor_work, ..) > > > > kfree_rcu_monitor() > > |krcp->bulk_head[0]'s work atta= ched to krwp->bulk_head_free[] > > |queue_rcu_work(system_wq, &krw= p->rcu_work) > > |if rwork->rcu.work is not in W= ORK_STRUCT_PENDING_BIT state, > > |call_rcu(&rwork->rcu, rcu_work= _rcufn) <--- request a new gp > > > > // There is a perious call_rcu(= .., rcu_work_rcufn) > > // gp end, rcu_work_rcufn() is = called. > > rcu_work_rcufn() > > |__queue_work(.., rwork->wq, &r= work->work); > > > > |kfree_rcu_work() > > |krwp->bulk_head_free[0] bulk i= s freed before new gp end!!! > > |The "from_cset" is freed befor= e new gp end. > > > > // the task is scheduled in after many ms. > > |css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, because css= _set_ptr is freed. > > > > v2: Use helper function instead of inserted code block at kfree_rcu_mon= itor(). > > > > Fixes: c014efeef76a ("rcu: Add multiple in-flight batches of kfree_rcu(= ) work") > > Signed-off-by: Ziwei Dai > > Please update the fixes tag to: > 5f3c8d620447 ("rcu/tree: Maintain separate array for vmalloc ptrs") Vlad pointed out in another thread that the fix is actually to 34c881745549= . So just to be sure, it could be updated to: Fixes: 34c881745549 ("rcu: Support kfree_bulk() interface in kfree_rcu()") Fixes: 5f3c8d620447 ("rcu/tree: Maintain separate array for vmalloc ptrs") thanks, - Joel