Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4606888iob; Sun, 8 May 2022 19:01:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw2Nus677yX9HBRCo27NbnDKZg4D/TRcVQxnLDEfV/mZLM3l8DyFwdTC4enFlKzYCFKvm7/ X-Received: by 2002:a17:90a:1602:b0:1d9:9ddd:1f8b with SMTP id n2-20020a17090a160200b001d99ddd1f8bmr24258427pja.130.1652061698329; Sun, 08 May 2022 19:01:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652061698; cv=none; d=google.com; s=arc-20160816; b=ErWWS99RAKbldh56xpLc4gfd0roYnSPAJM1Y3flfP/vHBOLibqvs0QvFcXveG9bYn8 sDf5I6q+IzVerEcvviVNpK6LNDeIEp7UHOFeTnPC5WWXm5VoUWSdW9v54sIZqv0u20lf 0yTTZalhesgJrr/MNM32xP75J5ZFA1BqtqEbDJRn7vmmCwFgemN00SVIw25MupysPFxa I/nqhqaGPyZ0cdEvu4eqge25dvX/Ny06n7BJ0gwPmRumKzjTrSWTh/8bribrIPWsPOWj xGu6FTkMCCfBBB4CHD0AHN1QlreOek2Ss0SM7PcoOTVuntSa3Q9Oj0wqfO9/S87IWloI 34EA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:sender:dkim-signature; bh=pgblE3j4iKhz7YnaakiOSpHlbjQP4SAyavbRgdtpp8I=; b=hQri5YiyqwfS+uX9rYvG/tZH/d0KVy4G+9HYdY+wmIZvrVN9PLdjw/HnftCuITEXft bJBMViDdMdkJxa4EM10dku72TYSD0cKnagG3yfrs7OHwSayEjXnp5Zcu0eK3Ij9VfZXQ 2VIiHtrZQzKQ38m7gx4aHHjMVNQsGL/c6mOCo9s/fBdj5iFeHWZKI7Ga3IIITGN33c+R V1bitsnqeBlzEGE3fCyXv7rMv9YzSmDf/zUpJVLBgQQMVXezzQrDyXsKxPAwZVWoM0Cd Vhpy6hMywZxapU43MGhc4/SJeiJVm8Su9l4C18V8ayFwkwkMk+woMjPDvOtYk1yiI2so eNlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Fpczt1Rm; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id f184-20020a636ac1000000b003aa65330b92si12951499pgc.486.2022.05.08.19.01.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 19:01:38 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Fpczt1Rm; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B008DDEC0; Sun, 8 May 2022 19:00:32 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359823AbiEEUFg (ORCPT + 99 others); Thu, 5 May 2022 16:05:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244961AbiEEUFf (ORCPT ); Thu, 5 May 2022 16:05:35 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 832085EDF0 for ; Thu, 5 May 2022 13:01:54 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id fv2so5138003pjb.4 for ; Thu, 05 May 2022 13:01:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=pgblE3j4iKhz7YnaakiOSpHlbjQP4SAyavbRgdtpp8I=; b=Fpczt1Rmo9bey40PtYJ41u9aQTEn4i74wmc8/xVaITuombJTgbBwdwhuoo8Da5pw/Z 8erpU+uD0pDZ0o01228ZgmGm0cgCuGbgwdGB6JwmoWNdO5HkSq94CrZZhvxzzoY/CMLE VDgwPcqO66XU8yGHYqVJR6Pl+x10LQkTz7W89lTrnzVOP2xCQeWsvTCCOaI+LJ346+gn jEgpseOVNtomk93eXvWarsB52hyROlIyAwsFaEM/sD+ZkQwV5d47sK7oHOBFVEsZFX8v +ZDg+PHi2e5K1y1ZhQbObIX1gZ/xS0307712RoViSArPBFAWzxVSIeksKwyj+3AwPt// JnjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=pgblE3j4iKhz7YnaakiOSpHlbjQP4SAyavbRgdtpp8I=; b=GAClRsbQAMdyglMSfXcwktFFSltiaSYEagL29l93SaGIcmcAIkWnZE3+/IUbf/31r2 nG6HESFTMPEYOmSt4WUltyCeZFZ3pUmIil4l3qjQuDWOhqEsiqdFzp5dfaewLK7OHnbo DA+hBYZl7A3XLKRy67HNoPBB69t+2/DyGFRp05gqhMwdowx7l/1plzOqjIlddPL/QUbB 64/sAxPqGHi2b70GU/FXiwAzB2Y+M7Y+GC65lR7gGUQF4+MQEsc3+jsV9dm/Hpf7YhLb jKgrjXTGlaFx/hyUQEcGBTnfkSI3mjv69a7xwdSfU/R4KU0hX/CBPSmRRVoRrwLA1bDu IFfA== X-Gm-Message-State: AOAM530KiTZvEMd7B3j7e/nAAMcoCVJDeBGhBuJ08DqZeWJmuFIAr5jk psPxHvGoGBlRtzVITuaxvaK0JyMeBEs= X-Received: by 2002:a17:902:8608:b0:158:b827:7721 with SMTP id f8-20020a170902860800b00158b8277721mr28760064plo.149.1651780913900; Thu, 05 May 2022 13:01:53 -0700 (PDT) Received: from localhost ([2620:10d:c090:400::4:29a5]) by smtp.gmail.com with ESMTPSA id s66-20020a625e45000000b0050dc76281dbsm1737123pfb.181.2022.05.05.13.01.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 13:01:53 -0700 (PDT) Sender: Tejun Heo Date: Thu, 5 May 2022 10:01:51 -1000 From: Tejun Heo To: Imran Khan Cc: viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/5] kernfs: make ->attr.open RCU protected. Message-ID: References: <20220428055431.3826852-1-imran.f.khan@oracle.com> <20220428055431.3826852-3-imran.f.khan@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220428055431.3826852-3-imran.f.khan@oracle.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=no 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 Thu, Apr 28, 2022 at 03:54:28PM +1000, Imran Khan wrote: > +static struct kernfs_open_node *kernfs_deref_on_raw(struct kernfs_node *kn) > +{ > + return rcu_dereference_raw(kn->attr.open); > +} Wrapping the above probably isn't helping anything. > +/* > + * Check ->attr.open corresponding to @kn while holding kernfs_open_file_mutex. > + * ->attr.open is modified under kernfs_open_file_mutex. So it can be safely > + * accessed outside RCU read-side critical section, while holding the mutex. > + */ > +static struct kernfs_open_node *kernfs_check_on_protected(struct kernfs_node *kn) > +{ > + return rcu_dereference_check(kn->attr.open, > + lockdep_is_held(&kernfs_open_file_mutex)); > +} Maybe name this just kernfs_deref_on()? > @@ -156,8 +188,9 @@ static void kernfs_seq_stop(struct seq_file *sf, void *v) > static int kernfs_seq_show(struct seq_file *sf, void *v) > { > struct kernfs_open_file *of = sf->private; > + struct kernfs_open_node *on = kernfs_deref_on_raw(of->kn); I suppose this is protected by the fact that @of is on @on? If so, just add the condition to the checked version. The condition doesn't have to be perfect - e.g. you can just say that neither @on's and @of's list_head isn't empty. While not comprehensive, it'd still provide meaningful protection against mistakes and be easier to understand if the deref accessor clearly explains the expectations. > @@ -201,7 +235,8 @@ static ssize_t kernfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) > goto out_free; > } > > - of->event = atomic_read(&of->kn->attr.open->event); > + on = kernfs_deref_on_raw(of->kn); > + of->event = atomic_read(&unrcu_pointer(on)->event); Ditto here. > @@ -815,7 +843,7 @@ void kernfs_drain_open_files(struct kernfs_node *kn) > __poll_t kernfs_generic_poll(struct kernfs_open_file *of, poll_table *wait) > { > struct kernfs_node *kn = kernfs_dentry_node(of->file->f_path.dentry); > - struct kernfs_open_node *on = kn->attr.open; > + struct kernfs_open_node *on = kernfs_deref_on_raw(kn); and here. > @@ -922,13 +950,13 @@ void kernfs_notify(struct kernfs_node *kn) > return; > > /* kick poll immediately */ > - spin_lock_irqsave(&kernfs_open_node_lock, flags); > - on = kn->attr.open; > + rcu_read_lock(); > + on = rcu_dereference(kn->attr.open); Shouldn't this be kernfs_deref_on() too? Thanks. -- tejun