From nobody Thu Nov 30 06:24:36 2023 X-Original-To: freebsd-wireless@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4SgmPx0pxbz52tqx for ; Thu, 30 Nov 2023 06:24:53 +0000 (UTC) (envelope-from adrian.chadd@gmail.com) Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SgmPw418Kz4VBn for ; Thu, 30 Nov 2023 06:24:52 +0000 (UTC) (envelope-from adrian.chadd@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-50bba815f30so807273e87.2 for ; Wed, 29 Nov 2023 22:24:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701325490; x=1701930290; darn=freebsd.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=I8JDdCiWOQX8Quq4m975NQtFZqKldPS7uVitdTqYRwA=; b=ZT6FRTbqibhfaicAKRsqZfK2inyum0FKlbyDgGMO8NI2ZWfinqhrSInBV146s7XOq6 sMyJa6wV6K/rKo+Iq45dNLU/PGSZO2Oend/fXz2u4Pzm3y+MaUf0EnHdouxcMZqHQ1xt ic6QtGqF3ljRiSKj6d5or7Fc0auwXIi2PdJOgIa1Z0DN9xNMAuCKAYsZ5hX7qEQSIx1r Na997Ln5lOxwn4ueDLIdTB9sDcgTWJ8H8XDI3J0XmnbSZYIG3dI9uj0VpxV9Uvx1pwnK S0L/wdXYker0akuB0tBl6jT6wZFPZGO/fipkI7UqCHoPVNOzyyQl61OGBGIo6tmfaMIm G0Iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701325490; x=1701930290; h=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=I8JDdCiWOQX8Quq4m975NQtFZqKldPS7uVitdTqYRwA=; b=VPuEY0DtoN2OtcfLMiDWLJ+sljQKyP2kkSTSzZmQ1NSmUVEAnCRGnpQwb1hUp7X+ss U6aitd2hmQV72PKdhFkyHaQJ3PlhviULv6MoirOEV1mgMuXm2Dgb2+cH8sn3BIcCYfgH 6uAiHomqZRseRBEG1/99YqQ6qZFv6CbUkcTAXJdd9dSmvSlm3mY6iAHldw3S2rsxIsh0 wcaA5TFfGPP7ErzHlBLUUvA4wewawRWv6eoZMFAy30vGarMJevBFon9upCPx1PhyLDnf 9+hrSMfEb2mryMosrZXtdMcor5zUKBtuYeTBp3eifw7TV7hYbwf9CebAuy+bTw9+k4Zl UxaQ== X-Gm-Message-State: AOJu0Yzkvkt03gqG9HNGkAOIf9h3yMc4wofOpVdOeBVGh0P6gJooRBCO Cv7UAy/v7WwjW4VS23pGTncypR+AtylMruxPqUpKIrE1 X-Google-Smtp-Source: AGHT+IH7K0qN4Ab/n7nblVby5k1Y3Rz5WTEP4QowMXVqsU4B0ZJ/b0pInBpSzmZeklZ/OEkL8ocpJrKNimexzfG9Ll8= X-Received: by 2002:a05:6512:124e:b0:500:9a45:63b with SMTP id fb14-20020a056512124e00b005009a45063bmr13502691lfb.13.1701325489916; Wed, 29 Nov 2023 22:24:49 -0800 (PST) List-Id: Discussions List-Archive: https://lists.freebsd.org/archives/freebsd-wireless List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-wireless@freebsd.org X-BeenThere: freebsd-wireless@freebsd.org MIME-Version: 1.0 References: <629e3534-705a-4dcc-ad16-edba170da251@app.fastmail.com> In-Reply-To: <629e3534-705a-4dcc-ad16-edba170da251@app.fastmail.com> From: Adrian Chadd Date: Wed, 29 Nov 2023 22:24:36 -0800 Message-ID: Subject: Re: Why newstate handler runs IEEE80211_LOCK/UNLOCK? To: Farhan Khan Cc: freebsd-wireless Content-Type: multipart/alternative; boundary="000000000000794ee0060b58b5a3" X-Spamd-Bar: ---- X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; TAGGED_FROM(0.00)[] X-Rspamd-Queue-Id: 4SgmPw418Kz4VBn --000000000000794ee0060b58b5a3 Content-Type: text/plain; charset="UTF-8" On Wed, 29 Nov 2023 at 22:12, Farhan Khan wrote: > Hi all, > > I'm studying the implementations of net80211 and noticed that in all > newstate handlers the code begins by running IEEE80211_UNLOCK(ic) and ends > with IEEE80211_LOCK(ic). I was not clear on why this was, I would have > expected the opposite order. Also, why not just use the softc device-wide > mutex over one for ieee80211com. Overall, I do not seem to understand the > intent of the unlock and am seeking clarification. > That part of the net80211 locking handling is ... unfortunately unfun. Without doing that, there'd be lots of lock order inversion issues and sleeping whilst lock held issues (since it's a mutex, not a sleep lock.) The newstate code in net80211 at least (now?) runs in a taskqueue, so whenever something changes state, it isn't happening in a random drivers rx/tx/ioctl path. That way newstate transitions are at hopefully serialised and not running in overlapping/concurrent threads. However, since drivers do a /lot/ of potentially sleeping work in the newstate path - think all the sleeping that goes on when things wait for firmware commands to complete - you can't hold a mutex across those. I tried cleaning that up years ago but it was .. very tricky to try and change. The wireless drivers would need to make sure they do /no/ blocking activity in the newstate routines, which is .. tricky. -adrian > > Thank you, > -- > Farhan Khan > PGP Fingerprint: 1312 89CE 663E 1EB2 179C 1C83 C41D 2281 F8DA C0DE > > > --000000000000794ee0060b58b5a3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Wed, 29 Nov 2023 at 22:12, Farhan = Khan <farhan@farhan.codes> wrote:
Hi all,

I'm studying the implementations of net80211 and noticed that in all ne= wstate handlers the code begins by running IEEE80211_UNLOCK(ic) and ends wi= th IEEE80211_LOCK(ic). I was not clear on why this was, I would have expect= ed the opposite order. Also, why not just use the softc device-wide mutex o= ver one for ieee80211com. Overall, I do not seem to understand the intent o= f the unlock and am seeking clarification.

<= div>That part of the net80211 locking handling is ... unfortunately unfun. = Without doing that, there'd be lots of lock order inversion issues and = sleeping whilst lock=C2=A0held issues (since it's a mutex, not a sleep = lock.)
The newstate code in net80211 at least (now?) runs in a ta= skqueue, so whenever something changes state, it isn't happening in a r= andom drivers rx/tx/ioctl path. That way newstate transitions are at hopefu= lly=C2=A0serialised and not running in overlapping/concurrent threads.

However, since drivers do a /lot/ of potentially sleep= ing work in the newstate path - think all the sleeping that goes on when th= ings wait for firmware commands to complete - you can't hold a mutex ac= ross those.

I tried cleaning that up years ago but= it was .. very tricky to try and change. The wireless drivers would need t= o make sure they do /no/ blocking activity in the newstate routines, which = is .. tricky.



-adria= n

=C2=A0

Thank you,
--
Farhan Khan
PGP Fingerprint: 1312 89CE 663E 1EB2 179C 1C83 C41D 2281 F8DA C0DE


--000000000000794ee0060b58b5a3--