Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp346847pxj; Fri, 14 May 2021 05:06:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyc/w3InvNYnPXdwCJZJ4xbDrFsSXKM9/imILEyS2GI+skCamW4XrPBp8m2u6L5lAWeKBR+ X-Received: by 2002:a05:6402:17d9:: with SMTP id s25mr24235282edy.337.1620993991459; Fri, 14 May 2021 05:06:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620993991; cv=none; d=google.com; s=arc-20160816; b=VBuNxJnC9An0sUMHu1KWTWQAjFEZUfndghXFaWKePReCQhhCgoGKyyN8w0mcxSkwLN 3e8YID6+lpSaYL+vxuqzsFFRBcc+aH9aP9ipkTDsCDs40UkRARBCD9981vJwYT/5iU7F UVqwDNGxU4rWxtmHzudIlXUfBE4el6DhPKEfDHO4uV4ow0vTkBoVcXdj+cb/ywmRy5+l 794I0zbRKSlrCBcR2y+9tytXjhPfF901o9nHR3PiB6HLLWIxYe919sda6BhvQrOmhr2k RxtRrB3g00M0n8A+fqT8VsqCe/XylrDfo/UGZGDIBygybW9A99UTma+/TCkg8SYNkRKE dJ7w== 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:dkim-signature; bh=IuuCItDUGLV0tdtVuKODb7PuvmdclIu199LYlyde0kI=; b=G0FcjYL1lqirKy/BUMhlN1lNJd+ij7dR8MxJMSRsZww1TSQ/XNXq9hz1Apbj5cd2P/ N0dodlJJI2OoRiJDKXW9k6OVvfwOQBPls9BvABdQQ8YT8/wj+ILUm6Y3EOLr2DcSQl5O kS4z4PFGAu0J+lLOoLLDfRrC0ELSSiGICfhM4zO3QhwXjDgk1i3jU2asK3Bo2z1ogr+5 RkVfL2voxfZa3OJj/1puemFzIklOWkA9otuve8kyPDV0o1x9R8/yMIqOtaFg/1PAG5vu 3QJbyqFIB5iwSbQzNjB2TYGpXagLHcC0BLLFMHEr/wrYfdcq0PFKVX1w4cMXVLLbEI3q DnZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=Pstq6ZBl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id pj24si379119ejb.649.2021.05.14.05.06.05; Fri, 14 May 2021 05:06:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=Pstq6ZBl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232541AbhENMFt (ORCPT + 99 others); Fri, 14 May 2021 08:05:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231394AbhENMFs (ORCPT ); Fri, 14 May 2021 08:05:48 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7109C061574 for ; Fri, 14 May 2021 05:04:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=IuuCItDUGLV0tdtVuKODb7PuvmdclIu199LYlyde0kI=; b=Pstq6ZBlF4chwup7HwRi4wea7K il7OxIqJX00WtMmA9lFrN/fNB86sgX0uUZAb0r0kXGDgqMr+HAoFyZjkEwVZZmXUWtE7PajHn+NR/ +osg3O2fa+iEZmou0eBKi935kybyMHAugLM5oiKqVPFHwvqZoTwdWjUj73qGF7zQa4H9H9ekhAKSj 2ooyxK+DNa5pn4pbDpDQsMc910PKVMz4hGknC/n94HfNNdJHJ2AkT2Rn9QxYl34ZyRC1G4qaDLSC3 l5wgkJ5EYp/lVnC+V+57KS+FdblAfTrOCmW9uh+uqm5BRrS9r5+shPPhVAEW30XcWo01zfBljOtM5 +2xNhBsw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lhWYF-007zBL-DB; Fri, 14 May 2021 12:04:19 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id AC45530001C; Fri, 14 May 2021 14:04:14 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 8DA802BE0F84B; Fri, 14 May 2021 14:04:14 +0200 (CEST) Date: Fri, 14 May 2021 14:04:14 +0200 From: Peter Zijlstra To: Daniel Jordan Cc: Huang Ying , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Dan Carpenter , Andrea Parri , Andi Kleen , Dave Hansen , Omar Sandoval , Paul McKenney , Tejun Heo , Will Deacon , Miaohe Lin Subject: Re: [PATCH] mm, swap: Remove unnecessary smp_rmb() in swap_type_to_swap_info() Message-ID: References: <20210513064837.3949064-1-ying.huang@intel.com> <20210514015946.nivgnoobef4nqwmw@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210514015946.nivgnoobef4nqwmw@oracle.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 13, 2021 at 09:59:46PM -0400, Daniel Jordan wrote: > On Thu, May 13, 2021 at 02:46:10PM +0200, Peter Zijlstra wrote: > > Ah, I think I see what you meant to say, it would perhaps help if you > > write it like so: > > > > > > diff --git a/mm/swapfile.c b/mm/swapfile.c > > index 149e77454e3c..94735248dcd2 100644 > > --- a/mm/swapfile.c > > +++ b/mm/swapfile.c > > @@ -99,11 +99,10 @@ atomic_t nr_rotate_swap = ATOMIC_INIT(0); > > > > static struct swap_info_struct *swap_type_to_swap_info(int type) > > { > > - if (type >= READ_ONCE(nr_swapfiles)) > > + if (type >= MAX_SWAPFILES) > > return NULL; > > > > - smp_rmb(); /* Pairs with smp_wmb in alloc_swap_info. */ > > - return READ_ONCE(swap_info[type]); > > + return READ_ONCE(swap_info[type]); /* rcu_dereference() */ > > } > > > > static inline unsigned char swap_count(unsigned char ent) > > @@ -2869,14 +2868,11 @@ static struct swap_info_struct *alloc_swap_info(void) > > } > > if (type >= nr_swapfiles) { > > p->type = type; > > - WRITE_ONCE(swap_info[type], p); > > /* > > - * Write swap_info[type] before nr_swapfiles, in case a > > - * racing procfs swap_start() or swap_next() is reading them. > > - * (We never shrink nr_swapfiles, we never free this entry.) > > + * Publish the swap_info_struct. > > */ > > - smp_wmb(); > > - WRITE_ONCE(nr_swapfiles, nr_swapfiles + 1); > > + smp_store_release(&swap_info[type], p); /* rcu_assign_pointer() */ > > + nr_swapfiles++; > > Yes, this does help, I didn't understand why smp_wmb stayed around in > the original post. > > I think the only access smp_store_release() orders is p->type. Wouldn't > it be kinda inconsistent to only initialize that one field before > publishing when many others would be done at the end of > alloc_swap_info() after the fact? p->type doesn't seem special. For > instance, get_swap_page_of_type() touches si->lock soon after it calls > swap_type_to_swap_info(), so there could be a small window where there's > a non-NULL si with an uninitialized lock. > > It's not as if this is likely to be a problem in practice, it would just > make it harder to understand why smp_store_release is there. Maybe all > we need is a WRITE_ONCE, or if it's really necessary for certain fields > to be set before publication then move them up and explain? You also care about the zero fill from kvzalloc(). Without the smp_store_release() the zero-fill from the memset() might only be visible 'late'. Unless that also isn't a problem?