Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp257786yba; Wed, 24 Apr 2019 00:11:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqzzPX5Lyg0Ed/kDrP6zBJYTZHYp0J+5ZlEQlLkI1LDMpe+oJKSgDMdtLNVKQPVrTJ1qy5P9 X-Received: by 2002:aa7:8615:: with SMTP id p21mr31060608pfn.98.1556089902369; Wed, 24 Apr 2019 00:11:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556089902; cv=none; d=google.com; s=arc-20160816; b=u3X304d8WD6O3F5R5nnBn+/SaRqQm1pUgZ/f+IcuNv3JshejQCe0/0gtNAlQxdekdz wQeXSt878a/ZFeNAfb2QnoOnPF/LV8CVkiCv6BewdPhEdpWBF390H1Xc/ahmk8cAHDyZ 6s9rKQoe1QRXZE9rKqCo/Rw7ZL+jgOwP0jRdY6jjGA/v2+s8qKfb5W5lWMKMTvlcgUpw +a/XRMZQ5aH2PilZo5dcBWFGGNoQCKNrMOD5tsXgxjRfMfntUDycZdKwRIZxFbpU7T9p PN+5lhrqWsPbuVaSQDZw/JvExFDtIY3MBbabo99Jr7GbLGVJdPfY9H6HKvdapsDHfnLk vL+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ehC7gqea2vti0BBQFwW9XKtZ4cq4raV9z3lJdyKr98c=; b=pzOD900zTLUwTFKkGT7/AYhE0clof7/t8K1RuNZMRqizLtgko5Gch54kMRjKm3NaNN /G1f+g/UtfFgjctxxrJGeFd964urD9YAB0uheTNwCM3Zh/z3Wz2NpqQ1Tuf4egtR7qHU ClZlX4ucS0ayPwHL0NCxZZ5Rv02FEqp//KB5bqtWjqc8c0ZjlAZE9Swr5/8TtUXPL/7C sbTKpK4Q5HBrTbXE66chktPLDpW1Aur5z6HHbf3QHLFkB24phSQDF8Y/4tKRBIKz/1Wo d3yUOv9+/RJQP87SwXe5EjJ7+d9Nel1lawoEi/VvwdyWGFTyT4F2hJiFtAlhtza0pMme z4+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=ZPwsO6Fb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j143si20031639pfd.124.2019.04.24.00.11.26; Wed, 24 Apr 2019 00:11:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=ZPwsO6Fb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729992AbfDXHKF (ORCPT + 99 others); Wed, 24 Apr 2019 03:10:05 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:41936 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725934AbfDXHKF (ORCPT ); Wed, 24 Apr 2019 03:10:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; 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:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=ehC7gqea2vti0BBQFwW9XKtZ4cq4raV9z3lJdyKr98c=; b=ZPwsO6Fb/2SmRiK6MNTYyC0LE 3pcn4nDruVwaqF7/BTzEFTCCcBDcvYtZIju21NOTa0hbOpy3RKR+SM2Dw1YN/LxQ2tx8+PfBCxKat 8eoe4Hp0buY22S/HPpM/HqQy/Mx2kQ9rLQ/n14H8mQsqw7nWxwBumws6CyitkXLLpAgtN7ZThG3cZ njODZLz/i8d7o54Lqub+xTVctSq7JIvzRMiHQfURWfZvWXe4zPjVW+hxXmfJNR5mI6zP8lQ2uUPNf u5JFbIHi/fAmXjlDfk5E0xuyX3WDPRZh6xRJBm7rZz0rrZAEAMiCXS71KBMChN2UpTusSxPzRk3EI MmfzfJsXw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hJC2a-0003eU-LO; Wed, 24 Apr 2019 07:10:00 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 295A229BBFB81; Wed, 24 Apr 2019 09:09:59 +0200 (CEST) Date: Wed, 24 Apr 2019 09:09:59 +0200 From: Peter Zijlstra To: Waiman Long Cc: Linus Torvalds , Ingo Molnar , Will Deacon , Thomas Gleixner , Linux List Kernel Mailing , the arch/x86 maintainers , Davidlohr Bueso , Tim Chen , huang ying Subject: Re: [PATCH v4 14/16] locking/rwsem: Guard against making count negative Message-ID: <20190424070959.GE4038@hirez.programming.kicks-ass.net> References: <4cbd3c18-c9c0-56eb-4e01-ee355a69057a@redhat.com> <20190419102647.GP7905@worktop.programming.kicks-ass.net> <20190419120207.GO4038@hirez.programming.kicks-ass.net> <20190419130304.GV14281@hirez.programming.kicks-ass.net> <20190419131522.GW14281@hirez.programming.kicks-ass.net> <57620139-92a3-4a21-56bd-5d6fff23214f@redhat.com> <7b1bfc26-6e90-bd65-ab46-08413acd80e9@redhat.com> <20190423141714.GO11158@hirez.programming.kicks-ass.net> <4f62d7f2-e5f6-500e-3e70-b1d1978f7140@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4f62d7f2-e5f6-500e-3e70-b1d1978f7140@redhat.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 23, 2019 at 03:12:16PM -0400, Waiman Long wrote: > That is true in general, but doing preempt_disable/enable across > function boundary is ugly and prone to further problems down the road. We do worse things in this code, and the thing Linus proposes is actually quite simple, something like so: --- --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -912,7 +904,7 @@ rwsem_down_read_slowpath(struct rw_semap raw_spin_unlock_irq(&sem->wait_lock); break; } - schedule(); + schedule_preempt_disabled(); lockevent_inc(rwsem_sleep_reader); } @@ -1121,6 +1113,7 @@ static struct rw_semaphore *rwsem_downgr */ inline void __down_read(struct rw_semaphore *sem) { + preempt_disable(); if (unlikely(atomic_long_fetch_add_acquire(RWSEM_READER_BIAS, &sem->count) & RWSEM_READ_FAILED_MASK)) { rwsem_down_read_slowpath(sem, TASK_UNINTERRUPTIBLE); @@ -1129,10 +1122,12 @@ inline void __down_read(struct rw_semaph } else { rwsem_set_reader_owned(sem); } + preempt_enable(); } static inline int __down_read_killable(struct rw_semaphore *sem) { + preempt_disable(); if (unlikely(atomic_long_fetch_add_acquire(RWSEM_READER_BIAS, &sem->count) & RWSEM_READ_FAILED_MASK)) { if (IS_ERR(rwsem_down_read_slowpath(sem, TASK_KILLABLE))) @@ -1142,6 +1137,7 @@ static inline int __down_read_killable(s } else { rwsem_set_reader_owned(sem); } + preempt_enable(); return 0; }